Example #1
0
        // View-Based Classification
        public static void ViewBasedClassification(Instances insts, int folds, int view, int k)
        {
            java.util.Random rand = new java.util.Random(1);

            insts.setClassIndex(insts.numAttributes() - 1);

            Vote vote = new Vote();

            Classifier[] classifiers = new Classifier[k];

            for (int i = 1; i <= k; i++)
            {
                IBk IBk = new IBk();    // Instance-based (IB) learner
                IBk.setKNN(i);
                classifiers[i - 1] = IBk;
            }

            vote.setClassifiers(classifiers);
            vote.setCombinationRule(new SelectedTag(Vote.MAJORITY_VOTING_RULE, Vote.TAGS_RULES));
            vote.buildClassifier(insts);      // Generate classifiers

            AbstractOutput output = new PlainText();

            output.setBuffer(new java.lang.StringBuffer());
            output.setHeader(insts);

            Evaluation evaluation = new Evaluation(insts);

            evaluation.crossValidateModel(vote, insts, folds, rand, output);  // n-fold cross validation

            // Parse the output string
            string str = output.getBuffer().toString();

            str = str.Replace("  ", " ").Replace("  ", " ").Replace("  ", " ").Replace("  ", " ");
            string[] line = str.Split('\n');

            predicted[view] = new List <int>();
            predicted[view].Clear();
            actual.Clear();

            for (int i = 1; i < line.Length - 1; i++)
            {
                String[] linesplit = line[i].Trim().Split(' ');

                int index = linesplit[1].IndexOf(':');    // Actual class
                actual.Add(Convert.ToInt16(linesplit[1].Substring(0, index)) - 1);

                index = linesplit[2].IndexOf(':');        // Predicted class
                predicted[view].Add(Convert.ToInt16(linesplit[2].Substring(0, index)) - 1);
            }
        }