public static object ConvertToJavaTypes(object value)
        {
            if (value is Boolean) return new java.lang.Boolean((bool)value);
            if (value is int) return new java.lang.Integer((int)value);
            if (value is Int64) return new java.lang.Long((Int64)value);
            if (value is double) return new java.lang.Double((double)value);
            if (value is DateTime)
            {
                var datetime = (DateTime) value;
                return new java.util.Date(
                    datetime.Year, datetime.Month, datetime.Day,
                    datetime.Hour, datetime.Minute, datetime.Second);
            }
            if (value is string) return java.lang.String.valueOf(value);
            if (value is IEnumerable)
            {
                var enumerable = value as IEnumerable;
                var iterable = new java.util.ArrayList();
                foreach (var resultItem in enumerable)
                {
                    iterable.Add(resultItem);
                }
                return iterable;
            }

            return value;
        }
Exemple #2
0
 /// <summary>
 /// Converts list of integer to arraylist of java integers
 /// </summary>
 /// <param name="intList">the list of integer to convert</param>
 /// <returns>the converted list of integers as arraylist of java integers</returns>
 public static java.util.ArrayList ToJavaIntList(List <int> intList)
 {
     java.util.ArrayList javalist = new java.util.ArrayList();
     foreach (int i in intList)
     {
         javalist.Add(ToJavaInt(i));
     }
     return(javalist);
 }
        public List <double> testMLPUsingWeka(string[] attributeArray, string[] classNames, double[] dataValues, string classHeader, string defaultclass, string modelName, int hiddelLayers = 7, double learningRate = 0.03, double momentum = 0.4, int decimalPlaces = 2, int trainingTime = 1000)
        {
            java.util.ArrayList classLabel = new java.util.ArrayList();
            foreach (string className in classNames)
            {
                classLabel.Add(className);
            }
            weka.core.Attribute classHeaderName = new weka.core.Attribute(classHeader, classLabel);

            java.util.ArrayList attributeList = new java.util.ArrayList();
            foreach (string attribute in attributeArray)
            {
                weka.core.Attribute newAttribute = new weka.core.Attribute(attribute);
                attributeList.Add(newAttribute);
            }
            attributeList.add(classHeaderName);
            weka.core.Instances data = new weka.core.Instances("TestInstances", attributeList, 0);
            data.setClassIndex(data.numAttributes() - 1);
            // Set instance's values for the attributes
            weka.core.Instance inst_co = new DenseInstance(data.numAttributes());
            for (int i = 0; i < data.numAttributes() - 1; i++)
            {
                inst_co.setValue(i, dataValues.ElementAt(i));
            }

            inst_co.setValue(classHeaderName, defaultclass);
            data.add(inst_co);

            java.io.File path = new java.io.File("/models/");
            weka.classifiers.functions.MultilayerPerceptron clRead = loadModel(modelName, path);
            clRead.setHiddenLayers(hiddelLayers.ToString());
            clRead.setLearningRate(learningRate);
            clRead.setMomentum(momentum);
            clRead.setNumDecimalPlaces(decimalPlaces);
            clRead.setTrainingTime(trainingTime);
            weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
            myRandom.setInputFormat(data);
            data = weka.filters.Filter.useFilter(data, myRandom);
            double classValue = clRead.classifyInstance(data.get(0));

            double[]      predictionDistribution  = clRead.distributionForInstance(data.get(0));
            List <double> predictionDistributions = new List <double>();

            for (int predictionDistributionIndex = 0;
                 predictionDistributionIndex < predictionDistribution.Count();
                 predictionDistributionIndex++)
            {
                string classValueString1 = classLabel.get(predictionDistributionIndex).ToString();
                double prob = predictionDistribution[predictionDistributionIndex] * 100;
                predictionDistributions.Add(prob);
            }
            List <double> prediction = new List <double>();

            prediction.Add(classValue);
            prediction.AddRange(predictionDistributions);
            return(prediction);
        }
Exemple #4
0
        public static Instances GetInstancesForWeka(string[] domains, string[] labelsClass, Label status, ProgressBar progress)
        {
            java.util.ArrayList attributes = new java.util.ArrayList();

            //Добавление атрибутов
            InputLayerWrapperMulti wrapper = new InputLayerWrapperMulti();

            for (int i = 0; i < wrapper.namesAttributes.Length; i++)
            {
                attributes.Add(new weka.core.Attribute(wrapper.namesAttributes[i]));
            }

            java.util.ArrayList classes = new java.util.ArrayList();

            foreach (var item in labelsClass.ToList().Distinct())
            {
                classes.add(item);
            }

            attributes.Add(new weka.core.Attribute("class", classes)); //Метка класса

            Instances instances = new Instances($"Domains {DateTime.Now}", attributes, 0);

            int numAttributes = instances.numAttributes();
            int countRows     = domains.Length;

            for (int i = 0; i < countRows; i++)
            {
                instances.add(GetInstanceForWeka(domains[i], labelsClass[i], numAttributes));

                progress.Invoke(new Action(() =>
                {
                    progress.Value = i;
                    status.Text    = $"{i}/{countRows}";
                }));
            }

            return(instances);
        }
Exemple #5
0
        public string testEmotionUsingWeka(string[] attributeArray, string[] classNames, double[] dataValues, string classHeader, string defaultclass, string modelName, int hiddelLayers = 7, double learningRate = 0.03, double momentum = 0.4, int decimalPlaces = 2, int trainingTime = 1000)
        {
            List <double> predictions = testMLPUsingWeka(attributeArray, classNames, dataValues, classHeader, defaultclass, modelName, hiddelLayers, learningRate, momentum, decimalPlaces, trainingTime);

            java.util.ArrayList classLabel = new java.util.ArrayList();
            foreach (string className in classNames)
            {
                classLabel.Add(className);
            }
            string classValueString = classLabel.get(Int32.Parse(predictions.ElementAt(0).ToString())).ToString();

            return(classValueString);
        }
        public static object ConvertToJavaTypes(object value)
        {
            if (value is Boolean)
            {
                return(new java.lang.Boolean((bool)value));
            }
            if (value is int)
            {
                return(new java.lang.Integer((int)value));
            }
            if (value is Int64)
            {
                return(new java.lang.Long((Int64)value));
            }
            if (value is double)
            {
                return(new java.lang.Double((double)value));
            }
            if (value is DateTime)
            {
                var datetime = (DateTime)value;
                return(new java.util.Date(
                           datetime.Year, datetime.Month, datetime.Day,
                           datetime.Hour, datetime.Minute, datetime.Second));
            }
            if (value is string)
            {
                return(java.lang.String.valueOf(value));
            }
            if (value is IEnumerable)
            {
                var enumerable = value as IEnumerable;
                var iterable   = new java.util.ArrayList();
                foreach (var resultItem in enumerable)
                {
                    iterable.Add(resultItem);
                }
                return(iterable);
            }

            return(value);
        }
Exemple #7
0
        public string testHybridEmotionUsingWeka(string[] attributeArray, string[] classNames, double[] dataValues, string classHeader, string defaultclass, string modelName)
        {
            java.util.ArrayList classLabel = new java.util.ArrayList();
            foreach (string className in classNames)
            {
                classLabel.Add(className);
            }
            weka.core.Attribute classHeaderName = new weka.core.Attribute(classHeader, classLabel);

            java.util.ArrayList attributeList = new java.util.ArrayList();
            foreach (string attribute in attributeArray)
            {
                weka.core.Attribute newAttribute = new weka.core.Attribute(attribute);
                attributeList.Add(newAttribute);
            }
            attributeList.add(classHeaderName);
            weka.core.Instances data = new weka.core.Instances("TestInstances", attributeList, 0);
            data.setClassIndex(data.numAttributes() - 1);
            // Set instance's values for the attributes
            weka.core.Instance inst_co = new DenseInstance(data.numAttributes());
            for (int i = 0; i < data.numAttributes() - 1; i++)
            {
                inst_co.setValue(i, dataValues.ElementAt(i));
            }

            inst_co.setValue(classHeaderName, defaultclass);
            data.add(inst_co);

            java.io.File path = new java.io.File("/models/");
            weka.classifiers.meta.Bagging clRead   = loadBaggingModel(modelName, path);
            weka.filters.Filter           myRandom = new weka.filters.unsupervised.instance.Randomize();
            myRandom.setInputFormat(data);
            data = weka.filters.Filter.useFilter(data, myRandom);
            double classValue       = clRead.classifyInstance(data.get(0));
            string classValueString = classLabel.get(Int32.Parse(classValue.ToString())).ToString();

            return(classValueString);
        }
Exemple #8
0
        public static void CreateArffFiles()
        {
            java.util.ArrayList atts;
            java.util.ArrayList attsRel;
            java.util.ArrayList attVals;
            java.util.ArrayList attValsRel;
            Instances           data;
            Instances           dataRel;

            double[] vals;
            double[] valsRel;
            int      i;

            // 1. set up attributes
            atts = new java.util.ArrayList();
            // - numeric
            atts.Add(new weka.core.Attribute("att1"));
            // - nominal
            attVals = new java.util.ArrayList();
            for (i = 0; i < 5; i++)
            {
                attVals.add("val" + (i + 1));
            }

            weka.core.Attribute nominal = new weka.core.Attribute("att2", attVals);
            atts.add(nominal);
            // - string
            atts.add(new weka.core.Attribute("att3", (java.util.ArrayList)null));
            // - date
            atts.add(new weka.core.Attribute("att4", "yyyy-MM-dd"));
            // - relational
            attsRel = new java.util.ArrayList();
            // -- numeric
            attsRel.add(new weka.core.Attribute("att5.1"));
            // -- nominal
            attValsRel = new java.util.ArrayList();
            for (i = 0; i < 5; i++)
            {
                attValsRel.Add("val5." + (i + 1));
            }
            attsRel.add(new weka.core.Attribute("att5.2", attValsRel));
            dataRel = new Instances("att5", attsRel, 0);
            atts.add(new weka.core.Attribute("att5", dataRel, 0));

            // 2. create Instances object
            data = new Instances("MyRelation", atts, 0);

            // 3. fill with data
            // first instance
            vals = new double[data.numAttributes()];
            // - numeric
            vals[0] = Math.PI;
            // - nominal
            vals[1] = attVals.indexOf("val3");
            // - string
            vals[2] = data.attribute(2).addStringValue("This is a string!");
            // - date
            vals[3] = data.attribute(3).parseDate("2001-11-09");
            // - relational
            dataRel = new Instances(data.attribute(4).relation(), 0);
            // -- first instance
            valsRel    = new double[2];
            valsRel[0] = Math.PI + 1;
            valsRel[1] = attValsRel.indexOf("val5.3");
            weka.core.Instance inst = new DenseInstance(2);
            inst.setValue(1, valsRel[0]);
            inst.setValue(1, valsRel[1]);
            dataRel.add(inst);
            // -- second instance
            valsRel    = new double[2];
            valsRel[0] = Math.PI + 2;
            valsRel[1] = attValsRel.indexOf("val5.2");
            dataRel.add(inst);
            vals[4] = data.attribute(4).addRelation(dataRel);
            // add
            weka.core.Instance inst2 = new DenseInstance(4);
            inst2.setValue(1, vals[0]);
            inst2.setValue(1, vals[1]);
            inst2.setValue(1, vals[2]);
            inst2.setValue(1, vals[3]);
            data.add(inst2);

            // second instance
            vals = new double[data.numAttributes()];  // important: needs NEW array!
                                                      // - numeric
            vals[0] = Math.E;
            // - nominal
            vals[1] = attVals.indexOf("val1");
            // - string
            vals[2] = data.attribute(2).addStringValue("And another one!");
            // - date
            vals[3] = data.attribute(3).parseDate("2000-12-01");
            // - relational
            dataRel = new Instances(data.attribute(4).relation(), 0);
            // -- first instance
            valsRel    = new double[2];
            valsRel[0] = Math.E + 1;
            valsRel[1] = attValsRel.indexOf("val5.4");
            dataRel.add(inst);
            // -- second instance
            valsRel    = new double[2];
            valsRel[0] = Math.E + 2;
            valsRel[1] = attValsRel.indexOf("val5.1");
            dataRel.add(inst);
            vals[4] = data.attribute(4).addRelation(dataRel);
            // add
            data.add(inst2);

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

            // 4. output data
            for (int x = 0; x < data.numInstances(); x++)
            {
                weka.core.Instance ins = data.instance(x);
                System.Console.WriteLine(ins.value(x).ToString());
            }



            return;
        }
        public List <double> testSMOUsingWeka(string[] attributeArray, string[] classNames, double[] dataValues, string classHeader, string defaultclass, string modelName, int hiddelLayers = 7, double learningRate = 0.03, double momentum = 0.4, int decimalPlaces = 2, int trainingTime = 1000)
        {
            java.util.ArrayList classLabel = new java.util.ArrayList();

            foreach (string className in classNames)
            {
                classLabel.Add(className);
            }
            weka.core.Attribute classHeaderName = new weka.core.Attribute(classHeader, classLabel);

            java.util.ArrayList attributeList = new java.util.ArrayList();
            foreach (string attribute in attributeArray)
            {
                weka.core.Attribute newAttribute = new weka.core.Attribute(attribute);
                attributeList.Add(newAttribute);
            }
            attributeList.add(classHeaderName);
            weka.core.Instances data = new weka.core.Instances("TestInstances", attributeList, 0);

            data.setClassIndex(data.numAttributes() - 1);
            // Set instance's values for the attributes
            weka.core.Instance inst_co = new DenseInstance(data.numAttributes());
            for (int i = 0; i < data.numAttributes() - 1; i++)
            {
                inst_co.setValue(i, Math.Round(dataValues.ElementAt(i), 5));
            }

            inst_co.setValue(classHeaderName, defaultclass);
            data.add(inst_co);
            weka.core.Instance currentInst = data.get(0);
            int j = 0;

            //foreach (float value in dataValues)
            //{
            //    // double roundedValue = Math.Round(value);
            //    //var rounded = Math.Floor(value * 100) / 100;
            //    if (array.ElementAt(j) != value)
            //    {
            //        System.Console.WriteLine("Masla occur");
            //    }
            //    j++;
            //}
            //double predictedClass = cl.classifyInstance(data.get(0));

            weka.classifiers.functions.SMO clRead = new weka.classifiers.functions.SMO();
            try
            {
                java.io.File path = new java.io.File("/models/");
                clRead = loadSMOModel(modelName, path);
            }
            catch (Exception e)
            {
                //string p1 = Assembly.GetExecutingAssembly().Location;
                string ClassifierName = Path.GetFileName(Path.GetFileName(modelName));
                string Path1          = HostingEnvironment.MapPath(@"~//libs//models//" + ClassifierName);
                //string Path1 = HostingEnvironment.MapPath(@"~//libs//models//FusionCustomized.model");
                clRead = (weka.classifiers.functions.SMO)weka.core.SerializationHelper.read(modelName);
            }
            // weka.classifiers.functions.SMO clRead = loadSMOModel(modelName, path);
            clRead.setBatchSize("100");

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

            double classValue = clRead.classifyInstance(data.get(0));

            double[] predictionDistribution = clRead.distributionForInstance(data.get(0));
            //for (int predictionDistributionIndex = 0;
            //  predictionDistributionIndex < predictionDistribution.Count();
            //  predictionDistributionIndex++)
            //{
            //    string classValueString1 = classLabel.get(predictionDistributionIndex).ToString();
            //    double prob= predictionDistribution[predictionDistributionIndex]*100;
            //    System.Console.WriteLine(classValueString1 + ":" + prob);
            //}
            List <double> prediction = new List <double>();

            prediction.Add(classValue);
            //prediction.AddRange(predictionDistribution);
            return(prediction);
        }