コード例 #1
0
ファイル: WekaUtils.cs プロジェクト: vseledkin/MLEA
        public static MyEvaluation TestInstances(Instances testInstances, Classifier cls, CostMatrix costMatrix = null)
        {
            //Evaluation eval = new Evaluation(testInstances, new CostMatrix(new BufferedReader(new FileReader(m_costFileName))));
            MyEvaluation eval;

            if (costMatrix != null)
            {
                eval = new MyEvaluation(costMatrix);
            }
            else
            {
                eval = new MyEvaluation();
            }
            //eval.crossValidateModel(cls, origInstances, 5, new java.util.Random(1));
            //WriteLog(eval.toSummaryString());
            //WriteLog("Confusion matrix is " + eval.toMatrixString());
            //WriteLog("total cose is " + eval.totalCost());
            //WriteLog("Crossvalidate is done.");
            //using (System.IO.StreamWriter sw = new StreamWriter(string.Format("{0}\\crossValidateResult.txt", m_baseDir), true))
            //{
            //    sw.WriteLine(string.Format("CrossValidate Data from {0} to {1}", m_trainTimeStart.ToString(m_dateTimeFormat), m_trainTimeEnd.ToString(m_dateTimeFormat)));
            //    sw.WriteLine(eval.toSummaryString());
            //    sw.WriteLine("Confusion matrix is " + eval.toMatrixString());
            //}

            //eval.evaluateModel(cls, origInstances);
            //WriteEvalSummary(eval, string.Format("Train Data from {0} to {1}", m_trainTimeStart.ToString(m_dateFormat), m_trainTimeEnd.ToString(m_dateFormat)));

            eval.evaluateModel(cls, testInstances);

            return(eval);
        }
コード例 #2
0
ファイル: TestManager.cs プロジェクト: wushian/MLEA
        public void BuildExcludeModels()
        {
            Parameters.TotalCanUseMemory = 600 * 1000 * 1000;

            TestParameters.SaveDataFile = false;
            //m_batchBufferMinutes = (new TimeSpan(365 + 5, 0, 0, 0)).TotalMinutes;

            weka.classifiers.Classifier cls = new weka.classifiers.functions.LibSVM();
            (cls as weka.classifiers.AbstractClassifier).setOptions(weka.core.Utils.splitOptions("-S 0 -K 2"));
            //Classifier cls = new MincostLiblinearClassifier();
            //Classifier cls = new weka.classifiers.functions.LibLINEAR();
            //(cls as AbstractClassifier).setOptions(weka.core.Utils.splitOptions("-S 0 -P -C 1 -B 1"));
            //var cls = new SvmLightClassifier();
            //cls.setOptions(weka.core.Utils.splitOptions("-c 20 -l 4 -w 1 --p 1 --b 1"));

            var cp = new CandidateParameter("BuildExcludeModels");
            cp.DeleteUnusedIndicators();

            ParameterdCandidateStrategy realDealCandidate = new ParameterdCandidateStrategy(cp);

            SetTrainTime(new DateTime(2009, 1, 1), new DateTime(2009, 4, 30));
            //m_generateOneClassHp = 0;

            realDealCandidate.IterateClassifierInfos2(new Func<CandidateClassifier, bool>((clsInfo) =>
            {
                string modelFileName = clsInfo.WekaData.GetExcludeModelFileName(clsInfo.Name);
                //if (!System.IO.File.Exists(modelFileName))
                {
                    WekaUtils.Instance.WriteLog(string.Format("{0} is building exclude model.", clsInfo.Name));

                    //clsInfo.WekaData.m_currentTestHour = clsInfo.Hour;
                    clsInfo.WekaData.GenerateData(true, false);
                    WekaUtils.TrainInstances(clsInfo.WekaData.CurrentTrainInstancesNew, modelFileName, cls);

                    MyEvaluation eval = new MyEvaluation();
                    eval.evaluateModel(cls, clsInfo.WekaData.CurrentTrainInstancesNew);
                    return true;
                }
                //else
                //{
                //    m_currentTestHour = h;
                //    GenerateData(true, false);

                //    cls = WekaUtils.TryLoadClassifier(modelFileName);
                //    var eval = WekaUtils.TestInstances(m_trainInstancesNew[k], cls);
                //}
            }));
        }
コード例 #3
0
        public void ExecuteBest(DateTime nowDate, List<CandidateClassifier> minScoreInfos)
        {
            OutputRealDealSummary(nowDate);

            float totalCost = 0;
            int totalDeal = 0;

            if (minScoreInfos != null)
            {
                List<DealInfo> candidateDeals = new List<DealInfo>();
                foreach (var minScoreInfo in minScoreInfos)
                {
                    minScoreInfo.WekaData.GenerateData(false, true);
                    weka.core.Instances minTestInstances = minScoreInfo.WekaData.CurrentTestInstances;
                    weka.core.Instances minTestInstancesNew = minScoreInfo.WekaData.CurrentTestInstancesNew;

                    if (minTestInstances.numInstances() > 0)
                    {
                        MyEvaluation eval = new MyEvaluation(minScoreInfo.CostMatrix);
                        eval.evaluateModel(minScoreInfo.CurrentTestRet, minScoreInfo.CurrentClassValue);

                        float vol;
                        // vol = (float)minScoreInfo.MoneyManagement.GetVolume(null);
                        vol = 0.1F;
                        //vol = (float)Math.Round(minScore / -20000.0, 1);
                        //WekaUtils.DebugAssert(vol > 0);

                        int tp = (int)eval.numTruePositives(1);
                        int fp = (int)eval.numFalsePositives(1);

                        double minScore = minScoreInfo.Deals.NowScore;
                        //WekaUtils.Instance.WriteLog(string.Format("Best Classifier: N={0},TC={1},TP={2},FP={3},TD={4},TV={5},TTP={6},TFP={7},",
                        //        minScoreInfo.Name, minScoreInfo.Deals.NowScore.ToString(Parameters.DoubleFormatString),
                        //        minScoreInfo.Deals.NowTp, minScoreInfo.Deals.NowFp, minScoreInfo.Deals.NowDeal,
                        //        minScoreInfo.Deals.TotalVolume.ToString("N2"), tp, fp),
                        //    true, ConsoleColor.DarkGreen);

                        // Exclude
                        //if (TestParameters.EnableExcludeClassifier)
                        //{
                        //if (minScoreInfo.ExcludeClassifier == null)
                        //{
                        //    string modelFileName4Exclude = GetExcludeModelFileName(minScoreInfo.Name);
                        //    minScoreInfo.ExcludeClassifier = WekaUtils.TryLoadClassifier(modelFileName4Exclude);
                        //}
                        //}

                        for (int i = 0; i < minScoreInfo.CurrentTestRet.Length; i++)
                        {
                            if (minScoreInfo.CurrentTestRet[i] == 2)
                            {
                                if (minScoreInfo.ExcludeClassifier != null)
                                {
                                    double cv2 = minScoreInfo.ExcludeClassifier.classifyInstance(minTestInstancesNew.instance(i));
                                    if (cv2 != 2)
                                        continue;
                                }
                                candidateDeals.Add(new DealInfo(WekaUtils.GetDateValueFromInstances(minTestInstances, 0, i),
                                    (float)WekaUtils.GetValueFromInstance(minTestInstances, "mainClose", i),
                                    minScoreInfo.DealType,
                                    (float)vol,
                                    (float)(minScoreInfo.CurrentClassValue[i] == 2 ? -minScoreInfo.Tp : minScoreInfo.Sl),
                                    WekaUtils.GetDateValueFromInstances(minTestInstances, 1, i)));
                            }
                        }
                        float nowCost = (float)eval.totalCost();
                        int nowDeal = tp + fp;
                        totalCost += nowCost * vol;
                        totalDeal += nowDeal;

                        //float diff = Math.Abs(totalCost - realDealsInfo.TotalCost);
                        //WekaUtils.DebugAssert(diff < 5);
                        //WekaUtils.DebugAssert(Math.Abs(totalDeal - (realDealsInfo.NowDeal + realDealsInfo.CurrentDeal)) == 0);
                        //if (diff > 0.5)
                        //    totalCost = realDealsInfo.TotalCost;

                        IterateClassifierInfos((k, i, j, h) =>
                        {
                            if (m_classifierInfoIdxs[k, i, j, h] == minScoreInfo)
                            {
                                m_totalScores[k, i, j, h] += nowCost;
                                m_totalDeals[k, i, j, h] += nowDeal;
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        });
                    }
                }

                int selectCount = 50;
                for (int i = 0; i < selectCount; ++i)
                {
                    if (candidateDeals.Count == 0)
                        break;

                    int selectedDealIdx = (int)Math.Round(m_randomGenerator.NextDouble() * candidateDeals.Count);
                    if (selectedDealIdx == candidateDeals.Count)
                        selectedDealIdx = candidateDeals.Count - 1;

                    m_realDealsInfo.AddDeal(candidateDeals[selectedDealIdx]);
                    candidateDeals.RemoveAt(selectedDealIdx);
                }

            }

            //if (m_enableDetailLogLevel2)
            //{
            //    IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            if (h == m_currentTestHour && m_totalScores[k, i, j, h] != 0)
            //            {
            //                WekaUtils.Instance.WriteLog(string.Format("Predict score for {0}: TC={1}, TD={2}", m_classifierInfoIdxs[k, i, j, h].Name, m_totalScores[k, i, j, h].ToString(Parameters.DoubleFormatString), m_totalDeals[k, i, j, h]));
            //            }
            //        });

            //}
            if (!string.IsNullOrEmpty(currentSummary))
            {
                WekaUtils.Instance.WriteLog(currentSummary, true, ConsoleColor.Red);
                System.Console.Title = nowDate.ToString(Parameters.DateTimeFormat) + ":" + currentSummary;
            }

            //if (m_enableDetailLogLevel2)
            //{
            //    if (TestParameters.EnablePerhourTrain)
            //    {
            //        double[] totalCostPerHour = new double[m_classifierInfoIdxs.GetLength(3)];
            //        int[] totalDealPerHour = new int[m_classifierInfoIdxs.GetLength(3)];
            //        IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            totalCostPerHour[h] += m_totalScores[k, i, j, h];
            //            totalDealPerHour[h] += m_totalDeals[k, i, j, h];
            //        });
            //        for (int i = 0; i < totalCostPerHour.Length; ++i)
            //        {
            //            if (totalCostPerHour[i] == 0)
            //                continue;

            //            WekaUtils.Instance.WriteLog(string.Format("Predict score Per hour of {0}: TC={1}, TD={2}", i, totalCostPerHour[i].ToString(Parameters.DoubleFormatString), totalDealPerHour[i]));
            //        }
            //    }

            //if ((m_testTimeStart.Month == 4 || m_testTimeStart.Month == 3) && m_testTimeStart.Day == 20)
            //{
            //    foreach (var cls in m_classifierInfos)
            //    {
            //        WekaUtils.Instance.WriteLog(cls.Value.Deals.PrintAll());
            //    }
            //}
            //if (m_currentTestHour == 0)
            //{
            //    var cls = m_classifierInfoIdxs[0, 0, 0, 0];
            //    WekaUtils.Instance.WriteLog((cls.Classifier as RandomClassifier).GetCountInfo());
            //}
            //}

            //if (m_saveDataFile)
            //{
            //    var files = System.IO.Directory.GetFiles(m_baseDir, "*.arff");
            //    if (files.Length > 100)
            //    {
            //        foreach (string fileName in files)
            //        {
            //            try
            //            {
            //                System.IO.File.Delete(fileName);
            //            }
            //            catch (Exception)
            //            {
            //            }
            //        }
            //    }
            //}
        }
コード例 #4
0
        public void ExecuteBest(DateTime nowDate, List <CandidateClassifier> minScoreInfos)
        {
            OutputRealDealSummary(nowDate);

            float totalCost = 0;
            int   totalDeal = 0;

            if (minScoreInfos != null)
            {
                List <DealInfo> candidateDeals = new List <DealInfo>();
                foreach (var minScoreInfo in minScoreInfos)
                {
                    minScoreInfo.WekaData.GenerateData(false, true);
                    weka.core.Instances minTestInstances    = minScoreInfo.WekaData.CurrentTestInstances;
                    weka.core.Instances minTestInstancesNew = minScoreInfo.WekaData.CurrentTestInstancesNew;

                    if (minTestInstances.numInstances() > 0)
                    {
                        MyEvaluation eval = new MyEvaluation(minScoreInfo.CostMatrix);
                        eval.evaluateModel(minScoreInfo.CurrentTestRet, minScoreInfo.CurrentClassValue);

                        float vol;
                        // vol = (float)minScoreInfo.MoneyManagement.GetVolume(null);
                        vol = 0.1F;
                        //vol = (float)Math.Round(minScore / -20000.0, 1);
                        //WekaUtils.DebugAssert(vol > 0);

                        int tp = (int)eval.numTruePositives(1);
                        int fp = (int)eval.numFalsePositives(1);

                        double minScore = minScoreInfo.Deals.NowScore;
                        //WekaUtils.Instance.WriteLog(string.Format("Best Classifier: N={0},TC={1},TP={2},FP={3},TD={4},TV={5},TTP={6},TFP={7},",
                        //        minScoreInfo.Name, minScoreInfo.Deals.NowScore.ToString(Parameters.DoubleFormatString),
                        //        minScoreInfo.Deals.NowTp, minScoreInfo.Deals.NowFp, minScoreInfo.Deals.NowDeal,
                        //        minScoreInfo.Deals.TotalVolume.ToString("N2"), tp, fp),
                        //    true, ConsoleColor.DarkGreen);

                        // Exclude
                        //if (TestParameters.EnableExcludeClassifier)
                        //{
                        //if (minScoreInfo.ExcludeClassifier == null)
                        //{
                        //    string modelFileName4Exclude = GetExcludeModelFileName(minScoreInfo.Name);
                        //    minScoreInfo.ExcludeClassifier = WekaUtils.TryLoadClassifier(modelFileName4Exclude);
                        //}
                        //}

                        for (int i = 0; i < minScoreInfo.CurrentTestRet.Length; i++)
                        {
                            if (minScoreInfo.CurrentTestRet[i] == 2)
                            {
                                if (minScoreInfo.ExcludeClassifier != null)
                                {
                                    double cv2 = minScoreInfo.ExcludeClassifier.classifyInstance(minTestInstancesNew.instance(i));
                                    if (cv2 != 2)
                                    {
                                        continue;
                                    }
                                }
                                candidateDeals.Add(new DealInfo(WekaUtils.GetDateValueFromInstances(minTestInstances, 0, i),
                                                                (float)WekaUtils.GetValueFromInstance(minTestInstances, "mainClose", i),
                                                                minScoreInfo.DealType,
                                                                (float)vol,
                                                                (float)(minScoreInfo.CurrentClassValue[i] == 2 ? -minScoreInfo.Tp : minScoreInfo.Sl),
                                                                WekaUtils.GetDateValueFromInstances(minTestInstances, 1, i)));
                            }
                        }
                        float nowCost = (float)eval.totalCost();
                        int   nowDeal = tp + fp;
                        totalCost += nowCost * vol;
                        totalDeal += nowDeal;

                        //float diff = Math.Abs(totalCost - realDealsInfo.TotalCost);
                        //WekaUtils.DebugAssert(diff < 5);
                        //WekaUtils.DebugAssert(Math.Abs(totalDeal - (realDealsInfo.NowDeal + realDealsInfo.CurrentDeal)) == 0);
                        //if (diff > 0.5)
                        //    totalCost = realDealsInfo.TotalCost;

                        IterateClassifierInfos((k, i, j, h) =>
                        {
                            if (m_classifierInfoIdxs[k, i, j, h] == minScoreInfo)
                            {
                                m_totalScores[k, i, j, h] += nowCost;
                                m_totalDeals[k, i, j, h]  += nowDeal;
                                return(false);
                            }
                            else
                            {
                                return(true);
                            }
                        });
                    }
                }


                int selectCount = 50;
                for (int i = 0; i < selectCount; ++i)
                {
                    if (candidateDeals.Count == 0)
                    {
                        break;
                    }

                    int selectedDealIdx = (int)Math.Round(m_randomGenerator.NextDouble() * candidateDeals.Count);
                    if (selectedDealIdx == candidateDeals.Count)
                    {
                        selectedDealIdx = candidateDeals.Count - 1;
                    }

                    m_realDealsInfo.AddDeal(candidateDeals[selectedDealIdx]);
                    candidateDeals.RemoveAt(selectedDealIdx);
                }
            }

            //if (m_enableDetailLogLevel2)
            //{
            //    IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            if (h == m_currentTestHour && m_totalScores[k, i, j, h] != 0)
            //            {
            //                WekaUtils.Instance.WriteLog(string.Format("Predict score for {0}: TC={1}, TD={2}", m_classifierInfoIdxs[k, i, j, h].Name, m_totalScores[k, i, j, h].ToString(Parameters.DoubleFormatString), m_totalDeals[k, i, j, h]));
            //            }
            //        });

            //}
            if (!string.IsNullOrEmpty(currentSummary))
            {
                WekaUtils.Instance.WriteLog(currentSummary, true, ConsoleColor.Red);
                System.Console.Title = nowDate.ToString(Parameters.DateTimeFormat) + ":" + currentSummary;
            }

            //if (m_enableDetailLogLevel2)
            //{
            //    if (TestParameters.EnablePerhourTrain)
            //    {
            //        double[] totalCostPerHour = new double[m_classifierInfoIdxs.GetLength(3)];
            //        int[] totalDealPerHour = new int[m_classifierInfoIdxs.GetLength(3)];
            //        IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            totalCostPerHour[h] += m_totalScores[k, i, j, h];
            //            totalDealPerHour[h] += m_totalDeals[k, i, j, h];
            //        });
            //        for (int i = 0; i < totalCostPerHour.Length; ++i)
            //        {
            //            if (totalCostPerHour[i] == 0)
            //                continue;

            //            WekaUtils.Instance.WriteLog(string.Format("Predict score Per hour of {0}: TC={1}, TD={2}", i, totalCostPerHour[i].ToString(Parameters.DoubleFormatString), totalDealPerHour[i]));
            //        }
            //    }

            //if ((m_testTimeStart.Month == 4 || m_testTimeStart.Month == 3) && m_testTimeStart.Day == 20)
            //{
            //    foreach (var cls in m_classifierInfos)
            //    {
            //        WekaUtils.Instance.WriteLog(cls.Value.Deals.PrintAll());
            //    }
            //}
            //if (m_currentTestHour == 0)
            //{
            //    var cls = m_classifierInfoIdxs[0, 0, 0, 0];
            //    WekaUtils.Instance.WriteLog((cls.Classifier as RandomClassifier).GetCountInfo());
            //}
            //}

            //if (m_saveDataFile)
            //{
            //    var files = System.IO.Directory.GetFiles(m_baseDir, "*.arff");
            //    if (files.Length > 100)
            //    {
            //        foreach (string fileName in files)
            //        {
            //            try
            //            {
            //                System.IO.File.Delete(fileName);
            //            }
            //            catch (Exception)
            //            {
            //            }
            //        }
            //    }
            //}
        }