I trained and created a MultilayerPerceptron model using weka.jar version 3.6.10 . I saved the model file to my computer and now I would like to use it to classify a single instance in my Java code. I would like to get a prediction for the attribute "class". i found answer here And I changed the values to what I needed. What I do is the following:
import weka.classifiers.Classifier; import weka.classifiers.functions.MultilayerPerceptron; import weka.core.Attribute; import weka.core.FastVector; import weka.core.Instance; import weka.core.Instances; import weka.core.SparseInstance; import weka.core.SerializationHelper; public class JavaApplication { public static void main(String[] args) { JavaApplication q = new JavaApplication(); double result = q.classify(-1.18,12.76,1.7297841); System.out.println(result); } private Instance inst_co; public double classify(double x, double y, double z) { // Create attributes to be used with classifiers // Test the model double result = -1; try { FastVector attributeList = new FastVector(); Attribute x_acc= new Attribute("x_acc"); Attribute y_acc= new Attribute("y_acc"); Attribute z_acc= new Attribute("z_acc"); FastVector classVal = new FastVector(); classVal.addElement("Walking"); classVal.addElement("Jogging"); classVal.addElement("Downstairs"); classVal.addElement("Sitting"); classVal.addElement("Upstairs"); attributeList.addElement(x_acc); attributeList.addElement(y_acc); attributeList.addElement(z_acc); attributeList.addElement(new Attribute("@@class@@",classVal)); Instances data = new Instances("TestInstances",attributeList,0); // Create instances for each pollutant with attribute values latitude, // longitude and pollutant itself inst_co = new SparseInstance(data.numAttributes()); data.add(inst_co); // Set instance's values for the attributes "latitude", "longitude", and // "pollutant concentration" inst_co.setValue(x_acc, x); inst_co.setValue(y_acc, y); inst_co.setValue(z_acc, z); // inst_co.setMissing(cluster); // load classifier from file Classifier cls_co = (MultilayerPerceptron) SerializationHelper .read("/Users/ALL-TECH/Desktop/Sensors application/FewDataGenerated/model.model"); result = cls_co.classifyInstance(inst_co); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
and my arff file looks like this :
@RELATION fewOfDataCsv @ATTRIBUTE x_acc NUMERIC @ATTRIBUTE y_acc NUMERIC @ATTRIBUTE z_acc NUMERIC @ATTRIBUTE class {Upstairs,Downstairs,Walking,Jogging,Sitting} @DATA -1.18,12.76,1.7297841 ,Upstairs 0.93,10.99,0.08172209 ,Upstairs 0.08,11.35,0.46309182 ,Upstairs 1.88,9.47,3.405087 ,Walking 0.89,9.38,3.3778462 ,Walking 1.38,11.54,3.336985 ,Walking 2.83,3.68,-3.255263 ,Jogging -1.8,2.45,7.082581 ,Jogging 16.63,9.89,-1.56634 ,Jogging 12.53,1.88,-6.3198414 ,Jogging 7.46,2.3,6.4 ,Sitting 7.5,2.3,6.44 ,Sitting 7.46,2.3,6.47 ,Sitting -1.23,8.28,0.040861044 ,Downstairs -1.92,6.28,1.1441092 ,Downstairs -1.73,5.75,2.152015 ,Downstairs
the result (i really don't know where that number coming from ):
run: 3.0 BUILD SUCCESSFUL (total time: 1 second)
there is somthing missing in my code ? if someone can help i will be thankful .
https://stackoverflow.com/questions/67341322/classifying-single-instance-in-weka-multilayerperceptron May 01, 2021 at 06:54AM
没有评论:
发表评论