List of usage examples for weka.core Instances instance
publicInstance instance(int index)
From source file:meka.classifiers.multilabel.PLST.java
License:Open Source License
/** * Transforms the instance in the prediction process before given to the internal multi-label * or multi-target classifier. The instance is passed having the original set of labels, these * must be replaced with the transformed labels (attributes) so that the internla classifier * can predict them.//w ww. jav a 2 s . co m * * @param x The instance to transform. Consists of features and labels. * @return The transformed instance. Consists of features and transformed labels. */ @Override public Instance transformInstance(Instance x) throws Exception { Instances tmpInst = new Instances(x.dataset()); tmpInst.delete(); tmpInst.add(x); Instances features = this.extractPart(tmpInst, false); Instances labels = new Instances(this.m_PatternInstances); labels.add(new DenseInstance(labels.numAttributes())); Instances result = Instances.mergeInstances(labels, features); result.setClassIndex(labels.numAttributes()); return result.instance(0); }
From source file:meka.classifiers.multitarget.NSR.java
License:Open Source License
public Instances convertInstances(Instances D, int L) throws Exception { //Gather combinations HashMap<String, Integer> distinctCombinations = MLUtils.classCombinationCounts(D); if (getDebug()) System.out.println("Found " + distinctCombinations.size() + " unique combinations"); //Prune combinations MLUtils.pruneCountHashMap(distinctCombinations, m_P); if (getDebug()) System.out.println("Pruned to " + distinctCombinations.size() + " with P=" + m_P); // Remove all class attributes Instances D_ = MLUtils.deleteAttributesAt(new Instances(D), MLUtils.gen_indices(L)); // Add a new class attribute D_.insertAttributeAt(new Attribute("CLASS", new ArrayList(distinctCombinations.keySet())), 0); // create the class attribute D_.setClassIndex(0);// w w w . j a v a 2s. co m //Add class values for (int i = 0; i < D.numInstances(); i++) { String y = MLUtils.encodeValue(MLUtils.toIntArray(D.instance(i), L)); // add it if (distinctCombinations.containsKey(y)) //if its class value exists D_.instance(i).setClassValue(y); // decomp else if (m_N > 0) { String d_subsets[] = SuperLabelUtils.getTopNSubsets(y, distinctCombinations, m_N); for (String s : d_subsets) { int w = distinctCombinations.get(s); Instance copy = (Instance) (D_.instance(i)).copy(); copy.setClassValue(s); copy.setWeight(1.0 / d_subsets.length); D_.add(copy); } } } // remove with missing class D_.deleteWithMissingClass(); // keep the header of new dataset for classification m_InstancesTemplate = new Instances(D_, 0); if (getDebug()) System.out.println("" + D_); return D_; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * LabelCardinality - return the label cardinality of dataset D of L labels. *//*from w w w . ja v a 2 s . com*/ public static final double labelCardinality(Instances D, int L) { double sum = 0.0; double numInstances = (double) D.numInstances(); for (int i = 0; i < D.numInstances(); i++) { for (int j = 0; j < L; j++) { if (!D.instance(i).isMissing(j)) { sum += D.instance(i).value(j); } } } return (double) sum / numInstances; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * LabelCardinalities - return the frequency of each label of dataset D. */// ww w .ja v a 2 s.c o m public static final double[] labelCardinalities(Instances D) { int L = D.classIndex(); double lc[] = new double[L]; for (int j = 0; j < L; j++) { int count = 0; for (int i = 0; i < D.numInstances(); i++) { //if for missing valueses if (!D.instance(i).isMissing(j)) { lc[j] += D.instance(i).value(j); count++; } } lc[j] /= count; //D.numInstances(); } return lc; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * MostCommonCombination - Most common label combination in D (of L labels). *///from w ww . java 2 s.c o m public static final String mostCommonCombination(Instances D, int L) { HashMap<String, Integer> hm = new HashMap<String, Integer>(D.numInstances()); double max_v = 0.0; int max_i = 0; for (int i = 0; i < D.numInstances(); i++) { String y = MLUtils.toBitString(D.instance(i), L); Integer v = hm.get(y); if (v == null) { hm.put(y, 0); } else { if (v > max_v) { max_v = v; max_i = i; } hm.put(y, v + 1); } } return MLUtils.toBitString(D.instance(max_i), L); }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * CountCombinations - return a mapping of each distinct label combination and its count. * NOTE: A sparse representation would be much better for many applications, i.e., instead of using toBitString(...), use toSparseRepresentation(...) instead. * @param D dataset /*w ww .j a v a2 s. c o m*/ * @param L number of labels * @return a HashMap where a String representation of each label combination is associated with an Integer count, e.g., "00010010",3 */ public static final HashMap<String, Integer> countCombinations(Instances D, int L) { HashMap<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < D.numInstances(); i++) { //String y = MLUtils.toSparseRepresentation(D.instance(i),L); String y = MLUtils.toBitString(D.instance(i), L); Integer c = map.get(y); map.put(y, c == null ? 1 : c + 1); } return map; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * ClassCombinationCounts - multi-target version of countCombinations(...). * NOTE: uses the encodeValue(...) function which does NOT consider sparse data. * TODO: use LabelVector instead of Strings * @param D dataset //www. j av a 2 s . c o m * @return a HashMap where a String representation of each class combination is associated with an Integer count, e.g. [0,2,2,3,2],5 */ public static final HashMap<String, Integer> classCombinationCounts(Instances D) { int L = D.classIndex(); HashMap<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < D.numInstances(); i++) { String y = encodeValue(toIntArray(D.instance(i), L)); Integer c = map.get(y); map.put(y, c == null ? 1 : c + 1); } return map; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * SetLabelsMissing - Set all labels in D to missing. *//*ww w . ja v a 2 s. c om*/ public static Instances setLabelsMissing(Instances D) { int L = D.classIndex(); for (int i = 0; i < D.numInstances(); i++) { for (int j = 0; j < L; j++) { D.instance(i).setMissing(j); } } return D; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * Stack two Instances together row-wise. *///from ww w.j a v a2 s . c om public static final Instances combineInstances(Instances D1, Instances D2) { Instances D = new Instances(D1); for (int i = 0; i < D2.numInstances(); i++) { D.add(D2.instance(i)); } return D; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * GetXfromD - Extract attributes as a double X[][] from Instances D. * TODO: getXfromInstances would be a better name. *//* w w w . java2s . c om*/ public static double[][] getXfromD(Instances D) { int N = D.numInstances(); int L = D.classIndex(); int d = D.numAttributes() - L; //System.out.println("d="+d); double X[][] = new double[N][d]; for (int i = 0; i < N; i++) { for (int k = 0; k < d; k++) { X[i][k] = D.instance(i).value(k + L); } } return X; }