Пример #1
0
        /// <summary>
        /// Phần train cho K-SVMeans
        /// </summary>
        private void TrainKSVMeans(bool isBatchMode)
        {
            string strTrainFile = null;

            if (isBatchMode)
            {
                strTrainFile = _trainFilePath;
            }
            else
            {
                strTrainFile = tbxTrainFilePath.Text;
            }

            int iNumCluster = (int)nmNumCluster.Value;
            int iPos = strTrainFile.LastIndexOf('_');
            string strMutualPath = strTrainFile.Remove(iPos + 1);
            string strClusterModelFile = strMutualPath + "_clusterModel.txt";
            string[] strClusterResultFiles = new string[iNumCluster];
            string[] strSVMModelFiles = new string[iNumCluster];

            for (int i = 0; i < iNumCluster; i++)
            {
                strClusterResultFiles[i] = strMutualPath + "cluster" + (i + 1).ToString() + ".txt";
                strSVMModelFiles[i] = strMutualPath + "model" + (i + 1).ToString() + ".txt";
            }
            // Thực hiện cluster
            SampleDataBUS samDataBUS = new SampleDataBUS();
            samDataBUS.Read(strTrainFile);
            Clustering clustering = new Clustering(iNumCluster, samDataBUS.Samples, DistanceType.Manhattan);
            clustering.Run(strClusterModelFile, false);
            samDataBUS.WriteIntoCluster(strClusterResultFiles, clustering.SampleData.ClusterIndices);
            // Thực hiện train SVM
            int iProgressBaseline = 0;
            for (int i = 0; i < iNumCluster; i++)
            {
                Problem prob = Problem.Read(strClusterResultFiles[i]);
                Parameter param = new Parameter();
                iProgressBaseline = i * 100 / iNumCluster;
                if (cmbModelSelection.SelectedItem.ToString() == "Grid search")
                {
                    string strLogFile = strMutualPath + "GridCluster" + (i + 1).ToString() + ".txt";
                    double dblC;
                    double dblGamma;
                    ParameterSelection paramSel = new ParameterSelection();
                    paramSel.NFOLD = Int32.Parse(tbxNumFold.Text);
                    paramSel.EndEpochEvent += new CrossEpochEventHandler(
                    delegate(object senderNetwork, CrossEpochEventArgs args)
                    {
                        tlsProgressBar.Value = iProgressBaseline + (int)(args.TrainingIteration * 100d / (args.Cycles * iNumCluster));
                        tlsStatus.Text = "Cluster: " + (i + 1) + " | Current parameter set: " + args.TrainingIteration;
                        Application.DoEvents();
                    });
                    paramSel.Grid(prob, param, strLogFile, out dblC, out dblGamma);
                    param.C = dblC;
                    param.Gamma = dblGamma;
                    param.Probability = ckbProbEstimate.Checked;
                    Model model = Training.Train(prob, param);
                    Model.Write(strSVMModelFiles[i], model);
                }
                else if (cmbModelSelection.SelectedItem.ToString() == "Use default values")
                {
                    if (tbxC.Text == "" || tbxGamma.Text == "")
                    {
                        MessageBox.Show("Please fill in parameters!");
                        return;
                    }
                    param.C = double.Parse(tbxC.Text);
                    param.Gamma = double.Parse(tbxGamma.Text);
                    param.Probability = ckbProbEstimate.Checked;
                    Model model = Training.Train(prob, param);
                    Model.Write(strSVMModelFiles[i], model);
                }

            }
            tlsProgressBar.Value = 0;
        }
Пример #2
0
        private double[] doPrediction(double[] inputValue4Predicts, int numDayPredict,int modelType)
        {
            double[] result = null;
            string strModelFile = null;
            string strTestFile = null;

            if (modelType == 0)//DT-ANN
            {
                strModelFile = _trainModel + "DT-ANN\\" + cmbStockID.SelectedItem.ToString()+ "_" + numDayPredict.ToString() + "_model.txt";
                // Load model lên
                BackpropagationNetwork bpNetwork;
                Stream stream = File.Open(strModelFile, FileMode.Open);
                BinaryFormatter bformatter = new BinaryFormatter();
                bpNetwork = (BackpropagationNetwork)bformatter.Deserialize(stream);
                stream.Close();

                // Tạo tập mẫu để test
                TrainingSample testSample = new TrainingSample(inputValue4Predicts);

                // Thực hiện test
                double[] dblTemp = bpNetwork.Run(testSample.InputVector);
                result = new double[1];

                result[0] = ConverterBUS.Convert2Trend(dblTemp);
            }
            else
            {
                //strModelFile = _trainModel + "K-SVMeans\\"; //+cmbStockID.SelectedItem.ToString() + "_" + numDayPredict.ToString() + "_model.txt";
                string strMutualPath = _trainModel + "K-SVMeans\\" +cmbStockID.SelectedItem.ToString() + "_" + numDayPredict.ToString();
                string strClusterModelFile = strMutualPath + "__clusterModel.txt";

                // Thực hiện cluster
                SampleDataBUS samDataBUS = new SampleDataBUS();
                samDataBUS.Samples = new double[1][];
                samDataBUS.Samples[0] = inputValue4Predicts;

                //samDataBUS.Read(strTestFile);
                Clustering clustering = new Clustering(samDataBUS.Samples, DistanceType.Manhattan);
                clustering.Run(strClusterModelFile, true);

                int clusterIndex = clustering.SampleData.ClusterIndices[0];

                string strSVMModelFiles = strMutualPath + "_model" + (++clusterIndex).ToString() + ".txt";

                Model model = Model.Read(strSVMModelFiles);

                Node[] nodes = new Node[10];

                for (int i = 0; i < 10; i++)
                {
                    nodes[i] = new Node(i, inputValue4Predicts[i]);
                }

                double[] predictProbability = Prediction.PredictProbability(model, nodes);

                //khoi tao label 3 pt
                int[] labels = new int[3];
                Procedures.svm_get_labels(model, labels);

                result = new double[3];

                for (int j = 0; j < labels.Length; j++)
                {
                    if (labels[j] == 1)
                    {
                        result[0] = predictProbability[j];
                    }
                    else if (labels[j] == 0)
                    {
                        result[1] = predictProbability[j];
                    }
                    else
                    {
                        result[2] = predictProbability[j];
                    }
                }
            }
            return result;
        }
Пример #3
0
        /// <summary>
        /// Phần test cho K-SVMeans
        /// </summary>
        private void TestKSVMeans(bool isBatchMode)
        {
            string strModelFile = null;
            string strTestFile = null;
            if (isBatchMode)
            {
                strModelFile = _modelFilePath;
                strTestFile = _testFilePath;
            }
            else
            {
                strModelFile = tbxModelFilePath.Text;
                strTestFile = tbxTestFilePath.Text;
            }

            int iPos = strTestFile.LastIndexOf('_');
            string strMutualPath = strTestFile.Remove(iPos + 1);
            int iNumCluster = (int)nmNumCluster.Value;
            string strClusterModelFile = strMutualPath + "_clusterModel.txt";
            string[] strClusterResultFiles = new string[iNumCluster];
            string[] strSVMModelFiles = new string[iNumCluster];
            string[] strPredictedFiles = new string[iNumCluster];
            string strStatisticFile = strMutualPath + "statistic.txt";
            for (int i = 0; i < iNumCluster; i++)
            {
                strClusterResultFiles[i] = strMutualPath + "testcluster" + (i + 1).ToString() + ".txt";
                strSVMModelFiles[i] = strMutualPath + "model" + (i + 1).ToString() + ".txt";
                strPredictedFiles[i] = strMutualPath + "predict" + (i + 1).ToString() + ".txt";
            }

            // Thực hiện cluster
            SampleDataBUS samDataBUS = new SampleDataBUS();
            samDataBUS.Read(strTestFile);
            Clustering clustering = new Clustering(samDataBUS.Samples, DistanceType.Manhattan);
            clustering.Run(strClusterModelFile, true);
            samDataBUS.WriteIntoCluster(strClusterResultFiles, clustering.SampleData.ClusterIndices);

            // Thực hiện test SVM
            StreamWriter writer = new StreamWriter(strMutualPath + "performance.txt");
            double dblTotalPrecision = 0;
            for (int i = 0; i < iNumCluster; i++)
            {
                Problem prob = Problem.Read(strClusterResultFiles[i]);
                Model model = Model.Read(strSVMModelFiles[i]);
                double dblPrecision = Prediction.Predict(prob, strPredictedFiles[i], model, ckbProbEstimate.Checked);
                writer.WriteLine("Cluster " + (i + 1).ToString() + ": " + dblPrecision);
                if (clustering.Clusters[i].NumSample > 0)
                {
                    dblTotalPrecision += dblPrecision * clustering.Clusters[i].NumSample;
                }
            }
            writer.WriteLine("All: " + dblTotalPrecision / samDataBUS.DataLines.Length);
            writer.Close();
            _resultShow += "<font color = \"blue\">Accuracy = " + (dblTotalPrecision / samDataBUS.DataLines.Length).ToString() + "%</font><br>";
            StatisticTrend2File(strPredictedFiles, strStatisticFile);
        }