private boolean[] getTrueLabels(Instance instance, int numLabels, int[] labelIndices) { boolean[] trueLabels = new boolean[numLabels]; for (int counter = 0; counter < numLabels; counter++) { int classIdx = labelIndices[counter]; String classValue = instance.attribute(classIdx).value((int) instance.value(classIdx)); trueLabels[counter] = classValue.equals("1"); } return trueLabels; }
private boolean[] getTrueLabels(Instance instance, int numLabels, int[] labelIndices) { boolean[] trueLabels = new boolean[numLabels]; for (int counter = 0; counter < numLabels; counter++) { int classIdx = labelIndices[counter]; String classValue = instance.attribute(classIdx).value((int) instance.value(classIdx)); trueLabels[counter] = classValue.equals("1"); } return trueLabels; }
public List<String> performPrediction(Classifier cl, Instances data) throws Exception { List<String> results = new ArrayList<>(); for (int j = 0; j < data.size(); j++) { double[] vals = null; try { vals = cl.distributionForInstance(data.instance(j)); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } List<String> outcomes = new ArrayList<String>(); for (int i = 0; i < vals.length; i++) { if (vals[i] >= threshold) { String label = data.instance(j).attribute(i).name(); outcomes.add(label); } } results.add(StringUtils.join(outcomes, ",")); } return results; }
/** * Builds a string listing the attribute values in a specified range of indices, * separated by commas and enclosed in brackets. * * @param instance the instance to print the values from * @return a string listing values of the attributes in the range */ protected String attributeValuesString(Instance instance) { StringBuffer text = new StringBuffer(); if (m_Attributes != null) { text.append(" <" + TAG_ATTRIBUTES + ">\n"); m_Attributes.setUpper(instance.numAttributes() - 1); for (int i=0; i<instance.numAttributes(); i++) { if (m_Attributes.isInRange(i) && i != instance.classIndex()) { text.append(" <" + TAG_ATTRIBUTE + " " + ATT_INDEX + "=\"" + (i+1) + "\"" + " " + ATT_NAME + "=\"" + sanitize(instance.attribute(i).name()) + "\"" + " " + ATT_TYPE + "=\"" + Attribute.typeToString(instance.attribute(i).type()) + "\"" + ">"); text.append(sanitize(instance.toString(i))); text.append("</" + TAG_ATTRIBUTE + ">\n"); } } text.append(" </" + TAG_ATTRIBUTES + ">\n"); } return text.toString(); }
/** * Returns index of subset instance is assigned to. Returns -1 if instance is * assigned to more than one subset. * * @exception Exception if something goes wrong */ @Override public final int whichSubset(Instance instance) throws Exception { if (instance.isMissing(m_attIndex)) { return -1; } else { if (instance.attribute(m_attIndex).isNominal()) { return (int) instance.value(m_attIndex); } else if (instance.value(m_attIndex) <= m_splitPoint) { return 0; } else { return 1; } } }
private void processSingleton(Instance current, ArrayList<BinaryItem> singletons) throws Exception { if (current instanceof SparseInstance) { for (int j = 0; j < current.numValues(); j++) { int attIndex = current.index(j); singletons.get(attIndex).increaseFrequency(); } } else { for (int j = 0; j < current.numAttributes(); j++) { if (!current.isMissing(j)) { if (current.attribute(j).numValues() == 1 || current.value(j) == m_positiveIndex - 1) { singletons.get(j).increaseFrequency(); } } } } }
/** * Returns the subset an instance falls into. * * @param instance the instance to check * @return the subset the instance falls into * @throws Exception if something goes wrong */ protected int whichSubset(Instance instance) throws Exception { if (instance.isMissing(m_AttIndex)) { return 2; } else if (instance.attribute(m_AttIndex).isNominal()) { if ((int)instance.value(m_AttIndex) == m_SplitPoint) { return 0; } else { return 1; } } else { if (instance.value(m_AttIndex) <= m_SplitPoint) { return 0; } else { return 1; } } }
/** * Returns index of subset instance is assigned to. Returns -1 if instance is * assigned to more than one subset. * * @exception Exception if something goes wrong */ @Override public final int whichSubset(Instance instance) throws Exception { if (instance.isMissing(m_attIndex)) { return -1; } else { if (instance.attribute(m_attIndex).isNominal()) { return (int) instance.value(m_attIndex); } else if (instance.value(m_attIndex) <= m_splitPoint) { return 0; } else { return 1; } } }
public final int whichSubset(Instance instance) throws Exception { if (instance.isMissing(m_attIndex)) return -1; else{ if (instance.attribute(m_attIndex).isNominal()) return (int)instance.value(m_attIndex); else if (Utils.smOrEq(instance.value(m_attIndex),m_splitPoint)) return 0; else return 1; } }
public final int whichSubset(Instance instance) throws Exception { if (instance.isMissing(m_attIndex)) return -1; else{ if (instance.attribute(m_attIndex).isNominal()) return (int)instance.value(m_attIndex); else if (Utils.smOrEq(instance.value(m_attIndex),m_splitPoint)) return 0; else return 1; } }
private void processSingleton(Instance current, ArrayList<BinaryItem> singletons) throws Exception { if (current instanceof SparseInstance) { for (int j = 0; j < current.numValues(); j++) { int attIndex = current.index(j); singletons.get(attIndex).increaseFrequency(); } } else { for (int j = 0; j < current.numAttributes(); j++) { if (!current.isMissing(j)) { if (current.attribute(j).numValues() == 1 || current.value(j) == m_positiveIndex - 1) { singletons.get(j).increaseFrequency(); } } } } }
/** * ensure that all variables are nominal and that there are no missing values * * @param instance instance to check and quantize and/or fill in missing * values * @return filtered instance * @throws Exception if a filter (Discretize, ReplaceMissingValues) fails */ protected Instance normalizeInstance(Instance instance) throws Exception { if ((m_nNonDiscreteAttribute > -1) && (instance.attribute(m_nNonDiscreteAttribute).type() != Attribute.NOMINAL)) { m_DiscretizeFilter.input(instance); instance = m_DiscretizeFilter.output(); } m_MissingValuesFilter.input(instance); instance = m_MissingValuesFilter.output(); return instance; } // normalizeInstance
/** * ensure that all variables are nominal and that there are no missing values * * @param instance instance to check and quantize and/or fill in missing * values * @return filtered instance * @throws Exception if a filter (Discretize, ReplaceMissingValues) fails */ protected Instance normalizeInstance(Instance instance) throws Exception { if ((m_nNonDiscreteAttribute > -1) && (instance.attribute(m_nNonDiscreteAttribute).type() != Attribute.NOMINAL)) { m_DiscretizeFilter.input(instance); instance = m_DiscretizeFilter.output(); } m_MissingValuesFilter.input(instance); instance = m_MissingValuesFilter.output(); return instance; } // normalizeInstance
@Override public Instances transformInstances(MultiLabelInstances mlData) throws Exception { // calculate label occurences numOfLabels = mlData.getNumLabels(); Instances data = mlData.getDataSet(); labelOccurance = new int[numOfLabels]; labelIndices = mlData.getLabelIndices(); int numInstances = data.numInstances(); for (int i = 0; i < numInstances; i++) { for (int j = 0; j < numOfLabels; j++) { if (data.instance(i).attribute(labelIndices[j]).value((int) data.instance(i).value(labelIndices[j])).equals("1")) { labelOccurance[j]++; } } } return super.transformInstances(mlData); }
/** * returns the TYPE of the attribute at the given position * * @param rowIndex the index of the row * @param columnIndex the index of the column * @return the attribute type */ public int getType(int rowIndex, int columnIndex) { int result; result = Attribute.STRING; if ((rowIndex < 0) && columnIndex > 0 && columnIndex < getColumnCount()) { result = m_Data.attribute(columnIndex - 1).type(); } else if ((rowIndex >= 0) && (rowIndex < getRowCount()) && (columnIndex > 0) && (columnIndex < getColumnCount())) { result = m_Data.instance(rowIndex).attribute(columnIndex - 1).type(); } return result; }
/** * returns the TYPE of the attribute at the given position * * @param rowIndex the index of the row * @param columnIndex the index of the column * @return the attribute type */ public int getType(int rowIndex, int columnIndex) { int result; result = Attribute.STRING; if ((rowIndex < 0) && columnIndex > 0 && columnIndex < getColumnCount()) { result = m_Data.attribute(columnIndex - 1).type(); } else if ((rowIndex >= 0) && (rowIndex < getRowCount()) && (columnIndex > 0) && (columnIndex < getColumnCount())) { result = m_Data.instance(rowIndex).attribute(columnIndex - 1).type(); } return result; }
/** * returns the TYPE of the attribute at the given position * * @param rowIndex the index of the row * @param columnIndex the index of the column * @return the attribute type */ public int getType(int rowIndex, int columnIndex) { int result; result = Attribute.STRING; if ((rowIndex < 0) && isAttribute(columnIndex)) { result = m_Data.attribute(getAttributeIndex(columnIndex)).type(); } else if ((rowIndex >= 0) && (rowIndex < getRowCount()) && isAttribute(columnIndex)) { result = m_Data.instance(rowIndex).attribute(getAttributeIndex(columnIndex)).type(); } return result; }
@Override public void add(Instance instance) { double[] values = instance.toDoubleArray(); Object[] obvValues = new Object[table.getSchema().getColumnCount()]; for (int i = 0; i < table.getSchema().getColumnCount(); i++) { Attribute att = instance.attribute(i); Class<?> c = table.getSchema().getColumnType(i); if(att.isString() && ObviousWekaUtils.isString(c)) { obvValues[i] = instance.attribute(i).value(i); } else if (att.isNumeric() && ObviousWekaUtils.isNumeric(c)) { obvValues[i] = instance.value(att); } else if (att.isDate() && ObviousWekaUtils.isDate(c)) { obvValues[i] = new Date((long) values[i]); } else { obvValues[i] = null; } } Tuple tuple = new TupleImpl(table.getSchema(), obvValues); m_Instances.addElement(new ObviousWekaInstance(tuple, this)); table.addRow(tuple); }
@Override public Instance output() { Instance outInst = m_delegate.output(); if (m_hasStringAtts && outInst != null) { for (int i = 0; i < outInst.dataset().numAttributes(); i++) { if (outInst.dataset().attribute(i).isString() && !outInst.isMissing(i)) { String val = outInst.stringValue(i); outInst.attribute(i).setStringValue(val); outInst.setValue(i, 0); } } } return outInst; }
/** * returns the TYPE of the attribute at the given position * * @param rowIndex the index of the row * @param columnIndex the index of the column * @return the attribute type */ public int getType(int rowIndex, int columnIndex) { int result; result = Attribute.STRING; if ((rowIndex < 0) && isAttribute(columnIndex)) { result = m_Data.attribute(getAttributeIndex(columnIndex)).type(); } else if ((rowIndex >= 0) && (rowIndex < getRowCount()) && isAttribute(columnIndex)) { result = m_Data.instance(rowIndex).attribute(getAttributeIndex(columnIndex)).type(); } return result; }