List of usage examples for weka.core Instances classIndex
publicint classIndex()
From source file:meka.classifiers.multilabel.PCC.java
License:Open Source License
/** * GetKs - return [K_1,K_2,...,K_L] where each Y_j \in {1,...,K_j}. * In the multi-label case, K[j] = 2 for all j = 1,...,L. * @param D a dataset//from ww w .ja va2s .c om * @return an array of the number of values that each label can take */ private static int[] getKs(Instances D) { int L = D.classIndex(); int K[] = new int[L]; for (int k = 0; k < L; k++) { K[k] = D.attribute(k).numValues(); } return K; }
From source file:meka.classifiers.multilabel.PMCC.java
License:Open Source License
@Override public void buildClassifier(Instances D) throws Exception { m_R = new Random(m_S); // Variables/*from w ww . j av a 2 s. c o m*/ int L = D.classIndex(); int N = D.numInstances(); int d = D.numAttributes() - L; h = new CC[m_M]; w = new double[m_M]; //int s[][] = new int[m_M][L]; // for interest's sake if (m_Is >= m_M) { //HashMap<String,CC> id2cc = new HashMap<String,CC>(); // Make CC int s[] = MLUtils.gen_indices(L); MLUtils.randomize(s, m_R); h[0] = buildCC(Arrays.copyOf(s, s.length), D); // @todo move into setChain(..) w[0] = payoff(h[0], D); //id2cc.put(Arrays.toString(s),h[0]); // save a copy //s[0] = s_; if (getDebug()) System.out.println("s[0] = " + Arrays.toString(s)); for (int t = 0; t < m_Is; t++) { // propose a chain s' ~ pi(s'|s) int s_[] = (m_O > 0) ? pi(Arrays.copyOf(s, s.length), m_R, t, m_Beta) : // default cond. option - with temperature A.swap(Arrays.copyOf(s, s.length), m_R); // special simple option - swap two elements // build h' with sequence s' //CC h_ = rebuildCC(getClosest(id2cc,Arrays.toString(s_)),s_,D); CC h_ = buildCC(Arrays.copyOf(s_, s_.length), D); //id2cc.put(Arrays.toString(s_), h_); // rate h' (by its performance on the training data) double w_ = payoff(h_, D); // accept h' weighted more than the weakest h in the population int min = Utils.sort(w)[0]; // (min index) if (w_ > w[min]) { w[min] = w_; h[min] = h_; if (getDebug()) System.out.println(" accepted h_ with score " + w_ + " > " + w[min]); s = s_; } else if (getDebug()) System.out.println(" DENIED h_ with score " + w_ + " !> score " + w[min]); } if (getDebug()) System.out.println("---"); // normalise weights Utils.normalize(w); } else { throw new Exception( "[Error] Number of chains evaluated (Is) should be at least as great as the population selected (M), and always greater than 0."); } }
From source file:meka.classifiers.multilabel.RAkEL.java
License:Open Source License
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D);//from ww w. jav a 2 s . c o m int L = D.classIndex(); Random r = new Random(m_S); if (getDebug()) System.out.println("Building " + m_M + " models of " + m_K + " random subsets:"); m_InstancesTemplates = new Instances[m_M]; kMap = new int[m_M][m_K]; m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_M); for (int i = 0; i < m_M; i++) { kMap[i] = SuperLabelUtils.get_k_subset(L, m_K, r); if (getDebug()) System.out.println("\tmodel " + (i + 1) + "/" + m_M + ": " + Arrays.toString(kMap[i]) + ", P=" + m_P + ", N=" + m_N); Instances D_i = SuperLabelUtils.makePartitionDataset(D, kMap[i], m_P, m_N); m_Classifiers[i].buildClassifier(D_i); m_InstancesTemplates[i] = new Instances(D_i, 0); } }
From source file:meka.classifiers.multilabel.RAkELd.java
License:Open Source License
@Override public void buildClassifier(Instances D) throws Exception { int L = D.classIndex(); int N = D.numInstances(); Random r = new Random(m_S); // Note: a slightly round-about way of doing it: int num = (int) Math.ceil(L / m_K); kMap = SuperLabelUtils.generatePartition(A.make_sequence(L), num, r, true); m_M = kMap.length;//w w w.j a va 2s . co m m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_M); m_InstancesTemplates = new Instances[m_M]; if (getDebug()) System.out.println("Building " + m_M + " models of " + m_K + " partitions:"); for (int i = 0; i < m_M; i++) { if (getDebug()) System.out.println("\tpartitioning model " + (i + 1) + "/" + m_M + ": " + Arrays.toString(kMap[i]) + ", P=" + m_P + ", N=" + m_N); Instances D_i = SuperLabelUtils.makePartitionDataset(D, kMap[i], m_P, m_N); if (getDebug()) System.out.println("\tbuilding model " + (i + 1) + "/" + m_M + ": " + Arrays.toString(kMap[i])); m_Classifiers[i].buildClassifier(D_i); m_InstancesTemplates[i] = new Instances(D_i, 0); } }
From source file:meka.classifiers.multitarget.CCp.java
License:Open Source License
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D);// w w w . ja v a 2 s.c o m int L = D.classIndex(); prepareChain(L); if (getDebug()) System.out.print(":- Chain ("); root = new meka.classifiers.multitarget.CCp.Link(retrieveChain(), 0, D); if (getDebug()) System.out.println(" ) -:"); }
From source file:meka.classifiers.multitarget.NSR.java
License:Open Source License
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D);//ww w . ja v a 2s .c o m int L = D.classIndex(); try { m_Classifier.buildClassifier(convertInstances(D, L)); } catch (Exception e) { if (m_P > 0) { m_P--; System.err.println("Not enough distinct class values, trying again with P = " + m_P + " ..."); buildClassifier(D); } else throw new Exception("Failed to construct a classifier."); } }
From source file:meka.classifiers.multitarget.RAkELd.java
License:Open Source License
@Override public void buildClassifier(Instances D) throws Exception { /*/* w ww .ja v a 2 s . co m*/ This is a slow way of doing things at the moment, making use of multitarget.SCC functionality, even though multilabel.RAkELd is not a meta multi-label classifier. */ int L = D.classIndex(); int N = D.numInstances(); Random r = new Random(m_S); // Note: a slightly round-about way of doing it: int num = (int) Math.ceil(L / m_K); kMap = SuperLabelUtils.generatePartition(A.make_sequence(L), num, r, true); m_M = kMap.length; vMap = new int[m_M][][]; m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_M); m_InstancesTemplates = new Instances[m_M]; if (getDebug()) System.out.println("Building " + m_M + " models of " + m_K + " partitions:"); D = SuperLabelUtils.SLTransformation(D, kMap, m_P, m_N); for (int i = 0; i < m_M; i++) { /* if (getDebug()) System.out.println("\tpartitioning model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i])+", P="+m_P+", N="+m_N); Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i],m_P,m_N); */ Instances D_i = F.keepLabels(D, D.classIndex(), new int[] { i }); D_i.setClassIndex(0); //vMap[i] = SuperLabelUtils.extractValues(D_i); if (getDebug()) System.out.println("\tbuilding model " + (i + 1) + "/" + m_M + ": " + Arrays.toString(kMap[i])); m_Classifiers[i].buildClassifier(D_i); m_InstancesTemplates[i] = new Instances(D_i, 0); } }
From source file:meka.classifiers.multitarget.SCC.java
License:Open Source License
@Override public void buildClassifier(Instances D) throws Exception { int N = D.numInstances(); // only for printouts int U = MLUtils.numberOfUniqueCombinations(D); // only for printouts int L = D.classIndex(); rand = new Random(m_S); if (!(m_Classifier instanceof MultiTargetClassifier)) { throw new Exception( "[Error] The base classifier must be multi-target capable, i.e., from meka.classifiers.multitarget."); }/*from w ww . ja v a 2 s . c o m*/ // 0. SPLIT INTO TRAIN AND VALIDATION SET/S Instances D_r = new Instances(D); D_r.randomize(rand); Instances D_train = new Instances(D_r, 0, D_r.numInstances() * i_SPLIT / 100); Instances D_test = new Instances(D_r, D_train.numInstances(), D_r.numInstances() - D_train.numInstances()); // 1. BUILD BR or EBR if (getDebug()) System.out.print("1. BUILD & Evaluate BR: "); CR cr = new CR(); cr.setClassifier(((ProblemTransformationMethod) m_Classifier).getClassifier()); // assume PT Result result_1 = Evaluation.evaluateModel((ProblemTransformationMethod) cr, D_train, D_test, "PCut1", "5"); double acc1 = (Double) result_1.getMeasurement(i_ErrFn); if (getDebug()) System.out.println(" " + acc1); int partition[][] = SuperLabelUtils.generatePartition(A.make_sequence(L), rand); // 2. SELECT / MODIFY INDICES (using LEAD technique) if (getDebug()) System.out.println("2. GET ERR-CHI-SQUARED MATRIX: "); double MER[][] = StatUtils.condDepMatrix(D_test, result_1); if (getDebug()) System.out.println(MatrixUtils.toString(MER)); /* * 3. SIMULATED ANNEALING * Always accept if best, progressively less likely accept otherwise. */ if (getDebug()) System.out.println("3. COMBINE NODES TO FIND THE BEST COMBINATION ACCORDING TO CHI"); double w = rating(partition, MER); if (getDebug()) System.out.println("@0 : " + SuperLabelUtils.toString(partition) + "\t(" + w + ")"); for (int i = 0; i < m_I; i++) { int partition_[][] = mutateCombinations(MatrixUtils.deep_copy(partition), rand); double w_ = rating(partition_, MER); // this is really p_MER(partition_) if (w_ > w) { // ACCEPT partition = partition_; w = w_; if (getDebug()) System.out.println("@" + i + " : " + SuperLabelUtils.toString(partition) + "\t(" + w + ")"); } else { // MAYBE ACCEPT double diff = Math.abs(w_ - w); double p = (2. * (1. - sigma(diff * i / 1000.))); if (p > rand.nextDouble()) { // OK, ACCEPT NOW if (getDebug()) System.out.println( "@" + i + " : " + SuperLabelUtils.toString(partition_) + "\t(" + w_ + ")*"); partition = partition_; w = w_; } } } /* * METHOD 2 * refine the set we started with above, with a few iterations. * we mutate a set, and accept whenever the classification performance is GREATER */ if (m_Iv > 0) { if (getDebug()) System.out.println("4. REFINING THE INITIAL SET WITH SOME OLD-FASHIONED INTERNAL EVAL"); // Build & evaluate the classifier with the latest partition result_1 = testClassifier((ProblemTransformationMethod) m_Classifier, D_train, D_test, partition); w = (Double) result_1.getMeasurement(i_ErrFn); if (getDebug()) System.out.println("@0 : " + SuperLabelUtils.toString(partition) + "\t(" + w + ")"); for (int i = 0; i < m_Iv; i++) { int partition_[][] = mutateCombinations(MatrixUtils.deep_copy(partition), rand); // Build the classifier with the new combination trainClassifier(m_Classifier, D_train, partition); // Evaluate on D_test Result result_2 = testClassifier((ProblemTransformationMethod) m_Classifier, D_train, D_test, partition_); double w_ = (Double) result_2.getMeasurement(i_ErrFn); if (w_ > w) { w = w_; partition = partition_; if (getDebug()) System.out.println( "@" + (i + 1) + "' : " + SuperLabelUtils.toString(partition) + "\t(" + w + ")"); } } } // 4. DECIDE HOW GOOD THEY ARE, COMPARE EACH LABEL TO BR-result? if (getDebug()) System.out.println("4. TRAIN " + SuperLabelUtils.toString(partition)); trainClassifier(m_Classifier, D, partition); if (getDebug()) { //System.out.println("E_acc P "+m_P+" "+(mt.m_InstancesTemplate.numInstances()/(double)N) +" "+(MLUtils.numberOfUniqueCombinations(mt.m_InstancesTemplate)/(double)U)); } // 5. MOVE ON ... }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * LabelCardinality - return the label cardinality of dataset D. *//*from w w w. ja va2 s. co m*/ public static final double labelCardinality(Instances D) { return labelCardinality(D, D.classIndex()); }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * LabelCardinalities - return the frequency of each label of dataset D. *//* w w w .j a v a2 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; }