public Instances transformInstances(MultiLabelInstances mlData) throws Exception { labelIndices = mlData.getLabelIndices(); numOfLabels = mlData.getNumLabels(); Instances data = mlData.getDataSet(); Instances transformed = new Instances(mlData.getDataSet(), 0); // delete all labels transformed = RemoveAllLabels.transformInstances(transformed, labelIndices); // add single label attribute ArrayList<String> classValues = new ArrayList<String>(numOfLabels); for (int x = 0; x < numOfLabels; x++) { classValues.add("Class" + (x + 1)); } Attribute newClass = new Attribute("Class", classValues); transformed.insertAttributeAt(newClass, transformed.numAttributes()); transformed.setClassIndex(transformed.numAttributes() - 1); for (int instanceIndex = 0; instanceIndex < data.numInstances(); instanceIndex++) { // System.out.println(data.instance(instanceIndex).toString()); List<Instance> result = transformInstance(data.instance(instanceIndex)); for (Instance instance : result) { // System.out.println(instance.toString()); transformed.add(instance); // System.out.println(transformed.instance(transformed.numInstances()-1)); } } return transformed; }
/** * Calculate metric value * * @param mlData Multi-label dataset to which calculate the metric * @return Value of the metric */ public double calculate(MultiLabelInstances mlData) { Instances instances = mlData.getDataSet(); int nInstances = mlData.getNumInstances(); double avg; double var2; double var4; double val; int nNumeric = 0; double mean = 0; Set<Attribute> attributesSet = mlData.getFeatureAttributes(); for (Attribute att : attributesSet) { if (att.isNumeric()) { nNumeric++; avg = instances.meanOrMode(att); var2 = 0; var4 = 0; for (Instance inst : instances) { val = inst.value(att); var2 += Math.pow(val - avg, 2); var4 += Math.pow(val - avg, 4); } double kurtosis = (nInstances * var4 / Math.pow(var2, 2)) - 3; double sampleKurtosis = (kurtosis * (nInstances + 1) + 6) * (nInstances - 1) / ((nInstances - 2) * (nInstances - 3)); mean += sampleKurtosis; } } if (nNumeric > 0) { mean = mean / nNumeric; } else { mean = Double.NaN; } this.value = mean; return value; }
@Override protected void buildInternal(MultiLabelInstances trainingSet) throws Exception { header = new Instances(trainingSet.getDataSet(), 0); baseLearner.build(trainingSet); }