private static void writeFile(weka.core.Instances features, string filename) { if(FILE_FORMAT == "csv") { using (TextWriter writer = new StreamWriter(filename)) { if(features.numInstances() == 0) return; for(int i = 0; i < features.numAttributes(); i++) { writer.Write(features.attribute(i)); if(i < features.numAttributes() - 1) writer.Write(DELIMITER); } writer.WriteLine(); for(int i = 0; i < features.numInstances(); i++) { for(int j = 0; j < features.numAttributes(); j++) { writer.Write(features.instance(i).value(j)); if(j < features.numAttributes() - 1) writer.Write(DELIMITER); } writer.WriteLine(); } } } else if (FILE_FORMAT == "arff") { using (TextWriter writer = new StreamWriter(filename)) { writer.WriteLine(features.toString()); } } }
public weka.core.Instance GenerateFeatures(PokerHand hand, int rIdx, int aIdx, weka.core.Instances data, bool generateClass = true) { // Check that we are using limit betting. Debug.Assert(hand.Context.BettingType == BettingType.FixedLimit); var results = new weka.core.Instance(data.numAttributes()); results.setDataset(data); int attIdx = 0; foreach (var method in typeof(LimitFeatureGenerator).GetMethods()) { // Get all the features of this class. var attributes = method.GetCustomAttributes(typeof(Feature), true); if (attributes.Length == 0) continue; // Get the feature attribute on this method. var attr = ((Feature)attributes[0]); // Get the name for this attribute string name = attr.Name; //Console.WriteLine("Hand: {0} Feature: {1}", hand.Context.ID, name); // Get the feature only if it's applicable to this situation. object feature = null; if(rIdx >= (int)attr.MinRound && rIdx <= (int)attr.MaxRound) feature = method.Invoke(this, new object[] { hand, rIdx, aIdx }); if (SkipMissingFeatures && (feature == null || feature.ToString() == "?")) continue; switch (attr.FType) { case FeatureType.Continuous: results.setValue(attIdx, (double)feature); break; case FeatureType.Discrete: results.setValue(attIdx, (int)feature); break; case FeatureType.Boolean: case FeatureType.Nominal: case FeatureType.Enum: { var attribute = data.attribute(attIdx); var attVal = attribute.indexOfValue(feature.ToString()); if(attVal < 0 || attVal > attribute.numValues()) throw new Exception(string.Format ("Invalid attribute value: {0} for attribute {1} (idx: {2} total values: {3}", feature.ToString(), name, attVal, attribute.numValues())); results.setValue(attribute, attVal); } break; case FeatureType.String: { var attribute = data.attribute(attIdx); results.setValue(attribute, feature.ToString()); } break; default: throw new Exception("Unspecified feature type for feature: " + method.Name); } attIdx++; } if(generateClass) { var classAttr = data.classAttribute(); switch (hand.Rounds[rIdx].Actions[aIdx].Type) { case ActionType.Bet: case ActionType.Raise: results.setClassValue(classAttr.indexOfValue("Raise")); break; case ActionType.Call: case ActionType.Check: results.setClassValue(classAttr.indexOfValue("Call")); break; case ActionType.Fold: results.setClassValue(classAttr.indexOfValue("Fold"));; break; default: break; } } return results; }
//public void SetTimeDays(DateTime trainStart, int trainDays, int testDays) //{ // SetTimeMinutesFromTrainStart(trainStart, trainDays * 60 * 24, testDays * 60 * 24); //} public void GenerateEaOrder(CandidateClassifier bestClassifierInfo, weka.core.Instances testInstances) { if (bestClassifierInfo == null) return; //int j = m_bestClassifierInfo.DealType == m_dealType[0] ? 0 : 1; string eaorderFileName = string.Format("{0}\\ea_order.txt", TestParameters.BaseDir); //Instances testInstances = bestClassifierInfo.CurrentTestInstances; //Instances testInstancesNew = null; //Instances testInstances = null; //if (m_saveDataFile) //{ // SetTraining(false); // string testFileName = GetArffFileName(m_dealType[j], m_newFileAppend); // WekaUtils.Instance.WriteLog("Load instance of " + testFileName); // testInstancesNew = WekaUtils.LoadInstances(testFileName); // testInstances = WekaUtils.LoadInstances(GetArffFileName(m_dealType[j])); //} //else //{ // testInstancesNew = m_testInstancesNew[j]; // testInstances = m_testInstances[j]; //} System.Data.DataTable dt = null; if (!testInstances.attribute(0).isDate()) { dt = Feng.Data.DbHelper.Instance.ExecuteDataTable(string.Format("SELECT Date FROM {0} WHERE Date >= '{1}' AND Date < '{2}' {4}", "EURUSD" + "_" + "M1", WekaData.m_testTimeStart.ToString(Parameters.DateTimeFormat), WekaData.m_testTimeEnd.ToString(Parameters.DateTimeFormat), "ORDER BY Time")); if (dt.Rows.Count != testInstances.numInstances()) { WekaUtils.Instance.WriteLog("different count!"); return; } } //else //{ // if (testInstancesNew.numInstances() != testInstances.numInstances()) // { // WekaUtils.Instance.WriteLog("different count!"); // return; // } //} // todo: save info in file //for (int j = 0; j < m_dealType.Length; ++j) //{ // if (m_saveDataFile) // { // string modelFileName = GetModelFileName(m_dealType[j]); // WriteLog("Load model of " + modelFileName); // //ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modelFileName)); // //Classifier cls = (Classifier)ois.readObject(); // //ois.close(); // m_bestClassifier[m_dealType[j]] = (Classifier)weka.core.SerializationHelper.read(modelFileName); // } //} //foreach (string testFileName in System.IO.Directory.GetFiles(m_baseDir, NormalizeFileName(string.Format("{0}_{1}_{2}_*.arff", m_symbol, dt1.ToString(m_dateTimeFormat), dt2.ToString(m_dateTimeFormat))))) //{ // string[] ss2 = System.IO.Path.GetFileNameWithoutExtension(testFileName).Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries); // if (ss2.Length != 5) // continue; // try // { // m_trainTimeStart = ConvertFileNameToDateTime(ss2[1]); // m_trainTimeEnd = ConvertFileNameToDateTime(ss2[2]); // m_testTimeStart = ConvertFileNameToDateTime(ss2[3]); // m_testTimeEnd = ConvertFileNameToDateTime(ss2[4]); // } // catch (Exception) // { // continue; // } double[] tr = bestClassifierInfo.CurrentTestRet; double[] cv = bestClassifierInfo.CurrentClassValue; using (StreamWriter sw = new StreamWriter(eaorderFileName, true)) { for (int i = 0; i < testInstances.numInstances(); i++) { string clsLabels; double v = tr[i]; if (v != double.NaN) { clsLabels = testInstances.classAttribute().value((int)v);// CalcAction(v); } else { clsLabels = "0"; } string clsLabel; if (clsLabels == "2") { clsLabel = bestClassifierInfo.DealType == 'B' ? "1" : "-1"; } else { clsLabel = "0"; } //if (clsLabels.Length >= 2) //{ // if (clsLabels[0] == "1" && clsLabels[1] == "0") // clsLabel = "1"; // else if (clsLabels[0] == "0" && clsLabels[1] == "1") // clsLabel = "-1"; // else if (clsLabels[0] == "1" && clsLabels[1] == "1") // clsLabel = "2"; //} //else if (clsLabels.Length == 1) //{ // if (clsLabels[0] == "1") // { // if (m_dealType[0] == "B") // clsLabel = "1"; // else if (m_dealType[0] == "S") // clsLabel = "-1"; // } //} //else //{ // throw new AssertException("invalid clsLabels.Length"); //} string date; string dateClose = string.Empty; if (testInstances.attribute(0).isDate()) { date = WekaUtils.GetDateValueFromInstances(testInstances, 0, i).ToString(Parameters.DateTimeFormat); dateClose = WekaUtils.GetDateValueFromInstances(testInstances, 1, i).ToString(Parameters.DateTimeFormat); } else { date = ((DateTime)dt.Rows[i]["Date"]).ToString(Parameters.DateTimeFormat); } sw.WriteLine(string.Format("{0}, {1}, {2}, {3}, 0, 0, {4}, {5}", m_actions[clsLabel], date, bestClassifierInfo.Tp, bestClassifierInfo.Sl, dateClose, cv[i] == v ? "Right" : "Wrong")); //ret[ConvertFileNameToDateTime(date)] = clsLabel; } } //// save labeled data //BufferedWriter writer = new BufferedWriter(new FileWriter(string.Format("c:\\eurusd_m1_arff.result"))); //writer.write(labeled.toString()); //writer.newLine(); //writer.flush(); //writer.close(); }