Java Code Examples for weka.core.Instance#classValue()
The following examples show how to use
weka.core.Instance#classValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NBNodeAdaptive.java From tsml with GNU General Public License v3.0 | 6 votes |
@Override public void updateNode(Instance inst) throws Exception { String trueClass = inst.classAttribute().value((int) inst.classValue()); int trueClassIndex = (int) inst.classValue(); if (majorityClass().equals(trueClass)) { m_majClassCorrectWeight += inst.weight(); } if (m_bayes.classifyInstance(inst) == trueClassIndex) { m_nbCorrectWeight += inst.weight(); } super.updateNode(inst); }
Example 2
Source File: DatasetLists.java From tsml with GNU General Public License v3.0 | 6 votes |
public static void dataDescriptionDataNotSplit(String[] fileNames, String problemPath){ //Produce summary descriptions //dropboxPath=uciPath; OutFile f=new OutFile(problemPath+"DataDimensions.csv"); f.writeLine("problem,numinstances,numAttributes,numClasses,classDistribution"); try{ for(int i=0;i<fileNames.length;i++){ Instances allData=DatasetLoading.loadDataNullable(problemPath+fileNames[i]+"/"+fileNames[i]); // allData.randomize(new Random()); // OutFile combo=new OutFile(problemPath+tscProblems85[i]+"/"+tscProblems85[i]+".arff"); // combo.writeString(allData.toString()); int[] classCounts=new int[allData.numClasses()]; for(Instance ins: allData) classCounts[(int)(ins.classValue())]++; f.writeString(fileNames[i]+","+allData.numInstances()+","+(allData.numAttributes()-1)+","+allData.numClasses()); for(int c:classCounts) f.writeString(","+(c/(double)allData.numInstances())); f.writeString("\n"); } }catch(Exception e){ System.out.println(" ERRROR"+e); } }
Example 3
Source File: Distribution.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Adds given instance to all bags weighting it according to given weights. * * @exception Exception if something goes wrong */ public final void addWeights(Instance instance, double [] weights) throws Exception { int classIndex; int i; classIndex = (int)instance.classValue(); for (i=0;i<m_perBag.length;i++) { double weight = instance.weight() * weights[i]; m_perClassPerBag[i][classIndex] = m_perClassPerBag[i][classIndex] + weight; m_perBag[i] = m_perBag[i] + weight; m_perClass[classIndex] = m_perClass[classIndex] + weight; totaL = totaL + weight; } }
Example 4
Source File: IB1.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Classifies the given test instance. * * @param instance the instance to be classified * @return the predicted class for the instance * @throws Exception if the instance can't be classified */ public double classifyInstance(Instance instance) throws Exception { if (m_Train.numInstances() == 0) { throw new Exception("No training instances!"); } double distance, minDistance = Double.MAX_VALUE, classValue = 0; updateMinMax(instance); Enumeration enu = m_Train.enumerateInstances(); while (enu.hasMoreElements()) { Instance trainInstance = (Instance) enu.nextElement(); if (!trainInstance.classIsMissing()) { distance = distance(instance, trainInstance); if (distance < minDistance) { minDistance = distance; classValue = trainInstance.classValue(); } } } return classValue; }
Example 5
Source File: ConjunctiveRule.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Private function to compute number of accurate instances * based on the specified predicted class * * @param data the data in question * @param clas the predicted class * @return the default accuracy number */ private double computeAccu(Instances data, int clas){ double accu = 0; for(int i=0; i<data.numInstances(); i++){ Instance inst = data.instance(i); if((int)inst.classValue() == clas) accu += inst.weight(); } return accu; }
Example 6
Source File: Distribution.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Subtracts given instance from given bag. * * @exception Exception if something goes wrong */ public final void sub(int bagIndex,Instance instance) throws Exception { int classIndex; double weight; classIndex = (int)instance.classValue(); weight = instance.weight(); m_perClassPerBag[bagIndex][classIndex] = m_perClassPerBag[bagIndex][classIndex]-weight; m_perBag[bagIndex] = m_perBag[bagIndex]-weight; m_perClass[classIndex] = m_perClass[classIndex]-weight; totaL = totaL-weight; }
Example 7
Source File: ClusterMembership.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Convert a single instance over. The converted instance is added to * the end of the output queue. * * @param instance the instance to convert * @throws Exception if something goes wrong */ protected void convertInstance(Instance instance) throws Exception { // set up values double [] instanceVals = new double[outputFormatPeek().numAttributes()]; double [] tempvals; if (instance.classIndex() >= 0) { tempvals = new double[outputFormatPeek().numAttributes() - 1]; } else { tempvals = new double[outputFormatPeek().numAttributes()]; } int pos = 0; for (int j = 0; j < m_clusterers.length; j++) { if (m_clusterers[j] != null) { double [] probs; if (m_removeAttributes != null) { m_removeAttributes.input(instance); probs = logs2densities(j, m_removeAttributes.output()); } else { probs = logs2densities(j, instance); } System.arraycopy(probs, 0, tempvals, pos, probs.length); pos += probs.length; } } tempvals = Utils.logs2probs(tempvals); System.arraycopy(tempvals, 0, instanceVals, 0, tempvals.length); if (instance.classIndex() >= 0) { instanceVals[instanceVals.length - 1] = instance.classValue(); } push(new DenseInstance(instance.weight(), instanceVals)); }
Example 8
Source File: Evaluation.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Updates all the statistics about a predictors performance for the current * test instance. * * @param predictedValue the numeric value the classifier predicts * @param instance the instance to be classified * @throws Exception if the class of the instance is not set */ protected void updateStatsForPredictor(double predictedValue, Instance instance) throws Exception { if (!instance.classIsMissing()) { // Update stats m_WithClass += instance.weight(); if (Utils.isMissingValue(predictedValue)) { m_Unclassified += instance.weight(); return; } m_SumClass += instance.weight() * instance.classValue(); m_SumSqrClass += instance.weight() * instance.classValue() * instance.classValue(); m_SumClassPredicted += instance.weight() * instance.classValue() * predictedValue; m_SumPredicted += instance.weight() * predictedValue; m_SumSqrPredicted += instance.weight() * predictedValue * predictedValue; updateNumericScores(makeDistribution(predictedValue), makeDistribution(instance.classValue()), instance.weight()); } else m_MissingClass += instance.weight(); if (m_pluginMetrics != null) { for (AbstractEvaluationMetric m : m_pluginMetrics) { if (m instanceof StandardEvaluationMetric) { ((StandardEvaluationMetric) m).updateStatsForPredictor( predictedValue, instance); } else if (m instanceof InformationTheoreticEvaluationMetric) { ((InformationTheoreticEvaluationMetric) m).updateStatsForPredictor( predictedValue, instance); } } } }
Example 9
Source File: FastShapelets.java From tsml with GNU General Public License v3.0 | 5 votes |
void readTrainData(Instances data) { orgData = InstanceTools.fromWekaInstancesList(data); orgClassFreq = new int[numClass]; Org_Label = new ArrayList<>(); for (Instance i : data) { Org_Label.add((int) i.classValue()); orgClassFreq[(int) i.classValue()]++; } }
Example 10
Source File: Efficient1NN.java From tsml with GNU General Public License v3.0 | 5 votes |
public int classifyInstance(final Instances trainData, final Instance query, final int queryIndex, final SequenceStatsCache cache) { int[] classCounts = new int[train.numClasses()]; double dist; Instance candidate = trainData.get(0); double bsfDistance = distance(query, candidate, Double.POSITIVE_INFINITY); classCounts[(int) candidate.classValue()]++; for (int candidateIndex = 1; candidateIndex < trainData.size(); candidateIndex++) { candidate = trainData.get(candidateIndex); dist = lowerBound(query, candidate, queryIndex, candidateIndex, bsfDistance, cache); if (dist <= bsfDistance) { dist = distance(query, candidate, bsfDistance); if (dist < bsfDistance) { bsfDistance = dist; classCounts = new int[trainData.numClasses()]; classCounts[(int) candidate.classValue()]++; } else if (dist == bsfDistance) { classCounts[(int) candidate.classValue()]++; } } } int bsfClass = -1; double bsfCount = -1; for (int i = 0; i < classCounts.length; i++) { if (classCounts[i] > bsfCount) { bsfCount = classCounts[i]; bsfClass = i; } } return bsfClass; }
Example 11
Source File: Efficient1NN.java From tsml with GNU General Public License v3.0 | 5 votes |
@Override public double[] distributionForInstance(Instance instance) throws Exception { double bsfDistance = Double.MAX_VALUE; // for tie splitting int[] classCounts = new int[this.train.numClasses()]; double thisDist; int sumOfBest = 0; for (Instance i : this.train) { thisDist = distance(instance, i, bsfDistance); if (thisDist < bsfDistance) { bsfDistance = thisDist; classCounts = new int[train.numClasses()]; classCounts[(int) i.classValue()]++; sumOfBest = 1; } else if (thisDist == bsfDistance) { classCounts[(int) i.classValue()]++; sumOfBest++; } } double[] classDistributions = new double[this.train.numClasses()]; for (int c = 0; c < classCounts.length; c++) { classDistributions[c] = (double) classCounts[c] / sumOfBest; } return classDistributions; }
Example 12
Source File: NNge.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Performs the update of the classifier * * @param instance the new instance * @throws Exception if the update fails */ private void update(Instance instance) throws Exception { if (instance.classIsMissing()) { return; } instance.replaceMissingValues(m_MissingVector); m_Train.add(instance); /* Update the minimum and maximum for all the attributes */ updateMinMax(instance); /* update the mutual information datas */ updateMI(instance); /* Nearest Exemplar */ Exemplar nearest = nearestExemplar(instance); /* Adjust */ if(nearest == null){ Exemplar newEx = new Exemplar(this, m_Train, 10, instance.classValue()); newEx.generalise(instance); initWeight(newEx); addExemplar(newEx); return; } adjust(instance, nearest); /* Generalise */ generalise(instance); }
Example 13
Source File: TSCHIEFWrapper.java From tsml with GNU General Public License v3.0 | 5 votes |
private TSDataset toPFDataset(Instances insts) { TSDataset dset = new TSDataset(insts.numInstances()); for (Instance inst : insts) { TimeSeries ts = new TimeSeries(getSeries(inst), (int)inst.classValue()); dset.add(ts); } return dset; }
Example 14
Source File: Distribution.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Deletes given instance from given bag. * * @exception Exception if something goes wrong */ public final void del(int bagIndex,Instance instance) throws Exception { int classIndex; double weight; classIndex = (int)instance.classValue(); weight = instance.weight(); m_perClassPerBag[bagIndex][classIndex] = m_perClassPerBag[bagIndex][classIndex]-weight; m_perBag[bagIndex] = m_perBag[bagIndex]-weight; m_perClass[classIndex] = m_perClass[classIndex]-weight; totaL = totaL-weight; }
Example 15
Source File: PlainText.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Store the prediction made by the classifier as a string. * * @param dist the distribution to use * @param inst the instance to generate text from * @param index the index in the dataset * @throws Exception if something goes wrong */ protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception { int width = 7 + m_NumDecimals; int prec = m_NumDecimals; Instance withMissing = (Instance)inst.copy(); withMissing.setDataset(inst.dataset()); double predValue = 0; if (Utils.sum(dist) == 0) { predValue = Utils.missingValue(); } else { if (inst.classAttribute().isNominal()) { predValue = Utils.maxIndex(dist); } else { predValue = dist[0]; } } // index append(Utils.padLeft("" + (index+1), 6)); if (inst.dataset().classAttribute().isNumeric()) { // actual if (inst.classIsMissing()) append(" " + Utils.padLeft("?", width)); else append(" " + Utils.doubleToString(inst.classValue(), width, prec)); // predicted if (Utils.isMissingValue(predValue)) append(" " + Utils.padLeft("?", width)); else append(" " + Utils.doubleToString(predValue, width, prec)); // error if (Utils.isMissingValue(predValue) || inst.classIsMissing()) append(" " + Utils.padLeft("?", width)); else append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec)); } else { // actual append(" " + Utils.padLeft(((int) inst.classValue()+1) + ":" + inst.toString(inst.classIndex()), width)); // predicted if (Utils.isMissingValue(predValue)) append(" " + Utils.padLeft("?", width)); else append(" " + Utils.padLeft(((int) predValue+1) + ":" + inst.dataset().classAttribute().value((int)predValue), width)); // error? if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1)) append(" " + " + "); else append(" " + " "); // prediction/distribution if (m_OutputDistribution) { if (Utils.isMissingValue(predValue)) { append(" " + "?"); } else { append(" "); for (int n = 0; n < dist.length; n++) { if (n > 0) append(","); if (n == (int) predValue) append("*"); append(Utils.doubleToString(dist[n], prec)); } } } else { if (Utils.isMissingValue(predValue)) append(" " + "?"); else append(" " + Utils.doubleToString(dist[(int)predValue], prec)); } } // attributes append(" " + attributeValuesString(withMissing) + "\n"); }
Example 16
Source File: SAXVSM.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * If skip = one of <0 ... numInstances-1>, will not include instance at that index into the corpus * Part of leave one out cv, while avoiding unnecessary repeats of the BoP transformation */ private Instances tfxidf(Instances bopData, int skip) { int numClasses = bopData.numClasses(); int numInstances = bopData.numInstances(); int numTerms = bopData.numAttributes()-1; //minus class attribute //initialise class weights double[][] classWeights = new double[numClasses][numTerms]; //build class bags int inst = 0; for (Instance in : bopData) { if (inst++ == skip) //skip 'this' one, for leave-one-out cv continue; int classVal = (int)in.classValue(); for (int j = 0; j < numTerms; ++j) { classWeights[classVal][j] += in.value(j); } } //apply tf x idf for (int i = 0; i < numTerms; ++i) { //for each term double df = 0; //document frequency for (int j = 0; j < numClasses; ++j) //find how many classes (documents) this term appears in if (classWeights[j][i] != 0) ++df; if (df != 0) { //if it appears if (df != numClasses) { //but not in all, apply weighting for (int j = 0; j < numClasses; ++j) if (classWeights[j][i] != 0) classWeights[j][i] = Math.log(1 + classWeights[j][i]) * Math.log(numClasses / df); } else { //appears in all //avoid log calculations //if df == num classes -> idf = log(N/df) = log(1) = 0 for (int j = 0; j < numClasses; ++j) classWeights[j][i] = 0; } } } Instances tfxidfCorpus = new Instances(bopData, numClasses); for (int i = 0; i < numClasses; ++i) tfxidfCorpus.add(new SparseInstance(1.0, classWeights[i])); return tfxidfCorpus; }
Example 17
Source File: CollectiveForest.java From collective-classification-weka-package with GNU General Public License v3.0 | 4 votes |
/** * performs the actual building of the classifier * * @throws Exception if building fails */ @Override protected void buildClassifier() throws Exception { Classifier tree; int i; int n; int nextSeed; double[] dist; Instances bagData; boolean[] inBag; double outOfBagCount; double errorSum; Instance outOfBagInst; m_PureTrainNodes = 0; m_PureTestNodes = 0; for (i = 0; i < getNumTrees(); i++) { // info if (getVerbose()) System.out.print("."); // get next seed number nextSeed = m_Random.nextInt(); // bagging? if (getUseBagging()) { // inBag-dataset/array inBag = new boolean[m_TrainsetNew.numInstances()]; bagData = resample(m_TrainsetNew, nextSeed, inBag); // build i.th tree tree = initClassifier(nextSeed); // determine and store distributions for (n = 0; n < m_TestsetNew.numInstances(); n++) { dist = tree.distributionForInstance(m_TestsetNew.instance(n)); m_List.addDistribution(m_TestsetNew.instance(n), dist); } // determine out-of-bag-error outOfBagCount = 0; errorSum = 0; for (n = 0; n < inBag.length; n++) { if (!inBag[n]) { outOfBagInst = m_TrainsetNew.instance(n); outOfBagCount += outOfBagInst.weight(); if (m_TrainsetNew.classAttribute().isNumeric()) { errorSum += outOfBagInst.weight() * StrictMath.abs(tree.classifyInstance(outOfBagInst) - outOfBagInst.classValue()); } else { if (tree.classifyInstance(outOfBagInst) != outOfBagInst.classValue()) { errorSum += outOfBagInst.weight(); } } } } m_OutOfBagError = errorSum / outOfBagCount; } else { // build i.th tree tree = initClassifier(nextSeed); // determine and store distributions for (n = 0; n < m_TestsetNew.numInstances(); n++) { dist = tree.distributionForInstance(m_TestsetNew.instance(n)); m_List.addDistribution(m_TestsetNew.instance(n), dist); } } // get information about pure nodes try { if (tree instanceof AdditionalMeasureProducer) { m_PureTrainNodes += ((AdditionalMeasureProducer) tree).getMeasure( "measurePureTrainNodes"); m_PureTestNodes += ((AdditionalMeasureProducer) tree).getMeasure( "measurePureTestNodes"); } } catch (Exception e) { e.printStackTrace(); } tree = null; } if (getVerbose()) System.out.println(); }
Example 18
Source File: NNge.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Generalise an Exemplar (not necessarily predictedExemplar) to match instance. * predictedExemplar must be in NNge's lists * * @param newInst the new instance * @throws Exception in case of inconsitent situation */ private void generalise(Instance newInst) throws Exception { Exemplar first = m_ExemplarsByClass[(int) newInst.classValue()]; int n = 0; /* try to generalise with the n first exemplars */ while(n < m_NumAttemptsOfGene && first != null){ /* find the nearest one starting from first */ Exemplar closest = first, cur = first; double smallestDist = first.squaredDistance(newInst), dist; while(cur.nextWithClass != null){ cur = cur.nextWithClass; dist = cur.squaredDistance(newInst); if(dist < smallestDist){ smallestDist = dist; closest = cur; } } /* remove the Examplar from NNge's lists */ if(closest == first) first = first.nextWithClass; removeExemplar(closest); /* try to generalise */ closest.preGeneralise(newInst); if(!detectOverlapping(closest)){ closest.validateGeneralisation(); addExemplar(closest); return; } /* it didn't work, put ungeneralised exemplar on the top of the lists */ closest.cancelGeneralisation(); addExemplar(closest); n++; } /* generalisation failled : add newInst as a new Examplar */ Exemplar newEx = new Exemplar(this, m_Train, 5, newInst.classValue()); newEx.generalise(newInst); initWeight(newEx); addExemplar(newEx); }
Example 19
Source File: DecisionStump.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Finds best split for nominal attribute and numeric class * and returns value. * * @param index attribute index * @return value of criterion for the best split * @throws Exception if something goes wrong */ protected double findSplitNominalNumeric(int index) throws Exception { double bestVal = Double.MAX_VALUE, currVal; double[] sumsSquaresPerValue = new double[m_Instances.attribute(index).numValues()], sumsPerValue = new double[m_Instances.attribute(index).numValues()], weightsPerValue = new double[m_Instances.attribute(index).numValues()]; double totalSumSquaresW = 0, totalSumW = 0, totalSumOfWeightsW = 0, totalSumOfWeights = 0, totalSum = 0; double[] sumsSquares = new double[3], sumOfWeights = new double[3]; double[][] bestDist = new double[3][1]; // Compute counts for all the values for (int i = 0; i < m_Instances.numInstances(); i++) { Instance inst = m_Instances.instance(i); if (inst.isMissing(index)) { m_Distribution[2][0] += inst.classValue() * inst.weight(); sumsSquares[2] += inst.classValue() * inst.classValue() * inst.weight(); sumOfWeights[2] += inst.weight(); } else { weightsPerValue[(int)inst.value(index)] += inst.weight(); sumsPerValue[(int)inst.value(index)] += inst.classValue() * inst.weight(); sumsSquaresPerValue[(int)inst.value(index)] += inst.classValue() * inst.classValue() * inst.weight(); } totalSumOfWeights += inst.weight(); totalSum += inst.classValue() * inst.weight(); } // Check if the total weight is zero if (totalSumOfWeights <= 0) { return bestVal; } // Compute sum of counts without missing ones for (int i = 0; i < m_Instances.attribute(index).numValues(); i++) { totalSumOfWeightsW += weightsPerValue[i]; totalSumSquaresW += sumsSquaresPerValue[i]; totalSumW += sumsPerValue[i]; } // Make split counts for each possible split and evaluate for (int i = 0; i < m_Instances.attribute(index).numValues(); i++) { m_Distribution[0][0] = sumsPerValue[i]; sumsSquares[0] = sumsSquaresPerValue[i]; sumOfWeights[0] = weightsPerValue[i]; m_Distribution[1][0] = totalSumW - sumsPerValue[i]; sumsSquares[1] = totalSumSquaresW - sumsSquaresPerValue[i]; sumOfWeights[1] = totalSumOfWeightsW - weightsPerValue[i]; currVal = variance(m_Distribution, sumsSquares, sumOfWeights); if (currVal < bestVal) { bestVal = currVal; m_SplitPoint = (double)i; for (int j = 0; j < 3; j++) { if (sumOfWeights[j] > 0) { bestDist[j][0] = m_Distribution[j][0] / sumOfWeights[j]; } else { bestDist[j][0] = totalSum / totalSumOfWeights; } } } } m_Distribution = bestDist; return bestVal; }
Example 20
Source File: DecisionTable.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Inserts an instance into the hash table * * @param inst instance to be inserted * @param instA to create the hash key from * @throws Exception if the instance can't be inserted */ private void insertIntoTable(Instance inst, double [] instA) throws Exception { double [] tempClassDist2; double [] newDist; DecisionTableHashKey thekey; if (instA != null) { thekey = new DecisionTableHashKey(instA); } else { thekey = new DecisionTableHashKey(inst, inst.numAttributes(), false); } // see if this one is already in the table tempClassDist2 = (double []) m_entries.get(thekey); if (tempClassDist2 == null) { if (m_classIsNominal) { newDist = new double [m_theInstances.classAttribute().numValues()]; //Leplace estimation for (int i = 0; i < m_theInstances.classAttribute().numValues(); i++) { newDist[i] = 1.0; } newDist[(int)inst.classValue()] = inst.weight(); // add to the table m_entries.put(thekey, newDist); } else { newDist = new double [2]; newDist[0] = inst.classValue() * inst.weight(); newDist[1] = inst.weight(); // add to the table m_entries.put(thekey, newDist); } } else { // update the distribution for this instance if (m_classIsNominal) { tempClassDist2[(int)inst.classValue()]+=inst.weight(); // update the table m_entries.put(thekey, tempClassDist2); } else { tempClassDist2[0] += (inst.classValue() * inst.weight()); tempClassDist2[1] += inst.weight(); // update the table m_entries.put(thekey, tempClassDist2); } } }