Ejemplo n.º 1
0
        public static double classifyTrain_Test(string classifierFileName, Classifier _classifier)
        {
            double performance = 0.0;

            try
            {
                FileReader          javaFileReader = new FileReader(classifierFileName);
                weka.core.Instances insts          = new weka.core.Instances(javaFileReader);
                javaFileReader.close();

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

                System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");

                int trainSize             = insts.numInstances() * percentSplit / 100;
                int testSize              = insts.numInstances() - trainSize;
                weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);

                _classifier.buildClassifier(train);

                int numCorrect   = 0;
                var numnerOfInst = insts.numInstances();
                int dataIndex    = 0;
                for (int i = trainSize; i < numnerOfInst; i++)
                {
                    dataIndex++;
                    weka.core.Instance currentInst = insts.instance(i);

                    double   predictClass = _classifier.classifyInstance(currentInst);
                    double[] dist         = _classifier.distributionForInstance(currentInst);


                    string actualClass    = insts.classAttribute().value((int)insts.instance(i).classValue());
                    string predictedClass = insts.classAttribute().value((int)predictClass);


                    var abcd = _classifier.getClass();

                    if (predictedClass == actualClass)
                    {
                        numCorrect++;
                    }
                }
                performance = (double)((double)numCorrect / (double)testSize) * 100;

                System.Console.WriteLine(numCorrect + " out of " + testSize + " correct (" + performance.toString() + "%)");
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
            }

            return(performance);
        }
    // Test the classification result of each map that a user played,
    // with the data available as if they were playing through it
    public static void classifyTest(String dataString, String playerID)
    {
        String results = "";
        try {
            java.io.StringReader stringReader = new java.io.StringReader(dataString);
            java.io.BufferedReader buffReader = new java.io.BufferedReader(stringReader);

            /* NOTE THAT FOR NAIVE BAYES ALL WEIGHTS CAN BE = 1*/
            //weka.core.converters.ConverterUtils.DataSource source = new weka.core.converters.ConverterUtils.DataSource("iris.arff");
            weka.core.Instances data = new weka.core.Instances(buffReader); //source.getDataSet();
            // setting class attribute if the data format does not provide this information
            // For example, the XRFF format saves the class attribute information as well
            if (data.classIndex() == -1)
                data.setClassIndex(data.numAttributes() - 1);

            weka.classifiers.Classifier cl;
            for (int i = 3; i < data.numInstances(); i++) {
                cl = new weka.classifiers.bayes.NaiveBayes();
                //cl = new weka.classifiers.trees.J48();
                //cl = new weka.classifiers.lazy.IB1();
                //cl = new weka.classifiers.functions.MultilayerPerceptron();
                ((weka.classifiers.functions.MultilayerPerceptron)cl).setHiddenLayers("12");

                weka.core.Instances subset = new weka.core.Instances(data,0,i);
                cl.buildClassifier(subset);

                weka.classifiers.Evaluation eval = new weka.classifiers.Evaluation(subset);
         		eval.crossValidateModel(cl, subset, 3, new java.util.Random(1));
                results = results + eval.pctCorrect(); // For accuracy measurement
                /* For Mathews Correlation Coefficient */
                //double TP = eval.numTruePositives(1);
                //double FP = eval.numFalsePositives(1);
                //double TN = eval.numTrueNegatives(1);
                //double FN = eval.numFalseNegatives(1);
                //double correlationCoeff = ((TP*TN)-(FP*FN))/Math.Sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN));
                //results = results + correlationCoeff;
                if (i != data.numInstances()-1)
                    results = results + ", ";
                if(i == data.numInstances()-1)
                    Debug.Log("Player: " + playerID + ", Num Maps: " + data.numInstances() + ", AUC: " + eval.areaUnderROC(1));
            }
        } catch (java.lang.Exception ex)
        {
            Debug.LogError(ex.getMessage());
        }
        // Write values to file for a matlab read
        // For accuracy
         	StreamWriter writer = new StreamWriter("DataForMatlab/"+playerID+"_CrossFoldValidations_NeuralNet.txt");

        //StreamWriter writer = new StreamWriter("DataForMatlab/"+playerID+"_CrossFoldCorrCoeff.txt"); // For mathews cc
        writer.WriteLine(results);
        writer.Close();
        Debug.Log(playerID + " has been written to file");
    }
        public static double SupportVectorMachineTest(weka.core.Instances insts)
        {
            try
            {
                //weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader("iris.arff"));

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


                SupportVectorMachine = new weka.classifiers.functions.SMO();

                weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary();

                myDummy.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myDummy);


                weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize();
                myNormalize.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myNormalize);

                weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
                myRandom.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myRandom);

                int trainSize             = insts.numInstances() * percentSplit / 100;
                int testSize              = insts.numInstances() - trainSize;
                weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);


                SupportVectorMachine.buildClassifier(train);


                int numCorrect = 0;
                for (int i = trainSize; i < insts.numInstances(); i++)
                {
                    weka.core.Instance currentInst    = insts.instance(i);
                    double             predictedClass = SupportVectorMachine.classifyInstance(currentInst);
                    if (predictedClass == insts.instance(i).classValue())
                    {
                        numCorrect++;
                    }
                }
                return((double)numCorrect / (double)testSize * 100.0);
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
                return(0);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds teta results of gini results to the list
        /// Change the attributes of the arff file
        /// Adds the attributes to arff file
        /// </summary>
        /// <param name="insts"></param>
        /// <param name="result"></param>
        /// <param name="path"></param>
        private void CreateNewDataset(weka.core.Instances insts, List <double[]> result, string path)
        {
            //Tetaları Listeye Ekle
            List <List <string> > lst = new List <List <string> >();

            for (int i = 0; i < insts.numInstances(); i++)
            {
                lst.Add(new List <string>());
                for (int j = 0; j < insts.instance(i).numValues() - 1; j++)
                {
                    string value = insts.instance(i).toString(j);
                    for (int k = 0; k < categories[j].Length; k++)
                    {
                        if (insts.instance(i).toString(j) == categories[j][k])
                        {
                            lst[lst.Count - 1].Add(String.Format("{0:0.00}", result[j][k]));
                            break;
                        }
                    }
                }
            }
            //Attiribute Değiştir
            for (int i = 0; i < insts.numAttributes() - 1; i++)
            {
                string name = insts.attribute(i).name().ToString();
                insts.deleteAttributeAt(i);
                weka.core.Attribute att = new weka.core.Attribute(name);
                insts.insertAttributeAt(att, i);
            }

            //Attiributeları yaz
            for (int i = 0; i < insts.numInstances(); i++)
            {
                for (int j = 0; j < insts.instance(i).numValues() - 1; j++)
                {
                    insts.instance(i).setValue(j, Convert.ToDouble(lst[i][j]));
                }
            }

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            var saver = new ArffSaver();

            saver.setInstances(insts);
            saver.setFile(new java.io.File(path));
            saver.writeBatch();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Determines all categories --> categories
        /// Determines category numbers of each attributes -->> categoryTypeNumber
        /// Determines target numbers and amounts of each categories of each attributes  -->> categoryTypeTargetNumber
        /// [i][j][k] i means attributes, j means  categories, k means targets
        /// </summary>
        /// <param name="insts"></param>
        private void DataPreparation(weka.core.Instances insts)
        {
            for (int i = 0; i < insts.numAttributes(); i++)
            {
                string[] categoryType = new string[insts.attribute(i).numValues()];
                for (int j = 0; j < insts.attribute(i).numValues(); j++)
                {
                    categoryType[j] = insts.attribute(i).value(j).ToString();
                }
                categories.Add(categoryType);
            }

            List <List <string> > lst = new List <List <string> >();

            for (int i = 0; i < insts.numInstances(); i++)
            {
                lst.Add(new List <string>());

                for (int j = 0; j < insts.instance(i).numValues(); j++)
                {
                    lst[lst.Count - 1].Add(insts.instance(i).toString(j));
                }
            }

            List <int[]>   categoryTypeNumber       = new List <int[]>();
            List <int[, ]> categoryTypeTargetNumber = new List <int[, ]>();

            for (int i = 0; i < categories.Count; i++)
            {
                categoryTypeNumber.Add(new int[categories[i].Length]);
                categoryTypeTargetNumber.Add(new int[categories[i].Length, categories[categories.Count - 1].Length]);
            }

            for (int i = 0; i < lst.Count; i++)                    //Satır
            {
                for (int j = 0; j < lst[i].Count; j++)             //Sütün
                {
                    for (int k = 0; k < categories[j].Length; k++) //Kategori Sayısı
                    {
                        string targetValue = lst[i][lst[i].Count - 1];
                        if (lst[i][j].Contains(categories[j][k]))
                        {
                            categoryTypeNumber[j][k] += 1;
                            for (int trgt = 0; trgt < categories[categories.Count - 1].Length; trgt++)
                            {
                                if (targetValue == categories[categories.Count - 1][trgt])
                                {
                                    categoryTypeTargetNumber[j][k, trgt] += 1;
                                }
                            }
                        }
                    }
                }
            }
            Twoing(insts, categoryTypeNumber, categoryTypeTargetNumber);
            Gini(insts, categoryTypeNumber, categoryTypeTargetNumber);
            LogInfo("Dataset is saved.\r\n\r\n");
            LogInfo("TWOING : " + twoingPath + "\r\n\r\n");
            LogInfo("GINI : " + giniPath + "\r\n");
        }
Ejemplo n.º 6
0
        public void trainMachineForEmotionUsingWeka(string wekaFile, string modelName, int hiddelLayers = 7, double learningRate = 0.03, double momentum = 0.4, int decimalPlaces = 2, int trainingTime = 1000)
        {
            //"C:\\Users\\Gulraiz\\Desktop\\Genereted2.arff" "MLP"
            try
            {
                weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(wekaFile));
                insts.setClassIndex(insts.numAttributes() - 1);
                weka.classifiers.functions.MultilayerPerceptron cl;
                cl = new weka.classifiers.functions.MultilayerPerceptron();
                cl.setHiddenLayers(hiddelLayers.ToString());
                cl.setLearningRate(learningRate);
                cl.setMomentum(momentum);
                cl.setNumDecimalPlaces(decimalPlaces);
                cl.setTrainingTime(trainingTime);

                System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");

                //randomize the order of the instances in the dataset.
                weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
                myRandom.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myRandom);

                int trainSize             = insts.numInstances() * percentSplit / 100;
                int testSize              = insts.numInstances() - trainSize;
                weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);
                java.io.File        path  = new java.io.File("/models/");
                cl.buildClassifier(train);
                saveModel(cl, modelName, path);
                #region test whole set
                //int numCorrect = 0;
                //for (int i = trainSize; i < insts.numInstances(); i++)
                //{
                //    weka.core.Instance currentInst = insts.instance(i);
                //    double predictedClass = cl.classifyInstance(currentInst);
                //    if (predictedClass == insts.instance(i).classValue())
                //        numCorrect++;
                //}

                //System.Console.WriteLine(numCorrect + " out of " + testSize + " correct (" +
                //           (double)((double)numCorrect / (double)testSize * 100.0) + "%)");
                #endregion
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
            }
        }
        public static double NaiveBayesTest(weka.core.Instances insts)
        {
            try
            {
                //weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader("iris.arff"));

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


                NaiveBayescl = new weka.classifiers.bayes.NaiveBayes();


                //discretize
                weka.filters.Filter myDiscretize = new weka.filters.unsupervised.attribute.Discretize();
                myDiscretize.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myDiscretize);

                weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
                myRandom.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myRandom);

                int trainSize             = insts.numInstances() * percentSplit / 100;
                int testSize              = insts.numInstances() - trainSize;
                weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);

                NaiveBayescl.buildClassifier(train);


                int numCorrect = 0;
                for (int i = trainSize; i < insts.numInstances(); i++)
                {
                    weka.core.Instance currentInst    = insts.instance(i);
                    double             predictedClass = NaiveBayescl.classifyInstance(currentInst);
                    if (predictedClass == insts.instance(i).classValue())
                    {
                        numCorrect++;
                    }
                }
                return((double)numCorrect / (double)testSize * 100.0);
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
                return(0);
            }
        }
Ejemplo n.º 8
0
  public static void Main(string[] args) {
    try {
      int runs = 1;
      string algo = "";
      string data = "";
      if(args.Length>0) runs = Convert.ToInt32(args[0]);
      if(args.Length>1) algo = args[1];
      if(args.Length>2) data = args[2];

      Stopwatch read = new Stopwatch(), 
        build = new Stopwatch(), 
        classify = new Stopwatch();
      for (int cnt=0; cnt<runs; cnt++) {
        read.Start();
        Instances train = new Instances(new java.io.FileReader(data+"train.arff"));
        train.setClassIndex(train.numAttributes() - 1);
        Instances test = new Instances(new java.io.FileReader(data+"test.arff"));
        test.setClassIndex(test.numAttributes() - 1);
        read.Stop();

        Classifier[] clList = {
          new weka.classifiers.bayes.NaiveBayes(),
          new weka.classifiers.trees.RandomForest(),
          new weka.classifiers.trees.J48(),
          new weka.classifiers.functions.MultilayerPerceptron(),
          new weka.classifiers.rules.ConjunctiveRule(),
          new weka.classifiers.functions.SMO()
        };

        build.Start();
        foreach (Classifier classifier in clList) {
          if(algo.Equals("") || algo.Equals("All") || classifier.getClass().getSimpleName().Equals(algo))
              classifier.buildClassifier(train);
        }
        build.Stop();

        classify.Start();
        foreach (Classifier classifier in clList) {
          if(algo.Equals("") || algo.Equals("All") || classifier.getClass().getSimpleName().Equals(algo)) {
              int numCorrect = 0;
              for (int i = 0; i < test.numInstances(); i++)
              {
                  if (classifier.classifyInstance(test.instance(i)) == test.instance(i).classValue())
                      numCorrect++;
              }
              //Console.Write(classifier.getClass().getSimpleName() + "\t" + numCorrect + " out of " + test.numInstances() + " correct (" +(100.0 * numCorrect / test.numInstances()) + "%)");
          }
        }
        classify.Stop();
      }
      Console.WriteLine("{\""+ algo + "\"," + read.ElapsedMilliseconds + "," + build.ElapsedMilliseconds + "," + classify.ElapsedMilliseconds + "," + (read.ElapsedMilliseconds+build.ElapsedMilliseconds+classify.ElapsedMilliseconds)+"};");
      if(args.Length>3) Console.ReadLine();
    } catch (java.lang.Exception e){
      e.printStackTrace();
    }
  }
        /// <summary>
        /// Train
        /// </summary>
        /// <param name="instances"></param>
        /// <returns></returns>
        public TrainModel Train(weka.core.Instances instances, Classifier classifier)
        {
            const int percentSplit = 66;
            int       trainSize    = instances.numInstances() * percentSplit / 100;
            int       testSize     = instances.numInstances() - trainSize;

            weka.core.Instances train = new weka.core.Instances(instances, 0, trainSize);

            classifier.buildClassifier(train);

            return(this.Classifier = new TrainModel()
            {
                PercentSplit = percentSplit,
                classifier = classifier,
                TestSize = testSize,
                TrainSize = trainSize,
                Instance = instances
            });
        }
Ejemplo n.º 10
0
        public void trainMachineForHybridUsingWeka(string wekaFile, string modelName)
        {
            weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(wekaFile));
            insts.setClassIndex(insts.numAttributes() - 1);
            weka.classifiers.Classifier bagging = new weka.classifiers.meta.Bagging();

            System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");

            //randomize the order of the instances in the dataset.
            weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
            myRandom.setInputFormat(insts);
            insts = weka.filters.Filter.useFilter(insts, myRandom);

            int trainSize = insts.numInstances() * percentSplit / 100;
            int testSize  = insts.numInstances() - trainSize;

            weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);
            java.io.File        path  = new java.io.File("/models/");
            bagging.buildClassifier(train);
            saveModel(bagging, modelName, path);
        }
Ejemplo n.º 11
0
        public static void JackKnife_Test_prepare(string classifierFileName, int baseClasses, Classifier _classifie)
        {
            for (int singleClass = 1; singleClass <= baseClasses; singleClass++)
            {
                string eachFileName = String.Format("{0}_{1}.arff", classifierFileName, singleClass);

                FileReader          javaFileReader = new FileReader(eachFileName);
                weka.core.Instances insts          = new weka.core.Instances(javaFileReader);
                javaFileReader.close();

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

                var totalnstances = insts.numInstances();

                //insts.re
            }
        }
        public void trainSMOUsingWeka(string wekaFile, string modelName)
        {
            try
            {
                weka.core.converters.CSVLoader csvLoader = new weka.core.converters.CSVLoader();
                csvLoader.setSource(new java.io.File(wekaFile));
                weka.core.Instances insts = csvLoader.getDataSet();
                //weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(wekaFile));
                insts.setClassIndex(insts.numAttributes() - 1);

                cl = new weka.classifiers.functions.SMO();
                cl.setBatchSize("100");

                cl.setCalibrator(new weka.classifiers.functions.Logistic());
                cl.setKernel(new weka.classifiers.functions.supportVector.PolyKernel());
                cl.setEpsilon(1.02E-12);
                cl.setC(1.0);
                cl.setDebug(false);
                cl.setChecksTurnedOff(false);
                cl.setFilterType(new SelectedTag(weka.classifiers.functions.SMO.FILTER_NORMALIZE, weka.classifiers.functions.SMO.TAGS_FILTER));

                System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");

                //randomize the order of the instances in the dataset.
                // weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
                //myRandom.setInputFormat(insts);
                // insts = weka.filters.Filter.useFilter(insts, myRandom);

                int trainSize             = insts.numInstances() * percentSplit / 100;
                int testSize              = insts.numInstances() - trainSize;
                weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);
                java.io.File        path  = new java.io.File("/models/");
                cl.buildClassifier(train);
                saveModel(cl, modelName, path);
                #region test whole set
                int numCorrect = 0;
                for (int i = 0; i < insts.numInstances(); i++)
                {
                    weka.core.Instance currentInst = insts.instance(i);
                    if (i == 12)
                    {
                        array = new List <float>();
                        foreach (float value in currentInst.toDoubleArray())
                        {
                            array.Add(value);
                        }
                    }

                    double predictedClass = cl.classifyInstance(currentInst);
                    if (predictedClass == insts.instance(i).classValue())
                    {
                        numCorrect++;
                    }
                }

                System.Console.WriteLine(numCorrect + " out of " + testSize + " correct (" +
                                         (double)((double)numCorrect / (double)testSize * 100.0) + "%)");
                #endregion
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Build the learning model for classification
        /// </summary>
        /// <param name="InstancesList">list of instances </param>
        /// <param name="NumberofClusters">Number of Clusters</param>
        /// <param name="TextBoxForFeedback">Text box for the results (can be NULL)</param>
        /// <param name="PanelForVisualFeedback">Panel to display visual results if avalaible (can be NULL)</param>
        public Classifier PerformTraining(FormForClassificationInfo WindowForClassificationParam, Instances InstancesList, /*int NumberofClusters,*/ RichTextBox TextBoxForFeedback,
                                            Panel PanelForVisualFeedback, out weka.classifiers.Evaluation ModelEvaluation, bool IsCellular)
        {
            //   weka.classifiers.Evaluation ModelEvaluation = null;
            // FormForClassificationInfo WindowForClassificationParam = new FormForClassificationInfo(GlobalInfo);
            ModelEvaluation = null;
            //  if (WindowForClassificationParam.ShowDialog() != System.Windows.Forms.DialogResult.OK) return null;
            //   weka.classifiers.Evaluation ModelEvaluation = new Evaluation(

            cParamAlgo ClassifAlgoParams = WindowForClassificationParam.GetSelectedAlgoAndParameters();
            if (ClassifAlgoParams == null) return null;

            //this.Cursor = Cursors.WaitCursor;

            //  cParamAlgo ClassificationAlgo = WindowForClassificationParam.GetSelectedAlgoAndParameters();
            cListValuesParam Parameters = ClassifAlgoParams.GetListValuesParam();

            //Classifier this.CurrentClassifier = null;

            // -------------------------- Classification -------------------------------
            // create the instances
            // InstancesList = this.ListInstances;
            this.attValsWithoutClasses = new FastVector();

            if (IsCellular)
                for (int i = 0; i < cGlobalInfo.ListCellularPhenotypes.Count; i++)
                    this.attValsWithoutClasses.addElement(cGlobalInfo.ListCellularPhenotypes[i].Name);
            else
                for (int i = 0; i < cGlobalInfo.ListWellClasses.Count; i++)
                    this.attValsWithoutClasses.addElement(cGlobalInfo.ListWellClasses[i].Name);

            InstancesList.insertAttributeAt(new weka.core.Attribute("Class", this.attValsWithoutClasses), InstancesList.numAttributes());
            //int A = Classes.Count;
            for (int i = 0; i < Classes.Count; i++)
                InstancesList.get(i).setValue(InstancesList.numAttributes() - 1, Classes[i]);

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

            weka.core.Instances train = new weka.core.Instances(InstancesList, 0, InstancesList.numInstances());

            if (PanelForVisualFeedback != null)
                PanelForVisualFeedback.Controls.Clear();

            #region List classifiers

            #region J48
            if (ClassifAlgoParams.Name == "J48")
            {
                this.CurrentClassifier = new weka.classifiers.trees.J48();
                ((J48)this.CurrentClassifier).setMinNumObj((int)Parameters.ListDoubleValues.Get("numericUpDownMinInstLeaf").Value);
                ((J48)this.CurrentClassifier).setConfidenceFactor((float)Parameters.ListDoubleValues.Get("numericUpDownConfFactor").Value);
                ((J48)this.CurrentClassifier).setNumFolds((int)Parameters.ListDoubleValues.Get("numericUpDownNumFolds").Value);
                ((J48)this.CurrentClassifier).setUnpruned((bool)Parameters.ListCheckValues.Get("checkBoxUnPruned").Value);
                ((J48)this.CurrentClassifier).setUseLaplace((bool)Parameters.ListCheckValues.Get("checkBoxLaplacianSmoothing").Value);
                ((J48)this.CurrentClassifier).setSeed((int)Parameters.ListDoubleValues.Get("numericUpDownSeedNumber").Value);
                ((J48)this.CurrentClassifier).setSubtreeRaising((bool)Parameters.ListCheckValues.Get("checkBoxSubTreeRaising").Value);

                //   CurrentClassif.SetJ48Tree((J48)this.CurrentClassifier, Classes.Length);
                this.CurrentClassifier.buildClassifier(train);
                // display results training
                // display tree
                if (PanelForVisualFeedback != null)
                {
                    GViewer GraphView = DisplayTree(GlobalInfo, ((J48)this.CurrentClassifier), IsCellular).gViewerForTreeClassif;
                    GraphView.Size = new System.Drawing.Size(PanelForVisualFeedback.Width, PanelForVisualFeedback.Height);
                    GraphView.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
                    PanelForVisualFeedback.Controls.Clear();
                    PanelForVisualFeedback.Controls.Add(GraphView);
                }
            }
            #endregion
            #region Random Tree
            else if (ClassifAlgoParams.Name == "RandomTree")
            {
                this.CurrentClassifier = new weka.classifiers.trees.RandomTree();

                if ((bool)Parameters.ListCheckValues.Get("checkBoxMaxDepthUnlimited").Value)
                    ((RandomTree)this.CurrentClassifier).setMaxDepth(0);
                else
                    ((RandomTree)this.CurrentClassifier).setMaxDepth((int)Parameters.ListDoubleValues.Get("numericUpDownMaxDepth").Value);
                ((RandomTree)this.CurrentClassifier).setSeed((int)Parameters.ListDoubleValues.Get("numericUpDownSeed").Value);
                ((RandomTree)this.CurrentClassifier).setMinNum((double)Parameters.ListDoubleValues.Get("numericUpDownMinWeight").Value);

                if ((bool)Parameters.ListCheckValues.Get("checkBoxIsBackfitting").Value)
                {
                    ((RandomTree)this.CurrentClassifier).setNumFolds((int)Parameters.ListDoubleValues.Get("numericUpDownBackFittingFolds").Value);
                }
                else
                {
                    ((RandomTree)this.CurrentClassifier).setNumFolds(0);
                }
                this.CurrentClassifier.buildClassifier(train);
                //string StringForTree = ((RandomTree)this.CurrentClassifier).graph().Remove(0, ((RandomTree)this.CurrentClassifier).graph().IndexOf("{") + 2);

                //Microsoft.Msagl.GraphViewerGdi.GViewer GraphView = new GViewer();
                //GraphView.Graph = GlobalInfo.WindowHCSAnalyzer.ComputeAndDisplayGraph(StringForTree);//.Remove(StringForTree.Length - 3, 3));

                //GraphView.Size = new System.Drawing.Size(panelForGraphicalResults.Width, panelForGraphicalResults.Height);
                //GraphView.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
                //this.panelForGraphicalResults.Controls.Clear();
                //this.panelForGraphicalResults.Controls.Add(GraphView);

            }
            #endregion
            #region Random Forest
            else if (ClassifAlgoParams.Name == "RandomForest")
            {
                this.CurrentClassifier = new weka.classifiers.trees.RandomForest();

                if ((bool)Parameters.ListCheckValues.Get("checkBoxMaxDepthUnlimited").Value)
                    ((RandomForest)this.CurrentClassifier).setMaxDepth(0);
                else
                    ((RandomForest)this.CurrentClassifier).setMaxDepth((int)Parameters.ListDoubleValues.Get("numericUpDownMaxDepth").Value);

                ((RandomForest)this.CurrentClassifier).setNumTrees((int)Parameters.ListDoubleValues.Get("numericUpDownNumTrees").Value);
                ((RandomForest)this.CurrentClassifier).setSeed((int)Parameters.ListDoubleValues.Get("numericUpDownSeed").Value);

                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region KStar
            else if (ClassifAlgoParams.Name == "KStar")
            {
                this.CurrentClassifier = new weka.classifiers.lazy.KStar();
                ((KStar)this.CurrentClassifier).setGlobalBlend((int)Parameters.ListDoubleValues.Get("numericUpDownGlobalBlend").Value);
                ((KStar)this.CurrentClassifier).setEntropicAutoBlend((bool)Parameters.ListCheckValues.Get("checkBoxBlendAuto").Value);
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region SVM
            else if (ClassifAlgoParams.Name == "SVM")
            {
                this.CurrentClassifier = new weka.classifiers.functions.SMO();
                ((SMO)this.CurrentClassifier).setC((double)Parameters.ListDoubleValues.Get("numericUpDownC").Value);
                ((SMO)this.CurrentClassifier).setKernel(WindowForClassificationParam.GeneratedKernel);
                ((SMO)this.CurrentClassifier).setRandomSeed((int)Parameters.ListDoubleValues.Get("numericUpDownSeed").Value);
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region KNN
            else if (ClassifAlgoParams.Name == "KNN")
            {
                this.CurrentClassifier = new weka.classifiers.lazy.IBk();

                string OptionDistance = " -K " + (int)Parameters.ListDoubleValues.Get("numericUpDownKNN").Value + " -W 0 ";

                string WeightType = (string)Parameters.ListTextValues.Get("comboBoxDistanceWeight").Value;
                switch (WeightType)
                {
                    case "No Weighting":
                        OptionDistance += "";
                        break;
                    case "1/Distance":
                        OptionDistance += "-I";
                        break;
                    case "1-Distance":
                        OptionDistance += "-F";
                        break;
                    default:
                        break;
                }
                OptionDistance += " -A \"weka.core.neighboursearch.LinearNNSearch -A \\\"weka.core.";

                string DistanceType = (string)Parameters.ListTextValues.Get("comboBoxDistance").Value;
                // OptionDistance += " -A \"weka.core.";
                switch (DistanceType)
                {
                    case "Euclidean":
                        OptionDistance += "EuclideanDistance";
                        break;
                    case "Manhattan":
                        OptionDistance += "ManhattanDistance";
                        break;
                    case "Chebyshev":
                        OptionDistance += "ChebyshevDistance";
                        break;
                    default:
                        break;
                }

                if (!(bool)Parameters.ListCheckValues.Get("checkBoxNormalize").Value)
                    OptionDistance += " -D";
                OptionDistance += " -R ";

                OptionDistance += "first-last\\\"\"";
                ((IBk)this.CurrentClassifier).setOptions(weka.core.Utils.splitOptions(OptionDistance));

                //((IBk)this.CurrentClassifier).setKNN((int)Parameters.ListDoubleValues.Get("numericUpDownKNN").Value);
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region Multilayer Perceptron
            else if (ClassifAlgoParams.Name == "Perceptron")
            {
                this.CurrentClassifier = new weka.classifiers.functions.MultilayerPerceptron();
                ((MultilayerPerceptron)this.CurrentClassifier).setMomentum((double)Parameters.ListDoubleValues.Get("numericUpDownMomentum").Value);
                ((MultilayerPerceptron)this.CurrentClassifier).setLearningRate((double)Parameters.ListDoubleValues.Get("numericUpDownLearningRate").Value);
                ((MultilayerPerceptron)this.CurrentClassifier).setSeed((int)Parameters.ListDoubleValues.Get("numericUpDownSeed").Value);
                ((MultilayerPerceptron)this.CurrentClassifier).setTrainingTime((int)Parameters.ListDoubleValues.Get("numericUpDownTrainingTime").Value);
                ((MultilayerPerceptron)this.CurrentClassifier).setNormalizeAttributes((bool)Parameters.ListCheckValues.Get("checkBoxNormAttribute").Value);
                ((MultilayerPerceptron)this.CurrentClassifier).setNormalizeNumericClass((bool)Parameters.ListCheckValues.Get("checkBoxNormNumericClasses").Value);
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region ZeroR
            else if (ClassifAlgoParams.Name == "ZeroR")
            {
                this.CurrentClassifier = new weka.classifiers.rules.OneR();
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region OneR
            else if (ClassifAlgoParams.Name == "OneR")
            {
                this.CurrentClassifier = new weka.classifiers.rules.OneR();
                ((OneR)this.CurrentClassifier).setMinBucketSize((int)Parameters.ListDoubleValues.Get("numericUpDownMinBucketSize").Value);
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region Naive Bayes
            else if (ClassifAlgoParams.Name == "NaiveBayes")
            {
                this.CurrentClassifier = new weka.classifiers.bayes.NaiveBayes();
                ((NaiveBayes)this.CurrentClassifier).setUseKernelEstimator((bool)Parameters.ListCheckValues.Get("checkBoxKernelEstimator").Value);
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            #region Logistic
            else if (ClassifAlgoParams.Name == "Logistic")
            {
                this.CurrentClassifier = new weka.classifiers.functions.Logistic();
                ((Logistic)this.CurrentClassifier).setUseConjugateGradientDescent((bool)Parameters.ListCheckValues.Get("checkBoxUseConjugateGradientDescent").Value);
                ((Logistic)this.CurrentClassifier).setRidge((double)Parameters.ListDoubleValues.Get("numericUpDownRidge").Value);
                this.CurrentClassifier.buildClassifier(train);
            }
            #endregion
            //weka.classifiers.functions.SMO
            //BayesNet

            #endregion

            if (TextBoxForFeedback != null)
            {
                TextBoxForFeedback.Clear();
                TextBoxForFeedback.AppendText(this.CurrentClassifier.ToString());
            }

            TextBoxForFeedback.AppendText("\n" + (InstancesList.numAttributes() - 1) + " attributes:\n\n");
            for (int IdxAttributes = 0; IdxAttributes < InstancesList.numAttributes() - 1; IdxAttributes++)
            {
                TextBoxForFeedback.AppendText(IdxAttributes + "\t: " + InstancesList.attribute(IdxAttributes).name() + "\n");
            }

            #region evaluation of the model and results display

            if ((WindowForClassificationParam.numericUpDownFoldNumber.Enabled) && (TextBoxForFeedback != null))
            {

                TextBoxForFeedback.AppendText("\n-----------------------------\nModel validation\n-----------------------------\n");
                ModelEvaluation = new weka.classifiers.Evaluation(InstancesList);
                ModelEvaluation.crossValidateModel(this.CurrentClassifier, InstancesList, (int)WindowForClassificationParam.numericUpDownFoldNumber.Value, new java.util.Random(1));
                TextBoxForFeedback.AppendText(ModelEvaluation.toSummaryString());
                TextBoxForFeedback.AppendText("\n-----------------------------\nConfusion Matrix:\n-----------------------------\n");
                double[][] ConfusionMatrix = ModelEvaluation.confusionMatrix();

                string NewLine = "";
                for (int i = 0; i < ConfusionMatrix[0].Length; i++)
                {
                    NewLine += "c" + i + "\t";
                }
                TextBoxForFeedback.AppendText(NewLine + "\n\n");

                for (int j = 0; j < ConfusionMatrix.Length; j++)
                {
                    NewLine = "";
                    for (int i = 0; i < ConfusionMatrix[0].Length; i++)
                    {
                        NewLine += ConfusionMatrix[j][i] + "\t";
                    }
                    // if
                    TextBoxForFeedback.AppendText(NewLine + "| c" + j + " <=> " + cGlobalInfo.ListCellularPhenotypes[j].Name + "\n");
                }
            }
            #endregion

            return this.CurrentClassifier;
        }
Ejemplo n.º 14
0
		/// <summary> Generates the classifier.
		/// 
		/// </summary>
		/// <param name="instances">set of instances serving as training data 
		/// </param>
		/// <exception cref="Exception">if the classifier has not been generated successfully
		/// </exception>
		public override void  buildClassifier(Instances instances)
		{
			//UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MAX_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
			double bestVal = System.Double.MaxValue, currVal;
			//UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MAX_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
			double bestPoint = - System.Double.MaxValue, sum;
			int bestAtt = - 1, numClasses;
			
			if (instances.checkForStringAttributes())
			{
				throw new Exception("Can't handle string attributes!");
			}
			
			double[][] bestDist = new double[3][];
			for (int i = 0; i < 3; i++)
			{
				bestDist[i] = new double[instances.numClasses()];
			}
			
			m_Instances = new Instances(instances);
			m_Instances.deleteWithMissingClass();
			
			if (m_Instances.numInstances() == 0)
			{
				throw new System.ArgumentException("No instances without missing " + "class values in training file!");
			}
			
			if (instances.numAttributes() == 1)
			{
				throw new System.ArgumentException("Attribute missing. Need at least one " + "attribute other than class attribute!");
			}
			
			if (m_Instances.classAttribute().Nominal)
			{
				numClasses = m_Instances.numClasses();
			}
			else
			{
				numClasses = 1;
			}
			
			// For each attribute
			bool first = true;
			for (int i = 0; i < m_Instances.numAttributes(); i++)
			{
				if (i != m_Instances.classIndex())
				{
					
					// Reserve space for distribution.
					double[][] tmpArray = new double[3][];
					for (int i2 = 0; i2 < 3; i2++)
					{
						tmpArray[i2] = new double[numClasses];
					}
					m_Distribution = tmpArray;
					
					// Compute value of criterion for best split on attribute
					if (m_Instances.attribute(i).Nominal)
					{
						currVal = findSplitNominal(i);
					}
					else
					{
						currVal = findSplitNumeric(i);
					}
					if ((first) || (currVal < bestVal))
					{
						bestVal = currVal;
						bestAtt = i;
						bestPoint = m_SplitPoint;
						for (int j = 0; j < 3; j++)
						{
							Array.Copy(m_Distribution[j], 0, bestDist[j], 0, numClasses);
						}
					}
					
					// First attribute has been investigated
					first = false;
				}
			}
			
			// Set attribute, split point and distribution.
			m_AttIndex = bestAtt;
			m_SplitPoint = bestPoint;
			m_Distribution = bestDist;
			if (m_Instances.classAttribute().Nominal)
			{
				for (int i = 0; i < m_Distribution.Length; i++)
				{
					double sumCounts = Utils.sum(m_Distribution[i]);
					if (sumCounts == 0)
					{
						// This means there were only missing attribute values
						Array.Copy(m_Distribution[2], 0, m_Distribution[i], 0, m_Distribution[2].Length);
						Utils.normalize(m_Distribution[i]);
					}
					else
					{
						Utils.normalize(m_Distribution[i], sumCounts);
					}
				}
			}
			
			// Save memory
			m_Instances = new Instances(m_Instances, 0);
		}
        public async Task <string> classifyTest(weka.classifiers.Classifier cl)
        {
            string a    = "";
            double rate = 0;

            try
            {
                //instsTest = Instances.mergeInstances(ins,null);

                /*if (ins.classIndex() == -1)
                 *  ins.setClassIndex(insts.numAttributes() - 1);*/

                System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");

                weka.filters.Filter normalized = new weka.filters.unsupervised.attribute.Normalize();
                normalized.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, normalized);

                //randomize the order of the instances in the dataset.
                weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
                myRandom.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myRandom);

                //replace missing values
                weka.filters.Filter replaceMissingValues = new weka.filters.unsupervised.attribute.ReplaceMissingValues();
                replaceMissingValues.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, replaceMissingValues);


                int trainSize             = insts.numInstances() * percentSplit / 100;
                int testSize              = insts.numInstances() - trainSize;
                weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);

                cl.buildClassifier(train);

                //double label = cl.classifyInstance(instsTest.instance(0));
                double label = cl.classifyInstance(ins);
                ins.setClassValue(label);
                //instsTest.instance(0).setClassValue(label);
                a = ins.toString(ins.numAttributes() - 1);

                weka.core.SerializationHelper.write("mymodel.model", cl);
                int numCorrect = 0;
                for (int i = trainSize; i < insts.numInstances(); i++)
                {
                    weka.core.Instance currentInst    = insts.instance(i);
                    double             predictedClass = cl.classifyInstance(currentInst);
                    if (predictedClass == insts.instance(i).classValue())
                    {
                        numCorrect++;
                    }
                }

                rate = (double)((double)numCorrect / (double)testSize * 100.0);
            }
            catch (java.lang.Exception ex)
            {
                //ex.printStackTrace();
                rate = -1;
            }
            return(rate.ToString() + ";" + a ?? "");
        }
Ejemplo n.º 16
0
		/// <summary> Creates a new set of instances by copying a 
		/// subset of another set.
		/// 
		/// </summary>
		/// <param name="source">the set of instances from which a subset 
		/// is to be created
		/// </param>
		/// <param name="first">the index of the first instance to be copied
		/// </param>
		/// <param name="toCopy">the number of instances to be copied
		/// </param>
		/// <exception cref="IllegalArgumentException">if first and toCopy are out of range
		/// </exception>
		//@ requires 0 <= first;
		//@ requires 0 <= toCopy;
		//@ requires first + toCopy <= source.numInstances();
		public Instances(Instances source, int first, int toCopy):this(source, toCopy)
		{
			
			if ((first < 0) || ((first + toCopy) > source.numInstances()))
			{
				throw new System.ArgumentException("Parameters first and/or toCopy out " + "of range");
			}
			source.copyInstances(first, this, toCopy);
		}
Ejemplo n.º 17
0
		/// <summary> Constructor copying all instances and references to
		/// the header information from the given set of instances.
		/// 
		/// </summary>
		/// <param name="instances">the set to be copied
		/// </param>
		public Instances(Instances dataset):this(dataset, dataset.numInstances())
		{
			
			dataset.copyInstances(0, this, dataset.numInstances());
		}
Ejemplo n.º 18
0
		/// <summary> Creates a new dataset of the same size using random sampling
		/// with replacement.
		/// 
		/// </summary>
		/// <param name="random">a random number generator
		/// </param>
		/// <returns> the new dataset
		/// </returns>
		public virtual Instances resample(System.Random random)
		{
			
			Instances newData = new Instances(this, numInstances());
			while (newData.numInstances() < numInstances())
			{
				newData.add(instance(random.Next(numInstances())));
			}
			return newData;
		}
Ejemplo n.º 19
0
        public static double classifyCrossFold_Train_Test_onlySelectedClass(string classifierFileName, int baseClasses, Classifier _classifier)
        {
            double performance = 0.0;

            try
            {
                List <BrResult> results = new List <BrResult>();
                for (int singleClass = 1; singleClass <= baseClasses; singleClass++)
                {
                    string eachFileName = String.Format("{0}_{1}.arff", classifierFileName, singleClass);

                    BrResult result = new BrResult();
                    result.classNumber = singleClass;

                    FileReader          javaFileReader = new FileReader(eachFileName);
                    weka.core.Instances insts          = new weka.core.Instances(javaFileReader);
                    javaFileReader.close();

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


                    List <Result> eachResults = new List <Result>();

                    var       totalnstances  = insts.numInstances();
                    var       foldsInstances = totalnstances / 10;
                    Instances foldsData      = new Instances(insts);
                    var       folds          = 10;
                    int       numCorrect     = 0;
                    int       dataIndex      = 0;
                    for (int n = 0; n < folds; n++)
                    {
                        System.Console.WriteLine("Performing " + n + " folds");

                        Instances trainFold         = foldsData.trainCV(folds, n);
                        var       numnerOfTrainInst = trainFold.numInstances();

                        Instances testFold         = foldsData.testCV(folds, n);
                        var       numnerOfTestInst = testFold.numInstances();


                        _classifier.buildClassifier(trainFold);

                        //List<Result> eachResults = new List<Result>();
                        for (int test = 0; test < numnerOfTestInst; test++)
                        {
                            dataIndex++;
                            Result eachRow = new Result();
                            eachRow.lineIndex = 0;
                            weka.core.Instance currentInst = testFold.instance(test);

                            double predictClass = _classifier.classifyInstance(currentInst);
                            //double[] dist = _classifier.distributionForInstance(currentInst);

                            string actualClass    = testFold.classAttribute().value((int)testFold.instance(test).classValue());
                            string predictedClass = testFold.classAttribute().value((int)predictClass);

                            //var abcd = _classifier.getClass();

                            if (predictedClass == actualClass)
                            {
                                eachRow.correct = "1";
                                numCorrect++;
                            }
                            else
                            {
                                eachRow.correct = "0";
                            }
                            eachRow.lineIndex      = dataIndex;
                            eachRow.classActual    = actualClass;
                            eachRow.classPredicted = predictedClass;

                            eachResults.Add(eachRow);
                        }
                    }
                    result.classResult = eachResults;
                    results.Add(result);
                    //System.Console.WriteLine(numCorrect + " out of " + testSize + " correct (" + (double)((double)numCorrect / (double)testSize * 100.0) + "%)");
                }

                #region Evaludation Matrix
                var evaluationMatrix = new Dictionary <int, string>();

                foreach (var res in results)
                {
                    foreach (var classRes in res.classResult)
                    {
                        if (!evaluationMatrix.Keys.Contains(classRes.lineIndex))
                        {
                            evaluationMatrix[classRes.lineIndex] = classRes.correct.toString();
                        }
                        else
                        {
                            evaluationMatrix[classRes.lineIndex] = evaluationMatrix[classRes.lineIndex].toString() + "," + classRes.correct.toString();
                        }
                    }
                }
                #endregion

                #region
                int correnctlyClassified   = 0;
                int incorrenctlyClassified = 0;
                int totalData = evaluationMatrix.Count;
                foreach (var key in evaluationMatrix.Keys)
                {
                    string   multiLevelClass = evaluationMatrix[key].ToString();
                    string[] a = multiLevelClass.Split(',');

                    int classPredect = 0;
                    for (int i = 0; i < a.Length; i++)
                    {
                        if (a[i] == "0")
                        {
                            classPredect++;
                        }
                    }
                    if (classPredect == 0)
                    {
                        correnctlyClassified++;
                    }
                    else if (classPredect > 0)
                    {
                        incorrenctlyClassified++;
                    }
                }

                performance = (double)((double)correnctlyClassified / (double)totalData) * 100;
                System.Console.WriteLine(performance);
                #endregion
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
            }
            return(performance);
        }
    // Test the classification result of each map that a user played,
    // with the data available as if they were playing through it
    public static void classifyTest(String dataString, String playerID)
    {
        try {
            java.io.StringReader stringReader = new java.io.StringReader(dataString);
            java.io.BufferedReader buffReader = new java.io.BufferedReader(stringReader);

            /* NOTE THAT FOR NAIVE BAYES ALL WEIGHTS CAN BE = 1*/
            //weka.core.converters.ConverterUtils.DataSource source = new weka.core.converters.ConverterUtils.DataSource("iris.arff");
            weka.core.Instances thisData = new weka.core.Instances(buffReader); //source.getDataSet();
            if (thisData.classIndex() == -1)
                thisData.setClassIndex(thisData.numAttributes() - 1);

            weka.core.Instances thisUniqueData = new weka.core.Instances(thisData);
            if (thisUniqueData.classIndex() == -1)
                thisUniqueData.setClassIndex(thisUniqueData.numAttributes() - 1);
            thisUniqueData.delete();

            if (allUniqueData == null) {
                allUniqueData = new weka.core.Instances(thisData);
                if (allUniqueData.classIndex() == -1)
                    allUniqueData.setClassIndex(allUniqueData.numAttributes() - 1);
                allUniqueData.delete();
            }

            weka.core.InstanceComparator com = new weka.core.InstanceComparator(false);

            for (int i = 0; i < thisData.numInstances(); i++)
            {
                bool dup = false;
                for (int j = 0; j < allUniqueData.numInstances(); j++)
                {
                    if (com.compare(thisData.instance(i),allUniqueData.instance(j)) == 0)
                    {
                        Debug.Log("Duplicate found!");
                        dup = true;
                        break;
                    }
                }
                if (!dup)
                    allUniqueData.add(thisData.instance(i));
                else
                    dupInstances++;
            }

            for (int i = 0; i < thisData.numInstances(); i++)
            {
                bool dup = false;
                for (int j = 0; j < thisUniqueData.numInstances(); j++)
                {
                    if (com.compare(thisData.instance(i),thisUniqueData.instance(j)) == 0)
                    {
                        Debug.Log("Duplicate found!");
                        dup = true;
                        break;
                    }
                }
                if (!dup)
                    thisUniqueData.add(thisData.instance(i));
                else
                    dupInstancesSamePlayer++;
            }

            //Debug.Log("All Data Instance Count = " + thisData.numInstances());
            //Debug.Log("Unique Data Instance Count = " + thisUniqueData.numInstances());
            //Debug.Log("Done!");

        } catch (java.lang.Exception ex)
        {
            Debug.LogError(ex.getMessage());
        }
    }
Ejemplo n.º 21
0
		/// <summary> Merges two sets of Instances together. The resulting set will have
		/// all the attributes of the first set plus all the attributes of the 
		/// second set. The number of instances in both sets must be the same.
		/// 
		/// </summary>
		/// <param name="first">the first set of Instances
		/// </param>
		/// <param name="second">the second set of Instances
		/// </param>
		/// <returns> the merged set of Instances
		/// </returns>
		/// <exception cref="IllegalArgumentException">if the datasets are not the same size
		/// </exception>
		public static Instances mergeInstances(Instances first, Instances second)
		{
			
			if (first.numInstances() != second.numInstances())
			{
				throw new System.ArgumentException("Instance sets must be of the same size");
			}
			
			// Create the vector of merged attributes
			FastVector newAttributes = new FastVector();
			for (int i = 0; i < first.numAttributes(); i++)
			{
				newAttributes.addElement(first.attribute(i));
			}
			for (int i = 0; i < second.numAttributes(); i++)
			{
				newAttributes.addElement(second.attribute(i));
			}
			
			// Create the set of Instances
			Instances merged = new Instances(first.relationName() + '_' + second.relationName(), newAttributes, first.numInstances());
			// Merge each instance
			for (int i = 0; i < first.numInstances(); i++)
			{
				merged.add(first.instance(i).mergeInstance(second.instance(i)));
			}
			return merged;
		}
Ejemplo n.º 22
0
		/// <summary> Select only instances with weights that contribute to 
		/// the specified quantile of the weight distribution
		/// 
		/// </summary>
		/// <param name="data">the input instances
		/// </param>
		/// <param name="quantile">the specified quantile eg 0.9 to select 
		/// 90% of the weight mass
		/// </param>
		/// <returns> the selected instances
		/// </returns>
		protected internal virtual Instances selectWeightQuantile(Instances data, double quantile)
		{
			
			int numInstances = data.numInstances();
			Instances trainData = new Instances(data, numInstances);
			double[] weights = new double[numInstances];
			
			double sumOfWeights = 0;
			for (int i = 0; i < numInstances; i++)
			{
				weights[i] = data.instance(i).weight();
				sumOfWeights += weights[i];
			}
			double weightMassToSelect = sumOfWeights * quantile;
			int[] sortedIndices = Utils.sort(weights);
			
			// Select the instances
			sumOfWeights = 0;
			for (int i = numInstances - 1; i >= 0; i--)
			{
				Instance instance = (Instance) data.instance(sortedIndices[i]).copy();
				trainData.add(instance);
				sumOfWeights += weights[sortedIndices[i]];
				if ((sumOfWeights > weightMassToSelect) && (i > 0) && (weights[sortedIndices[i]] != weights[sortedIndices[i - 1]]))
				{
					break;
				}
			}
			if (m_Debug)
			{
				System.Console.Error.WriteLine("Selected " + trainData.numInstances() + " out of " + numInstances);
			}
			return trainData;
		}
Ejemplo n.º 23
0
        /// <summary>
        /// Calculates Pleft, Prigth, rigthTargets , PclassDivideTleft, PclassDivideTleft and teta numbers for each inctance
        /// Then calling the creating dataset function
        /// </summary>
        /// <param name="insts"></param>
        /// <param name="categoryTypeNumber"></param>
        /// <param name="categoryTypeTargetNumber"></param>
        private void Twoing(weka.core.Instances insts, List <int[]> categoryTypeNumber, List <int[, ]> categoryTypeTargetNumber)
        {
            List <double[]> categoryTetaNumber = new List <double[]>();

            for (int i = 0; i < categoryTypeNumber.Count; i++)
            {
                categoryTetaNumber.Add(new double[categoryTypeNumber[i].Length]);
            }

            for (int i = 0; i < categoryTypeNumber.Count - 1; i++)
            {
                for (int j = 0; j < categoryTypeNumber[i].Length; j++)
                {
                    Double pLeft       = Convert.ToDouble(categoryTypeNumber[i][j]) / Convert.ToDouble(insts.numInstances());
                    Double pRight      = 1 - pLeft;
                    Double sumFunction = 0;
                    for (int k = 0; k < categoryTypeNumber[categoryTypeNumber.Count - 1].Length; k++)
                    {
                        Double PclassDivideTleft    = Convert.ToDouble(categoryTypeTargetNumber[i][j, k]) / Convert.ToDouble(categoryTypeNumber[i][j]);
                        int    sagtarafıntargetları = 0;
                        for (int h = 0; h < categoryTypeNumber[i].Length; h++)
                        {
                            if (h != j)
                            {
                                sagtarafıntargetları += categoryTypeTargetNumber[i][h, k];
                            }
                        }
                        Double PclassDivideTRigt = Convert.ToDouble(sagtarafıntargetları) / Convert.ToDouble((insts.numInstances() - categoryTypeNumber[i][j]));
                        sumFunction += Math.Abs(PclassDivideTleft - PclassDivideTRigt);
                    }

                    categoryTetaNumber[i][j] = 2 * pLeft * pRight * sumFunction;
                }
            }
            Instances fileInst = new Instances(insts);

            CreateNewDataset(fileInst, categoryTetaNumber, twoingPath);
        }
Ejemplo n.º 24
0
        public static void BayesTest()
        {
            try
            {
                weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader("iris.arff"));
                insts.setClassIndex(insts.numAttributes() - 1);

                weka.classifiers.Classifier cl = new weka.classifiers.bayes.BayesNet();
                System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");

                //randomize the order of the instances in the dataset.
                weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
                myRandom.setInputFormat(insts);
                insts = weka.filters.Filter.useFilter(insts, myRandom);

                int trainSize             = insts.numInstances() * percentSplit / 100;
                int testSize              = insts.numInstances() - trainSize;
                weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);
                weka.core.Instances test  = new weka.core.Instances(insts, 0, 0);


                cl.buildClassifier(train);
                //print model
                System.Console.WriteLine(cl);

                int numCorrect = 0;
                for (int i = trainSize; i < insts.numInstances(); i++)
                {
                    weka.core.Instance currentInst    = insts.instance(i);
                    double             predictedClass = cl.classifyInstance(currentInst);
                    test.add(currentInst);

                    double[] prediction = cl.distributionForInstance(currentInst);

                    for (int x = 0; x < prediction.Length; x++)
                    {
                        System.Console.WriteLine("Probability of class [{0}] for [{1}] is: {2}", currentInst.classAttribute().value(x), currentInst, Math.Round(prediction[x], 4));
                    }
                    System.Console.WriteLine();

                    if (predictedClass == insts.instance(i).classValue())
                    {
                        numCorrect++;
                    }
                }
                System.Console.WriteLine(numCorrect + " out of " + testSize + " correct (" +
                                         (double)((double)numCorrect / (double)testSize * 100.0) + "%)");

                // Train the model
                weka.classifiers.Evaluation eTrain = new weka.classifiers.Evaluation(train);
                eTrain.evaluateModel(cl, train);

                // Print the results as in Weka explorer:
                //Print statistics
                String strSummaryTrain = eTrain.toSummaryString();
                System.Console.WriteLine(strSummaryTrain);

                //Print detailed class statistics
                System.Console.WriteLine(eTrain.toClassDetailsString());

                //Print confusion matrix
                System.Console.WriteLine(eTrain.toMatrixString());

                // Get the confusion matrix
                double[][] cmMatrixTrain = eTrain.confusionMatrix();


                // Test the model
                weka.classifiers.Evaluation eTest = new weka.classifiers.Evaluation(test);
                eTest.evaluateModel(cl, test);

                // Print the results as in Weka explorer:
                //Print statistics
                String strSummaryTest = eTest.toSummaryString();
                System.Console.WriteLine(strSummaryTest);

                //Print detailed class statistics
                System.Console.WriteLine(eTest.toClassDetailsString());

                //Print confusion matrix
                System.Console.WriteLine(eTest.toMatrixString());

                // Get the confusion matrix
                double[][] cmMatrixTest = eTest.confusionMatrix();
            }

            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
            }
        }
Ejemplo n.º 25
0
        public static void Test()
        {
            weka.core.Instances data = new weka.core.Instances(new java.io.FileReader("./data/Classification/Communication.arff"));
            data.setClassIndex(data.numAttributes() - 1);

            weka.classifiers.Classifier cls = new weka.classifiers.bayes.BayesNet();


            //Save BayesNet results in .txt file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("./data/Classification/Communication_Report.txt"))
            {
                file.WriteLine("Performing " + percentSplit + "% split evaluation.");

                int runs = 1;

                // perform cross-validation
                for (int i = 0; i < runs; i++)
                {
                    // randomize data
                    int seed = i + 1;
                    java.util.Random    rand     = new java.util.Random(seed);
                    weka.core.Instances randData = new weka.core.Instances(data);
                    randData.randomize(rand);

                    //weka.classifiers.Evaluation eval = new weka.classifiers.Evaluation(randData);

                    int trainSize             = (int)Math.Round((double)data.numInstances() * percentSplit / 100);
                    int testSize              = data.numInstances() - trainSize;
                    weka.core.Instances train = new weka.core.Instances(data, 0, 0);
                    weka.core.Instances test  = new weka.core.Instances(data, 0, 0);
                    train.setClassIndex(train.numAttributes() - 1);
                    test.setClassIndex(test.numAttributes() - 1);

                    //Print classifier analytics for all the dataset
                    file.WriteLine("EVALUATION OF TEST DATASET.");

                    //int numCorrect = 0;
                    for (int j = 0; j < data.numInstances(); j++)
                    {
                        weka.core.Instance currentInst = randData.instance(j);

                        if (j < trainSize)
                        {
                            train.add(currentInst);
                        }

                        else
                        {
                            test.add(currentInst);

                            /*
                             * double predictedClass = cls.classifyInstance(currentInst);
                             *
                             * double[] prediction = cls.distributionForInstance(currentInst);
                             *
                             * for (int p = 0; p < prediction.Length; p++)
                             * {
                             *  file.WriteLine("Probability of class [{0}] for [{1}] is: {2}", currentInst.classAttribute().value(p), currentInst, Math.Round(prediction[p], 4));
                             * }
                             * file.WriteLine();
                             *
                             * file.WriteLine();
                             * if (predictedClass == data.instance(j).classValue())
                             *  numCorrect++;*/
                        }
                    }

                    // build and evaluate classifier
                    cls.buildClassifier(train);

                    // Test the model
                    weka.classifiers.Evaluation eval = new weka.classifiers.Evaluation(randData);
                    eval.evaluateModel(cls, test);

                    // Print the results as in Weka explorer:
                    //Print statistics
                    String strSummaryTest = eval.toSummaryString();

                    file.WriteLine(strSummaryTest);
                    file.WriteLine();

                    //Print detailed class statistics
                    file.WriteLine(eval.toClassDetailsString());
                    file.WriteLine();

                    //Print confusion matrix
                    file.WriteLine(eval.toMatrixString());
                    file.WriteLine();

                    // Get the confusion matrix
                    double[][] cmMatrixTest = eval.confusionMatrix();

                    System.Console.WriteLine("Bayesian Network results saved in Communication_Report.txt file successfully.");
                }
            }
        }
Ejemplo n.º 26
0
		/// <summary> Performs one boosting iteration.</summary>
		private void  performIteration(double[][] trainYs, double[][] trainFs, double[][] probs, Instances data, double origSumOfWeights)
		{
			
			if (m_Debug)
			{
				System.Console.Error.WriteLine("Training classifier " + (m_NumGenerated + 1));
			}
			
			// Build the new models
			for (int j = 0; j < m_NumClasses; j++)
			{
				if (m_Debug)
				{
					System.Console.Error.WriteLine("\t...for class " + (j + 1) + " (" + m_ClassAttribute.name() + "=" + m_ClassAttribute.value_Renamed(j) + ")");
				}
				
				// Make copy because we want to save the weights
				Instances boostData = new Instances(data);
				
				// Set instance pseudoclass and weights
				for (int i = 0; i < probs.Length; i++)
				{
					
					// Compute response and weight
					double p = probs[i][j];
					double z, actual = trainYs[i][j];
					if (actual == 1 - m_Offset)
					{
						z = 1.0 / p;
						if (z > Z_MAX)
						{
							// threshold
							z = Z_MAX;
						}
					}
					else
					{
						z = (- 1.0) / (1.0 - p);
						if (z < - Z_MAX)
						{
							// threshold
							z = - Z_MAX;
						}
					}
					double w = (actual - p) / z;
					
					// Set values for instance
					Instance current = boostData.instance(i);
					current.setValue(boostData.classIndex(), z);
					current.Weight = current.weight() * w;
				}
				
				// Scale the weights (helps with some base learners)
				double sumOfWeights = boostData.sumOfWeights();
				double scalingFactor = (double) origSumOfWeights / sumOfWeights;
				for (int i = 0; i < probs.Length; i++)
				{
					Instance current = boostData.instance(i);
					current.Weight = current.weight() * scalingFactor;
				}
				
				// Select instances to train the classifier on
				Instances trainData = boostData;
				if (m_WeightThreshold < 100)
				{
					trainData = selectWeightQuantile(boostData, (double) m_WeightThreshold / 100);
				}
				else
				{
					if (m_UseResampling)
					{
						double[] weights = new double[boostData.numInstances()];
						for (int kk = 0; kk < weights.Length; kk++)
						{
							weights[kk] = boostData.instance(kk).weight();
						}
						trainData = boostData.resampleWithWeights(m_RandomInstance, weights);
					}
				}
				
				// Build the classifier
				m_Classifiers[j][m_NumGenerated].buildClassifier(trainData);
			}
			
			// Evaluate / increment trainFs from the classifier
			for (int i = 0; i < trainFs.Length; i++)
			{
				double[] pred = new double[m_NumClasses];
				double predSum = 0;
				for (int j = 0; j < m_NumClasses; j++)
				{
					pred[j] = m_Shrinkage * m_Classifiers[j][m_NumGenerated].classifyInstance(data.instance(i));
					predSum += pred[j];
				}
				predSum /= m_NumClasses;
				for (int j = 0; j < m_NumClasses; j++)
				{
					trainFs[i][j] += (pred[j] - predSum) * (m_NumClasses - 1) / m_NumClasses;
				}
			}
			m_NumGenerated++;
			
			// Compute the current probability estimates
			for (int i = 0; i < trainYs.Length; i++)
			{
                probs[i] = Calculateprobs(trainFs[i]);
			}
		}
Ejemplo n.º 27
0
		/// <summary> Builds the boosted classifier</summary>
		public virtual void  buildClassifier(Instances data)
		{
			m_RandomInstance = new Random(m_Seed);
			Instances boostData;
			int classIndex = data.classIndex();
			
			if (data.classAttribute().Numeric)
			{
				throw new Exception("LogitBoost can't handle a numeric class!");
			}
			if (m_Classifier == null)
			{
				throw new System.Exception("A base classifier has not been specified!");
			}
			
			if (!(m_Classifier is WeightedInstancesHandler) && !m_UseResampling)
			{
				m_UseResampling = true;
			}
			if (data.checkForStringAttributes())
			{
				throw new Exception("Cannot handle string attributes!");
			}
			if (m_Debug)
			{
				System.Console.Error.WriteLine("Creating copy of the training data");
			}
			
			m_NumClasses = data.numClasses();
			m_ClassAttribute = data.classAttribute();
			
			// Create a copy of the data 
			data = new Instances(data);
			data.deleteWithMissingClass();
			
			// Create the base classifiers
			if (m_Debug)
			{
				System.Console.Error.WriteLine("Creating base classifiers");
			}
			m_Classifiers = new Classifier[m_NumClasses][];
			for (int j = 0; j < m_NumClasses; j++)
			{
				m_Classifiers[j] = Classifier.makeCopies(m_Classifier, this.NumIterations);
			}
			
			// Do we want to select the appropriate number of iterations
			// using cross-validation?
			int bestNumIterations = this.NumIterations;
			if (m_NumFolds > 1)
			{
				if (m_Debug)
				{
					System.Console.Error.WriteLine("Processing first fold.");
				}
				
				// Array for storing the results
				double[] results = new double[this.NumIterations];
				
				// Iterate throught the cv-runs
				for (int r = 0; r < m_NumRuns; r++)
				{
					
					// Stratify the data
					data.randomize(m_RandomInstance);
					data.stratify(m_NumFolds);
					
					// Perform the cross-validation
					for (int i = 0; i < m_NumFolds; i++)
					{
						
						// Get train and test folds
						Instances train = data.trainCV(m_NumFolds, i, m_RandomInstance);
						Instances test = data.testCV(m_NumFolds, i);
						
						// Make class numeric
						Instances trainN = new Instances(train);
						trainN.ClassIndex = - 1;
						trainN.deleteAttributeAt(classIndex);
						trainN.insertAttributeAt(new weka.core.Attribute("'pseudo class'"), classIndex);
						trainN.ClassIndex = classIndex;
						m_NumericClassData = new Instances(trainN, 0);
						
						// Get class values
						int numInstances = train.numInstances();
						double[][] tmpArray = new double[numInstances][];
						for (int i2 = 0; i2 < numInstances; i2++)
						{
							tmpArray[i2] = new double[m_NumClasses];
						}
						double[][] trainFs = tmpArray;
						double[][] tmpArray2 = new double[numInstances][];
						for (int i3 = 0; i3 < numInstances; i3++)
						{
							tmpArray2[i3] = new double[m_NumClasses];
						}
						double[][] trainYs = tmpArray2;
						for (int j = 0; j < m_NumClasses; j++)
						{
							for (int k = 0; k < numInstances; k++)
							{
								trainYs[k][j] = (train.instance(k).classValue() == j)?1.0 - m_Offset:0.0 + (m_Offset / (double) m_NumClasses);
							}
						}
						
						// Perform iterations
						double[][] probs = initialProbs(numInstances);
						m_NumGenerated = 0;
						double sumOfWeights = train.sumOfWeights();
						for (int j = 0; j < this.NumIterations; j++)
						{
							performIteration(trainYs, trainFs, probs, trainN, sumOfWeights);
							Evaluation eval = new Evaluation(train);
							eval.evaluateModel(this, test);
							results[j] += eval.correct();
						}
					}
				}
				
				// Find the number of iterations with the lowest error
				//UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MAX_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
				double bestResult = - System.Double.MaxValue;
				for (int j = 0; j < this.NumIterations; j++)
				{
					if (results[j] > bestResult)
					{
						bestResult = results[j];
						bestNumIterations = j;
					}
				}
				if (m_Debug)
				{
					System.Console.Error.WriteLine("Best result for " + bestNumIterations + " iterations: " + bestResult);
				}
			}
			
			// Build classifier on all the data
			int numInstances2 = data.numInstances();
			double[][] trainFs2 = new double[numInstances2][];
			for (int i4 = 0; i4 < numInstances2; i4++)
			{
				trainFs2[i4] = new double[m_NumClasses];
			}
			double[][] trainYs2 = new double[numInstances2][];
			for (int i5 = 0; i5 < numInstances2; i5++)
			{
				trainYs2[i5] = new double[m_NumClasses];
			}
			for (int j = 0; j < m_NumClasses; j++)
			{
				for (int i = 0, k = 0; i < numInstances2; i++, k++)
				{
					trainYs2[i][j] = (data.instance(k).classValue() == j)?1.0 - m_Offset:0.0 + (m_Offset / (double) m_NumClasses);
				}
			}
			
			// Make class numeric
			data.ClassIndex = - 1;
			data.deleteAttributeAt(classIndex);
			data.insertAttributeAt(new weka.core.Attribute("'pseudo class'"), classIndex);
			data.ClassIndex = classIndex;
			m_NumericClassData = new Instances(data, 0);
			
			// Perform iterations
			double[][] probs2 = initialProbs(numInstances2);
            double logLikelihood = CalculateLogLikelihood(trainYs2, probs2);
			m_NumGenerated = 0;
			if (m_Debug)
			{
				System.Console.Error.WriteLine("Avg. log-likelihood: " + logLikelihood);
			}
			double sumOfWeights2 = data.sumOfWeights();
			for (int j = 0; j < bestNumIterations; j++)
			{
				double previousLoglikelihood = logLikelihood;
				performIteration(trainYs2, trainFs2, probs2, data, sumOfWeights2);
                logLikelihood = CalculateLogLikelihood(trainYs2, probs2);
				if (m_Debug)
				{
					System.Console.Error.WriteLine("Avg. log-likelihood: " + logLikelihood);
				}
				if (System.Math.Abs(previousLoglikelihood - logLikelihood) < m_Precision)
				{
					return ;
				}
			}
		}
Ejemplo n.º 28
0
		/// <summary> Splits the given set of instances into subsets.
		/// 
		/// </summary>
		/// <exception cref="Exception">if something goes wrong
		/// </exception>
		public Instances[] split(Instances data)
		{
			
			Instances[] instances = new Instances[m_numSubsets];
			double[] weights;
			double newWeight;
			Instance instance;
			int subset, i, j;
			
			for (j = 0; j < m_numSubsets; j++)
				instances[j] = new Instances((Instances) data, data.numInstances());
			for (i = 0; i < data.numInstances(); i++)
			{
				instance = ((Instances) data).instance(i);
				weights = GetWeights(instance);
				subset = whichSubset(instance);
				if (subset > - 1)
					instances[subset].add(instance);
				else
					for (j = 0; j < m_numSubsets; j++)
						if (Utils.gr(weights[j], 0))
						{
							newWeight = weights[j] * instance.weight();
							instances[j].add(instance);
							instances[j].lastInstance().Weight = newWeight;
						}
			}
			for (j = 0; j < m_numSubsets; j++)
				instances[j].compactify();
			
			return instances;
		}
Ejemplo n.º 29
0
		/// <summary> Evaluates the classifier on a given set of instances. Note that
		/// the data must have exactly the same format (e.g. order of
		/// attributes) as the data used to train the classifier! Otherwise
		/// the results will generally be meaningless.
		/// 
		/// </summary>
		/// <param name="classifier">machine learning classifier
		/// </param>
		/// <param name="data">set of test instances for evaluation
		/// </param>
		/// <returns> the predictions
		/// </returns>
		/// <throws>  Exception if model could not be evaluated  </throws>
		/// <summary> successfully 
		/// </summary>
		public virtual double[] evaluateModel(Classifier classifier, Instances data)
		{
			
			double[] predictions = new double[data.numInstances()];
			
			for (int i = 0; i < data.numInstances(); i++)
			{
				predictions[i] = evaluateModelOnce((Classifier) classifier, data.instance(i));
			}
			return predictions;
		}
Ejemplo n.º 30
0
		/// <summary> Sets distribution associated with model.</summary>
		public override void  resetDistribution(Instances data)
		{
			
			Instances insts = new Instances(data, data.numInstances());
			for (int i = 0; i < data.numInstances(); i++)
			{
				if (whichSubset(data.instance(i)) > - 1)
				{
					insts.add(data.instance(i));
				}
			}
			Distribution newD = new Distribution(insts, this);
			newD.addInstWithUnknown(data, m_attIndex);
			m_distribution = newD;
		}
Ejemplo n.º 31
0
		public override void  buildClassifier(Instances insts)
		{
			
			if (insts.checkForStringAttributes())
			{
				throw new Exception("Cannot handle string attributes!");
			}
			if (insts.numClasses() > 2)
			{
				throw new System.Exception("Can only handle two-class datasets!");
			}
			if (insts.classAttribute().Numeric)
			{
				throw new Exception("Can't handle a numeric class!");
			}
			
			// Filter data
			m_Train = new Instances(insts);
			m_Train.deleteWithMissingClass();
			m_ReplaceMissingValues = new ReplaceMissingValues();
			m_ReplaceMissingValues.setInputFormat(m_Train);
			m_Train = Filter.useFilter(m_Train, m_ReplaceMissingValues);
			
			m_NominalToBinary = new NominalToBinary();
			m_NominalToBinary.setInputFormat(m_Train);
			m_Train = Filter.useFilter(m_Train, m_NominalToBinary);
			
			/** Randomize training data */
			//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.util.Random.Random'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
			m_Train.randomize(new System.Random((System.Int32) m_Seed));
			
			/** Make space to store perceptrons */
			m_Additions = new int[m_MaxK + 1];
			m_IsAddition = new bool[m_MaxK + 1];
			m_Weights = new int[m_MaxK + 1];
			
			/** Compute perceptrons */
			m_K = 0;
			for (int it = 0; it < m_NumIterations; it++)
			{
				for (int i = 0; i < m_Train.numInstances(); i++)
				{
					Instance inst = m_Train.instance(i);
					if (!inst.classIsMissing())
					{
						int prediction = makePrediction(m_K, inst);
						//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
						int classValue = (int) inst.classValue();
						if (prediction == classValue)
						{
							m_Weights[m_K]++;
						}
						else
						{
							m_IsAddition[m_K] = (classValue == 1);
							m_Additions[m_K] = i;
							m_K++;
							m_Weights[m_K]++;
						}
						if (m_K == m_MaxK)
						{
							//UPGRADE_NOTE: Labeled break statement was changed to a goto statement. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1012'"
							goto out_brk;
						}
					}
				}
			}
			//UPGRADE_NOTE: Label 'out_brk' was added. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1011'"

out_brk: ;
			
		}
Ejemplo n.º 32
-1
        public static void Test_predictClass(string classifierFileName)
        {
            FileReader javaFileReader = new FileReader(classifierFileName);

            weka.core.Instances insts = new weka.core.Instances(javaFileReader);
            javaFileReader.close();

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

            weka.classifiers.Classifier cl = new weka.classifiers.trees.J48();
            System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");



            #region Manual Cross Fold
            Instances foldsData = new Instances(insts);
            int       folds     = 10;
            for (int n = 0; n < folds; n++)
            {
                Instances trainFold = foldsData.trainCV(folds, n);
                Instances testFold  = foldsData.testCV(folds, n);
            }
            #endregion



            #region
            int trainSize             = insts.numInstances() * percentSplit / 100;
            int testSize              = insts.numInstances() - trainSize;
            weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);

            cl.buildClassifier(train);
            #endregion

            //Classifier cls = new J48();
            Evaluation       eval = new Evaluation(insts);
            java.util.Random rand = new java.util.Random(1);  // using seed = 1
            int fold = 10;
            eval.crossValidateModel(cl, insts, fold, rand);
            System.Console.WriteLine("toClassDetailsString" + eval.toClassDetailsString());
            System.Console.WriteLine("toMatrixString\n" + eval.toMatrixString());
            System.Console.WriteLine("toCumulativeMarginDistributionString\n" + eval.toCumulativeMarginDistributionString());
            //System.Console.WriteLine("predictions\n" + eval.predictions());
            System.Console.ReadKey();

            //var numnerOfInst = insts.numInstances();

            //for (int i = trainSize; i < numnerOfInst; i++)
            //{
            //    weka.core.Instance currentInst = insts.instance(i);

            //    double pred = cl.classifyInstance(currentInst);
            //    System.Console.WriteLine("class Index: " + insts.instance(i).classIndex());
            //    System.Console.WriteLine(", class value: " + insts.instance(i).classValue());
            //    System.Console.WriteLine(", ID: " + insts.instance(i).value(0));
            //    System.Console.WriteLine(", actual: " + insts.classAttribute().value((int)insts.instance(i).classValue()));
            //    System.Console.WriteLine(", predicted: " + insts.classAttribute().value((int)pred));

            //}
        }