List of usage examples for weka.core Instance dataset
public Instances dataset();
From source file:boostingPL.boosting.AdaBoostPL.java
License:Open Source License
@Override public double classifyInstance(Instance inst) throws Exception { int classNum = inst.dataset().classAttribute().numValues(); double[] H = new double[classNum]; for (int j = 0; j < corWeights.length; j++) { int classValue = merge(inst, j, classNum); if (classValue >= 0) { H[classValue] += corWeights[j]; }/* ww w. j a va2 s . co m*/ } return (double) maxIdx(H); }
From source file:boostingPL.boosting.AdaBoostPL.java
License:Open Source License
@Override public double[] distributionForInstance(Instance inst) throws Exception { int classNum = inst.dataset().classAttribute().numValues(); double[] H = new double[classNum]; double sum = 0; for (int j = 0; j < corWeights.length; j++) { int classValue = merge(inst, j, classNum); if (classValue >= 0) { H[classValue] += corWeights[j]; sum += corWeights[j];//w w w. j a va 2 s . com } } // normalize for (int i = 0; i < H.length; i++) { H[i] /= sum; } return H; }
From source file:boostingPL.boosting.SAMME.java
License:Open Source License
@Override public double classifyInstance(Instance inst) throws Exception { int classNum = inst.dataset().classAttribute().numValues(); double[] H = new double[classNum]; for (int j = 0; j < cweights.length; j++) { int classValue = (int) classifiers[j].classifyInstance(inst); H[classValue] += cweights[j];/*from w w w .j a v a 2 s . c o m*/ } return (double) maxIdx(H); }
From source file:boostingPL.boosting.SAMMEPL.java
License:Open Source License
@Override public double[] distributionForInstance(Instance inst) throws Exception { int classNum = inst.dataset().classAttribute().numValues(); double[] H = new double[classNum]; double sum = 0; for (int i = 0; i < classifiers.length; i++) { for (int j = 0; j < classifiers[i].length; j++) { int classValue = (int) classifiers[i][j].classifyInstance(inst); H[classValue] += corWeights[i][j]; sum += corWeights[i][j];//w w w.j ava 2s . c o m } } /* System.out.print("instance ["); for (double i : H) { System.out.print(" "+i+" "); } System.out.println("]"); */ // normalize for (int i = 0; i < H.length; i++) { H[i] /= sum; } return H; }
From source file:cn.ict.zyq.bestConf.COMT2.COMT2.java
License:Open Source License
/** * @param samplePoint : some attributes are flexible; for such attributes, we use values of the samplepoint * @return//from w w w. j a va 2 s. c o m * @throws Exception */ public Instance getInstanceWithPossibleMaxY(Instance samplePoint) throws Exception { Instance retval = null; //we actually have the model if (models != null) { ArrayList<Branch2>[] branchLists = new ArrayList[ModelNum]; for (int m = 0; m < ModelNum; m++) { branchLists[m] = getLeavesInfoForM5P(models[m]); } //now we intersect each leaf ArrayList<Branch2> combined = branchLists[0]; for (int m = 1; m < ModelNum; m++) { combined = intersectBranch2Lists(combined, branchLists[m]); } //now we find the best in the combined list Instance temp; for (Branch2 branch : combined) { temp = branch.maxPoint(samplePoint.dataset()); if (retval == null || retval.classValue() < temp.classValue()) { retval = temp; System.out.println("Current best performance is : " + retval.classValue()); } } } return retval; }
From source file:com.yahoo.labs.samoa.instances.WekaToSamoaInstanceConverter.java
License:Apache License
/** * Samoa instance from weka instance./* ww w .j av a 2 s . co m*/ * * @param inst the inst * @return the instance */ public Instance samoaInstance(weka.core.Instance inst) { Instance samoaInstance; if (inst instanceof weka.core.SparseInstance) { double[] attributeValues = new double[inst.numValues()]; int[] indexValues = new int[inst.numValues()]; for (int i = 0; i < inst.numValues(); i++) { if (inst.index(i) != inst.classIndex()) { attributeValues[i] = inst.valueSparse(i); indexValues[i] = inst.index(i); } } samoaInstance = new SparseInstance(inst.weight(), attributeValues, indexValues, inst.numAttributes()); } else { samoaInstance = new DenseInstance(inst.weight(), inst.toDoubleArray()); //samoaInstance.deleteAttributeAt(inst.classIndex()); } if (this.samoaInstanceInformation == null) { this.samoaInstanceInformation = this.samoaInstancesInformation(inst.dataset()); } samoaInstance.setDataset(samoaInstanceInformation); samoaInstance.setClassValue(inst.classValue()); return samoaInstance; }
From source file:cotraining.copy.Evaluation_D.java
License:Open Source License
/** * Evaluates the classifier on a single instance and records the * prediction (if the class is nominal). * * @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 */// w ww. j a va 2 s .c om public double evaluateModelOnceAndRecordPrediction(Classifier classifier, 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(); } double[] dist = classifier.distributionForInstance(classMissing); pred = Utils.maxIndex(dist); if (dist[(int) pred] <= 0) { pred = Instance.missingValue(); } updateStatsForClassifier(dist, instance); m_Predictions.addElement(new NominalPrediction(instance.classValue(), dist, instance.weight())); } else { pred = classifier.classifyInstance(classMissing); updateStatsForPredictor(pred, instance); } return pred; }
From source file:cotraining.copy.Evaluation_D.java
License:Open Source License
/** * store the prediction made by the classifier as a string * /*from w ww . j a v 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(); }
From source file:data.Bag.java
License:Open Source License
/** * Constructor./*from ww w . j a v a 2 s .co m*/ * * @param instance * A Weka's Instance to be transformed into a Bag. * @throws Exception * To be handled in an upper level. * */ public Bag(Instance instance) throws Exception { super(instance); m_AttValues = instance.toDoubleArray(); m_Weight = instance.weight(); m_Dataset = instance.dataset(); }
From source file:data.generation.target.utils.PrincipalComponents.java
License:Open Source License
/** * Transform an instance in original (unormalized) format. Convert back * to the original space if requested./*from www .j a va 2 s .c o m*/ * @param instance an instance in the original (unormalized) format * @return a transformed instance * @throws Exception if instance cant be transformed */ public Instance convertInstance(Instance instance) throws Exception { if (m_eigenvalues == null) { throw new Exception("convertInstance: Principal components not " + "built yet"); } double[] newVals = new double[m_outputNumAtts]; Instance tempInst = (Instance) instance.copy(); if (!instance.dataset().equalHeaders(m_trainHeader)) { throw new Exception("Can't convert instance: header's don't match: " + "PrincipalComponents\n" + "Can't convert instance: header's don't match."); } m_replaceMissingFilter.input(tempInst); m_replaceMissingFilter.batchFinished(); tempInst = m_replaceMissingFilter.output(); /*if (m_normalize) { m_normalizeFilter.input(tempInst); m_normalizeFilter.batchFinished(); tempInst = m_normalizeFilter.output(); }*/ m_nominalToBinFilter.input(tempInst); m_nominalToBinFilter.batchFinished(); tempInst = m_nominalToBinFilter.output(); if (m_attributeFilter != null) { m_attributeFilter.input(tempInst); m_attributeFilter.batchFinished(); tempInst = m_attributeFilter.output(); } if (!m_center) { m_standardizeFilter.input(tempInst); m_standardizeFilter.batchFinished(); tempInst = m_standardizeFilter.output(); } else { m_centerFilter.input(tempInst); m_centerFilter.batchFinished(); tempInst = m_centerFilter.output(); } if (m_hasClass) { newVals[m_outputNumAtts - 1] = instance.value(instance.classIndex()); } double cumulative = 0; for (int i = m_numAttribs - 1; i >= 0; i--) { double tempval = 0.0; for (int j = 0; j < m_numAttribs; j++) { tempval += (m_eigenvectors[j][m_sortedEigens[i]] * tempInst.value(j)); } newVals[m_numAttribs - i - 1] = tempval; cumulative += m_eigenvalues[m_sortedEigens[i]]; if ((cumulative / m_sumOfEigenValues) >= m_coverVariance) { break; } } if (!m_transBackToOriginal) { if (instance instanceof SparseInstance) { return new SparseInstance(instance.weight(), newVals); } else { return new Instance(instance.weight(), newVals); } } else { if (instance instanceof SparseInstance) { return convertInstanceToOriginal(new SparseInstance(instance.weight(), newVals)); } else { return convertInstanceToOriginal(new Instance(instance.weight(), newVals)); } } }