List of usage examples for weka.core Instance setDataset
public void setDataset(Instances instances);
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.c om * * @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. * /*w w w . j a v a2 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:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * store the prediction made by the classifier as a string * //from w w w .ja v a 2 s .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(); }
From source file:boostingPL.boosting.InstancesHelper.java
License:Open Source License
public static Instance createInstance(String text, Instances insts) { // numeric attributes String[] items = text.split(","); double[] ds = new double[items.length]; for (int i = 0; i < ds.length - 1; i++) { ds[i] = Double.parseDouble(items[i]); }// w w w . j a va2 s.c om // nominal class attribute ds[items.length - 1] = insts.classAttribute().indexOfValue(items[items.length - 1]); Instance inst = new DenseInstance(1, ds); inst.setDataset(insts); return inst; }
From source file:br.fapesp.myutils.MyUtils.java
License:Open Source License
/** * Generates a Gaussian data set with K clusters and m dimensions * /*w ww .j a v a 2s. c o m*/ * @param centers * K x m matrix * @param sigmas * K x m matrix * @param pointsPerCluster * number of points per cluster * @param seed * for the RNG * @param randomize * should the order of the instances be randomized? * @param supervised * should class label be present? if true, the class is the m+1 * attribute * * @return */ public static Instances genGaussianDataset(double[][] centers, double[][] sigmas, int pointsPerCluster, long seed, boolean randomize, boolean supervised) { Random r = new Random(seed); int K = centers.length; // number of clusters int m = centers[0].length; // number of dimensions FastVector atts = new FastVector(m); for (int i = 0; i < m; i++) atts.addElement(new Attribute("at" + i)); if (supervised) { FastVector cls = new FastVector(K); for (int i = 0; i < K; i++) cls.addElement("Gauss-" + i); atts.addElement(new Attribute("Class", cls)); } Instances data; if (supervised) data = new Instances(K + "-Gaussians-supervised", atts, K * pointsPerCluster); else data = new Instances(K + "-Gaussians", atts, K * pointsPerCluster); if (supervised) data.setClassIndex(m); Instance ith; for (int i = 0; i < K; i++) { for (int j = 0; j < pointsPerCluster; j++) { if (!supervised) ith = new DenseInstance(m); else ith = new DenseInstance(m + 1); ith.setDataset(data); for (int k = 0; k < m; k++) ith.setValue(k, centers[i][k] + (r.nextGaussian() * sigmas[i][k])); if (supervised) ith.setValue(m, "Gauss-" + i); data.add(ith); } } // run randomization filter if desired if (randomize) data.randomize(r); return data; }
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 w w. j av a2 s . 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
/** * Classify an unseen instance using the learned classifier. * Output the most probable class.// www . j ava2s.c o m * * @param pi_Instance The instance, an array of objects (can mix numeric and nominal attributes - see {@link WekaAttribute}) * @return The index of the most probable class * @see WekaAttribute */ public double Classify(Object[] pi_Instance) { Instance inst = CreateInstance(pi_Instance); inst.setDataset(m_TrainingSet); double out = 0; try { out = m_Scheme.classifyInstance(inst); } catch (Exception e) { System.out.println(e.getMessage()); } return out; }
From source file:ca.uottawa.balie.WekaLearner.java
License:Open Source License
/** * Cluster an unseen instance using the learned clusterizer. * Output the most probable cluster.// ww w . jav a 2s. com * * @param pi_Instance The instance, an array of objects (can mix numeric and nominal attributes - see {@link WekaAttribute}) * @return The index of the most probable cluster * @see WekaAttribute */ public double Cluster(Object[] pi_Instance) { Instance inst = CreateInstance(pi_Instance); inst.setDataset(m_TrainingSet); double out = 0; try { out = m_ClusterScheme.clusterInstance(inst); } catch (Exception e) { System.out.println(e.getMessage()); } return out; }
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./* w w w . j av a 2 s . 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:categorization.SpectralWEKA.java
License:Open Source License
public void buildClusterer(ArrayList<String> seqDB, double[][] sm) { seqList = seqDB;//from w w w . j a v a2 s . c o m this.setSimMatrix(sm); Attribute seqString = new Attribute("sequence", (FastVector) null); FastVector attrInfo = new FastVector(); attrInfo.addElement(seqString); Instances data = new Instances("data", attrInfo, 0); for (int i = 0; i < seqList.size(); i++) { Instance currentInst = new Instance(1); currentInst.setDataset(data); currentInst.setValue(0, seqList.get(i)); data.add(currentInst); } try { buildClusterer(data); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }