private Instances makeClusterDataSetClass(Instances format, weka.clusterers.Clusterer clusterer, String relationNameModifier) throws Exception { weka.filters.unsupervised.attribute.Add addF = new weka.filters.unsupervised.attribute.Add(); addF.setAttributeIndex("last"); String clustererName = clusterer.getClass().getName(); clustererName = clustererName.substring(clustererName.lastIndexOf('.') + 1, clustererName.length()); addF.setAttributeName("assigned_cluster: " + clustererName); // if (format.classAttribute().isNominal()) { String clusterLabels = "0"; /* * Enumeration enu = format.classAttribute().enumerateValues(); * clusterLabels += (String)enu.nextElement(); while (enu.hasMoreElements()) * { clusterLabels += ","+(String)enu.nextElement(); } */ for (int i = 1; i <= clusterer.numberOfClusters() - 1; i++) { clusterLabels += "," + i; } addF.setNominalLabels(clusterLabels); // } addF.setInputFormat(format); Instances newInstances = weka.filters.Filter.useFilter(format, addF); newInstances.setRelationName(format.relationName() + relationNameModifier); return newInstances; }
private Instances makeClusterDataSetClass(Instances format, weka.clusterers.Clusterer clusterer, String relationNameModifier) throws Exception { weka.filters.unsupervised.attribute.Add addF = new weka.filters.unsupervised.attribute.Add(); addF.setAttributeIndex("last"); String clustererName = clusterer.getClass().getName(); clustererName = clustererName.substring(clustererName.lastIndexOf('.') + 1, clustererName.length()); addF.setAttributeName("assigned_cluster: " + clustererName); // if (format.classAttribute().isNominal()) { String clusterLabels = "0"; /* * Enumeration enu = format.classAttribute().enumerateValues(); * clusterLabels += (String)enu.nextElement(); while (enu.hasMoreElements()) * { clusterLabels += ","+(String)enu.nextElement(); } */ for (int i = 1; i <= clusterer.numberOfClusters() - 1; i++) { clusterLabels += "," + i; } addF.setNominalLabels(clusterLabels); // } addF.setInputFormat(format); Instances newInstances = weka.filters.Filter.useFilter(format, addF); newInstances.setRelationName(format.relationName() + relationNameModifier); return newInstances; }
classLabels += "," + (String) enu.nextElement(); addF.setNominalLabels(classLabels);
classLabels += "," + (String) enu.nextElement(); addF.setNominalLabels(classLabels);
public Instances getPredictionInstancesSingleLabel(Instances testData, Classifier cl) throws Exception { StringBuffer classVals = new StringBuffer(); for (int i = 0; i < testData.classAttribute().numValues(); i++) { if (classVals.length() > 0) { classVals.append(","); } classVals.append(testData.classAttribute().value(i)); } // get predictions List<Double> labelPredictionList = new ArrayList<Double>(); for (int i = 0; i < testData.size(); i++) { labelPredictionList.add(cl.classifyInstance(testData.instance(i))); } // add an attribute with the predicted values at the end off the attributes Add filter = new Add(); filter.setAttributeName(WekaTestTask.PREDICTION_CLASS_LABEL_NAME); if (classVals.length() > 0) { filter.setAttributeType(new SelectedTag(Attribute.NOMINAL, Add.TAGS_TYPE)); filter.setNominalLabels(classVals.toString()); } filter.setInputFormat(testData); testData = Filter.useFilter(testData, filter); // fill predicted values for each instance for (int i = 0; i < labelPredictionList.size(); i++) { testData.instance(i).setValue(testData.classIndex() + 1, labelPredictionList.get(i)); } return testData; }
public Instances getPredictionInstancesSingleLabel(Instances testData, Classifier cl) throws Exception { StringBuffer classVals = new StringBuffer(); for (int i = 0; i < testData.classAttribute().numValues(); i++) { if (classVals.length() > 0) { classVals.append(","); } classVals.append(testData.classAttribute().value(i)); } // get predictions List<Double> labelPredictionList = new ArrayList<Double>(); for (int i = 0; i < testData.size(); i++) { labelPredictionList.add(cl.classifyInstance(testData.instance(i))); } // add an attribute with the predicted values at the end off the attributes Add filter = new Add(); filter.setAttributeName(WekaTestTask.PREDICTION_CLASS_LABEL_NAME); if (classVals.length() > 0) { filter.setAttributeType(new SelectedTag(Attribute.NOMINAL, Add.TAGS_TYPE)); filter.setNominalLabels(classVals.toString()); } filter.setInputFormat(testData); testData = Filter.useFilter(testData, filter); // fill predicted values for each instance for (int i = 0; i < labelPredictionList.size(); i++) { testData.instance(i).setValue(testData.classIndex() + 1, labelPredictionList.get(i)); } return testData; }
if (classVals.length() > 0) { filter.setAttributeType(new SelectedTag(Attribute.NOMINAL, Add.TAGS_TYPE)); filter.setNominalLabels(classVals.toString());
private Instances getPredictionInstancesMultiLabel(Instances testData, Classifier cl, double[] thresholdArray) throws Exception { int numLabels = testData.classIndex(); // get predictions List<double[]> labelPredictionList = new ArrayList<double[]>(); for (int i = 0; i < testData.numInstances(); i++) { labelPredictionList.add(cl.distributionForInstance(testData.instance(i))); } // add attributes to store predictions in test data Add filter = new Add(); for (int i = 0; i < numLabels; i++) { filter.setAttributeIndex(Integer.toString(numLabels + i + 1)); filter.setNominalLabels("0,1"); filter.setAttributeName( testData.attribute(i).name() + "_" + WekaTestTask.PREDICTION_CLASS_LABEL_NAME); filter.setInputFormat(testData); testData = Filter.useFilter(testData, filter); } // fill predicted values for each instance for (int i = 0; i < labelPredictionList.size(); i++) { for (int j = 0; j < labelPredictionList.get(i).length; j++) { testData.instance(i).setValue(j + numLabels, labelPredictionList.get(i)[j] >= thresholdArray[j] ? 1. : 0.); } } return testData; }
private Instances getPredictionInstancesMultiLabel(Instances testData, Classifier cl, double[] thresholdArray) throws Exception { int numLabels = testData.classIndex(); // get predictions List<double[]> labelPredictionList = new ArrayList<double[]>(); for (int i = 0; i < testData.numInstances(); i++) { labelPredictionList.add(cl.distributionForInstance(testData.instance(i))); } // add attributes to store predictions in test data Add filter = new Add(); for (int i = 0; i < numLabels; i++) { filter.setAttributeIndex(Integer.toString(numLabels + i + 1)); filter.setNominalLabels("0,1"); filter.setAttributeName( testData.attribute(i).name() + "_" + WekaTestTask.PREDICTION_CLASS_LABEL_NAME); filter.setInputFormat(testData); testData = Filter.useFilter(testData, filter); } // fill predicted values for each instance for (int i = 0; i < labelPredictionList.size(); i++) { for (int j = 0; j < labelPredictionList.get(i).length; j++) { testData.instance(i).setValue(j + numLabels, labelPredictionList.get(i)[j] >= thresholdArray[j] ? 1. : 0.); } } return testData; }
for (int i = 0; i < numLabels; i++) { filter.setAttributeIndex(new Integer(numLabels + i + 1).toString()); filter.setNominalLabels("0,1"); filter.setAttributeName(testData.attribute(i).name() + "_" + TestTask.PREDICTION_CLASS_LABEL_NAME);
public void testAddNominal() { m_Filter = getFilter(); ((Add)m_Filter).setNominalLabels("hello,there,bob"); testBuffered(); testType(Attribute.NOMINAL); }
af.setNominalLabels(labels); af.setInputFormat(tempInst); tempInst = Filter.useFilter(tempInst, af);
public void testAddNominal() { m_Filter = getFilter(); ((Add)m_Filter).setNominalLabels("hello,there,bob"); testBuffered(); testType(Attribute.NOMINAL); }
clusterLabels += "," + i; addF.setNominalLabels(clusterLabels); addF.setInputFormat(newData); newData = weka.filters.Filter.useFilter(newData, addF);
/** * Constructor * * @param data a multi-label dataset */ public BinaryRelevanceTransformation(MultiLabelInstances data) { try { this.data = data; remove = new Remove(); int[] labelIndices = data.getLabelIndices(); int[] indices = new int[labelIndices.length]; System.arraycopy(labelIndices, 0, indices, 0, labelIndices.length); remove.setAttributeIndicesArray(indices); remove.setInvertSelection(false); remove.setInputFormat(data.getDataSet()); shell = Filter.useFilter(data.getDataSet(), remove); add = new Add(); add.setAttributeIndex("last"); add.setNominalLabels("0,1"); add.setAttributeName("BinaryRelevanceLabel"); add.setInputFormat(shell); shell = Filter.useFilter(shell, add); shell.setClassIndex(shell.numAttributes() - 1); } catch (Exception ex) { Logger.getLogger(BinaryRelevanceTransformation.class.getName()).log(Level.SEVERE, null, ex); } }
clusterLabels += "," + i; addF.setNominalLabels(clusterLabels); addF.setInputFormat(newData); newData = weka.filters.Filter.useFilter(newData, addF);
classLabels += "," + classAttribute.value(i); addF.setNominalLabels(classLabels);
addFilter.setNominalLabels(StringUtils.join(trainLabels, ',')); addFilter.setAttributeName(Constants.CLASS_ATTRIBUTE_NAME + COMPATIBLE_OUTCOME_CLASS); addFilter.setInputFormat(testData);
addFilter.setNominalLabels(StringUtils.join(trainLabels, ',')); addFilter.setAttributeName(Constants.CLASS_ATTRIBUTE_NAME + COMPATIBLE_OUTCOME_CLASS); addFilter.setInputFormat(testData);