/** * Checks if two headers are equivalent. * * @param dataset another dataset * @return true if the header of the given dataset is equivalent to this * header */ public/* @pure@ */boolean equalHeaders(Instances dataset) { return (equalHeadersMsg(dataset) == null); }
/** * Checks if two headers are equivalent. * * @param dataset another dataset * @return true if the header of the given dataset is equivalent to this * header */ public/* @pure@ */boolean equalHeaders(Instances dataset) { return (equalHeadersMsg(dataset) == null); }
/** * Sets a value of a relation-valued attribute. Creates a fresh list of * attribute values before it is set. * * @param index the value's index * @param data the value * @throws IllegalArgumentException if the attribute is not relation-valued. */ final void setValue(int index, Instances data) { if (isRelationValued()) { if (!data.equalHeaders(((RelationalAttributeInfo)m_AttributeInfo).m_Header)) { throw new IllegalArgumentException("Can't set relational value. " + "Headers not compatible.\n" + data.equalHeadersMsg(((RelationalAttributeInfo)m_AttributeInfo).m_Header)); } ((NominalAttributeInfo)m_AttributeInfo).m_Values = Utils.cast(((NominalAttributeInfo)m_AttributeInfo).m_Values.clone()); ((NominalAttributeInfo)m_AttributeInfo).m_Values.set(index, data); } else { throw new IllegalArgumentException("Can only set value for" + " relation-valued attributes!"); } }
/** * Sets a value of a relation-valued attribute. Creates a fresh list of * attribute values before it is set. * * @param index the value's index * @param data the value * @throws IllegalArgumentException if the attribute is not relation-valued. */ final void setValue(int index, Instances data) { if (isRelationValued()) { if (!data.equalHeaders(((RelationalAttributeInfo)m_AttributeInfo).m_Header)) { throw new IllegalArgumentException("Can't set relational value. " + "Headers not compatible.\n" + data.equalHeadersMsg(((RelationalAttributeInfo)m_AttributeInfo).m_Header)); } ((NominalAttributeInfo)m_AttributeInfo).m_Values = Utils.cast(((NominalAttributeInfo)m_AttributeInfo).m_Values.clone()); ((NominalAttributeInfo)m_AttributeInfo).m_Values.set(index, data); } else { throw new IllegalArgumentException("Can only set value for" + " relation-valued attributes!"); } }
/** * Checks if the headers of two instances are equivalent. If not, then returns * a message why they differ. * * @param dataset another instance * @return null if the header of the given instance is equivalent to this * instance's header, otherwise a message with details on why they * differ */ @Override public String equalHeadersMsg(Instance inst) { if (m_Dataset == null) { throw new UnassignedDatasetException( "Instance doesn't have access to a dataset!"); } return m_Dataset.equalHeadersMsg(inst.dataset()); }
/** * Checks if the headers of two instances are equivalent. If not, then returns * a message why they differ. * * @param dataset another instance * @return null if the header of the given instance is equivalent to this * instance's header, otherwise a message with details on why they * differ */ @Override public String equalHeadersMsg(Instance inst) { if (m_Dataset == null) { throw new UnassignedDatasetException( "Instance doesn't have access to a dataset!"); } return m_Dataset.equalHeadersMsg(inst.dataset()); }
+ m_data.equalHeadersMsg(toAggregate.m_data));
/** * Updates the classifier using the given instance. * * @param instance the instance to include * @throws Exception if instance could not be incorporated * successfully */ public void updateClassifier(Instance instance) throws Exception { if (m_Train.equalHeaders(instance.dataset()) == false) { throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); } update(instance); }
/** * Loads test data, if required. * * @param data * the current training data * @throws Exception * if test sets are not compatible with training data */ protected void loadTestData(Instances data) throws Exception { String msg; m_SearchSpaceTestInst = null; if (m_SearchSpaceTestSet.exists() && !m_SearchSpaceTestSet.isDirectory()) { m_SearchSpaceTestInst = DataSource.read(m_SearchSpaceTestSet .getAbsolutePath()); m_SearchSpaceTestInst.setClassIndex(data.classIndex()); msg = data.equalHeadersMsg(m_SearchSpaceTestInst); if (msg != null) { throw new IllegalArgumentException( "Test set for search space not compatible with training dta:\n" + msg); } m_SearchSpaceTestInst.deleteWithMissingClass(); log("Using test set for search space: " + m_SearchSpaceTestSet); } }
/** * Classifies a given instance. * * @param instance the instance to be classified * @return index of the predicted class as a double * @throws Exception if instance could not be classified * successfully */ public double classifyInstance(Instance instance) throws Exception { /* check the instance */ if (m_Train.equalHeaders(instance.dataset()) == false){ throw new Exception("NNge.classifyInstance : Incompatible instance types !\n" + m_Train.equalHeadersMsg(instance.dataset())); } Exemplar matched = nearestExemplar(instance); if(matched == null){ throw new Exception("NNge.classifyInstance : NNge hasn't been trained !"); } return matched.classValue(); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public NaiveBayes aggregate(NaiveBayes toAggregate) throws Exception { // Highly unlikely that discretization intervals will match between the // two classifiers if (m_UseDiscretization || toAggregate.getUseSupervisedDiscretization()) { throw new Exception("Unable to aggregate when supervised discretization " + "has been turned on"); } if (!m_Instances.equalHeaders(toAggregate.m_Instances)) { throw new Exception("Can't aggregate - data headers don't match: " + m_Instances.equalHeadersMsg(toAggregate.m_Instances)); } ((Aggregateable) m_ClassDistribution) .aggregate(toAggregate.m_ClassDistribution); // aggregate all conditional estimators for (int i = 0; i < m_Distributions.length; i++) { for (int j = 0; j < m_Distributions[i].length; j++) { ((Aggregateable) m_Distributions[i][j]) .aggregate(toAggregate.m_Distributions[i][j]); } } return this; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public NaiveBayes aggregate(NaiveBayes toAggregate) throws Exception { // Highly unlikely that discretization intervals will match between the // two classifiers if (m_UseDiscretization || toAggregate.getUseSupervisedDiscretization()) { throw new Exception("Unable to aggregate when supervised discretization " + "has been turned on"); } if (!m_Instances.equalHeaders(toAggregate.m_Instances)) { throw new Exception("Can't aggregate - data headers don't match: " + m_Instances.equalHeadersMsg(toAggregate.m_Instances)); } ((Aggregateable) m_ClassDistribution) .aggregate(toAggregate.m_ClassDistribution); // aggregate all conditional estimators for (int i = 0; i < m_Distributions.length; i++) { for (int j = 0; j < m_Distributions[i].length; j++) { ((Aggregateable) m_Distributions[i][j]) .aggregate(toAggregate.m_Distributions[i][j]); } } return this; }
/** * Aggregate the supplied reservoir into our reservoir. Does not increase the * size of the sample. * * @param toAggregate the reservoir sample to aggregate * @throws Exception if the structure of the instances in the sample to * aggregate does not match the structure of our sketch */ public void aggregateReservoir(WeightedReservoirSample toAggregate) throws Exception { if (toAggregate.getSample().size() > 0) { Instance structureCheck = toAggregate.getSample().peek().m_instance; if (!m_currentSketch.equalHeaders(structureCheck.dataset())) { throw new Exception( "Can't aggregate - instances structure is different: " + m_currentSketch.equalHeadersMsg(structureCheck.dataset())); } } m_weightedCenterSample.aggregate(toAggregate); }
/** * Adds the supplied instance to the training set. * * @param instance the instance to add * @throws Exception if instance could not be incorporated * successfully */ public void updateClassifier(Instance instance) throws Exception { if (m_Train == null) { throw new Exception("No training instance structure set!"); } else if (m_Train.equalHeaders(instance.dataset()) == false) { throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); } if (!instance.classIsMissing()) { m_NNSearch.update(instance); m_Train.add(instance); } }
public static void main(String[] args) throws Exception { if (args.length != 2) throw new IllegalArgumentException("Required arguments: <train> <predict>"); System.out.println("Loading train: " + args[0]); Instances train = DataSource.read(args[0]); MLUtils.prepareData(train); System.out.println("Loading predict: " + args[1]); Instances predict = DataSource.read(args[1]); MLUtils.prepareData(predict); // compatible? String msg = train.equalHeadersMsg(predict); if (msg != null) throw new IllegalStateException(msg); System.out.println("Build BR classifier on " + args[0]); BR classifier = new BR(); // further configuration of classifier classifier.buildClassifier(train); System.out.println("Use BR classifier on " + args[1]); for (int i = 0; i < predict.numInstances(); i++) { double[] dist = classifier.distributionForInstance(predict.instance(i)); System.out.println((i+1) + ": " + Utils.arrayToString(dist)); } } }
/** * Adds the supplied instance to the training set. * * @param instance the instance to add * @throws Exception if instance could not be incorporated * successfully */ public void updateClassifier(Instance instance) throws Exception { if (m_Train == null) { throw new Exception("No training instance structure set!"); } else if (m_Train.equalHeaders(instance.dataset()) == false) { throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); } if (!instance.classIsMissing()) { m_NNSearch.update(instance); m_Train.add(instance); } }
/** * Adds the supplied instance to the training set * * @param instance the instance to add * @throws Exception if instance could not be incorporated successfully */ public void updateClassifier(Instance instance) throws Exception { if (m_Train.equalHeaders(instance.dataset()) == false) throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); if ( instance.classIsMissing() ) return; m_Train.add(instance); // update relevant attributes ... update_m_Attributes(); }
/** * Updates the classifier. * * @param instance the instance to be put into the classifier * @throws Exception if the instance could not be included successfully */ public void updateClassifier(Instance instance) throws Exception { if (m_Train.equalHeaders(instance.dataset()) == false) { throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); } if (instance.classIsMissing()) { return; } m_Train.add(instance); updateMinMax(instance); }
/** * Adds the supplied instance to the training set * * @param instance the instance to add * @throws Exception if instance could not be incorporated successfully */ public void updateClassifier(Instance instance) throws Exception { if (m_Train.equalHeaders(instance.dataset()) == false) throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); if ( instance.classIsMissing() ) return; m_Train.add(instance); // update relevant attributes ... update_m_Attributes(); }
/** * Adds the supplied instance to the training set. * * @param instance the instance to add * @throws Exception if instance could not be incorporated * successfully */ public void updateClassifier(Instance instance) throws Exception { if (m_Train.equalHeaders(instance.dataset()) == false) { throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); } if (instance.classIsMissing()) { return; } m_Train.add(instance); m_NNSearch.update(instance); m_kNNValid = false; if ((m_WindowSize > 0) && (m_Train.numInstances() > m_WindowSize)) { boolean deletedInstance=false; while (m_Train.numInstances() > m_WindowSize) { m_Train.delete(0); deletedInstance=true; } //rebuild datastructure KDTree currently can't delete if(deletedInstance==true) m_NNSearch.setInstances(m_Train); } }