List of usage examples for weka.core Instance insertAttributeAt
public void insertAttributeAt(int position);
From source file:mulan.classifier.transformation.CalibratedLabelRanking.java
License:Open Source License
/** * This method does a prediction for an instance with the values of label missing * according to QWeighted algorithm for Multilabel Classification (QCMLPP2), which is * described in ://w w w . ja v a 2 s . c o m * Loza Mencia, E., Park, S.-H., and Fuernkranz, J. (2009) * Efficient voting prediction for pairwise multilabel classification. * In Proceedings of 17th European Symposium on Artificial * Neural Networks (ESANN 2009), Bruges (Belgium), April 2009 * * This method reduces the number of classifier evaluations and guarantees the same * Multilabel Output as ordinary Voting. But: the estimated confidences are only * approximated. Therefore, ranking-based performances are worse than ordinary voting. * @param instance * @return prediction * @throws java.lang.Exception */ public MultiLabelOutput makePredictionQW(Instance instance) throws Exception { int[] voteLabel = new int[numLabels]; int[] played = new int[numLabels + 1]; int[][] playedMatrix = new int[numLabels + 1][numLabels + 1]; int[] sortarr = new int[numLabels + 1]; double[] limits = new double[numLabels]; boolean[] bipartition = new boolean[numLabels]; double[] confidences = new double[numLabels]; int voteVirtual = 0; double limitVirtual = 0.0; boolean allEqualClassesFound = false; // delete all labels and add a new atribute at the end Instance newInstance = RemoveAllLabels.transformInstance(instance, labelIndices); newInstance.insertAttributeAt(newInstance.numAttributes()); //initialize the array voteLabel Arrays.fill(voteLabel, 0); // evaluate all classifiers of the calibrated label beforehand, #numLabels 1 vs. A evaluations MultiLabelOutput virtualMLO = virtualLabelModels.makePrediction(instance); boolean[] virtualBipartition = virtualMLO.getBipartition(); for (int i = 0; i < numLabels; i++) { if (virtualBipartition[i]) { voteLabel[i]++; } else { voteVirtual++; } played[i]++; playedMatrix[i][numLabels] = 1; playedMatrix[numLabels][i] = 1; limits[i] = played[i] - voteLabel[i]; } limitVirtual = numLabels - voteVirtual; played[numLabels] = numLabels; // apply QWeighted iteratively to estimate all relevant labels until the // calibrated label is found boolean found = false; int pos = 0; int player1 = -1; int player2 = -1; while (!allEqualClassesFound && pos < numLabels) { while (!found) { // opponent selection process: pair best against second best w.r.t. to number of "lost games" // player1 = pick player with min(limits[player]) && player isn't ranked sortarr = Utils.sort(limits); player1 = sortarr[0]; player2 = -1; int i = 1; // can we found unplayed matches of player1 ? if (played[player1] < numLabels) { // search for best opponent while (player2 == -1 && i < sortarr.length) { // already played ?? if (playedMatrix[player1][sortarr[i]] == 0) { player2 = sortarr[i]; } i++; } // play found Pairing and update stats int modelIndex = getRRClassifierIndex(player1, player2); newInstance.setDataset(metaDataTest[modelIndex]); double[] distribution = oneVsOneModels[modelIndex].distributionForInstance(newInstance); int maxIndex = (distribution[0] > distribution[1]) ? 0 : 1; // Ensure correct predictions both for class values {0,1} and {1,0} Attribute classAttribute = metaDataTest[modelIndex].classAttribute(); if (classAttribute.value(maxIndex).equals("1")) { voteLabel[player1 > player2 ? player2 : player1]++; } else { voteLabel[player1 > player2 ? player1 : player2]++; } // update stats played[player1]++; played[player2]++; playedMatrix[player1][player2] = 1; playedMatrix[player2][player1] = 1; limits[player1] = played[player1] - voteLabel[player1]; limits[player2] = played[player2] - voteLabel[player2]; } // full played, there are no opponents left else { found = true; } } //arrange already as relevant validated labels at the end of possible opponents limits[player1] = Double.MAX_VALUE; //check for possible labels, which can still gain greater or equal votes as the calibrated label allEqualClassesFound = true; for (int i = 0; i < numLabels; i++) { if (limits[i] <= limitVirtual) { allEqualClassesFound = false; } } // search for next relevant label found = false; pos++; } //Generate Multilabel Output for (int i = 0; i < numLabels; i++) { if (voteLabel[i] >= voteVirtual) { bipartition[i] = true; } else { bipartition[i] = false; } confidences[i] = 1.0 * voteLabel[i] / numLabels; } MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences); return mlo; }
From source file:mulan.classifier.transformation.MultiClassLearner.java
License:Open Source License
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { //delete labels instance = RemoveAllLabels.transformInstance(instance, labelIndices); instance.setDataset(null);//from w w w .ja va 2 s .c o m instance.insertAttributeAt(instance.numAttributes()); instance.setDataset(header); double[] distribution = baseClassifier.distributionForInstance(instance); MultiLabelOutput mlo = new MultiLabelOutput(MultiLabelOutput.ranksFromValues(distribution)); return mlo; }
From source file:mulan.classifier.transformation.MultiLabelStacking.java
License:Open Source License
/** * Attaches an index attribute at the beginning of each instance * * @param original//w w w. j a v a 2s .co m * @return */ protected Instances attachIndexes(Instances original) { ArrayList<Attribute> attributes = new ArrayList<Attribute>(original.numAttributes() + 1); for (int i = 0; i < original.numAttributes(); i++) { attributes.add(original.attribute(i)); } // Add attribute for holding the index at the beginning. attributes.add(0, new Attribute("Index")); Instances transformed = new Instances("Meta format", attributes, 0); for (int i = 0; i < original.numInstances(); i++) { Instance newInstance; newInstance = (Instance) original.instance(i).copy(); newInstance.setDataset(null); newInstance.insertAttributeAt(0); newInstance.setValue(0, i); transformed.add(newInstance); } transformed.setClassIndex(original.classIndex() + 1); return transformed; }
From source file:mulan.classifier.transformation.TwoStageClassifierChainArchitecture.java
License:Open Source License
/** * This method does a prediction for an instance with the values of label * missing according to Two Stage Pruned Classifier Chain (TSPCCA), which is * described in : Madjarov, Gj., Gjorgjevikj, D. and Dzeroski, S. Two stage * architecture for multi-label learning Pattern Recognition, vol. 45, pp. * 10191034, 2012//from w w w. j a va 2 s .c om * * @param instance the instance used * @return prediction the prediction made * @throws java.lang.Exception Potential exception thrown. To be handled in an upper level. */ private MultiLabelOutput makePredictionTSCCV(Instance instance) throws Exception { boolean[] bipartition = new boolean[numLabels]; double[] confidences = new double[numLabels]; int[] voteLabel = new int[numLabels + 1]; int[] noVoteLabel = new int[numLabels + 1]; int[] voteFromVirtualModels = new int[numLabels]; double[] confidenceFromVirtualModels = new double[numLabels]; //initialize the array voteLabel Arrays.fill(voteLabel, 0); Arrays.fill(noVoteLabel, 0); Arrays.fill(voteFromVirtualModels, 0); Arrays.fill(confidenceFromVirtualModels, 0.0); int voteVirtual = 0; MultiLabelOutput virtualMLO = virtualLabelModels.makePrediction(instance); boolean[] virtualBipartition = virtualMLO.getBipartition(); //number of classifiers of the first layer that forward the instance to the second layer int forwards = 0; for (int i = 0; i < numLabels; i++) { if (virtualMLO.hasConfidences()) { confidenceFromVirtualModels[i] = virtualMLO.getConfidences()[i]; //System.out.print(confidenceFromVirtualModels[i]); //System.out.print("\t"); } if (virtualBipartition[i]) { voteLabel[i]++; voteFromVirtualModels[i]++; } else { voteVirtual++; } if (confidenceFromVirtualModels[i] > threshold) { forwards++; } } Instance newInstanceFirstStage; //add predictions from the vurtual models if (instance instanceof SparseInstance) { newInstanceFirstStage = modifySparseInstance(instance, virtualMLO.getConfidences()); } else { newInstanceFirstStage = modifyDenseInstance(instance, virtualMLO.getConfidences()); } // delete all labels and add a new atribute at the end Instance newInstance = RemoveAllLabels.transformInstance(newInstanceFirstStage, labelIndices); newInstance.insertAttributeAt(newInstance.numAttributes()); int counter = 0; for (int label1 = 0; label1 < numLabels - 1; label1++) { for (int label2 = label1 + 1; label2 < numLabels; label2++) { if (!nodata[counter]) { if (confidenceFromVirtualModels[label1] > threshold && confidenceFromVirtualModels[label2] > threshold) { double distribution[]; try { newInstance.setDataset(metaDataTest[counter]); distribution = oneVsOneModels[counter].distributionForInstance(newInstance); } catch (Exception e) { System.out.println(e); return null; } int maxIndex = (distribution[0] > distribution[1]) ? 0 : 1; // Ensure correct predictions both for class values {0,1} and {1,0} Attribute classAttribute = metaDataTest[counter].classAttribute(); if (classAttribute.value(maxIndex).equals("1")) { voteLabel[label1]++; } else { voteLabel[label2]++; } } else if (confidenceFromVirtualModels[label1] > threshold) { voteLabel[label1]++; } else if (confidenceFromVirtualModels[label2] > threshold) { voteLabel[label2]++; } else { noVoteLabel[label1]++; noVoteLabel[label2]++; } } counter++; } } avgForwards += forwards; for (int i = 0; i < numLabels; i++) { if (voteLabel[i] >= voteVirtual) { bipartition[i] = true; confidences[i] = (1.0 * voteLabel[i]) / (numLabels - noVoteLabel[i]); } else { bipartition[i] = false; confidences[i] = 1.0 * confidenceFromVirtualModels[i] / numLabels; //confidences[i]=confidenceFromVirtualModels[i]; } //System.out.println(bipartition[i]); //System.out.println(confidences[i]); //confidences[i]*=confidenceFromVirtualModels[i]; } MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences); return mlo; }
From source file:mulan.classifier.transformation.TwoStageClassifierChainArchitecture.java
License:Open Source License
private Instance modifyDenseInstance(Instance instance, double[] confidences) { Instance modifiedIns = new DenseInstance(instance); for (int i = confidences.length - 1; i >= 0; i--) { modifiedIns.insertAttributeAt(0); modifiedIns.setValue(0, confidences[i]); }/*from w ww.j av a2 s.c om*/ return modifiedIns; }
From source file:mulan.classifier.transformation.TwoStagePrunedClassifierChainArchitecture.java
License:Open Source License
/** * This method does a prediction for an instance with the values of label * missing according to Two Stage Voting Method (TSVM), which is described * in : Madjarov, Gj., Gjorgjevikj, D. and Dzeroski, S. Efficient two stage * voting architecture for pairwise multi-label classification. In AI 2010: * Advances in Artificial Intelligence (J. Li, ed.), vol. 6464 of Lecture * Notes in Computer Science, pp. 164173, 2011 * * @param instance/* w w w. j a va2 s .co m*/ * @return prediction * @throws java.lang.Exception Potential exception thrown. To be handled in an upper level. */ private MultiLabelOutput makePredictionTSCCA(Instance instance) throws Exception { boolean[] bipartition = new boolean[numLabels]; double[] confidences = new double[numLabels]; int[] voteLabel = new int[numLabels + 1]; int[] noVoteLabel = new int[numLabels + 1]; int[] voteFromVirtualModels = new int[numLabels]; double[] confidenceFromVirtualModels = new double[numLabels]; //System.out.println("Instance:" + instance.toString()); //initialize the array voteLabel Arrays.fill(voteLabel, 0); Arrays.fill(noVoteLabel, 0); Arrays.fill(voteFromVirtualModels, 0); Arrays.fill(confidenceFromVirtualModels, 0.0); int voteVirtual = 0; MultiLabelOutput virtualMLO = virtualLabelModels.makePrediction(instance); boolean[] virtualBipartition = virtualMLO.getBipartition(); //number of classifiers of the first layer that forward the instance to the second layer int forwards = 0; for (int i = 0; i < numLabels; i++) { if (virtualMLO.hasConfidences()) { confidenceFromVirtualModels[i] = virtualMLO.getConfidences()[i]; //System.out.print(confidenceFromVirtualModels[i]); //System.out.print("\t"); } if (virtualBipartition[i]) { voteLabel[i]++; voteFromVirtualModels[i]++; } else { voteVirtual++; } if (confidenceFromVirtualModels[i] > threshold) { forwards++; } } int counter = 0; for (int label1 = 0; label1 < numLabels - 1; label1++) { for (int label2 = label1 + 1; label2 < numLabels; label2++) { Instance newInstanceFirstStage; //add predictions from the vurtual models if (instance instanceof SparseInstance) { newInstanceFirstStage = modifySparseInstance(instance, virtualMLO.getConfidences()[label1], virtualMLO.getConfidences()[label2]); } else { newInstanceFirstStage = modifyDenseInstance(instance, virtualMLO.getConfidences()[label1], virtualMLO.getConfidences()[label2]); } // delete all labels and add a new atribute at the end Instance newInstance = RemoveAllLabels.transformInstance(newInstanceFirstStage, labelIndices); newInstance.insertAttributeAt(newInstance.numAttributes()); if (!nodata[counter]) { if (confidenceFromVirtualModels[label1] > threshold && confidenceFromVirtualModels[label2] > threshold) { double distribution[]; try { newInstance.setDataset(metaDataTest[counter]); distribution = oneVsOneModels[counter].distributionForInstance(newInstance); } catch (Exception e) { System.out.println(e); return null; } int maxIndex = (distribution[0] > distribution[1]) ? 0 : 1; // Ensure correct predictions both for class values {0,1} and {1,0} Attribute classAttribute = metaDataTest[counter].classAttribute(); if (classAttribute.value(maxIndex).equals("1")) { voteLabel[label1]++; } else { voteLabel[label2]++; } } else if (confidenceFromVirtualModels[label1] > threshold) { voteLabel[label1]++; } else if (confidenceFromVirtualModels[label2] > threshold) { voteLabel[label2]++; } else { noVoteLabel[label1]++; noVoteLabel[label2]++; } } counter++; } } avgForwards += forwards; for (int i = 0; i < numLabels; i++) { if (voteLabel[i] >= voteVirtual) { bipartition[i] = true; confidences[i] = (1.0 * voteLabel[i]) / (numLabels - noVoteLabel[i]); } else { bipartition[i] = false; confidences[i] = 1.0 * confidenceFromVirtualModels[i] / numLabels; //confidences[i]=confidenceFromVirtualModels[i]; } //System.out.println(bipartition[i]); //System.out.println(confidences[i]); //confidences[i]*=confidenceFromVirtualModels[i]; } MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences); return mlo; }
From source file:mulan.classifier.transformation.TwoStagePrunedClassifierChainArchitecture.java
License:Open Source License
public Instance modifyDenseInstance(Instance ins, double value1, double value2) { Instance modifiedIns = new DenseInstance(ins); modifiedIns.insertAttributeAt(0); modifiedIns.setValue(0, value1);/*from w w w. j a v a2 s .c o m*/ modifiedIns.insertAttributeAt(0); modifiedIns.setValue(0, value2); return modifiedIns; }
From source file:mulan.transformations.IncludeLabelsTransformation.java
License:Open Source License
/** * * @param mlData multi-label data//from ww w . j a v a 2 s . co m * @return transformed instances * @throws Exception Potential exception thrown. To be handled in an upper level. */ public Instances transformInstances(MultiLabelInstances mlData) throws Exception { int numLabels = mlData.getNumLabels(); labelIndices = mlData.getLabelIndices(); // remove all labels Instances transformed = RemoveAllLabels.transformInstances(mlData); // add at the end an attribute with values the label names ArrayList<String> labelNames = new ArrayList<String>(numLabels); for (int counter = 0; counter < numLabels; counter++) { labelNames.add(mlData.getDataSet().attribute(labelIndices[counter]).name()); } Attribute attrLabel = new Attribute("Label", labelNames); transformed.insertAttributeAt(attrLabel, transformed.numAttributes()); // and at the end a binary attribute ArrayList<String> binaryValues = new ArrayList<String>(2); binaryValues.add("0"); binaryValues.add("1"); Attribute classAttr = new Attribute("Class", binaryValues); transformed.insertAttributeAt(classAttr, transformed.numAttributes()); // add instances transformed = new Instances(transformed, 0); transformed.setClassIndex(transformed.numAttributes() - 1); Instances data = mlData.getDataSet(); for (int instanceIndex = 0; instanceIndex < data.numInstances(); instanceIndex++) { for (int labelCounter = 0; labelCounter < numLabels; labelCounter++) { Instance temp; temp = RemoveAllLabels.transformInstance(data.instance(instanceIndex), labelIndices); temp.setDataset(null); temp.insertAttributeAt(temp.numAttributes()); temp.insertAttributeAt(temp.numAttributes()); temp.setDataset(transformed); temp.setValue(temp.numAttributes() - 2, (String) labelNames.get(labelCounter)); if (data.attribute(labelIndices[labelCounter]) .value((int) data.instance(instanceIndex).value(labelIndices[labelCounter])).equals("1")) { temp.setValue(temp.numAttributes() - 1, "1"); } else { temp.setValue(temp.numAttributes() - 1, "0"); } transformed.add(temp); } } return transformed; }
From source file:mulan.transformations.IncludeLabelsTransformation.java
License:Open Source License
/** * Transform an unlabeled instance to the format expected by * the binary classifier/*from w ww.jav a2s. c om*/ * * @param instance an unlabeled instance * @return a transformed unlabeled instance * @throws Exception Potential exception thrown. To be handled in an upper level. */ public Instance transformInstance(Instance instance) throws Exception { if (labelIndices == null) { System.out.println("Label Indices not set!!"); return null; } Instance transformedInstance = RemoveAllLabels.transformInstance(instance, labelIndices); transformedInstance.setDataset(null); transformedInstance.insertAttributeAt(transformedInstance.numAttributes()); transformedInstance.insertAttributeAt(transformedInstance.numAttributes()); return transformedInstance; }
From source file:mulan.transformations.LabelPowersetTransformation.java
License:Open Source License
public Instance transformInstance(Instance instance, int[] labelIndices) throws Exception { Instance transformedInstance = RemoveAllLabels.transformInstance(instance, labelIndices); transformedInstance.setDataset(null); transformedInstance.insertAttributeAt(transformedInstance.numAttributes()); transformedInstance.setDataset(transformedFormat); return transformedInstance; }