//load model String rootPath="/some/where/"; Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"tree.model"); //predict instance class values Instances originalTrain= //load or create Instances to predict //which instance to predict class value int s1=0; //perform your prediction double value=cls.classifyInstance(originalTrain.instance(s1)); //get the name of the class value String prediction=originalTrain.classAttribute().value((int)value); System.out.println("The predicted value of instance "+ Integer.toString(s1)+ ": "+prediction);
/** * Classifies the given test instance. * * @param instance the instance to be classified * @return the predicted most likely class for the instance or * Utils.missingValue() if no prediction is made * @throws Exception if an error occurred during the prediction */ @Override public double classifyInstance(Instance instance) throws Exception { return m_Classifier.classifyInstance(instance); }
/** * Classifies the given test instance. * * @param instance the instance to be classified * @return the predicted most likely class for the instance or * Utils.missingValue() if no prediction is made * @throws Exception if an error occurred during the prediction */ @Override public double classifyInstance(Instance instance) throws Exception { return m_Classifier.classifyInstance(instance); }
public double classifierInstance(Instance instance) throws Exception { return m_wrappedClassifier.classifyInstance(instance); }
@Override public double classifyInstance(Instance inst) throws Exception { Instance converted = constructMappedInstance(inst); return m_Classifier.classifyInstance(converted); }
@Override public double classifyInstance(Instance inst) throws Exception { Instance converted = constructMappedInstance(inst); return m_Classifier.classifyInstance(converted); }
@Override public double[] distributionForInstance(Instance instance) throws Exception { int c = instance.classIndex(); double result[] = new double[c]; Instance finstances[] = convertInstance(instance,c); for (int i = 0; i < c; i++) { result[i] = m_MultiClassifiers[i].classifyInstance(finstances[i]); //result[i] = m_MultiClassifiers[i].distributionForInstance(finstances[i])[1]; } return result; }
@Override public Object classify(Instance instance) { try { return utils.convertClass(wekaClass.classifyInstance(utils.instanceToWeka(instance))); } catch (Exception e) { throw new WekaException(e); } }
@Override public double[] distributionForInstance(Instance instance) throws Exception { int c = instance.classIndex(); double result[] = new double[c]; Instance finstances[] = convertInstance(instance,c); for (int i = 0; i < c; i++) { result[i] = m_MultiClassifiers[i].classifyInstance(finstances[i]); //result[i] = m_MultiClassifiers[i].distributionForInstance(finstances[i])[1]; } return result; }
@Override public double[] distributionForInstance(Instance instance) throws Exception { int c = instance.classIndex(); double result[] = new double[c]; Instance finstances[] = convertInstance(instance,c); for (int i = 0; i < c; i++) { result[i] = m_MultiClassifiers[i].classifyInstance(finstances[i]); //result[i] = m_MultiClassifiers[i].distributionForInstance(finstances[i])[1]; } return result; }
public static final boolean classify(final double[] vector) throws Exception { // Obtain or generate a Thread-local instance Operator op; synchronized (table) { // avoid clashes within weka final Thread t = Thread.currentThread(); op = table.get(t); if (null == op) { op = new Operator(); table.put(t, op); } } // Future weka versions will use new DenseInstance(1, vector) instead final Instance ins = new DenseInstance(1, vector); ins.setDataset(op.data); // Was trained to return true or false, represented in weka as 0 or 1 return 1 == ((int) Math.round(op.c.classifyInstance(ins))); }
/** * Add a label to an instance using a classifier * * @param classifier the classifier to use * @param inst the instance to append prediction to * @param instOrig the original instance * @throws WekaException if a problem occurs */ protected void predictLabelClassifier(weka.classifiers.Classifier classifier, Instance inst, Instance instOrig) throws WekaException { try { double pred = classifier.classifyInstance(instOrig); inst.setValue(inst.numAttributes() - 1, pred); } catch (Exception ex) { throw new WekaException(ex); } }
@Override public double[] distributionForInstance(Instance x) throws Exception { int L = x.classIndex(); //if there is only one class (as for e.g. in some hier. mtds) predict it if(L == 1) return new double[]{1.0}; Instance x_ = PSUtils.convertInstance(x,L,m_InstancesTemplate); //convertInstance(x,L); x_.setDataset(m_InstancesTemplate); //Get a classification double y[] = new double[x_.numClasses()]; y[(int)m_Classifier.classifyInstance(x_)] = 1.0; return PSUtils.convertDistribution(y,L,m_InstancesTemplate); }
@Override public double[] distributionForInstance(Instance x) throws Exception { int L = x.classIndex(); //if there is only one class (as for e.g. in some hier. mtds) predict it if(L == 1) return new double[]{1.0}; Instance x_ = PSUtils.convertInstance(x,L,m_InstancesTemplate); //convertInstance(x,L); x_.setDataset(m_InstancesTemplate); //Get a classification double y[] = new double[x_.numClasses()]; y[(int)m_Classifier.classifyInstance(x_)] = 1.0; return PSUtils.convertDistribution(y,L,m_InstancesTemplate); }
@Override public double[] distributionForInstance(Instance mlInstance) throws Exception { int c = mlInstance.classIndex(); //if there is only one class (as for e.g. in some hier. mtds) predict it if(c == 1) return new double[]{1.0}; Instance slInstance = convertInstance(mlInstance,c); slInstance.setDataset(m_InstancesTemplate); //Get a classification double result[] = new double[slInstance.numClasses()]; result[(int)m_Classifier.classifyInstance(slInstance)] = 1.0; return convertDistribution(result,c); }
public static void LoadAndTest(String filename_test, String filename_model) throws Exception { BufferedReader datafile_test = readDataFile(filename_test); Instances data_test = new Instances(datafile_test); data_test.setClassIndex(data_test.numAttributes() - 1); Classifier cls = (Classifier) weka.core.SerializationHelper.read(filename_model); int act = 0; for (int i = 0; i < data_test.numInstances(); i++) { double pred = cls.classifyInstance(data_test.instance(i)); double real = data_test.instance(i).classValue(); if (pred==real) { act = act + 1; } } double pct = (double) act / (double) data_test.numInstances(); System.out.println("Accuracy = " + pct); }
private void classify(Instance test) throws Exception { // copy Instance copy = (Instance)test.copy(); copy.setDataset(null); // delete attributes we don't need for(int i = excld.length-1; i >= 0; i--) { copy.deleteAttributeAt(this.excld[i]); } //set template copy.setDataset(this._template); //set class test.setValue(this.index,(int)(this.classifier.classifyInstance(copy))); //carry on if (next!=null) next.classify(test); }
private void classify(Instance test) throws Exception { // copy Instance copy = (Instance)test.copy(); copy.setDataset(null); // delete attributes we don't need for(int i = excld.length-1; i >= 0; i--) { copy.deleteAttributeAt(this.excld[i]); } //set template copy.setDataset(this._template); //set class test.setValue(this.index,(int)(this.classifier.classifyInstance(copy))); //carry on if (next!=null) next.classify(test); }
private void classify(Instance test) throws Exception { // copy Instance copy = (Instance)test.copy(); copy.setDataset(null); // delete attributes we don't need for(int i = excld.length-1; i >= 0; i--) { copy.deleteAttributeAt(this.excld[i]); } //set template copy.setDataset(this._template); //set class test.setValue(this.index,(int)(this.classifier.classifyInstance(copy))); //carry on if (next!=null) next.classify(test); }
@Override public String classify(Instance instance) throws Exception { weka.core.Instance wekaInstance = new weka.core.Instance(wekaTrainingData.numAttributes()); wekaInstance.setDataset(wekaTrainingData); double[] histogramPercent = instance.getHistogramPercent(); for (int i = 0; i < histogramPercent.length; i++) { wekaInstance.setValue(i, histogramPercent[i]); } wekaInstance.setMissing(wekaTrainingData.attribute("class")); double wekaClassification = classifier.classifyInstance(wekaInstance); String classification = wekaTrainingData.attribute("class").value((int)wekaClassification); return classification; }