List of usage examples for weka.core Instance setClassMissing
public void setClassMissing();
From source file:at.aictopic1.sentimentanalysis.machinelearning.impl.TwitterClassifer.java
public Integer classify(Tweet[] tweets) { // TEST/*from w ww . ja va 2 s . c o m*/ // Generate two tweet examples Tweet exOne = new Tweet("This is good and fantastic"); exOne.setPreprocessedText("This is good and fantastic"); Tweet exTwo = new Tweet("Horribly, terribly bad and more"); exTwo.setPreprocessedText("Horribly, terribly bad and more"); Tweet exThree = new Tweet( "I want to update lj and read my friends list, but I\\'m groggy and sick and blargh."); exThree.setPreprocessedText( "I want to update lj and read my friends list, but I\\'m groggy and sick and blargh."); Tweet exFour = new Tweet("bad hate worst sick"); exFour.setPreprocessedText("bad hate worst sick"); tweets = new Tweet[] { exOne, exTwo, exThree, exFour }; // TEST // Load model // loadModel(); // Convert Tweet to Instance type // Get String Data // Create attributes for the Instances set Attribute twitter_id = new Attribute("twitter_id"); // Attribute body = new Attribute("body"); FastVector classVal = new FastVector(2); classVal.addElement("pos"); classVal.addElement("neg"); Attribute class_attr = new Attribute("class_attr", classVal); // Add them to a list FastVector attrVector = new FastVector(3); // attrVector.addElement(twitter_id); // attrVector.addElement(new Attribute("body", (FastVector) null)); // attrVector.addElement(class_attr); // Get the number of tweets and then create predictSet int numTweets = tweets.length; Enumeration structAttrs = dataStructure.enumerateAttributes(); // ArrayList<Attribute> attrList = new ArrayList<Attribute>(dataStructure.numAttributes()); while (structAttrs.hasMoreElements()) { attrVector.addElement((Attribute) structAttrs.nextElement()); } Instances predictSet = new Instances("predictInstances", attrVector, numTweets); // Instances predictSet = new Instances(dataStructure); predictSet.setClassIndex(2); // init prediction double prediction = -1; System.out.println("PredictSet matches source structure: " + predictSet.equalHeaders(dataStructure)); System.out.println("PredSet struct: " + predictSet.attribute(0)); System.out.println("PredSet struct: " + predictSet.attribute(1)); System.out.println("PredSet struct: " + predictSet.attribute(2)); // Array to return predictions //double[] tweetsClassified = new double[2][numTweets]; //List<Integer, Double> tweetsClass = new ArrayList<Integer, Double>(numTweets); for (int i = 0; i < numTweets; i++) { String content = (String) tweets[i].getPreprocessedText(); System.out.println("Tweet content: " + content); // attrList Instance tweetInstance = new Instance(predictSet.numAttributes()); tweetInstance.setDataset(predictSet); tweetInstance.setValue(predictSet.attribute(0), i); tweetInstance.setValue(predictSet.attribute(1), content); tweetInstance.setClassMissing(); predictSet.add(tweetInstance); try { // Apply string filter StringToWordVector filter = new StringToWordVector(); filter.setInputFormat(predictSet); Instances filteredPredictSet = Filter.useFilter(predictSet, filter); // Apply model prediction = trainedModel.classifyInstance(filteredPredictSet.instance(i)); filteredPredictSet.instance(i).setClassValue(prediction); System.out.println("Classification: " + filteredPredictSet.instance(i).toString()); System.out.println("Prediction: " + prediction); } catch (Exception ex) { Logger.getLogger(TwitterClassifer.class.getName()).log(Level.SEVERE, null, ex); } } return 0; }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Evaluates the classifier on a single instance and records the prediction * (if the class is nominal).// w w w .j a va 2 s. co m * * @param classifier machine learning classifier * @param instance the test instance to be classified * @return the prediction made by the clasifier * @throws Exception if model could not be evaluated successfully or the data * contains string attributes */ public double evaluateModelOnceAndRecordPrediction(List<LibSVM> classifier, List<Double> classifierWeight, Instance instance) throws Exception { Instance classMissing = (Instance) instance.copy(); double pred = 0; classMissing.setDataset(instance.dataset()); classMissing.setClassMissing(); if (m_ClassIsNominal) { if (m_Predictions == null) { m_Predictions = new FastVector(); } List<double[]> prob = new ArrayList<double[]>();// double[] finalProb = new double[instance.numClasses()]; for (int i = 0; i < classifier.size(); i++) { double[] dist = classifier.get(i).distributionForInstance(classMissing);// prob.add(dist); } for (int i = 0; i < finalProb.length; i++) { for (int j = 0; j < classifier.size(); j++) { finalProb[i] += prob.get(j)[i] * classifierWeight.get(j); } } double sum = 0; for (int i = 0; i < finalProb.length; i++) { sum += finalProb[i]; } for (int i = 0; i < finalProb.length; i++) { finalProb[i] = finalProb[i] / sum; } pred = Utils.maxIndex(finalProb); if (finalProb[(int) pred] <= 0) { pred = Instance.missingValue(); } updateStatsForClassifier(finalProb, instance); m_Predictions.addElement(new NominalPrediction(instance.classValue(), finalProb, instance.weight())); } else { pred = classifier.get(0).classifyInstance(classMissing); updateStatsForPredictor(pred, instance); } return pred; }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Evaluates the classifier on a single instance. * //from w w w . java2 s . co m * @param classifier machine learning classifier * @param instance the test instance to be classified * @return the prediction made by the clasifier * @throws Exception if model could not be evaluated successfully or the data * contains string attributes */ public double evaluateModelOnce(Classifier classifier, Instance instance) throws Exception { Instance classMissing = (Instance) instance.copy(); double pred = 0; classMissing.setDataset(instance.dataset()); classMissing.setClassMissing(); if (m_ClassIsNominal) { double[] dist = classifier.distributionForInstance(classMissing); pred = Utils.maxIndex(dist); if (dist[(int) pred] <= 0) { pred = Instance.missingValue(); } updateStatsForClassifier(dist, instance); } else { pred = classifier.classifyInstance(classMissing); updateStatsForPredictor(pred, instance); } return pred; }
From source file:core.classification.Classifiers.java
License:Open Source License
private Instance buildYNCInstance(Context p, Context c) { double RAREA = 0, H = 0, D = 0, V = 0.; int parentClass = 1, childClass = 1; RAREA = ((double) c.area()) / p.area(); H = c.getH(p);/* www . j av a2 s . co m*/ D = c.getD(p); V = c.getDX(p); parentClass = (p.getSymbolClass() > 0) ? p.getSymbolClass() : 1; childClass = (c.getSymbolClass() > 0) ? c.getSymbolClass() : 1; double[] values = new double[7]; values[0] = dataStructYC.attribute(0).indexOfValue("" + parentClass); values[1] = dataStructYC.attribute(1).indexOfValue("" + childClass); values[2] = RAREA; values[3] = H; values[4] = D; values[5] = V; values[6] = dataStructYC.attribute(6).indexOfValue("N"); Instance inst = new Instance(1.0, values); inst.setDataset(dataStructYC); inst.setClassMissing(); return inst; }
From source file:core.me.Context.java
License:Open Source License
public Instance buildSCAInstance() { Instances dataStruc = Classifiers.getInst(false).getDataStructSCA(); double[] values = new double[2]; values[0] = this.theSymbol.getRatio(); values[1] = dataStruc.attribute(1).indexOfValue("0"); Instance inst = new Instance(1.0, values); inst.setDataset(dataStruc);// w ww.ja va 2 s . co m inst.setClassMissing(); return inst; }
From source file:core.me.Context.java
License:Open Source License
public Instance buildSCBInstance() { Instances dataStruc = Classifiers.getInst(false).getDataStructSCB(); double H = 0, D = 0, DX = 0.; int parentClass = 1; if (this.getParent() != null) { H = this.getH(); D = this.getD(); DX = this.getDX(); parentClass = this.getParentSymbol().getSymbolClass(); }//from w ww .j a v a 2s . c o m double[] values = new double[5]; values[0] = H; values[1] = D; values[2] = DX; values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(parentClass)); values[4] = dataStruc.attribute(4).indexOfValue("0"); Instance inst = new Instance(1.0, values); inst.setDataset(dataStruc); inst.setClassMissing(); return inst; }
From source file:core.me.Context.java
License:Open Source License
public Instance buildSCC1Instance() { Instances dataStruc = Classifiers.getInst(false).getDataStructSCC(Relationship.INLINE); double LH, LD, LDX; int LCLASS;/*from w ww.j a va2 s.c o m*/ double height = (double) this.theSymbol.getHeight(); LH = height / ((double) this.horSymbol.getHeight()); LD = (this.theSymbol.getCenter() - this.horSymbol.getCenter()) / height; LDX = this.getHor().getDX(); LCLASS = this.horSymbol.getSymbolClass(); double[] values = new double[5]; values[0] = LH; values[1] = LD; values[2] = LDX; values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(LCLASS)); values[4] = dataStruc.attribute(4).indexOfValue("0"); Instance inst = new Instance(1.0, values); inst.setDataset(dataStruc); inst.setClassMissing(); return inst; }
From source file:core.me.Context.java
License:Open Source License
public Instance buildSCC2Instance() { Instances dataStruc = Classifiers.getInst(false).getDataStructSCC(Relationship.SUPERSCRIPT); double EH, ED, EDX; int ECLASS;// ww w . j a v a2 s . c o m double height = (double) this.theSymbol.getHeight(); EH = height / ((double) this.supSymbol.getHeight()); ED = (this.theSymbol.getCenter() - this.supSymbol.getCenter()) / height; EDX = this.getSup().getDX(); ECLASS = this.supSymbol.getSymbolClass(); double[] values = new double[5]; values[0] = EH; values[1] = ED; values[2] = EDX; values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(ECLASS)); values[4] = dataStruc.attribute(4).indexOfValue("0"); Instance inst = new Instance(1.0, values); inst.setDataset(dataStruc); inst.setClassMissing(); return inst; }
From source file:core.me.Context.java
License:Open Source License
public Instance buildSCC3Instance() { Instances dataStruc = Classifiers.getInst(false).getDataStructSCC(Relationship.SUBSCRIPT); double SH, SD, SDX; int SCLASS;//from w w w . java2 s . c o m double height = (double) this.theSymbol.getHeight(); SH = height / ((double) this.subSymbol.getHeight()); SD = (this.theSymbol.getCenter() - this.subSymbol.getCenter()) / height; SDX = this.getSub().getDX(); SCLASS = this.subSymbol.getSymbolClass(); double[] values = new double[5]; values[0] = SH; values[1] = SD; values[2] = SDX; values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(SCLASS)); values[4] = dataStruc.attribute(4).indexOfValue("0"); Instance inst = new Instance(1.0, values); inst.setDataset(dataStruc); inst.setClassMissing(); return inst; }
From source file:core.me.Context.java
License:Open Source License
public Instance buildRCInstance() { Instances dataStruc = Classifiers.getInst(false).getDataStructRC(); double H = 0, D = 0, DX = 0.; int parentClass = 1; if (this.getParent() != null) { H = ((double) this.getParentSymbol().getHeight()) / this.theSymbol.getHeight(); D = (this.getParentSymbol().getCenter() - this.theSymbol.getCenter()) / this.getParentSymbol().getHeight(); DX = this.getDX(); parentClass = this.getParentSymbol().getSymbolClass(); }/* ww w. j av a 2 s .c o m*/ double[] values = new double[6]; values[0] = H; values[1] = D; values[2] = DX; values[3] = dataStruc.attribute(3).indexOfValue("" + this.theClass.get()); values[4] = dataStruc.attribute(4).indexOfValue("" + parentClass); values[5] = dataStruc.attribute(5).indexOfValue("0"); Instance inst = new Instance(1.0, values); inst.setDataset(dataStruc); inst.setClassMissing(); return inst; }