Esempio n. 1
0
        public static string WEKA_GETMLP(weka.core.Instances insts)
        {
            string THEOUTPUT = " ";

            try
            {
                insts.setClassIndex(insts.numAttributes() - 1);
                weka.classifiers.functions.MultilayerPerceptron mlp = new weka.classifiers.functions.MultilayerPerceptron();

                //SETTING PARAMETERS
                weka.core.Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H 3");
                mlp.buildClassifier(insts);


                THEOUTPUT = mlp.ToString();
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
            }

            return(THEOUTPUT);

            //new Program().Method3
        }
Esempio n. 2
0
        public static void CalculateSuccessForAnn(weka.core.Instances originalInsts)
        {
            try
            {
                var form = Form.ActiveForm as Form1;

                form.successPrcAnn.Text = "Training...";
                form.successRtAnn.Text  = "../" + testSize;

                weka.core.Instances insts = originalInsts;

                // Pre-process
                insts = ConvertNominalToNumeric(insts);
                insts = Normalize(insts);

                // Classify
                weka.classifiers.Classifier cl    = new weka.classifiers.functions.MultilayerPerceptron();
                weka.core.Instances         train = new weka.core.Instances(insts, 0, trainSize);
                cl.buildClassifier(train);

                int    numCorrect = 0;
                double percentage = 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++;
                    }

                    percentage              = (double)numCorrect / (double)testSize * 100.0;
                    form.successRtAnn.Text  = numCorrect + "/" + testSize;
                    form.successPrcAnn.Text = String.Format("{0:0.00}", percentage) + "%";
                }
                succesRates.Add(Classifier.ANN, percentage);
                classifiers.Add(Classifier.ANN, cl);
            }
            catch (java.lang.Exception ex)
            {
                ex.printStackTrace();
                MessageBox.Show(ex.ToString(), "Error for Neural Network", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception)
            {
                MessageBox.Show("Error for  Neural Network", "Error for  Neural Network", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 3
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();
            }
        }
Esempio n. 4
0
    // 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 = 2; 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");
                //cl = new weka.classifiers.trees.RandomForest();

                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, subset.numInstances(), 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())
                {
                    results = results + ", ";
                }
                if (i == data.numInstances())
                {
                    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_RandomForest.txt");
        StreamWriter writer = new StreamWriter("DataForMatlab/" + playerID + "_LOOCrossFold_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");
    }