List of usage examples for weka.core Instance classIndex
public int classIndex();
From source file:WLSVM.java
License:Open Source License
/** * Converts an ARFF Instance into a string in the sparse format accepted by * LIBSVM/*w w w . j a v a 2 s. c om*/ * * @param instance * @return */ protected String InstanceToSparse(Instance instance) { String line = new String(); int c = (int) instance.classValue(); if (c == 0) c = -1; line = c + " "; for (int j = 1; j < instance.numAttributes(); j++) { if (j - 1 == instance.classIndex()) { continue; } if (instance.isMissing(j - 1)) continue; if (instance.value(j - 1) != 0) line += " " + j + ":" + instance.value(j - 1); } // System.out.println(line); return (line + "\n"); }
From source file:PrincipalComponents.java
License:Open Source License
/** * Transform an instance in original (unormalized) format. Convert back to * the original space if requested.//from w w w. j av a2s . c o m * * @param instance an instance in the original (unormalized) format * @return a transformed instance * @throws Exception if instance cant be transformed */ @Override 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" + instance.dataset().equalHeadersMsg(m_trainHeader)); } 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; int numAttAdded = 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 (numAttAdded > m_maxNumAttr) { break; } numAttAdded++; } if (!m_transBackToOriginal) { if (instance instanceof SparseInstance) { return new SparseInstance(instance.weight(), newVals); } else { return new DenseInstance(instance.weight(), newVals); } } else { if (instance instanceof SparseInstance) { return convertInstanceToOriginal(new SparseInstance(instance.weight(), newVals)); } else { return convertInstanceToOriginal(new DenseInstance(instance.weight(), newVals)); } } }
From source file:MPCKMeans.java
License:Open Source License
/** lookup the instance in the checksum hash, assuming transductive clustering * @param instance instance to be looked up * @return the index of the cluster to which the instance was assigned, -1 if the instance has not bee clustered */// w w w . jav a 2s . c om protected int lookupInstanceCluster(Instance instance) throws Exception { int classIdx = instance.classIndex(); double checksum = 0; // need to normalize using original metric, since cluster data is normalized similarly if (m_metric.doesNormalizeData()) { if (m_Trainable == TRAINING_INTERNAL) { m_metric.resetMetric(); } m_metric.normalizeInstanceWeighted(instance); } double[] values1 = instance.toDoubleArray(); for (int i = 0; i < values1.length; i++) { if (i != classIdx) { checksum += m_checksumCoeffs[i] * values1[i]; } } Object list = m_checksumHash.get(new Double((float) checksum)); if (list != null) { // go through the list of instances with the same checksum and find the one that is equivalent ArrayList checksumList = (ArrayList) list; for (int i = 0; i < checksumList.size(); i++) { int instanceIdx = ((Integer) checksumList.get(i)).intValue(); Instance listInstance = m_Instances.instance(instanceIdx); double[] values2 = listInstance.toDoubleArray(); boolean equal = true; for (int j = 0; j < values1.length && equal == true; j++) { if (j != classIdx) { if ((float) values1[j] != (float) values2[j]) { equal = false; } } } if (equal == true) { return m_ClusterAssignments[instanceIdx]; } } } return -1; }
From source file:adams.data.conversion.AbstractMatchWekaInstanceAgainstHeader.java
License:Open Source License
/** * Matches the input instance against the header. * * @param input the Instance to align to the header * @return the aligned Instance//ww w .j a va 2 s . com */ protected Instance match(Instance input) { Instance result; double[] values; int i; values = new double[m_Dataset.numAttributes()]; for (i = 0; i < m_Dataset.numAttributes(); i++) { values[i] = Utils.missingValue(); switch (m_Dataset.attribute(i).type()) { case Attribute.NUMERIC: case Attribute.DATE: values[i] = input.value(i); break; case Attribute.NOMINAL: if (m_Dataset.attribute(i).indexOfValue(input.stringValue(i)) != -1) values[i] = m_Dataset.attribute(i).indexOfValue(input.stringValue(i)); break; case Attribute.STRING: values[i] = m_Dataset.attribute(i).addStringValue(input.stringValue(i)); break; case Attribute.RELATIONAL: values[i] = m_Dataset.attribute(i).addRelation(input.relationalValue(i)); break; default: throw new IllegalStateException( "Unhandled attribute type: " + Attribute.typeToString(m_Dataset.attribute(i).type())); } } if (input instanceof SparseInstance) result = new SparseInstance(input.weight(), values); else result = new DenseInstance(input.weight(), values); result.setDataset(m_Dataset); // fix class index, if necessary if ((input.classIndex() != m_Dataset.classIndex()) && (m_Dataset.classIndex() < 0)) m_Dataset.setClassIndex(input.classIndex()); return result; }
From source file:adams.flow.condition.bool.WekaClassification.java
License:Open Source License
/** * Returns the index of the case that should get executed. * /* w ww. jav a 2 s. co m*/ * @param owner the owning actor * @param token the current token passing through the actor * @return the index, -1 if not available */ public int getCaseIndex(Actor owner, Token token) { int result; double classification; String msg; Instance inst; result = -1; if (m_OnTheFly && (m_Model == null)) { msg = setUpModel(owner); if (msg != null) { getLogger().severe(msg); return result; } } if ((token != null) && (token.getPayload() != null)) { inst = ((Instance) token.getPayload()); if (inst.classIndex() == -1) { getLogger().severe("No class set!"); return result; } if (!inst.classAttribute().isNominal()) { getLogger().severe("Class attribute is not nominal!"); return result; } try { classification = m_Model.classifyInstance(inst); if (Utils.isMissingValue(classification)) result = -1; else result = (int) classification; } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to obtain classification: ", e); } } return result; }
From source file:ann.MyANN.java
/** * mengubah Instance menjadi Data//from w w w. j av a 2 s . c om * @param instance Instance yang akan diubah menjadi kelas Data * @return kelas Data dari input */ private Data instanceToData(Instance instance) { ArrayList<Double> input = new ArrayList<>(); ArrayList<Double> target = new ArrayList<>(); for (int j = 0; j < instance.numAttributes() - 1; j++) { input.add(0.0); } if (instance.classAttribute().isNominal()) { for (int j = 0; j < instance.classAttribute().numValues(); j++) { target.add(0.0); } } else { target.add(0.0); } for (int j = 0; j < instance.numAttributes(); j++) { if (j == instance.classIndex()) { if (instance.attribute(j).isNominal()) target.set((int) instance.value(j), 1.0); else target.add(instance.value(j)); } else { input.set(j, instance.value(j)); } } return new Data(input, target); }
From source file:anndl.Anndl.java
private void ClassifyInstance(String fileinput) throws Exception { System.out.println("Membaca File Model .."); Classifier cls = (Classifier) weka.core.SerializationHelper.read(fileinput); System.out.println("Masukan Data yang ingin diklasifikasikan, pisahkan dengan spasi :"); String testset = scanner.nextLine(); testset = "1 1 0"; String[] exploded = testset.split(" "); Instance newinstance = new Instance(exploded.length); for (int i = 0; i < exploded.length; i++) { String exploded1 = exploded[i]; System.out.println(exploded[i] + ";"); newinstance.setValue(i, Integer.valueOf(exploded[i])); }// www . ja v a 2 s . c om System.out.println("Melakukan klasifikasi ... "); double result = cls.classifyInstance(newinstance); System.out.println("Data ini:"); System.out.println(testset); System.out.println("memiliki label :"); System.out.println(newinstance.value(newinstance.classIndex())); }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * store the prediction made by the classifier as a string * /* w w w . jav a 2s .com*/ * @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:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Builds a string listing the attribute values in a specified range of * indices, separated by commas and enclosed in brackets. * /*from w ww .j a v a 2 s . c o m*/ * @param instance the instance to print the values from * @param attRange the range of the attributes to list * @return a string listing values of the attributes in the range */ protected static String attributeValuesString(Instance instance, Range attRange) { StringBuffer text = new StringBuffer(); if (attRange != null) { boolean firstOutput = true; attRange.setUpper(instance.numAttributes() - 1); for (int i = 0; i < instance.numAttributes(); i++) { if (attRange.isInRange(i) && i != instance.classIndex()) { if (firstOutput) { text.append("("); } else { text.append(","); } text.append(instance.toString(i)); firstOutput = false; } } if (!firstOutput) { text.append(")"); } } return text.toString(); }
From source file:br.preprocess.forms.ReducaoPCA.java
public void changeChart(int indexX, int indexY) { if (indexX > -1 && indexY > -1) { this.ch.clear(); for (int i = 0; i < this.pca.getDataset().getNumClass(); i++) for (Instance sample : this.pca.getDataset().getDataset()) if (sample.value(sample.classIndex()) == i) this.ch.update(sample.value(indexX), sample.value(indexY), i); Utils.addPlot(this.redChart, this.ch); }/*from w ww . jav a 2 s . c o m*/ }