public List <string> Classify(string model, string test) { List <string> ret = new List <string>(); try { java.io.ObjectInputStream ois = new java.io.ObjectInputStream(new java.io.FileInputStream(model)); weka.classifiers.Classifier cl = (weka.classifiers.Classifier)ois.readObject(); ois.close(); weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(test)); insts.setClassIndex(insts.numAttributes() - 1); for (int i = 0; i < 1; i++) { weka.core.Instance currentInst = insts.instance(i); double predictedClass = cl.classifyInstance(currentInst); double[] distrs = cl.distributionForInstance(currentInst); //string actual = insts.classAttribute().value((int)currentInst.classValue()); //string predicted = insts.classAttribute().value((int)predictedClass); // System.Console.WriteLine("ID: " + (i + 1) + ", " + predicted); for (int j = 0; j < distrs.Length; j++) { string predicted = insts.classAttribute().value(j); string distr = distrs[j].ToString("#0.000"); ret.Add(predicted + "," + distr); } } return(ret); } catch { return(ret); } }
public int ClassifyInstance(double[] attributes, out double[] percentages) { double classificationResult = 1.0; testInstance.setDataset(dataSet); testInstance.setClassMissing(); dataSet.add(testInstance); for (int i = 0; i < attributes.Length; i++) { testInstance.setValue(i, attributes[i]); } classificationResult = m_cl.classifyInstance(testInstance); dataSet.delete(0); percentages = m_cl.distributionForInstance(testInstance); return((int)classificationResult); }
public void Test2() { java.io.ObjectInputStream ois = new java.io.ObjectInputStream(new java.io.FileInputStream("D:\\android_analysis\\som_model.model")); weka.classifiers.Classifier cl = (weka.classifiers.Classifier)ois.readObject(); ois.close(); weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader("D:\\android_analysis\\test1.arff")); insts.setClassIndex(insts.numAttributes() - 1); for (int i = 0; i < insts.numInstances(); i++) { weka.core.Instance currentInst = insts.instance(i); double predictedClass = cl.classifyInstance(currentInst); double[] distrs = cl.distributionForInstance(currentInst); //string actual = insts.classAttribute().value((int)currentInst.classValue()); //string predicted = insts.classAttribute().value((int)predictedClass); // System.Console.WriteLine("ID: " + (i + 1) + ", " + predicted); } }
public double[] ClassifyNewData(double[] newData) { weka.core.Instance newInstance = new weka.core.Instance(1, newData); newInstance.setDataset(playerData); return(classifier.distributionForInstance(newInstance)); }