List of usage examples for weka.core Instance setMissing
public void setMissing(Attribute att);
From source file:PredictMention.java
protected void setTestData(String title, String description, String keywords) { testData = new Instances(trainedData); testData.clear();/*from w ww . ja v a 2 s .c om*/ Instance inst = new DenseInstance(4); inst.setDataset(testData); inst.setValue(0, title); inst.setValue(1, description); inst.setValue(2, keywords); inst.setMissing(3); testData.add(inst); }
From source file:adams.flow.transformer.WekaSetInstanceValue.java
License:Open Source License
/** * Executes the flow item./*from w w w . j a va 2s . co m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instance inst; int index; result = null; inst = (Instance) m_InputToken.getPayload(); inst = (Instance) inst.copy(); m_Index.setData(inst.dataset()); index = m_Index.getIntIndex(); try { if (m_Value.equals("?")) { inst.setMissing(index); } else { switch (inst.attribute(index).type()) { case Attribute.NUMERIC: inst.setValue(index, Utils.toDouble(m_Value)); break; case Attribute.DATE: inst.setValue(index, inst.attribute(index).parseDate(m_Value)); break; case Attribute.NOMINAL: case Attribute.STRING: inst.setValue(index, m_Value); break; case Attribute.RELATIONAL: result = "Relational attributes cannot be set!"; break; default: result = "Unhandled attribute type: " + inst.attribute(index).type(); } } } catch (Exception e) { result = handleException("Failed to set value: " + m_Index.getIndex() + " -> " + m_Value, e); } // broadcast data if (result == null) m_OutputToken = new Token(inst); return result; }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * store the prediction made by the classifier as a string * /*from ww w . j av a2 s. c o m*/ * @param classifier the classifier to use * @param inst the instance to generate text from * @param instNum the index in the dataset * @param attributesToOutput the indices of the attributes to output * @param printDistribution prints the complete distribution for nominal * classes, not just the predicted value * @return the prediction as a String * @throws Exception if something goes wrong * @see #printClassifications(Classifier, Instances, String, int, Range, * boolean) */ protected static String predictionText(Classifier classifier, Instance inst, int instNum, Range attributesToOutput, boolean printDistribution) throws Exception { StringBuffer result = new StringBuffer(); int width = 10; int prec = 3; Instance withMissing = (Instance) inst.copy(); withMissing.setDataset(inst.dataset()); withMissing.setMissing(withMissing.classIndex()); double predValue = classifier.classifyInstance(withMissing); // index result.append(Utils.padLeft("" + (instNum + 1), 6)); if (inst.dataset().classAttribute().isNumeric()) { // actual if (inst.classIsMissing()) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.doubleToString(inst.classValue(), width, prec)); } // predicted if (Instance.isMissingValue(predValue)) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.doubleToString(predValue, width, prec)); } // error if (Instance.isMissingValue(predValue) || inst.classIsMissing()) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec)); } } else { // actual result.append(" " + Utils.padLeft(((int) inst.classValue() + 1) + ":" + inst.toString(inst.classIndex()), width)); // predicted if (Instance.isMissingValue(predValue)) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.padLeft( ((int) predValue + 1) + ":" + inst.dataset().classAttribute().value((int) predValue), width)); } // error? if (!Instance.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue + 1 != (int) inst.classValue() + 1)) { result.append(" " + " + "); } else { result.append(" " + " "); } // prediction/distribution if (printDistribution) { if (Instance.isMissingValue(predValue)) { result.append(" " + "?"); } else { result.append(" "); double[] dist = classifier.distributionForInstance(withMissing); for (int n = 0; n < dist.length; n++) { if (n > 0) { result.append(","); } if (n == (int) predValue) { result.append("*"); } result.append(Utils.doubleToString(dist[n], prec)); } } } else { if (Instance.isMissingValue(predValue)) { result.append(" " + "?"); } else { result.append(" " + Utils.doubleToString( classifier.distributionForInstance(withMissing)[(int) predValue], prec)); } } } // attributes result.append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n"); return result.toString(); }
From source file:br.ufpe.cin.mpos.offload.DynamicDecisionSystem.java
License:Apache License
public synchronized boolean isRemoteAdvantage(int InputSize, Remotable.Classifier classifierRemotable) { boolean resp = false; try {// w ww . j av a 2s .c o m if ((!(this.classifierModel.equals(classifierRemotable.toString()))) || this.classifier == null) { Log.d("classificacao", "classificador=" + classifierRemotable.toString()); this.classifierModel = classifierRemotable.toString(); loadClassifier(classifierRemotable); } Cursor c = dc.getData(); int colunas = c.getColumnCount(); Instance instance = new DenseInstance(colunas - 2); ArrayList<String> values = new ArrayList<String>(); ArrayList<Attribute> atts = new ArrayList<Attribute>(); if (c.moveToFirst()) { for (int i = 1; i <= colunas - 2; i++) { String feature = c.getColumnName(i); String value = c.getString(i); Attribute attribute; if (feature.equals(DatabaseManager.InputSize)) { values.add("" + InputSize); attribute = new Attribute(DatabaseManager.InputSize); } else { String[] strings = populateAttributes(i); ArrayList<String> attValues = new ArrayList<String>(Arrays.asList(strings)); attribute = new Attribute(feature, attValues); if (value != null) { values.add(value); } } atts.add(attribute); } Instances instances = new Instances("header", atts, atts.size()); instances.setClassIndex(instances.numAttributes() - 1); instance.setDataset(instances); for (int i = 0; i < atts.size(); i++) { if (i == 9) { instance.setMissing(atts.get(9)); } else if (atts.get(i).name().equals(DatabaseManager.InputSize)) { instance.setValue(atts.get(i), InputSize); } else { instance.setValue(atts.get(i), values.get(i)); } } double value = -1; value = classifier.distributionForInstance(instance)[0]; Log.d("classificacao", instance.toString() + " classifiquei com o seguinte valor" + value); resp = (0.7 <= value); if (resp) { Log.d("classificacao", "sim"); Log.d("Finalizado", "classifiquei " + instance.toString() + " com sim"); } else { Log.d("classificacao", "nao"); Log.d("Finalizado", "classifiquei " + instance.toString() + " com nao"); } } } catch (Exception e) { e.printStackTrace(); Log.e("sqlLite", e.getMessage()); Log.e("sqlLite", "Causa: " + e.getCause()); } return resp; }
From source file:ca.uottawa.balie.WekaLearner.java
License:Open Source License
private Instance CreateInstance2(Object[] pi_Instance, String pi_Class) { Instance inst = new Instance(pi_Instance.length + 1); for (int i = 0; i != pi_Instance.length; ++i) { if (pi_Instance[i] instanceof Double) { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), ((Double) pi_Instance[i]).doubleValue()); } else if (pi_Instance[i] instanceof String) { try { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), (String) pi_Instance[i]); } catch (Exception e) { if (DEBUG) DebugInfo.Out("Unknow attribute for learner: " + (String) pi_Instance[i]); inst.setMissing((Attribute) m_WekaAttributes.elementAt(i)); }/*from w ww . jav a2 s. c o m*/ } } inst.setValue((Attribute) m_WekaAttributes.lastElement(), pi_Class); return inst; }
From source file:ca.uottawa.balie.WekaLearner.java
License:Open Source License
public Instance CreateUnlabeledInstance(Object[] pi_Instance) { Instance inst = new Instance(pi_Instance.length + 1); for (int i = 0; i != pi_Instance.length; ++i) { if (pi_Instance[i] instanceof Double) { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), ((Double) pi_Instance[i]).doubleValue()); } else if (pi_Instance[i] instanceof String) { try { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), (String) pi_Instance[i]); } catch (Exception e) { if (DEBUG) DebugInfo.Out("Unknow attribute for learner: " + (String) pi_Instance[i]); inst.setMissing((Attribute) m_WekaAttributes.elementAt(i)); }//from w w w . ja v a2s. c om } } inst.setMissing((Attribute) m_WekaAttributes.lastElement()); return inst; }
From source file:ca.uottawa.balie.WekaLearner.java
License:Open Source License
private Instance CreateInstance(Object[] pi_Instance) { Instance inst = new Instance(pi_Instance.length + 1); for (int i = 0; i != pi_Instance.length; ++i) { if (pi_Instance[i] instanceof Double) { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), ((Double) pi_Instance[i]).doubleValue()); } else if (pi_Instance[i] instanceof String) { try { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), (String) pi_Instance[i]); } catch (Exception e) { if (DEBUG) DebugInfo.Out("Unknow attribute for learner: " + (String) pi_Instance[i]); inst.setMissing((Attribute) m_WekaAttributes.elementAt(i)); }/*from w w w . j ava 2s. c om*/ } } return inst; }
From source file:ca.uottawa.balie.WekaLearner.java
License:Open Source License
/** * Classify an unseen instance using the learned classifier. * Ouput the likelihood of each class./*from ww w. j a va 2s . c o m*/ * * @param pi_Instance The instance, an array of objects (can mix numeric and nominal attributes - see {@link WekaAttribute}) * @return An array of probabilities, parallel to the possible classes * @see WekaAttribute */ public double[] GetDistribution(Object[] pi_Instance) { Instance inst = new Instance(pi_Instance.length + 1); for (int i = 0; i != pi_Instance.length; ++i) { if (pi_Instance[i] instanceof Double) { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), ((Double) pi_Instance[i]).doubleValue()); } else if (pi_Instance[i] instanceof String) { try { inst.setValue((Attribute) m_WekaAttributes.elementAt(i), (String) pi_Instance[i]); } catch (Exception e) { if (DEBUG) DebugInfo.Out("Unknow attribute for learner: " + (String) pi_Instance[i]); inst.setMissing((Attribute) m_WekaAttributes.elementAt(i)); } } } inst.setDataset(m_TrainingSet); double fDistribution[] = null; try { fDistribution = m_Scheme.distributionForInstance(inst); } catch (Exception e) { System.out.println(e.getMessage()); } return fDistribution; }
From source file:com.zazhu.BlueHub.BlueHub.java
License:Apache License
/** * receives the last reads from the sensors and creates the features we use * only the acc x,y,z (either from internal or external sensor) * /* w w w .j av a 2s . c o m*/ * @param sensorQueue * @throws Exception */ private Instance processingSenseData(Queue<String> sensorQueue, char whatSensor) throws Exception { BufferedReader reader; Instances format; Instance newInstance = null; Log.d(TAG, "Queue size = " + mQueueSize); if (sensorQueue.size() <= 0) throw new Exception("Queue empty"); // create the arrays that will contain the accelerometer data // s.x s.y s.z double[] sx = new double[sensorQueue.size()]; double[] sy = new double[sensorQueue.size()]; double[] sz = new double[sensorQueue.size()]; String rawReading; StringTokenizer st; int index; if (D) Log.e(TAG, "+++ COMPUTING FEATURES +++"); // 1. collect raw data. what kind of sensing data? external vs. internal switch (whatSensor) { case EXTERNAL: index = 0; while ((rawReading = sensorQueue.poll()) != null) { // FORMAT: // "Time_SensorName_SensorNumber_Counter_Xacc_Yacc_Zacc_Xgyro_Ygyro_checksum" // position of the values needed: s.x = 4, s.y = 5, s.z = 6 st = new StringTokenizer(rawReading, FIELD_SEP); // not needed data for (int i = 0; i < 4; i++) st.nextToken(); // s.x, s.y, s.z sx[index] = Double.valueOf(st.nextToken()); sy[index] = Double.valueOf(st.nextToken()); sz[index] = Double.valueOf(st.nextToken()); index += 1; } // 2. process raw data // 2.1 read the input format for the instance (TODO must be changed to // use weka classes) reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.format_extern))); try { format = new Instances(reader); if (format.classIndex() == -1) format.setClassIndex(format.numAttributes() - 1); // 2.2 create a new instance newInstance = new DenseInstance(7); newInstance.setDataset(format); // set attributes newInstance.setValue(format.attribute(0), Feature.getStd(sx)); newInstance.setValue(format.attribute(1), Feature.getStd(sy)); newInstance.setValue(format.attribute(2), Feature.getStd(sz)); newInstance.setValue(format.attribute(3), Feature.getMean(sx)); newInstance.setValue(format.attribute(4), Feature.getMean(sy)); newInstance.setValue(format.attribute(5), Feature.getMean(sz)); // set unknown class newInstance.setMissing(format.attribute(6)); } catch (IOException e) { e.printStackTrace(); } break; case INTERNAL: index = 0; while ((rawReading = sensorQueue.poll()) != null) { // FORMAT "Xacc_Yacc_Zacc" // position of the values needed: s.x = 0, s.y = 1, s.z = 2 st = new StringTokenizer(rawReading, FIELD_SEP); // s.x, s.y, s.z sx[index] = Double.valueOf(st.nextToken()); sy[index] = Double.valueOf(st.nextToken()); sz[index] = Double.valueOf(st.nextToken()); index += 1; } // 2. process raw data // 2.1 read the input format for the instance (TODO must be changed to // use weka classes) reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.format_intern))); try { format = new Instances(reader); if (format.classIndex() == -1) format.setClassIndex(format.numAttributes() - 1); // 2.2 create a new instance newInstance = new DenseInstance(7); newInstance.setDataset(format); // set attributes newInstance.setValue(format.attribute(0), Feature.getStd(sx)); newInstance.setValue(format.attribute(1), Feature.getStd(sy)); newInstance.setValue(format.attribute(2), Feature.getStd(sz)); newInstance.setValue(format.attribute(3), Feature.getMean(sx)); newInstance.setValue(format.attribute(4), Feature.getMean(sy)); newInstance.setValue(format.attribute(5), Feature.getMean(sz)); // set unknown class newInstance.setMissing(format.attribute(6)); } catch (IOException e) { e.printStackTrace(); } break; default: if (D) Log.e(TAG, "+++ COMPUTING FEATURES: NO VALUE FOR THE SENSOR READING +++"); break; } return newInstance; }
From source file:cotraining.copy.Evaluation_D.java
License:Open Source License
/** * store the prediction made by the classifier as a string * /* w w w.j av a 2s .c om*/ * @param classifier the classifier to use * @param inst the instance to generate text from * @param instNum the index in the dataset * @param attributesToOutput the indices of the attributes to output * @param printDistribution prints the complete distribution for nominal * classes, not just the predicted value * @return the prediction as a String * @throws Exception if something goes wrong * @see #printClassifications(Classifier, Instances, String, int, Range, boolean) */ protected static String predictionText(Classifier classifier, Instance inst, int instNum, Range attributesToOutput, boolean printDistribution) throws Exception { StringBuffer result = new StringBuffer(); int width = 10; int prec = 3; Instance withMissing = (Instance) inst.copy(); withMissing.setDataset(inst.dataset()); withMissing.setMissing(withMissing.classIndex()); double predValue = classifier.classifyInstance(withMissing); // index result.append(Utils.padLeft("" + (instNum + 1), 6)); if (inst.dataset().classAttribute().isNumeric()) { // actual if (inst.classIsMissing()) result.append(" " + Utils.padLeft("?", width)); else result.append(" " + Utils.doubleToString(inst.classValue(), width, prec)); // predicted if (Instance.isMissingValue(predValue)) result.append(" " + Utils.padLeft("?", width)); else result.append(" " + Utils.doubleToString(predValue, width, prec)); // error if (Instance.isMissingValue(predValue) || inst.classIsMissing()) result.append(" " + Utils.padLeft("?", width)); else result.append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec)); } else { // actual result.append(" " + Utils.padLeft(((int) inst.classValue() + 1) + ":" + inst.toString(inst.classIndex()), width)); // predicted if (Instance.isMissingValue(predValue)) result.append(" " + Utils.padLeft("?", width)); else result.append(" " + Utils.padLeft( ((int) predValue + 1) + ":" + inst.dataset().classAttribute().value((int) predValue), width)); // error? if (!Instance.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue + 1 != (int) inst.classValue() + 1)) result.append(" " + " + "); else result.append(" " + " "); // prediction/distribution if (printDistribution) { if (Instance.isMissingValue(predValue)) { result.append(" " + "?"); } else { result.append(" "); double[] dist = classifier.distributionForInstance(withMissing); for (int n = 0; n < dist.length; n++) { if (n > 0) result.append(","); if (n == (int) predValue) result.append("*"); result.append(Utils.doubleToString(dist[n], prec)); } } } else { if (Instance.isMissingValue(predValue)) result.append(" " + "?"); else result.append(" " + Utils.doubleToString( classifier.distributionForInstance(withMissing)[(int) predValue], prec)); } } // attributes result.append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n"); return result.toString(); }