List of usage examples for weka.core Instance weight
public double weight();
From source file:mulan.classifier.meta.thresholding.Meta.java
License:Open Source License
/** * A method that modify an instance/*from www . jav a 2 s. c o m*/ * * @param instance to modified * @param xBased the type for constructing the meta dataset * @return a transformed instance for the predictor of labels/threshold */ protected Instance modifiedInstanceX(Instance instance, String xBased) { Instance modifiedIns = null; MultiLabelOutput mlo = null; if (xBased.compareTo("Content-Based") == 0) { Instance tempInstance = RemoveAllLabels.transformInstance(instance, labelIndices); modifiedIns = DataUtils.createInstance(tempInstance, tempInstance.weight(), tempInstance.toDoubleArray()); } else if (xBased.compareTo("Score-Based") == 0) { double[] arrayOfScores = new double[numLabels]; try { mlo = baseLearner.makePrediction(instance); } catch (InvalidDataException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (ModelInitializationException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } arrayOfScores = mlo.getConfidences(); modifiedIns = DataUtils.createInstance(instance, numLabels); for (int i = 0; i < numLabels; i++) { modifiedIns.setValue(i, arrayOfScores[i]); } } else { //Rank-Based try { //Rank-Based double[] arrayOfScores = new double[numLabels]; mlo = baseLearner.makePrediction(instance); arrayOfScores = mlo.getConfidences(); ArrayList<Double> list = new ArrayList(); for (int i = 0; i < numLabels; i++) { list.add(arrayOfScores[i]); } Collections.sort(list); modifiedIns = DataUtils.createInstance(instance, numLabels); int j = numLabels - 1; for (Double x : list) { modifiedIns.setValue(j, x); j--; } } catch (InvalidDataException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (ModelInitializationException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } } return modifiedIns; }
From source file:mulan.classifier.meta.thresholding.MetaLabeler.java
License:Open Source License
protected Instances transformData(MultiLabelInstances trainingData) throws Exception { // initialize classifier instances classifierInstances = RemoveAllLabels.transformInstances(trainingData); classifierInstances = new Instances(classifierInstances, 0); Attribute target = null;/*from www .j a v a 2s. c o m*/ if (classChoice.equals("Nominal-Class")) { int countTrueLabels = 0; Set<Integer> treeSet = new TreeSet(); for (int instanceIndex = 0; instanceIndex < trainingData.getDataSet().numInstances(); instanceIndex++) { countTrueLabels = 0; for (int i = 0; i < numLabels; i++) { int labelIndice = labelIndices[i]; if (trainingData.getDataSet().attribute(labelIndice) .value((int) trainingData.getDataSet().instance(instanceIndex).value(labelIndice)) .equals("1")) { countTrueLabels++; } } treeSet.add(countTrueLabels); } ArrayList<String> classlabel = new ArrayList<String>(); for (Integer x : treeSet) { classlabel.add(x.toString()); } target = new Attribute("Class", classlabel); } else if (classChoice.equals("Numeric-Class")) { target = new Attribute("Class"); } classifierInstances.insertAttributeAt(target, classifierInstances.numAttributes()); classifierInstances.setClassIndex(classifierInstances.numAttributes() - 1); // create instances if (metaDatasetChoice.equals("Content-Based")) { for (int instanceIndex = 0; instanceIndex < trainingData.getNumInstances(); instanceIndex++) { Instance instance = trainingData.getDataSet().instance(instanceIndex); double[] values = instance.toDoubleArray(); double[] newValues = new double[classifierInstances.numAttributes()]; for (int i = 0; i < featureIndices.length; i++) { newValues[i] = values[featureIndices[i]]; } //set the number of true labels of an instance int numTrueLabels = countTrueLabels(instance); if (classChoice.compareTo("Nominal-Class") == 0) { newValues[newValues.length - 1] = classifierInstances .attribute(classifierInstances.numAttributes() - 1).indexOfValue("" + numTrueLabels); } else if (classChoice.compareTo("Numeric-Class") == 0) { newValues[newValues.length - 1] = numTrueLabels; } Instance newInstance = DataUtils.createInstance(instance, instance.weight(), newValues); classifierInstances.add(newInstance); } } else { for (int k = 0; k < kFoldsCV; k++) { //Split data to train and test sets MultiLabelLearner tempLearner; MultiLabelInstances mlTest; if (kFoldsCV == 1) { tempLearner = baseLearner; mlTest = trainingData; } else { Instances train = trainingData.getDataSet().trainCV(kFoldsCV, k); Instances test = trainingData.getDataSet().testCV(kFoldsCV, k); MultiLabelInstances mlTrain = new MultiLabelInstances(train, trainingData.getLabelsMetaData()); mlTest = new MultiLabelInstances(test, trainingData.getLabelsMetaData()); tempLearner = foldLearner.makeCopy(); tempLearner.build(mlTrain); } // copy features and labels, set metalabels for (int instanceIndex = 0; instanceIndex < mlTest.getDataSet().numInstances(); instanceIndex++) { Instance instance = mlTest.getDataSet().instance(instanceIndex); // initialize new class values double[] newValues = new double[classifierInstances.numAttributes()]; // create features valuesX(tempLearner, instance, newValues, metaDatasetChoice); //set the number of true labels of an instance int numTrueLabels = countTrueLabels(instance); if (classChoice.compareTo("Nominal-Class") == 0) { newValues[newValues.length - 1] = classifierInstances .attribute(classifierInstances.numAttributes() - 1) .indexOfValue("" + numTrueLabels); } else if (classChoice.compareTo("Numeric-Class") == 0) { newValues[newValues.length - 1] = numTrueLabels; } // add the new instance to classifierInstances Instance newInstance = DataUtils.createInstance(mlTest.getDataSet().instance(instanceIndex), mlTest.getDataSet().instance(instanceIndex).weight(), newValues); classifierInstances.add(newInstance); } } } return classifierInstances; }
From source file:mulan.classifier.neural.BPMLL.java
License:Open Source License
public MultiLabelOutput makePredictionInternal(Instance instance) throws InvalidDataException { Instance inputInstance = null;/* w ww . j av a 2 s . com*/ if (nominalToBinaryFilter != null) { try { nominalToBinaryFilter.input(instance); inputInstance = nominalToBinaryFilter.output(); inputInstance.setDataset(null); } catch (Exception ex) { throw new InvalidDataException("The input instance for prediction is invalid. " + "Instance is not consistent with the data the model was built for."); } } else { inputInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray()); } int numAttributes = inputInstance.numAttributes(); if (numAttributes < model.getNetInputSize()) { throw new InvalidDataException("Input instance do not have enough attributes " + "to be processed by the model. Instance is not consistent with the data the model was built for."); } // if instance has more attributes than model input, we assume that true outputs // are there, so we remove them List<Integer> someLabelIndices = new ArrayList<Integer>(); boolean labelsAreThere = false; if (numAttributes > model.getNetInputSize()) { for (int index : this.labelIndices) { someLabelIndices.add(index); } labelsAreThere = true; } if (normalizeAttributes) { normalizer.normalize(inputInstance); } int inputDim = model.getNetInputSize(); double[] inputPattern = new double[inputDim]; int indexCounter = 0; for (int attrIndex = 0; attrIndex < numAttributes; attrIndex++) { if (labelsAreThere && someLabelIndices.contains(attrIndex)) { continue; } inputPattern[indexCounter] = inputInstance.value(attrIndex); indexCounter++; } double[] labelConfidences = model.feedForward(inputPattern); double threshold = thresholdF.computeThreshold(labelConfidences); boolean[] labelPredictions = new boolean[numLabels]; Arrays.fill(labelPredictions, false); for (int labelIndex = 0; labelIndex < numLabels; labelIndex++) { if (labelConfidences[labelIndex] > threshold) { labelPredictions[labelIndex] = true; } // translate from bipolar output to binary labelConfidences[labelIndex] = (labelConfidences[labelIndex] + 1) / 2; } MultiLabelOutput mlo = new MultiLabelOutput(labelPredictions, labelConfidences); return mlo; }
From source file:mulan.classifier.transformation.ClassifierChain.java
License:Open Source License
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { boolean[] bipartition = new boolean[numLabels]; double[] confidences = new double[numLabels]; Instance tempInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray()); for (int counter = 0; counter < numLabels; counter++) { double distribution[] = new double[2]; try {/*from w ww .j a va2 s.c o m*/ distribution = ensemble[counter].distributionForInstance(tempInstance); } 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 = ensemble[counter].getFilter().getOutputFormat().classAttribute(); bipartition[chain[counter]] = (classAttribute.value(maxIndex).equals("1")) ? true : false; // The confidence of the label being equal to 1 confidences[chain[counter]] = distribution[classAttribute.indexOfValue("1")]; tempInstance.setValue(labelIndices[chain[counter]], maxIndex); } MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences); return mlo; }
From source file:mulan.regressor.transformation.RegressorChainSimple.java
License:Open Source License
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { double[] scores = new double[numLabels]; // create a new temporary instance so that the passed instance is not altered Instances dataset = instance.dataset(); Instance tempInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray()); for (int counter = 0; counter < numLabels; counter++) { dataset.setClassIndex(chain[counter]); tempInstance.setDataset(dataset); // find the appropriate position for that score in the scores array // i.e. which is the corresponding target int pos = 0; for (int i = 0; i < numLabels; i++) { if (chain[counter] == labelIndices[i]) { pos = i;/*from www. j a va 2s. c o m*/ break; } } scores[pos] = chainRegressors[counter].classifyInstance(tempInstance); tempInstance.setValue(chain[counter], scores[pos]); } MultiLabelOutput mlo = new MultiLabelOutput(scores, true); return mlo; }
From source file:mulan.transformations.RemoveAllLabels.java
License:Open Source License
public static Instance transformInstance(Instance instance, int[] labelIndices) { double[] oldValues = instance.toDoubleArray(); double[] newValues = new double[oldValues.length - labelIndices.length]; int counter1 = 0; int counter2 = 0; for (int i = 0; i < oldValues.length; i++) { if (counter1 < labelIndices.length) if (i == labelIndices[counter1]) { counter1++;/*from w w w . java 2s . co m*/ continue; } newValues[counter2] = oldValues[i]; counter2++; } return DataUtils.createInstance(instance, instance.weight(), newValues); }
From source file:myclassifier.myC45Pack.C45ClassifierSplitModel.java
/** * Splits the given set of instances into subsets. * * @exception Exception if something goes wrong *///from w w w .j av a2s . c om public Instances[] split(Instances dataSet) throws Exception { Instances[] newSubsets = new Instances[numSubsets]; double[] weights; double newWeight; Instance instance; int subset; for (int i = 0; i < numSubsets; i++) { newSubsets[i] = new Instances((Instances) dataSet, dataSet.numInstances()); } for (int i = 0; i < dataSet.numInstances(); i++) { instance = ((Instances) dataSet).instance(i); weights = getWeights(instance); subset = getSubsetIndex(instance); if (subset > -1) { newSubsets[subset].add(instance); } else { for (int j = 0; j < numSubsets; j++) { if (weights[j] > 0) { newWeight = weights[j] * instance.weight(); newSubsets[j].add(instance); newSubsets[j].lastInstance().setWeight(newWeight); } } } } for (int i = 0; i < numSubsets; i++) { newSubsets[i].compactify(); } return newSubsets; }
From source file:myclassifier.myC45Pack.ClassDistribution.java
/** * Adds instance to subDataset.// w w w . j a va2 s .c om * * @exception Exception if something goes wrong */ public void addInstance(int subDatasetIndex, Instance instance) throws Exception { int classIndex = (int) instance.classValue(); double weight = instance.weight(); w_perClassPerSubdataset[subDatasetIndex][classIndex] = w_perClassPerSubdataset[subDatasetIndex][classIndex] + weight; w_perSubdataset[subDatasetIndex] = w_perSubdataset[subDatasetIndex] + weight; w_perClass[classIndex] = w_perClass[classIndex] + weight; totalWeights = totalWeights + weight; }
From source file:myclassifier.myC45Pack.ClassDistribution.java
/** * Adds all instances with unknown values for given attribute, weighted * according to frequency of instances in each bag. * * @exception Exception if something goes wrong *//*w w w .j a v a 2 s .c o m*/ public void addInstWithMissValue(Instances dataSet, int attIndex) throws Exception { double[] valueProbs; double weight, newWeight; int classIndex; Instance instance; valueProbs = new double[w_perSubdataset.length]; for (int i = 0; i < w_perSubdataset.length; i++) { if (totalWeights == 0) { valueProbs[i] = 1.0 / valueProbs.length; } else { valueProbs[i] = w_perSubdataset[i] / totalWeights; } } Enumeration E = dataSet.enumerateInstances(); while (E.hasMoreElements()) { instance = (Instance) E.nextElement(); if (instance.isMissing(attIndex)) { classIndex = (int) instance.classValue(); weight = instance.weight(); w_perClass[classIndex] = w_perClass[classIndex] + weight; totalWeights += weight; for (int i = 0; i < w_perSubdataset.length; i++) { newWeight = valueProbs[i] * weight; w_perClassPerSubdataset[i][classIndex] += newWeight; w_perSubdataset[i] += newWeight; } } } }
From source file:myclassifier.myC45Pack.ClassDistribution.java
/** * Adds all instances in given range to given bag. * * @exception Exception if something goes wrong *///from ww w. j a va 2 s . c om public final void addRange(int subDatasetIndex, Instances dataSet, int startIndex, int lastIndex) throws Exception { double sumOfWeights = 0; int classIndex; Instance data; for (int i = startIndex; i < lastIndex; i++) { data = (Instance) dataSet.instance(i); classIndex = (int) data.classValue(); sumOfWeights += data.weight(); w_perClassPerSubdataset[subDatasetIndex][classIndex] += data.weight(); w_perClass[classIndex] += data.weight(); } w_perSubdataset[subDatasetIndex] += sumOfWeights; totalWeights += sumOfWeights; }