Exemple #1
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);
                }
            }
        }
Exemple #2
0
        public string BuildPricePatternDeals(bool withPrice = false)
        {
            WekaUtils.Instance.WriteLog("Now BuildPricePatternDeals");

            var cp = TestParameters2.CandidateParameter;
            string resultFile = TestParameters.GetBaseFilePath(string.Format("IncrementTest_{0}_{1}_{2}.txt",
                cp.MainSymbol, "Price", cp.MainPeriod));
            if (File.Exists(resultFile))
                return string.Empty;

            TestParameters.EnablePerhourTrain = false;
            TestParameters.UseFilter = false;

            cp.DeleteUnusedIndicators();

            var dataDates = TestManager.GetDataDateRange();
            SetTrainTime(dataDates[0], dataDates[1]);
            WekaData wekaData = new WekaData(cp);

            string arffFileName = TestParameters.GetBaseFilePath(string.Format("{0}_{1}_{2}.arff",
                cp.MainSymbol, "Price", cp.MainPeriod));
            weka.core.Instances allInstances;
            if (!System.IO.File.Exists(arffFileName))
            {
                WekaData.GenerateArffTemplate(true, false, cp);

                wekaData.UseNullHp = true;
                wekaData.GenerateData(true, false);

                allInstances = wekaData.CurrentTrainInstances;

                var hps = HpData.Instance.GetHpSum(cp.MainSymbol, cp.MainPeriod);
                DateTime nextHpDate = DateTime.MinValue;
                weka.core.Instances newAllInstances = new weka.core.Instances(allInstances, 0);
                //java.util.LinkedList deleteList = new java.util.LinkedList();
                for (int i = 0; i < allInstances.numInstances(); ++i)
                {
                    DateTime nowDate = WekaUtils.GetDateValueFromInstances(allInstances, 0, i);

                    if (TestParameters2.RealTimeMode && i == allInstances.numInstances() - 1)
                    {
                        allInstances.instance(i).setClassValue(0);
                        allInstances.instance(i).setValue(1, WekaUtils.GetTimeFromDate(Parameters.MaxDate) * 1000);

                        newAllInstances.add(allInstances.instance(i));
                    }
                    else if (hps.ContainsKey(nowDate))
                    {
                        int selectedDeal = hps[nowDate].Item1;
                        // 此时还不知道hp,不能滤
                        //if (TxtTest.Filter(nowDate, selectedDeal))
                        //{
                        //    deleteList.Add(allInstances.instance(i));
                        //}
                        //else
                        {
                            allInstances.instance(i).setClassValue(hps[nowDate].Item1);
                            allInstances.instance(i).setValue(1, hps[nowDate].Item2 * 1000);
                        }

                        //if (nowDate < nextHpDate)
                        //{
                        //    deleteList.Add(allInstances.instance(i));
                        //}
                        //else
                        //{
                        //    DateTime hpDate = WekaUtils.GetDateTimeValueFromInstances(allInstances, 1, i);
                        //    nextHpDate = hpDate;
                        //}
                        newAllInstances.add(allInstances.instance(i));
                    }
                    else
                    {
                        //deleteList.Add(allInstances.instance(i));
                    }
                }
                //allInstances.removeAll(deleteList);
                allInstances = newAllInstances;
                WekaUtils.SaveInstances(allInstances, arffFileName);
            }
            else
            {
                allInstances = WekaUtils.LoadInstances(arffFileName);
            }
            //TestParameters.ClassifierType = typeof(weka.classifiers.lazy.IBk);
            //var cls = WekaUtils.CreateClassifier(0, 0);
            //TestParameters.ClassifierType = typeof(weka.classifiers.functions.LibSVM);

            //cls = WekaUtils.CreateClassifier(typeof(ProbClassifier));

            //GenerateHpDateSpan(allInstances);
            //return;

            int n = (int)(24 / TestParameters2.MainPeriodOfHour);
            n = TestParameters2.nPeriod;
            if (!withPrice)
            {
                return TestManager.IncrementTest(allInstances, () =>
                {
                    return WekaUtils.CreateClassifier(typeof(MyLibLinear), 0, 0, "-S 1 -C 1.0 -E 0.01 -B 1.0");
                    //return WekaUtils.CreateClassifier(typeof(weka.classifiers.lazy.IBk));
                    //return WekaUtils.CreateClassifier(typeof(ProbClassifier));
                    //return WekaUtils.CreateClassifier(typeof(InstancesProb));
                },
                "1,2,3,4,5,6,7,8", resultFile, n);
            }
            else
            {
                return TestManager.IncrementTest(allInstances, () =>
                {
                    return WekaUtils.CreateClassifier(typeof(MyLibLinear), 0, 0, "-S 1 -C 1.0 -E 0.01 -B 1.0");
                },
                "1,2,3,5,6,7,8", resultFile, n);
            }
        }
Exemple #3
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)
    {
        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 thisData = new weka.core.Instances(buffReader);             //source.getDataSet();
            if (thisData.classIndex() == -1)
            {
                thisData.setClassIndex(thisData.numAttributes() - 1);
            }

            weka.core.Instances thisUniqueData = new weka.core.Instances(thisData);
            if (thisUniqueData.classIndex() == -1)
            {
                thisUniqueData.setClassIndex(thisUniqueData.numAttributes() - 1);
            }
            thisUniqueData.delete();

            if (allUniqueData == null)
            {
                allUniqueData = new weka.core.Instances(thisData);
                if (allUniqueData.classIndex() == -1)
                {
                    allUniqueData.setClassIndex(allUniqueData.numAttributes() - 1);
                }
                allUniqueData.delete();
            }

            weka.core.InstanceComparator com = new weka.core.InstanceComparator(false);

            for (int i = 0; i < thisData.numInstances(); i++)
            {
                bool dup = false;
                for (int j = 0; j < allUniqueData.numInstances(); j++)
                {
                    if (com.compare(thisData.instance(i), allUniqueData.instance(j)) == 0)
                    {
                        Debug.Log("Duplicate found!");
                        dup = true;
                        break;
                    }
                }
                if (!dup)
                {
                    allUniqueData.add(thisData.instance(i));
                }
                else
                {
                    dupInstances++;
                }
            }

            for (int i = 0; i < thisData.numInstances(); i++)
            {
                bool dup = false;
                for (int j = 0; j < thisUniqueData.numInstances(); j++)
                {
                    if (com.compare(thisData.instance(i), thisUniqueData.instance(j)) == 0)
                    {
                        Debug.Log("Duplicate found!");
                        dup = true;
                        break;
                    }
                }
                if (!dup)
                {
                    thisUniqueData.add(thisData.instance(i));
                }
                else
                {
                    dupInstancesSamePlayer++;
                }
            }


            //Debug.Log("All Data Instance Count = " + thisData.numInstances());
            //Debug.Log("Unique Data Instance Count = " + thisUniqueData.numInstances());
            //Debug.Log("Done!");
        } catch (java.lang.Exception ex)
        {
            Debug.LogError(ex.getMessage());
        }
    }