public static void TrainFromCSV(Policy thePolicy, string labelFeaturName, string csvAddress, int readLimit) { // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(csvAddress); //Read headers string[] headers = file.ReadLine().Split(','); double[] rewards = file.ReadLine().Split(',').Select(double.Parse).ToArray(); //Read data into feature vector string line; int counter = 0; while ((line = file.ReadLine()) != null) { //increment counter to use as import id counter++; //Read a line to a string array string[] dataobjects = line.Split(','); //Create a data vector from the headers and read data line DataVectorTraining dataVector = new DataVectorTraining(headers, dataobjects, rewards, labelFeaturName); //Submit to the reinforcement learner thePolicy.Learn(dataVector); //If limit reached, end early if (counter == readLimit) { break; } } file.Close(); }
public void Learn_xorOperation_SeeAssert(int iterations) { ///Why does this sometimes fail? There are no bad examples. string[] headers = new string[] { "bool1", "bool2", "xor" }; double[] importance = new double[] { 0, 0, 0 }; Policy thePolicy = new Policy(); #region Training Random rand = new Random(); for (int i = 0; i < iterations; i++) { bool bool1 = Convert.ToBoolean(rand.Next(0, 2)); bool bool2 = Convert.ToBoolean(rand.Next(0, 2)); object[] data = new object[] { bool1, bool2, (bool1 ^ bool2) }; DataVectorTraining dvt = new DataVectorTraining(headers, data, importance, "xor"); thePolicy.Learn(dvt); } #endregion string htmlTree = thePolicy.ToDecisionTree(new DecisionTree.TreeSettings() { ShowSubScores = true, ShowBlanks = true }).ToHtmlTree(); System.IO.File.WriteAllText("xor" + iterations + ".html", htmlTree); #region Assert var dataVectorTT = new DataVector( new string[] { "bool1", "bool2" }, new object[] { true, true }); var dataVectorFF = new DataVector( new string[] { "bool1", "bool2" }, new object[] { false, false }); var dataVectorTF = new DataVector( new string[] { "bool1", "bool2" }, new object[] { true, false }); var dataVectorFT = new DataVector( new string[] { "bool1", "bool2" }, new object[] { false, true }); Assert.False(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorTT))); Assert.False(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorTT))); Assert.False(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorFF))); Assert.False(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorFF))); Assert.True(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorTF))); Assert.True(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorTF))); Assert.True(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorFT))); Assert.True(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorFT))); #endregion }
public void Constructor_DataVector_OnlyQueries() { DataVectorTraining dvt = new DataVectorTraining( new string[] { "name1", "name2", "class" }, new object[] { 0.0, 0.5, "c" }, new double[] { 0.0, 0.0, 0.0 }, "class" ); State s = new State(dvt); }
public void Constructor_WrongClassName_Exception() { string[] featureNames = new string[] { "name1", "name2", "class" }; object[] featureValues = new object[] { 1.0, 2.0, 3.0 }; double[] importance = new double[] { -1.0, 0.0, 0.0 }; string labelFeatureName = "class2"; Assert.ThrowsAny <ArgumentException>(delegate { DataVectorTraining dvt = new DataVectorTraining(featureNames, featureValues, importance, labelFeatureName); }); }
public DataVectorTraining ReadLine(string labelFeatureName) { //Try to read a line string line = file.ReadLine(); if (line == null) { return(null); } //Read a line to a string array string[] dataobjects = line.Split(','); lineCounter++; //Build Datavector DataVectorTraining dvt = new DataVectorTraining(); foreach (var fieldNameKVP in fieldNames) { //Get feaure name and value string name = fieldNameKVP.Key; string value = dataobjects[fieldNameKVP.Value]; //Get importance value, if available double importance = 0; if (fieldImportances.ContainsKey(name)) { double.TryParse(dataobjects[fieldImportances[name]], out importance); } //Add to the datavector dvt.AddFeature(name, value, importance); //Track seen key-value pairs (expect the class label) if (name != labelFeatureName) { if (!fieldUniqueValues.ContainsKey(name)) { fieldUniqueValues.Add(name, new HashSet <object>()); } fieldUniqueValues[name].Add(value); } } //Set the label dvt.SetLabel(labelFeatureName, dvt[labelFeatureName].Value); return(dvt); }
public void Constructor_With2Inputs_2() { string[] featureNames = new string[] { "name1", "name2", "class" }; object[] featureValues = new object[] { 1.0, 2.0, 3.0 }; double[] importance = new double[] { -1.0, 0.0, 0.0 }; string labelFeatureName = "class"; DataVectorTraining dvt = new DataVectorTraining(featureNames, featureValues, importance, labelFeatureName); Assert.Equal(2, dvt.Features.Count); Assert.Equal(1.0, dvt["name1"].Value); Assert.Equal(-1.0, dvt["name1"].Importance); Assert.Equal(2.0, dvt["name2"].Value); Assert.Equal(0.0, dvt["name2"].Importance); Assert.Equal("class", dvt.Label.Name); Assert.Equal(3.0, dvt.Label.Value); }