/** * Returns the contents of an item set as a string. * * @param instances contains the relevant header information * @return string describing the item set */ public String toString(Instances instances) { StringBuffer text = new StringBuffer(); for (int i = 0; i < instances.numAttributes(); i++) { if (m_items[i] != -1) { text.append(instances.attribute(i).name() + '='); text.append(instances.attribute(i).value(m_items[i]) + ' '); } } text.append(m_counter); return text.toString(); }
@Override public String classify(Instance instance) throws Exception { weka.core.Instance wekaInstance = new weka.core.Instance(wekaTrainingData.numAttributes()); wekaInstance.setDataset(wekaTrainingData); double[] histogramPercent = instance.getHistogramPercent(); for (int i = 0; i < histogramPercent.length; i++) { wekaInstance.setValue(i, histogramPercent[i]); } wekaInstance.setMissing(wekaTrainingData.attribute("class")); double wekaClassification = classifier.classifyInstance(wekaInstance); String classification = wekaTrainingData.attribute("class").value((int)wekaClassification); return classification; }
/** * Prints the condition satisfied by instances in a subset. */ public final String rightSide(int index,Instances data) { StringBuffer text; text = new StringBuffer(); if (data.attribute(m_attIndex).isNominal()) text.append(" = "+ data.attribute(m_attIndex).value(index)); else if (index == 0) text.append(" <= "+ Utils.doubleToString(m_splitPoint,6)); else text.append(" > "+ Utils.doubleToString(m_splitPoint,6)); return text.toString(); }
/** * mapBack: returns the original indices (encoded in the class attribute). */ private int[] mapBack(Instances template, int i) { try { return MLUtils.toIntArray(template.classAttribute().value(i)); } catch(Exception e) { return new int[]{}; } }
/** * return a string describing this clusterer * * @return a description of the clusterer as a string */ @Override public String toString() { StringBuffer temp = new StringBuffer(); temp.append("\nFarthestFirst\n==============\n"); temp.append("\nCluster centroids:\n"); for (int i = 0; i < m_NumClusters; i++) { temp.append("\nCluster " + i + "\n\t"); for (int j = 0; j < m_ClusterCentroids.numAttributes(); j++) { if (m_ClusterCentroids.attribute(j).isNominal()) { temp.append(" " + m_ClusterCentroids.attribute(j).value( (int) m_ClusterCentroids.instance(i).value(j))); } else { temp.append(" " + m_ClusterCentroids.instance(i).value(j)); } } } temp.append("\n\n"); return temp.toString(); }
/** * Returns a map of values for this multi-class Instances. For example, values <code>{[2,3,1], [1,0,1, [2,0,1]}</code>. * @param D_ multi-class Instances * @return a map where map[d] returns an int[] array of all values referred to by the d-th classification. */ public static int[][] extractValues(Instances D_) { int numVals = D_.classAttribute().numValues(); int vMap[][] = new int[numVals][]; for (int d = 0; d < numVals; d++) { vMap[d] = MLUtils.toIntArray(D_.classAttribute().value(d)); } return vMap; }
/** * Prints a classification. * * @param dist the class distribution * @return the classificationn as a string * @throws Exception if the classification can't be printed */ protected String printClass(double[] dist) throws Exception { StringBuffer text = new StringBuffer(); if (m_Instances.classAttribute().isNominal()) { text.append(m_Instances.classAttribute().value(Utils.maxIndex(dist))); } else { text.append(dist[0]); } return text.toString() + "\n"; }
@Override public List<String> getPredictionLabels() { if (m_modelHeader == null) { return null; } if (m_modelHeader.classAttribute().isNominal()) { if (m_predictionLabels == null) { m_predictionLabels = new ArrayList<String>(); for (int i = 0; i < m_modelHeader.classAttribute().numValues(); i++) { m_predictionLabels.add(m_modelHeader.classAttribute().value(i)); } } } return m_predictionLabels; }
/** * Converts a {@link weka.core.Attribute} object to an amidst {@link Attribute} and add it to the current list of attributes. * @param attrWeka a {@link weka.core.Attribute} object. * @param attrList a {@code List} of {@link Attributes}. */ private static void convertAttribute(weka.core.Attribute attrWeka, List<Attribute> attrList){ StateSpaceType stateSpaceTypeAtt; if(attrWeka.isNominal()){ String[] vals = new String[attrWeka.numValues()]; for (int i=0; i<attrWeka.numValues(); i++) { vals[i] = attrWeka.value(i); } stateSpaceTypeAtt = new FiniteStateSpace(attrWeka.numValues()); }else{ stateSpaceTypeAtt = new RealStateSpace(); } Attribute att = new Attribute(attrWeka.index(),attrWeka.name(), stateSpaceTypeAtt); attrList.add(att); } }
/** * Returns the test represented by a string in Prolog notation. * * @return a string representing the test in Prolog notation */ public String toPrologString() { Attribute att = m_Dataset.attribute(m_AttIndex); StringBuffer str = new StringBuffer(); String attName = m_Dataset.attribute(m_AttIndex).name(); if (att.isNumeric()) { str = str.append(attName + " "); if (m_Not) { str = str.append(">= " + Utils.doubleToString(m_Split, 3)); } else { str = str.append("< " + Utils.doubleToString(m_Split, 3)); } } else { String value = att.value((int) m_Split); if (value == "false") { str = str.append("not(" + attName + ")"); } else { str = str.append(attName); } } return str.toString(); }
/** * Tests that ordering didn't change. */ public void testUnchangedOrder() { m_Filter = getFilter(SortLabels.SORT_CASESENSITIVE, "first-last"); testBuffered(); Instances result = performTest(); for (int i = 0; i < m_Instances.attribute(2).numValues(); i++) assertEquals("Values differ for index #" + (i+1), m_Instances.attribute(2).value(i), result.attribute(2).value(i)); }
private boolean checkLabelAttributeFormat(Attribute attribute) { if (attribute.isNominal() != true) { return false; } List<String> allowedValues = new ArrayList<String>(); allowedValues.add("0"); allowedValues.add("1"); int numValues = attribute.numValues(); if (allowedValues.size() != numValues) { return false; } for (int index = 0; index < numValues; index++) { String value = attribute.value(index); if (allowedValues.contains(value)) { allowedValues.remove(value); } } if (!allowedValues.isEmpty()) { return false; } return true; }
/** * This creates the required output units. */ private void setupOutputs() throws Exception { m_outputs = new NeuralEnd[m_numClasses]; for (int noa = 0; noa < m_numClasses; noa++) { if (m_numeric) { m_outputs[noa] = new NeuralEnd(m_instances.classAttribute().name()); } else { m_outputs[noa] = new NeuralEnd(m_instances.classAttribute().value(noa)); } m_outputs[noa].setX(.9); m_outputs[noa].setY((noa + 1.0) / (m_numClasses + 1)); m_outputs[noa].setLink(false, noa); NeuralNode temp = new NeuralNode(String.valueOf(m_nextId), m_random, m_sigmoidUnit); m_nextId++; temp.setX(.75); temp.setY((noa + 1.0) / (m_numClasses + 1)); addNode(temp); NeuralConnection.connect(temp, m_outputs[noa]); } }
/** * Returns the contents of an item set as a string. * * @param instances contains the relevant header information * @return string describing the item set */ public String toString(Instances instances) { StringBuffer text = new StringBuffer(); for (int i = 0; i < instances.numAttributes(); i++) { if (m_items[i] != -1) { text.append(instances.attribute(i).name() + '='); text.append(instances.attribute(i).value(m_items[i]) + ' '); } } text.append(m_counter); return text.toString(); }
/** * Remove all label attributes except labelToKeep * * @param instance * @param labelToKeep * @return transformed Instance */ public Instance transformInstance(Instance instance, int labelToKeep) { Instance transformedInstance; remove.input(instance); transformedInstance = remove.output(); add.input(transformedInstance); transformedInstance = add.output(); transformedInstance.setDataset(shell); int[] labelIndices = data.getLabelIndices(); if (data.getDataSet().attribute(labelIndices[labelToKeep]).value(0).equals("1")) { transformedInstance.setValue(shell.numAttributes() - 1, 1 - instance.value(labelIndices[labelToKeep])); } else { transformedInstance.setValue(shell.numAttributes() - 1, instance.value(labelIndices[labelToKeep])); } return transformedInstance; }
/** * Prints the condition satisfied by instances in a subset. * * @param index of subset * @param data training set. */ @Override public final String rightSide(int index, Instances data) { StringBuffer text; text = new StringBuffer(); if (data.attribute(m_attIndex).isNominal()) { text.append(" = " + data.attribute(m_attIndex).value(index)); } else if (index == 0) { text.append(" <= " + Utils.doubleToString(m_splitPoint, 6)); } else { text.append(" > " + Utils.doubleToString(m_splitPoint, 6)); } return text.toString(); }