Esempio n. 1
0
    //**************************************************************************************

    /// <summary>
    /// Returns classifier prediction based on provided parameters.
    /// </summary>
    /// <param name="iParameters"></param>
    public Prediction Predict(float[] iParameters)
    {
        if (iParameters is null)
        {
            throw new ArgumentNullException(nameof(iParameters));
        }

        WaitTillModelReady();

        // Create instances object
        var instances = new weka.core.Instances("Test", Attributes, 1);

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

        // Create single instance
        var instance = new weka.core.DenseInstance(instances.numAttributes() - 1);

        instance.setDataset(instances);

        // Fill instance with data
        for (int i = 0; i < iParameters.Length; i++)
        {
            instance.setValue(i, iParameters[i]);
        }

        // Get prediction
        double prediction = Model.classifyInstance(instance);

        // Convert prediction to decision
        return((Prediction)Enum.Parse(typeof(Prediction), instances.classAttribute().value((int)prediction)));
    }
    //returns the classification result as the lane number
    private double getAlgorithmResult(float[] attributes)
    {
        //sets up the current environment as a new state 'Instance'
        //labels as the same type as the training data, sets up the attribute values
        //leaves the classifier blank
        weka.core.Instance currentState = new weka.core.DenseInstance(6);
        currentState.setDataset(insts);
        currentState.setValue(0, (attributes[3] + 2));
        currentState.setValue(1, attributes[0]);
        currentState.setValue(2, attributes[1]);
        currentState.setValue(3, attributes[2]);
        currentState.setValue(4, totalTime);
        currentState.setClassMissing();
        //Debug.Log (currentState.ToString());
        double result = j48Classifier.classifyInstance(currentState);

        return(result);
    }
Esempio n. 3
0
        private void Train(string str)
        {
            if (string.IsNullOrEmpty(str))
                return;
            m_cls = CreateClassifier(str);
            if (m_cls == null)
            {
                MessageBox.Show("Can't Create Classifier!");
                return;
            }

            var trainInstances = CreateCurrentInstances();
            m_cls.buildClassifier(trainInstances);

            // TEST
            var data = CreateEmptyInstances();
            StringBuilder sb = new StringBuilder();

            if (m_cls is MLEA.IBatchClassifier)
            {
                MLEA.IBatchClassifier batchClassifier = m_cls as MLEA.IBatchClassifier;
                for (int i = 0; i < XLEN; i++)
                {
                    for (int j = 0; j < YLEN; j++)
                    {
                        var vals = new double[data.numAttributes()];
                        vals[0] = (double)i / XLEN;
                        vals[1] = (double)j / YLEN;

                        var instance = new weka.core.DenseInstance(1.0, vals);
                        data.add(instance);
                        instance.setDataset(data);
                    }
                }

                double[] ds = batchClassifier.classifyInstances(data);
                for (int i = 0; i < XLEN; i++)
                {
                    for (int j = 0; j < YLEN; j++)
                    {
                        double d = ds[i * XLEN + j];

                        if (m_enableLog)
                        {
                            string s = string.Format("{0}, {1}: {2}", data.instance(i * XLEN + j).value(0).ToString("N2"), data.instance(i * XLEN + j).value(1).ToString("N2"), d.ToString("N0"));
                            sb.AppendLine(s);
                        }

                        for (int ii = 0; ii < WXLEN / XLEN; ++ii)
                            for (int jj = 0; jj < WYLEN / YLEN; ++jj)
                                m_pictureBoxBitmap.SetPixel(i * WXLEN / XLEN + ii, j * WYLEN / YLEN + jj, GetValueColor((int)d, false));
                    }
                }
            }
            else
            {
                for (int i = 0; i < XLEN; i++)
                {
                    for (int j = 0; j < YLEN; j++)
                    {
                        var vals = new double[data.numAttributes()];
                        vals[0] = (double)i / XLEN;
                        vals[1] = (double)j / YLEN;

                        var instance = new weka.core.DenseInstance(1.0, vals);
                        data.add(instance);
                        instance.setDataset(data);

                        double d = m_cls.classifyInstance(instance);

                        if (m_enableLog)
                        {
                            string s = string.Format("{0}, {1}: {2}", vals[0].ToString("N2"), vals[1].ToString("N2"), d.ToString("N0"));
                            sb.AppendLine(s);
                        }

                        for (int ii = 0; ii < WXLEN / XLEN; ++ii)
                            for (int jj = 0; jj < WYLEN / YLEN; ++jj)
                                m_pictureBoxBitmap.SetPixel(i * WXLEN / XLEN + ii, j * WYLEN / YLEN + jj, GetValueColor((int)d, false));
                    }
                }
            }
            draw_all_points();

            this.Invoke(new Action(() =>
                {
                    if (m_enableLog)
                    {
                        txtLog.AppendText(sb.ToString());
                    }
                }));

            if (m_enableEvaluation)
            {
                Test(trainInstances);
            }
        }
Esempio n. 4
0
        private void Train(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return;
            }
            m_cls = CreateClassifier(str);
            if (m_cls == null)
            {
                MessageBox.Show("Can't Create Classifier!");
                return;
            }

            var trainInstances = CreateCurrentInstances();

            m_cls.buildClassifier(trainInstances);

            // TEST
            var           data = CreateEmptyInstances();
            StringBuilder sb   = new StringBuilder();

            if (m_cls is MLEA.IBatchClassifier)
            {
                MLEA.IBatchClassifier batchClassifier = m_cls as MLEA.IBatchClassifier;
                for (int i = 0; i < XLEN; i++)
                {
                    for (int j = 0; j < YLEN; j++)
                    {
                        var vals = new double[data.numAttributes()];
                        vals[0] = (double)i / XLEN;
                        vals[1] = (double)j / YLEN;

                        var instance = new weka.core.DenseInstance(1.0, vals);
                        data.add(instance);
                        instance.setDataset(data);
                    }
                }

                double[] ds = batchClassifier.classifyInstances(data);
                for (int i = 0; i < XLEN; i++)
                {
                    for (int j = 0; j < YLEN; j++)
                    {
                        double d = ds[i * XLEN + j];

                        if (m_enableLog)
                        {
                            string s = string.Format("{0}, {1}: {2}", data.instance(i * XLEN + j).value(0).ToString("N2"), data.instance(i * XLEN + j).value(1).ToString("N2"), d.ToString("N0"));
                            sb.AppendLine(s);
                        }

                        for (int ii = 0; ii < WXLEN / XLEN; ++ii)
                        {
                            for (int jj = 0; jj < WYLEN / YLEN; ++jj)
                            {
                                m_pictureBoxBitmap.SetPixel(i * WXLEN / XLEN + ii, j * WYLEN / YLEN + jj, GetValueColor((int)d, false));
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < XLEN; i++)
                {
                    for (int j = 0; j < YLEN; j++)
                    {
                        var vals = new double[data.numAttributes()];
                        vals[0] = (double)i / XLEN;
                        vals[1] = (double)j / YLEN;

                        var instance = new weka.core.DenseInstance(1.0, vals);
                        data.add(instance);
                        instance.setDataset(data);

                        double d = m_cls.classifyInstance(instance);

                        if (m_enableLog)
                        {
                            string s = string.Format("{0}, {1}: {2}", vals[0].ToString("N2"), vals[1].ToString("N2"), d.ToString("N0"));
                            sb.AppendLine(s);
                        }

                        for (int ii = 0; ii < WXLEN / XLEN; ++ii)
                        {
                            for (int jj = 0; jj < WYLEN / YLEN; ++jj)
                            {
                                m_pictureBoxBitmap.SetPixel(i * WXLEN / XLEN + ii, j * WYLEN / YLEN + jj, GetValueColor((int)d, false));
                            }
                        }
                    }
                }
            }
            draw_all_points();

            this.Invoke(new Action(() =>
            {
                if (m_enableLog)
                {
                    txtLog.AppendText(sb.ToString());
                }
            }));

            if (m_enableEvaluation)
            {
                Test(trainInstances);
            }
        }
Esempio n. 5
0
        //private void ConvertNorminalToString(string fileName)
        //{
        //    List<string> list = new List<string>();
        //    using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
        //    {
        //        while (true)
        //        {
        //            if (sr.EndOfStream)
        //                break;
        //            string s = sr.ReadLine();
        //            if (string.IsNullOrEmpty(s))
        //                continue;
        //            int idx = s.IndexOf(' ');
        //            string c = idx == -1 ? s : s.Substring(0, idx);
        //            if (Convert.ToDouble(c) == 0)
        //            {
        //                list.Add("-1.0 " + (idx == -1 ? string.Empty : s.Substring(idx + 1)));
        //            }
        //            else if (Convert.ToDouble(c) == 1)
        //            {
        //                list.Add("0.0 " + (idx == -1 ? string.Empty : s.Substring(idx + 1)));
        //            }
        //            else if (Convert.ToDouble(c) == 2)
        //            {
        //                list.Add("+1.0 " + (idx == -1 ? string.Empty : s.Substring(idx + 1)));
        //            }
        //            else
        //            {
        //                list.Add(s);
        //            }
        //        }
        //    }
        //    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName))
        //    {
        //        foreach (string s in list)
        //        {
        //            sw.WriteLine(s);
        //        }
        //    }
        //}
        //private Random m_randomGenerator;
        private void AddInstancesAccordWeight(Instances instances)
        {
            // 0, 2
            double[] weights = MincostLiblinearClassifier.GetCount(instances);
            if (weights == null)
                return;

            double c = m_tp / m_sl;
            if (c == 1 && weights[0] == weights[1])
                return;

            int n = 0;
            int toCopyClass = 0;
            if (c >= 1)
            {
                int shouldWeight1 = (int)(c * weights[1]);
                n = (int)(shouldWeight1 - weights[1]);
                toCopyClass = 2;
            }
            else
            {
                int shouldShouldWeight0 = (int)(1 / c * weights[0]);
                n = (int)(weights[1] - weights[0]);
                toCopyClass = 0;
            }
            //m_randomGenerator = new Random((int)System.DateTime.Now.Ticks);

            List<Instance> copyInstances = new List<Instance>();
            for (int i = 0; i < instances.numInstances(); ++i)
            {
                if (instances.instance(i).classValue() == toCopyClass)
                {
                    copyInstances.Add(instances.instance(i));
                }
            }

            int nAll = n / copyInstances.Count;
            for (int j = 0; j < nAll; ++j)
            {
                for (int i = 0; i < copyInstances.Count; ++i)
                {
                    Instance newInstance = new weka.core.DenseInstance(copyInstances[i]);
                    instances.add(newInstance);
                    newInstance.setDataset(instances);
                }
            }
            //for (int j = 0; j < n - nAll * copyInstances.Count; ++j)
            //{
            //    int idx = (int)(m_randomGenerator.NextDouble() * copyInstances.Count);
            //    idx = Math.Min(idx, copyInstances.Count - 1);
            //    Instance newInstance = new weka.core.DenseInstance(copyInstances[idx]);
            //    instances.add(newInstance);
            //    newInstance.setDataset(instances);
            //}

            if (n - nAll * copyInstances.Count > 0)
            {
                Instance avgInstance = new weka.core.DenseInstance(instances.numAttributes());
                for (int i = 0; i < avgInstance.numAttributes(); ++i)
                {
                    double sum = 0;
                    for (int j = 0; j < copyInstances.Count; ++j)
                    {
                        sum += copyInstances[j].value(i);
                    }
                    avgInstance.setValue(i, sum / copyInstances.Count);
                }
                for (int j = 0; j < n - nAll * copyInstances.Count; ++j)
                {
                    Instance newInstance = new weka.core.DenseInstance(avgInstance);
                    instances.add(newInstance);
                }
            }
        }
Esempio n. 6
0
        //private void ConvertNorminalToString(string fileName)
        //{
        //    List<string> list = new List<string>();
        //    using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
        //    {
        //        while (true)
        //        {
        //            if (sr.EndOfStream)
        //                break;

        //            string s = sr.ReadLine();
        //            if (string.IsNullOrEmpty(s))
        //                continue;

        //            int idx = s.IndexOf(' ');
        //            string c = idx == -1 ? s : s.Substring(0, idx);
        //            if (Convert.ToDouble(c) == 0)
        //            {
        //                list.Add("-1.0 " + (idx == -1 ? string.Empty : s.Substring(idx + 1)));
        //            }
        //            else if (Convert.ToDouble(c) == 1)
        //            {
        //                list.Add("0.0 " + (idx == -1 ? string.Empty : s.Substring(idx + 1)));
        //            }
        //            else if (Convert.ToDouble(c) == 2)
        //            {
        //                list.Add("+1.0 " + (idx == -1 ? string.Empty : s.Substring(idx + 1)));
        //            }
        //            else
        //            {
        //                list.Add(s);
        //            }
        //        }
        //    }
        //    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName))
        //    {
        //        foreach (string s in list)
        //        {
        //            sw.WriteLine(s);
        //        }
        //    }
        //}

        //private Random m_randomGenerator;
        private void AddInstancesAccordWeight(Instances instances)
        {
            // 0, 2
            double[] weights = MincostLiblinearClassifier.GetCount(instances);
            if (weights == null)
            {
                return;
            }

            double c = m_tp / m_sl;

            if (c == 1 && weights[0] == weights[1])
            {
                return;
            }

            int n           = 0;
            int toCopyClass = 0;

            if (c >= 1)
            {
                int shouldWeight1 = (int)(c * weights[1]);
                n           = (int)(shouldWeight1 - weights[1]);
                toCopyClass = 2;
            }
            else
            {
                int shouldShouldWeight0 = (int)(1 / c * weights[0]);
                n           = (int)(weights[1] - weights[0]);
                toCopyClass = 0;
            }
            //m_randomGenerator = new Random((int)System.DateTime.Now.Ticks);


            List <Instance> copyInstances = new List <Instance>();

            for (int i = 0; i < instances.numInstances(); ++i)
            {
                if (instances.instance(i).classValue() == toCopyClass)
                {
                    copyInstances.Add(instances.instance(i));
                }
            }

            int nAll = n / copyInstances.Count;

            for (int j = 0; j < nAll; ++j)
            {
                for (int i = 0; i < copyInstances.Count; ++i)
                {
                    Instance newInstance = new weka.core.DenseInstance(copyInstances[i]);
                    instances.add(newInstance);
                    newInstance.setDataset(instances);
                }
            }
            //for (int j = 0; j < n - nAll * copyInstances.Count; ++j)
            //{
            //    int idx = (int)(m_randomGenerator.NextDouble() * copyInstances.Count);
            //    idx = Math.Min(idx, copyInstances.Count - 1);
            //    Instance newInstance = new weka.core.DenseInstance(copyInstances[idx]);
            //    instances.add(newInstance);
            //    newInstance.setDataset(instances);
            //}

            if (n - nAll * copyInstances.Count > 0)
            {
                Instance avgInstance = new weka.core.DenseInstance(instances.numAttributes());
                for (int i = 0; i < avgInstance.numAttributes(); ++i)
                {
                    double sum = 0;
                    for (int j = 0; j < copyInstances.Count; ++j)
                    {
                        sum += copyInstances[j].value(i);
                    }
                    avgInstance.setValue(i, sum / copyInstances.Count);
                }
                for (int j = 0; j < n - nAll * copyInstances.Count; ++j)
                {
                    Instance newInstance = new weka.core.DenseInstance(avgInstance);
                    instances.add(newInstance);
                }
            }
        }