public void StringApplyTest3() { // Example for https://github.com/accord-net/framework/issues/581 // Let's say we have a dataset of US birds: string[] names = { "State", "Bird", "Color" }; string[][] data = { new[] { "Kansas", "Crow", "Black" }, new[] { "Ohio", "Pardal", "Yellow" }, new[] { "Hawaii", "Penguim", "Black" } }; // Create a codebook for the dataset var codebook = new Codification(names, data); // Transform the data into integer symbols int[][] values = codebook.Transform(data); // Transform the symbols into 1-of-K vectors double[][] states = Jagged.OneHot(values.GetColumn(0)); double[][] birds = Jagged.OneHot(values.GetColumn(1)); double[][] colors = Jagged.OneHot(values.GetColumn(2)); // Normalize each variable separately if needed states = states.Divide(codebook["State"].NumberOfSymbols); birds = birds.Divide(codebook["Bird"].NumberOfSymbols); colors = colors.Divide(codebook["Color"].NumberOfSymbols); // Create final feature vectors double[][] features = Matrix.Concatenate(states, birds, colors); Assert.AreEqual(new[] { 3, 3 }, states.GetLength()); Assert.AreEqual(new[] { 3, 3 }, birds.GetLength()); Assert.AreEqual(new[] { 3, 2 }, colors.GetLength()); Assert.AreEqual(new[] { 3, 8 }, features.GetLength()); // string t = features.ToCSharp(); var expected = new double[][] { new double[] { 0.333333333333333, 0, 0, 0.333333333333333, 0, 0, 0.5, 0 }, new double[] { 0, 0.333333333333333, 0, 0, 0.333333333333333, 0, 0, 0.5 }, new double[] { 0, 0, 0.333333333333333, 0, 0, 0.333333333333333, 0.5, 0 } }; Assert.IsTrue(features.IsEqual(expected, rtol: 1e-10)); }
void Update() { if (GameOver.reset) // Resetando as variáveis caso o jogo seja reiniciado { answer = null; firstKeyCollected = null; secondKeyCollected = null; thirdKeyCollected = null; exit1.GetComponent <MeshRenderer> ().enabled = true; exit2.GetComponent <MeshRenderer> ().enabled = true; firstExitOpen = false; secondExitOpen = false; PlayerControl.score = 0; CollisionBehaviors.keyCollected = null; GameOver.reset = false; } if (firstKeyCollected != null && // Quando as três chaves forem coletadas, a condição será atendida secondKeyCollected != null && thirdKeyCollected != null) { int[] query = codebook.Transform(new[, ] { // Tabela cujo valor será tratado { "First key", firstKeyCollected }, { "Second key", secondKeyCollected }, { "Third key", thirdKeyCollected } }); // Passando a tabela a ser tratada como argumento da árvore treinada | tree.Decide(tabelaParaTratar) // O resultado tratado (predicted) será em integer symbol int predicted = tree.Decide(query); answer = codebook.Revert("Exit", predicted); // Traduzindo o integer symbol para string Debug.Log(answer); if (answer == "First") { exit1.GetComponent <MeshRenderer> ().enabled = false; firstExitOpen = true; } else if (answer == "Second") { exit2.GetComponent <MeshRenderer> ().enabled = false; secondExitOpen = true; } } }
public void testForInstance(NaiveBayes nb, String[] testInstance, String Output) { try { // Obtain the numeric output that represents the answer int c = nb.Decide(codebook.Transform(testInstance)); // answer will be 0 // Now let us convert the numeric output to an actual "Yes" or "No" answer string result = codebook.Revert("GeneratedByProgram", c); // answer will be "No" Console.WriteLine("Test Data Input : " + testInstance[0] + "," + testInstance[1] + "\nExpectation: " + Output + "\nResult: " + result); } catch (Exception e) { Console.WriteLine("Test Data Input : " + testInstance[0] + "," + testInstance[1] + "\nExpectation: " + Output + "\nResult: " + "No"); } }
public void Uczenie(string[] naglowki, string[][] dane) { Codification kody = new Codification(naglowki, dane); int[][] symbole = kody.Transform(dane); int[][] daneWejsciowe = symbole.Get(null, 0, -1); KolumnaWynikow = symbole.GetColumn(-1); RandomForestLearning nauczyciel = new RandomForestLearning() { SampleRatio = IloscDanychModelu }; RandomForest las = nauczyciel.Learn(daneWejsciowe, KolumnaWynikow); Rezultaty = las.Decide(daneWejsciowe); }
public string Evaluate(string branch, string cType, string gender, string payment) { // The tree can now be queried for new examples through // its decide method. For example, we can create a query int[] query = codebook.Transform(new[, ] { { "Branch", branch }, { "Customer type", cType }, { "Gender", gender }, { "Payment", payment } }); // And then predict the label using int predicted = tree.Decide(query); // result will be 0 // We can translate it back to strings using return(codebook.Revert("Product line", predicted)); // Answer will be: "No" }
public void TrainNaiveBayesClassifier(List <string[]> trainingData) { string[] columnNames = { "input", "output" }; var nodePairs = trainingData.ToArray(); var codebook = new Codification(columnNames, nodePairs); var symbols = codebook.Transform(nodePairs); var inputs = symbols.Get(null, 0, -1); var outputs = symbols.GetColumn(-1); // Create a new Naive Bayes learning var learner = new NaiveBayesLearning(); // Learn a Naive Bayes model from the examples var nb = learner.Learn(inputs, outputs); // Use the Serializer class to save model and codebook Serializer.Save(codebook, "thesaurus_codebook.accord"); Serializer.Save(nb, "thesaurus_bayes.accord"); }
public string Rules2String() { int count = dt.Rows.Count; int[][] inputs = new int [count][]; string[] labels = new string[count]; int num = 0; foreach (DataRow dr in dt.Rows) { int res = Convert.ToInt32(dr[30]); inputs[num] = new int[30]; for (int sensor_i = 0; sensor_i < 30; sensor_i++) { inputs[num][sensor_i] = Convert.ToInt32(dr[sensor_i]); } labels[num] = "class-" + res.ToString(); num++; } var codebook = new Codification("Output", labels); int[] outputs = codebook.Transform("Output", labels); DecisionVariable[] dv = new DecisionVariable[30]; for (int i = 0; i < 30; i++) { string name = "sensor_" + (i + 1).ToString(); dv[i] = new DecisionVariable(name, DecisionVariableKind.Continuous); } //use C45 Spanning tree algorithm var C45 = new C45Learning(dv); DecisionTree tree = C45.Learn(inputs, outputs); int[] predicted = tree.Decide(inputs); double error = new ZeroOneLoss(outputs).Loss(predicted); DecisionSet rules = tree.ToRules(); return(rules.ToString(codebook, "Output", System.Globalization.CultureInfo.InvariantCulture)); }
public string simulate(int year, string generation, string sex) { var id3learning = new ID3Learning() { new DecisionVariable("year", 2016 - 1985), new DecisionVariable("generation", 6), new DecisionVariable("sex", 2), }; tree = id3learning.Learn(inputs, outputs); int[] query = codebook.Transform(new[, ] { { "year", year.ToString() }, { "generation", generation }, { "sex", sex }, }); int predicted = tree.Decide(query); string answer = codebook.Revert("risk", predicted); return(answer); }
private Vector2 Rank(string presidentLife, string towers, string meliants, string time, string playerMoney) { try { int[] query = codebook.Transform(new[, ] { { "LIFE", presidentLife }, { "TOWERS", towers }, { "MELIANTS", meliants }, { "TIME", time }, { "ENEMY_COINS", playerMoney }, }); int predicted = tree.Decide(query); string answer = codebook.Revert("POSITION", predicted); string[] splited = answer.Split('x'); return(new Vector2( float.Parse(splited[0], CultureInfo.InvariantCulture.NumberFormat), float.Parse(splited[1], CultureInfo.InvariantCulture.NumberFormat) )); } catch (Exception) { return(Vector2.zero); } }
private MulticlassSupportVectorLearning <Linear> _multiClassLearning; //index 2 #region Inicialization private int[] CreateCodification(string[] folders) { _outputCodeBook = new Codification(Constants.ColumnFolders, folders); return(_outputCodeBook.Transform(Constants.ColumnFolders, folders)); }
public void learn_doc2() { #region doc_learn_mitchell // In this example, we will be using the famous Play Tennis example by Tom Mitchell (1998). // In Mitchell's example, one would like to infer if a person would play tennis or not // based solely on four input variables. Those variables are all categorical, meaning that // there is no order between the possible values for the variable (i.e. there is no order // relationship between Sunny and Rain, one is not bigger nor smaller than the other, but are // just distinct). Moreover, the rows, or instances presented above represent days on which the // behavior of the person has been registered and annotated, pretty much building our set of // observation instances for learning: // Note: this example uses DataTables to represent the input data , but this is not required. DataTable data = new DataTable("Mitchell's Tennis Example"); data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // In order to try to learn a decision tree, we will first convert this problem to a more simpler // representation. Since all variables are categories, it does not matter if they are represented // as strings, or numbers, since both are just symbols for the event they represent. Since numbers // are more easily representable than text string, we will convert the problem to use a discrete // alphabet through the use of a Accord.Statistics.Filters.Codification codebook.</para> // A codebook effectively transforms any distinct possible value for a variable into an integer // symbol. For example, “Sunny” could as well be represented by the integer label 0, “Overcast” // by “1”, Rain by “2”, and the same goes by for the other variables. So:</para> // Create a new codification codebook to // convert strings into integer symbols var codebook = new Codification(data); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToJagged <int>("Outlook", "Temperature", "Humidity", "Wind"); int[] outputs = symbols.ToArray <int>("PlayTennis"); // For this task, in which we have only categorical variables, the simplest choice // to induce a decision tree is to use the ID3 algorithm by Quinlan. Let’s do it: // Create a teacher ID3 algorithm var id3learning = new ID3Learning() { // Now that we already have our learning input/ouput pairs, we should specify our // decision tree. We will be trying to build a tree to predict the last column, entitled // “PlayTennis”. For this, we will be using the “Outlook”, “Temperature”, “Humidity” and // “Wind” as predictors (variables which will we will use for our decision). Since those // are categorical, we must specify, at the moment of creation of our tree, the // characteristics of each of those variables. So: new DecisionVariable("Outlook", 3), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Temperature", 3), // 3 possible values (Hot, mild, cool) new DecisionVariable("Humidity", 2), // 2 possible values (High, normal) new DecisionVariable("Wind", 2) // 2 possible values (Weak, strong) // Note: It is also possible to create a DecisionVariable[] from a codebook: // DecisionVariable[] attributes = DecisionVariable.FromCodebook(codebook); }; // Learn the training instances! DecisionTree tree = id3learning.Learn(inputs, outputs); // Compute the training error when predicting training instances double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs)); // The tree can now be queried for new examples through // its decide method. For example, we can create a query int[] query = codebook.Transform(new[, ] { { "Outlook", "Sunny" }, { "Temperature", "Hot" }, { "Humidity", "High" }, { "Wind", "Strong" } }); // And then predict the label using int predicted = tree.Decide(query); // result will be 0 // We can translate it back to strings using string answer = codebook.Revert("PlayTennis", predicted); // Answer will be: "No" #endregion Assert.AreEqual(0, predicted); Assert.AreEqual("No", answer); Assert.AreEqual(0, error); }
public void learn_new_method() { #region doc_learn_1 // Suppose we have a data table relating the age of // a person and its categorical classification, as // in "child", "adult" or "elder". // The Codification filter is able to extract those // string labels and transform them into discrete // symbols, assigning integer labels to each of them // such as "child" = 0, "adult" = 1, and "elder" = 3. // Create the aforementioned sample table DataTable table = new DataTable("Sample data"); table.Columns.Add("Age", typeof(int)); table.Columns.Add("Label", typeof(string)); // age label table.Rows.Add(10, "child"); table.Rows.Add(07, "child"); table.Rows.Add(04, "child"); table.Rows.Add(21, "adult"); table.Rows.Add(27, "adult"); table.Rows.Add(12, "child"); table.Rows.Add(79, "elder"); table.Rows.Add(40, "adult"); table.Rows.Add(30, "adult"); // Now, let's say we need to translate those text labels // into integer symbols. Let's use a Codification filter: var codebook = new Codification(table); // After that, we can use the codebook to "translate" // the text labels into discrete symbols, such as: int a = codebook.Transform(columnName: "Label", value: "child"); // returns 0 int b = codebook.Transform(columnName: "Label", value: "adult"); // returns 1 int c = codebook.Transform(columnName: "Label", value: "elder"); // returns 2 // We can also do the reverse: string labela = codebook.Revert(columnName: "Label", codeword: 0); // returns "child" string labelb = codebook.Revert(columnName: "Label", codeword: 1); // returns "adult" string labelc = codebook.Revert(columnName: "Label", codeword: 2); // returns "elder" #endregion #region doc_learn_2 // We can also process an entire data table at once: DataTable result = codebook.Apply(table); // The resulting table can be transformed to jagged array: double[][] matrix = Matrix.ToJagged(result); // and the resulting matrix will be given by string str = matrix.ToCSharp(); #endregion // str == new double[][] // { // new double[] { 10, 0 }, // new double[] { 7, 0 }, // new double[] { 4, 0 }, // new double[] { 21, 1 }, // new double[] { 27, 1 }, // new double[] { 12, 0 }, // new double[] { 79, 2 }, // new double[] { 40, 1 }, // new double[] { 30, 1 } // }; #region doc_learn_3 // Now we will be able to feed this matrix to any machine learning // algorithm without having to worry about text labels in our data: // Use the first column as input variables, // and the second column as outputs classes // double[][] inputs = matrix.GetColumns(0); int[] outputs = matrix.GetColumn(1).ToInt32(); // Create a Multi-class learning algorithm for the machine var teacher = new MulticlassSupportVectorLearning <Linear>() { Learner = (p) => new SequentialMinimalOptimization <Linear>() { Complexity = 1 } }; // Run the learning algorithm var svm = teacher.Learn(inputs, outputs); // Compute the classification error (should be 0) double error = new ZeroOneLoss(outputs).Loss(svm.Decide(inputs)); // After we have learned the machine, we can use it to classify // new data points, and use the codebook to translate the machine // outputs to the original text labels: string result1 = codebook.Revert("Label", svm.Decide(new double[] { 10 })); // child string result2 = codebook.Revert("Label", svm.Decide(new double[] { 40 })); // adult string result3 = codebook.Revert("Label", svm.Decide(new double[] { 70 })); // elder #endregion Assert.AreEqual(0, a); Assert.AreEqual(1, b); Assert.AreEqual(2, c); Assert.AreEqual("child", labela); Assert.AreEqual("adult", labelb); Assert.AreEqual("elder", labelc); Assert.AreEqual("child", result1); Assert.AreEqual("adult", result2); Assert.AreEqual("elder", result3); }
public void learn_test() { // http://www.ats.ucla.edu/stat/stata/dae/mlogit.htm #region doc_learn_1 // This example downloads an example dataset from the web and learns a multinomial logistic // regression on it. However, please keep in mind that the Multinomial Logistic Regression // can also work without many of the elements that will be shown below, like the codebook, // DataTables, and a CsvReader. // Let's download an example dataset from the web to learn a multinomial logistic regression: CsvReader reader = CsvReader.FromUrl("https://raw.githubusercontent.com/rlowrance/re/master/hsbdemo.csv", hasHeaders: true); // Let's read the CSV into a DataTable. As mentioned above, this step // can help, but is not necessarily required for learning a the model: DataTable table = reader.ToTable(); // We will learn a MLR regression between the following input and output fields of this table: string[] inputNames = new[] { "write", "ses" }; string[] outputNames = new[] { "prog" }; // Now let's create a codification codebook to convert the string fields in the data // into integer symbols. This is required because the MLR model can only learn from // numeric data, so strings have to be transformed first. We can force a particular // interpretation for those columns if needed, as shown in the initializer below: var codification = new Codification() { { "write", CodificationVariable.Continuous }, { "ses", CodificationVariable.CategoricalWithBaseline, new[] { "low", "middle", "high" } }, { "prog", CodificationVariable.Categorical, new[] { "academic", "general" } }, }; // Learn the codification codification.Learn(table); // Now, transform symbols into a vector representation, growing the number of inputs: double[][] x = codification.Transform(table, inputNames, out inputNames).ToDouble(); double[][] y = codification.Transform(table, outputNames, out outputNames).ToDouble(); // Create a new Multinomial Logistic Regression Analysis: var analysis = new MultinomialLogisticRegressionAnalysis() { InputNames = inputNames, OutputNames = outputNames, }; // Learn the regression from the input and output pairs: MultinomialLogisticRegression regression = analysis.Learn(x, y); // Let's retrieve some information about what we just learned: int coefficients = analysis.Coefficients.Count; // should be 9 int numberOfInputs = analysis.NumberOfInputs; // should be 3 int numberOfOutputs = analysis.NumberOfOutputs; // should be 3 inputNames = analysis.InputNames; // should be "write", "ses: middle", "ses: high" outputNames = analysis.OutputNames; // should be "prog: academic", "prog: general", "prog: vocation" // The regression is best visualized when it is data-bound to a // Windows.Forms DataGridView or WPF DataGrid. You can get the // values for all different coefficients and discrete values: // DataGridBox.Show(regression.Coefficients); // uncomment this line // You can get the matrix of coefficients: double[][] coef = analysis.CoefficientValues; // Should be equal to: double[][] expectedCoef = new double[][] { new double[] { 2.85217775752471, -0.0579282723520426, -0.533293368378012, -1.16283850605289 }, new double[] { 5.21813357698422, -0.113601186660817, 0.291387041358367, -0.9826369387481 } }; // And their associated standard errors: double[][] stdErr = analysis.StandardErrors; // Should be equal to: double[][] expectedErr = new double[][] { new double[] { -2.02458003380033, -0.339533576505471, -1.164084923948, -0.520961533343425, 0.0556314901718 }, new double[] { -3.73971589217449, -1.47672790071382, -1.76795568348094, -0.495032307980058, 0.113563519656386 } }; // We can also get statistics and hypothesis tests: WaldTest[][] wald = analysis.WaldTests; // should all have p < 0.05 ChiSquareTest chiSquare = analysis.ChiSquare; // should be p=1.06300120956871E-08 double logLikelihood = analysis.LogLikelihood; // should be -179.98173272217591 // You can use the regression to predict the values: int[] pred = regression.Transform(x); // And get the accuracy of the prediction if needed: var cm = GeneralConfusionMatrix.Estimate(regression, x, y.ArgMax(dimension: 1)); double acc = cm.Accuracy; // should be 0.61 double kappa = cm.Kappa; // should be 0.2993487536492252 #endregion Assert.AreEqual(9, coefficients); Assert.AreEqual(3, numberOfInputs); Assert.AreEqual(3, numberOfOutputs); Assert.AreEqual(new[] { "write", "ses: middle", "ses: high" }, inputNames); Assert.AreEqual(new[] { "prog: academic", "prog: general", "prog: vocation" }, outputNames); Assert.AreEqual(0.61, acc, 1e-10); Assert.AreEqual(0.2993487536492252, kappa, 1e-10); Assert.AreEqual(1.06300120956871E-08, chiSquare.PValue, 1e-8); Assert.AreEqual(-179.98172637136295, logLikelihood, 1e-8); testmlr(analysis); }
//***First Attempt: This decision tree was looking for input values that were already existing from the creation of the tree //this does not comply with this situation as we need to input unknown values that can be predicted based on proximity to the //existing input values in the tree. //This tree requires a set amount of potential inputs which in this case is very high for each feature //TO DO: Research what tree can suit this situation(Look at iris example) public void DecisionTree() { // In this example, we will process the famous Fisher's Iris dataset in // which the task is to classify weather the features of an Iris flower // belongs to an Iris setosa, an Iris versicolor, or an Iris virginica: // // - https://en.wikipedia.org/wiki/Iris_flower_data_set // DataTable data = new DataTable("Mitchell's Tennis Example"); data.Columns.Add("Name", "Acousticness", "Danceability", "Energy", "Instrumentalness", "Liveness", "Loudness", "Speechiness", "Tempo", "Valence", "Target"); foreach (var item in TargetList) { data.Rows.Add(item.ToString()); } for (int i = 0; i < audioFeatures.Count; i++) { data.Rows[i]["Acousticness"] = audioFeatures[i].Acousticness; data.Rows[i]["Danceability"] = audioFeatures[i].Danceability; data.Rows[i]["Energy"] = audioFeatures[i].Energy; data.Rows[i]["Instrumentalness"] = audioFeatures[i].Instrumentalness; data.Rows[i]["Liveness"] = audioFeatures[i].Liveness; data.Rows[i]["Loudness"] = audioFeatures[i].Loudness; data.Rows[i]["Speechiness"] = audioFeatures[i].Speechiness; data.Rows[i]["Tempo"] = audioFeatures[i].Tempo; data.Rows[i]["Valence"] = audioFeatures[i].Valence; } for (int i = 0; i < TargetValues.Count; i++) { data.Rows[i]["Target"] = TargetValues[i].ToString(); } var codebook = new Codification(data); //// Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); #pragma warning disable CS0618 // Type or member is obsolete double[][] inputs = symbols.ToArray <double>("Acousticness", "Danceability", "Energy", "Instrumentalness", "Liveness", "Loudness", "Speechiness", "Tempo", "Valence"); #pragma warning restore CS0618 // Type or member is obsolete int[] outputs = symbols.ToArray <int>("Target"); // Create a teaching algorithm: var teacher = new C45Learning() { new DecisionVariable("Acousticness", DecisionVariableKind.Continuous), new DecisionVariable("Danceability", DecisionVariableKind.Continuous), new DecisionVariable("Energy", DecisionVariableKind.Continuous), new DecisionVariable("Instrumentalness", DecisionVariableKind.Continuous), new DecisionVariable("Liveness", DecisionVariableKind.Continuous), new DecisionVariable("Loudness", DecisionVariableKind.Continuous), new DecisionVariable("Speechiness", DecisionVariableKind.Continuous), new DecisionVariable("Tempo", DecisionVariableKind.Continuous), new DecisionVariable("Valence", DecisionVariableKind.Continuous) }; // Use the learning algorithm to induce a new tree: DecisionTree tree = teacher.Learn(inputs, outputs); // To get the estimated class labels, we can use //int[] predicted = tree.Decide(inputs); // The classification error (0.0266) can be computed as //double error = new ZeroOneLoss(outputs).Loss(predicted); int[] query = codebook.Transform(new[, ] { { "Valence", "0.37" }, { "Acousticness", "0.00187" }, { "Danceability", "0.808" }, { "Energy", "0.626" }, { "Instrumentalness", "0.159" }, { "Liveness", "0.376" }, { "Loudness", "-12.733" }, { "Speechiness", "0.168" }, { "Tempo", "123.99" } }); // And then predict the label using int predicted = tree.Decide(query); // result will be 0 // We can translate it back to strings using string answer = codebook.Revert("Target", predicted); // Answer will be: "No" Console.WriteLine(predicted); //// Moreover, we may decide to convert our tree to a set of rules: //DecisionSet rules = tree.ToRules(); //// And using the codebook, we can inspect the tree reasoning: //string ruleText = rules.ToString(codebook, "Output", // System.Globalization.CultureInfo.InvariantCulture); //var id3learning = new ID3Learning() //{ // new DecisionVariable("Acousticness", TargetList.Count), // new DecisionVariable("Danceability", TargetList.Count), // new DecisionVariable("Energy", TargetList.Count), // new DecisionVariable("Instrumentalness", TargetList.Count), // new DecisionVariable("Liveness", TargetList.Count), // new DecisionVariable("Loudness", TargetList.Count), // new DecisionVariable("Speechiness", TargetList.Count), // new DecisionVariable("Tempo", TargetList.Count), // new DecisionVariable("Valence", TargetList.Count) //}; //// Learn the training instances! //DecisionTree tree = id3learning.Learn(inputs, outputs); //// Compute the training error when predicting training instances //double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs)); //int[] query = codebook.Transform(new[,] //{ // { "Acousticness", "0.011"}, // { "Danceability", "0.905"}, // { "Energy", "0.905"}, // { "Instrumentalness", "0.000905"}, // { "Liveness", "0.302"}, // { "Loudness", "-2.743"}, // { "Speechiness", "0.103"}, // { "Tempo", "114.944"}, // { "Valence", "0.625"} //}); //// And then predict the label using //int predicted = tree.Decide(query); // result will be 0 //// We can translate it back to strings using //string answer = codebook.Revert("Target", predicted); // Answer will be: "No" //Console.WriteLine(predicted); }
public ClassifierTitanic() { rawData = new DataTable("Titanic Data"); trainingData = new DataTable(); testingData = new DataTable(); string filedata = System.IO.File.ReadAllText("../titanicData.txt"); string[] dataColumns = System.IO.File.ReadAllText("../titanicColumns.txt").Split(','); //Input columns are to be learned from string[] inputColumns = new string[dataColumns.Length - 1]; Array.Copy(dataColumns, 0, inputColumns, 0, dataColumns.Length - 1); //Output is what we are trying to predict string outputColumn = dataColumns[dataColumns.Length - 1]; //Create an easy way to store and manipulate data rawData.Columns.Add(inputColumns); rawData.Columns.Add(outputColumn); trainingData.Columns.Add(inputColumns); trainingData.Columns.Add(outputColumn); testingData.Columns.Add(inputColumns); testingData.Columns.Add(outputColumn); string[] lines = filedata.Split( new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { rawData.Rows.Add(line.Split(',')); } //Clean up data representation and missing data rawData = cleanData(rawData); DataTable[] dt = splitDataForTraining(rawData, .8, inputColumns, outputColumn); trainingData = dt[0]; testingData = dt[1]; //--------- codebook = new Codification(trainingData); DataTable symbols = codebook.Apply(trainingData); int[][] inputs = symbols.ToJagged <int>("Pclass", "Title", "Sex", "Age", "SibSp", "Parch", "Fare", "Cabin", "Embarked"); int[] outputs = symbols.ToArray <int>("Survived"); // We can either specify the decision attributes we want // manually, or we can ask the codebook to do it for us: DecisionVariable[] attributes = DecisionVariable.FromCodebook(codebook, inputColumns); // Create a teaching algorithm: var teacher = new C45Learning(); teacher.Add(attributes[0]); teacher.Add(attributes[1]); teacher.Add(attributes[4]); teacher.Add(new DecisionVariable("Age", new DoubleRange(0, 99))); teacher.Add(new DecisionVariable("SibSp", new DoubleRange(0, 10))); teacher.Add(new DecisionVariable("Parch", new DoubleRange(0, 10))); teacher.Add(new DecisionVariable("Fare", new DoubleRange(0, 400))); teacher.Add(attributes[10]); teacher.Add(attributes[11]); // and induce a decision tree from the data: DecisionTree tree = teacher.Learn(inputs, outputs); // To get the estimated class labels, we can use int[] predicted = tree.Decide(inputs); // Moreover, we may decide to convert our tree to a set of rules: DecisionSet rules = tree.ToRules(); // And using the codebook, we can inspect the tree reasoning: string ruleText = rules.ToString(codebook, "Survived", System.Globalization.CultureInfo.InvariantCulture); foreach (DataRow d in testingData.Rows) { int[] tempVars = codebook.Transform(new[, ] { { "Pclass", d[0].ToString() }, { "Title", d[1].ToString() }, { "Sex", d[4].ToString() }, { "Cabin", d[10].ToString() }, { "Embarked", d[11].ToString() } }); int[] query = { tempVars[0], tempVars[1], tempVars[2], int.Parse(d[5].ToString()), int.Parse(d[6].ToString()), int.Parse(d[7].ToString()), int.Parse(d[9].ToString()), tempVars[3], tempVars[4] }; int predictedValue = tree.Decide(query); int actualValue = int.Parse(d[12].ToString()); if (predictedValue == actualValue) { if (actualValue == 1) { truePositives++; } else { trueNegatives++; } } else { if (actualValue == 1) { falseNegatives++; } else { falsePositives++; } } } var dasdfasd = 5; //// And the classification error (of 0.0) can be computed as //double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs)); //// To compute a decision for one of the input points, //// such as the 25-th example in the set, we can use //// //int y = tree.Decide(inputs[25]); // should be 1 //int[][] inputs = symbols.ToJagged<int>("???"); //int[] outputs = symbols.ToArray<int>("Survived"); //string[] decisionVariables = { "???" }; //DecisionVariable[] attributes = DecisionVariable.FromCodebook(codebook, decisionVariables); //// Create a teacher ID3 algorithm //var id3learning = new ID3Learning(attributes); //tree = id3learning.Learn(inputs, outputs); //// Compute the training error when predicting training instances //double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs)); }
static void Main(string[] args) { // Create a new reader, opening a given path ExcelReader excel = new ExcelReader("Intake Inf Cohort 2017 - Training Set.xlsx"); ExcelReader excelTest = new ExcelReader("Intake Inf Cohort 2017 - Test Set.xlsx"); // Afterwards, we can query the file for all // worksheets within the specified workbook: string[] sheets = excel.GetWorksheetList(); string[] sheetsTest = excelTest.GetWorksheetList(); // Finally, we can request an specific sheet: DataTable data = excel.GetWorksheet(sheets[0]); DataTable dataTest = excelTest.GetWorksheet(sheets[0]); // Loop through each column in data foreach (DataColumn column in data.Columns) { // Replace empty with underscore column.ColumnName = column.ColumnName.Replace(" ", "_"); } // Create a new codification codebook to // convert strings into integer symbols Codification codibook = new Codification(data); // Set codibook Codification = codibook; // Translate our training data into integer symbols using our codebook: DataTable symbols = codibook.Apply(data); int[][] inputs = symbols.ToJagged <int>( codibook.Columns[5].ColumnName, codibook.Columns[7].ColumnName, codibook.Columns[8].ColumnName, codibook.Columns[9].ColumnName, codibook.Columns[12].ColumnName, codibook.Columns[13].ColumnName, codibook.Columns[14].ColumnName, codibook.Columns[15].ColumnName, codibook.Columns[16].ColumnName, codibook.Columns[20].ColumnName, codibook.Columns[29].ColumnName, codibook.Columns[30].ColumnName, codibook.Columns[34].ColumnName ); int[] outputs = symbols.ToMatrix <int>(codibook.Columns[6].ColumnName).GetColumn(0); // Create a teacher ID3 algorithm var id3 = new ID3Learning() { new DecisionVariable(codibook.Columns[5].ColumnName, 2), new DecisionVariable(codibook.Columns[7].ColumnName, codibook.Columns[7].NumberOfSymbols), new DecisionVariable(codibook.Columns[8].ColumnName, codibook.Columns[8].NumberOfSymbols), new DecisionVariable(codibook.Columns[9].ColumnName, 3), new DecisionVariable(codibook.Columns[12].ColumnName, 10), new DecisionVariable(codibook.Columns[13].ColumnName, 10), new DecisionVariable(codibook.Columns[14].ColumnName, 10), new DecisionVariable(codibook.Columns[15].ColumnName, 10), new DecisionVariable(codibook.Columns[16].ColumnName, 2), new DecisionVariable(codibook.Columns[20].ColumnName, 2), new DecisionVariable(codibook.Columns[29].ColumnName, 2), new DecisionVariable(codibook.Columns[30].ColumnName, 2), new DecisionVariable(codibook.Columns[34].ColumnName, 2), }; // Learn the training instances! Accord.MachineLearning.DecisionTrees.DecisionTree tree = id3.Learn(inputs, outputs); // Create a console table for display ConsoleTable table = new ConsoleTable("Studentnumber", "Advice", "Conclusion"); // Loop through each row in data foreach (DataRow row in dataTest.Rows) { // The tree can now be queried for new examples through // its decide method. For example, we can create a query int[] query = null; try { query = codibook.Transform(new[, ] { { codibook.Columns[5].ColumnName, row.ItemArray[5].ToString() }, { codibook.Columns[7].ColumnName, row.ItemArray[7].ToString() }, { codibook.Columns[8].ColumnName, row.ItemArray[8].ToString() }, { codibook.Columns[9].ColumnName, row.ItemArray[9].ToString() }, { codibook.Columns[12].ColumnName, row.ItemArray[12].ToString() }, { codibook.Columns[13].ColumnName, row.ItemArray[13].ToString() }, { codibook.Columns[14].ColumnName, row.ItemArray[14].ToString() }, { codibook.Columns[15].ColumnName, row.ItemArray[15].ToString() }, { codibook.Columns[16].ColumnName, row.ItemArray[16].ToString() }, { codibook.Columns[20].ColumnName, row.ItemArray[20].ToString() }, { codibook.Columns[29].ColumnName, row.ItemArray[29].ToString() }, { codibook.Columns[30].ColumnName, row.ItemArray[30].ToString() }, { codibook.Columns[34].ColumnName, row.ItemArray[34].ToString() }, }); } catch (Exception) { // Show the result of skipped students var studentnumber = row.ItemArray[0].ToString(); var advice = row.ItemArray[6].ToString(); var conclusion = "(Twijfel)"; table.AddRow(studentnumber, advice, conclusion); continue; } // And then predict the label using int predicted = tree.Decide(query); // Any predictions off are ignored for consistency if (predicted != -1) { // We can translate it back to strings using string answer = codibook.Revert("advies", predicted); // Show the result in the output var studentnumber = row.ItemArray[0].ToString(); var advice = row.ItemArray[6].ToString(); var conclusion = answer; table.AddRow(studentnumber, advice, conclusion); } else { // Show the result of skipped students var studentnumber = row.ItemArray[0].ToString(); var advice = row.ItemArray[6].ToString(); var conclusion = "(Twijfel)"; table.AddRow(studentnumber, advice, conclusion); } } // Write the table in console table.Write(); // Read Key Console.ReadKey(); }
private static void ReadStudentRecords() { var csv = new CsvReader(File.OpenText("IntakesTrainingSet.csv")); var records = csv.GetRecords <StudentInfo>().ToList(); //Convert data to table var data = new DataTable(); using (var reader = ObjectReader.Create(records)) { data.Load(reader); } // Loop through each column in data foreach (DataColumn column in data.Columns) { // Replace empty with underscore column.ColumnName = column.ColumnName.Replace(" ", "_"); } // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToJagged <int>( "was_aanwezig", //"gewogen_gemiddelde", "competenties", "capaciteiten", "intr_motivatie", "extr_motivatie", "is_mbo_deficient", "persoonlijk_bijspijker_advies", "Aanmelden_voor_verkort_opleidingstraject" ); int[] outputs = symbols.ToMatrix <int>("advies").GetColumn(0); var id3 = new ID3Learning() { new DecisionVariable("was_aanwezig", 2), //new DecisionVariable("gewogen_gemiddelde", codebook.Columns.First(c => c.ColumnName == "gewogen_gemiddelde").NumberOfSymbols), new DecisionVariable("competenties", 10), new DecisionVariable("capaciteiten", 10), new DecisionVariable("intr_motivatie", 10), new DecisionVariable("extr_motivatie", 10), new DecisionVariable("is_mbo_deficient", 2), new DecisionVariable("persoonlijk_bijspijker_advies", 2), new DecisionVariable("Aanmelden_voor_verkort_opleidingstraject", 2) }; DecisionTree tree = id3.Learn(inputs, outputs); //Now that we have a decision tree, load in the test set and test csv = new CsvReader(File.OpenText("IntakesTestSet.csv")); var testRecords = csv.GetRecords <StudentInfo>(); foreach (StudentInfo record in testRecords) { //Transform the values of the test set into the same internal values used in the training set int[] query = codebook.Transform(new[, ] { { "was_aanwezig", record.was_aanwezig }, //{ "gewogen_gemiddelde", record.gewogen_gemiddelde }, { "competenties", record.competenties }, { "capaciteiten", record.capaciteiten }, { "intr_motivatie", record.intr_motivatie }, { "extr_motivatie", record.extr_motivatie }, { "is_mbo_deficient", record.is_mbo_deficient }, { "persoonlijk_bijspijker_advies", record.persoonlijk_bijspijker_advies }, { "Aanmelden_voor_verkort_opleidingstraject", record.Aanmelden_voor_verkort_opleidingstraject }, } ); int predicted = tree.Decide(query); string answer; try { answer = codebook.Revert("advies", predicted); } catch (KeyNotFoundException) { Console.WriteLine($"Could not generate advice for student {record.studentnummer}"); continue; } Console.WriteLine($"Student {record.studentnummer}: {answer}"); } }
public void output_labels_test() { #region doc_learn_codification // Let's say we have the following data to be classified // into three possible classes. Those are the samples: // double[][] inputs = { // input output new double[] { 0, 1, 1, 0 }, // 0 new double[] { 0, 1, 0, 0 }, // 0 new double[] { 0, 0, 1, 0 }, // 0 new double[] { 0, 1, 1, 0 }, // 0 new double[] { 0, 1, 0, 0 }, // 0 new double[] { 1, 0, 0, 0 }, // 1 new double[] { 1, 0, 0, 0 }, // 1 new double[] { 1, 0, 0, 1 }, // 1 new double[] { 0, 0, 0, 1 }, // 1 new double[] { 0, 0, 0, 1 }, // 1 new double[] { 1, 1, 1, 1 }, // 2 new double[] { 1, 0, 1, 1 }, // 2 new double[] { 1, 1, 0, 1 }, // 2 new double[] { 0, 1, 1, 1 }, // 2 new double[] { 1, 1, 1, 1 }, // 2 }; // Now, suppose that our class labels are not contiguous. We // have 3 classes, but they have the class labels 5, 1, and 8 // respectively. In this case, we can use a Codification filter // to obtain a contiguous zero-indexed labeling before learning int[] output_labels = { 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 8, 8, 8, 8, 8, }; // Create a codification object to obtain a output mapping var codebook = new Codification <int>().Learn(output_labels); // Transform the original labels using the codebook int[] outputs = codebook.Transform(output_labels); // Create the multi-class learning algorithm for the machine var teacher = new MulticlassSupportVectorLearning <Gaussian>() { // Configure the learning algorithm to use SMO to train the // underlying SVMs in each of the binary class subproblems. Learner = (param) => new SequentialMinimalOptimization <Gaussian>() { // Estimate a suitable guess for the Gaussian kernel's parameters. // This estimate can serve as a starting point for a grid search. UseKernelEstimation = true } }; // Configure parallel execution options teacher.ParallelOptions.MaxDegreeOfParallelism = 1; // Learn a machine var machine = teacher.Learn(inputs, outputs); // Obtain class predictions for each sample int[] predicted = machine.Decide(inputs); // Translate the integers back to the original lagbels int[] predicted_labels = codebook.Revert(predicted); #endregion Assert.IsTrue(predicted_labels.IsEqual(output_labels)); }
public List <Meal> ID3(dynamic food, List <User> users) { //dtable(); // Dictionary<string,List<Category>> FoodCategory = new Dictionary<string,List<Category>>(); List <Meal> category = new List <Meal> { }; foreach (User u in users) { BMI = u.bmi; User = u.user; } //Iterate through food and add each food with range of macronutrients to category list foreach (var nw in food) { var value = (nw.Category.Predicted).Split(","); category.Add(new Meal { food = nw.Food.Food, protein = value[0], carb = value[1], fat = value[2], calorie = value[3], fiber = value[4], nprotein = nw.Food.Total_Protein, ncarb = nw.Food.Total_Carb, nfat = nw.Food.Total_Fat, ncalorie = nw.Food.Num_Calorie, nfiber = nw.Food.Fiber, serving = nw.Food.Serving, img = nw.Food.img, fgroup = nw.Food.fgroup, PrefID = nw.Food.ID, servingQty = nw.Food.ServingQty.ToString(), decision = "" }); } var csv = new CsvReader(File.OpenText(path)); var myCustomObjects = csv.GetRecords <MealData>(); DataTable dt = new DataTable("FoodDBSample"); DataRow row; dt.Columns.Add("Category", "Carb", "Protein", "Fat", "Calorie", "Fiber", "Decision"); foreach (var record in myCustomObjects) { row = dt.NewRow(); row["Category"] = record.Category; row["Carb"] = record.Carb; row["Protein"] = record.Protein; row["Fat"] = record.Fat; row["Calorie"] = record.Calorie; row["Fiber"] = record.Fiber; row["Decision"] = record.Outcome; dt.Rows.Add(row); } var codebook = new Codification(dt); DataTable symbols = codebook.Apply(dt); int[][] inputs = symbols.ToJagged <int>("Category", "Carb", "Protein", "Fat", "Calorie", "Fiber"); int[] outputs = symbols.ToArray <int>("Decision"); //specify which columns to use for making decisions var id3learning = new ID3Learning() { new DecisionVariable("Category", 4), new DecisionVariable("Carb", 2), new DecisionVariable("Protein", 2), new DecisionVariable("Fat", 2), new DecisionVariable("Calorie", 2), new DecisionVariable("Fiber", 2) }; // Learn the training instances! DecisionTree tree = id3learning.Learn(inputs, outputs); // Compute the training error when predicting training instances double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs)); List <dynamic> predict = new List <dynamic> { }; //iterate through data structure category and make prediction for each food foreach (var item in category) { predict.Add(codebook.Transform(new[, ] { { "Category", $"{BMI}" }, { "Carb", $"{item.carb}" }, { "Protein", $"{item.protein}" }, { "Fat", $"{item.fat}" }, { "Calorie", $"{item.calorie}" }, { "Fiber", $"{item.fiber}" } })); } //Accord.IO.Serializer.Save(tree, Path.Combine(basePath, "ID3TreeModel.bin")); //int predicted = tree.Decide(query); List <string> q = new List <string>(); foreach (var i in predict) { q.Add(codebook.Revert("Decision", tree.Decide(i))); } // We can translate it back to strings using //string answer = codebook.Revert("Decision", predicted); //foreach (var item in q) //{ // System.Diagnostics.Debug.WriteLine(item); //} var foods = category.Zip(q, (n, w) => new { Food = n, Decision = w }); List <Meal> positive = new List <Meal> { }; List <Meal> negative = new List <Meal> { }; foreach (var nw in foods) { nw.Food.decision = nw.Decision; } foreach (var item in foods) { //System.Diagnostics.Debug.WriteLine(item.Food.food + "\n" + item.Food.nprotein + "\n" + item.Food.ncarb + "\n" + item.Food.nfat + "\n" + item.Food.ncalorie + "\n" + item.Food.serving + "\n" + item.Food.img + "\n" + item.Food.fgroup+"\n"+item.Decision); //System.Diagnostics.Debug.WriteLine(item.Food.food+"\n"+item.Decision); if (item.Decision.Equals("positive")) { positive.Add(new Meal { CurrentUser = User, food = item.Food.food, nprotein = item.Food.nprotein, ncarb = item.Food.ncarb, nfat = item.Food.nfat, ncalorie = item.Food.ncalorie, nfiber = item.Food.nfiber, img = item.Food.img, serving = item.Food.serving, fgroup = item.Food.fgroup, PrefID = item.Food.PrefID, servingQty = item.Food.servingQty, decision = item.Decision }); } if (item.Decision.Equals("negative")) { negative.Add(new Meal { CurrentUser = User, food = item.Food.food, nprotein = item.Food.nprotein, ncarb = item.Food.ncarb, nfat = item.Food.nfat, ncalorie = item.Food.ncalorie, nfiber = item.Food.nfiber, img = item.Food.img, serving = item.Food.serving, fgroup = item.Food.fgroup, PrefID = item.Food.PrefID, servingQty = item.Food.servingQty, decision = item.Decision }); } } //foreach (var item in positive) //{ // //System.Diagnostics.Debug.WriteLine(item.food + "\n" + item.nprotein + "\n" + item.ncarb + "\n" + item.nfat + "\n" + item.ncalorie + "\n" + item.serving + "\n" + item.img + "\n" + item.fgroup + "\n" + item.decision); // System.Diagnostics.Debug.WriteLine(item.food + "\n" + "Protein: "+item.nprotein + "\n" + "Carb: " + item.ncarb + "\n" + "Fat: " + item.nfat + "\n" + "Calorie: " + item.ncalorie+"\n"+ "Fiber: "+item.nfiber+"\n"+ "Outcome: " + item.decision); //} //foreach (var item in negative) //{ // //System.Diagnostics.Debug.WriteLine(item.food + "\n" + item.nprotein + "\n" + item.ncarb + "\n" + item.nfat + "\n" + item.ncalorie + "\n" + item.serving + "\n" + item.img + "\n" + item.fgroup + "\n" + item.decision); // System.Diagnostics.Debug.WriteLine(item.food + "\n" + "Protein: " + item.nprotein + "\n" + "Carb: " + item.ncarb + "\n" + "Fat: " + item.nfat + "\n" + "Calorie: " + item.ncalorie + "\n" + "Fiber: " + item.nfiber + "\n" + "Outcome: " + item.decision); //} // Validation purposes only //var cm = GeneralConfusionMatrix.Estimate(tree, inputs, outputs); //double err = cm.Error; //double acc = cm.Accuracy; //double kappa = cm.Kappa; //validation(); //System.Diagnostics.Debug.WriteLine("error: " + err); //System.Diagnostics.Debug.WriteLine("accuracy: " + acc); DisplayDiet diet = new DisplayDiet(); diet.GetFoods(positive); diet.GetUser(users); //diet.CreateDiet(positive); return(positive); }
private void button_Click(object sender, RoutedEventArgs e) { //Decided against the Iris set which is in Accord //var iris = new Iris(); //double[][] inputs = iris.Instances; //int[] outputs = iris.ClassLabels; string[][] data = DataSet.CustomIris.iris_values.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Apply(x => x.Split(',')); //features double[][] inputs = data.GetColumns(0, 1, 2, 3).To <double[][]>(); //labels string[] labels = data.GetColumn(4); //Codebook translates any input into usable (integers) for the tree //var codebook = new Codification(outputs, inputs); var cb = new Codification("Output", labels); int[] outputs = cb.Transform("Output", labels); DecisionVariable[] features = { new DecisionVariable("sepal length", DecisionVariableKind.Continuous), new DecisionVariable("sepal width", DecisionVariableKind.Continuous), new DecisionVariable("petal length", DecisionVariableKind.Continuous), new DecisionVariable("petal width", DecisionVariableKind.Continuous), }; var decisionTree = new DecisionTree(inputs: features, classes: 3); var c45learner = new C45Learning(decisionTree); c45learner.Learn(inputs, outputs); int[] estimated = decisionTree.Decide(inputs); double error = new ZeroOneLoss(outputs).Loss(decisionTree.Decide(inputs)); //Why rules? DecisionSet decisionSet = decisionTree.ToRules(); string ruleText = decisionSet.ToString(cb, "Output", System.Globalization.CultureInfo.InvariantCulture); //var tree = new DecisionTree(inputs: features, classes: 3); #region UI //Set ouput to UI tb_output.Text = ruleText; //Calculate the flowers and input to UI -> TODO Bindings var setosaCount = 0; var versicolorCount = 0; var virginicaCount = 0; for (int i = 0; i < estimated.Length; i++) { if (estimated[i] == 0) { setosaCount++; } if (estimated[i] == 1) { versicolorCount++; } if (estimated[i] == 2) { virginicaCount++; } } tb_setosa.Text = setosaCount.ToString(); tb_versi.Text = versicolorCount.ToString(); tb_virgi.Text = virginicaCount.ToString(); #endregion UI }
public static void Run() { // In this example, we will be using the famous Play Tennis example by Tom Mitchell(1998). // In Mitchell's example, one would like to infer if a person would play tennis or not // based solely on four input variables. Those variables are all categorical, meaning that // there is no order between the possible values for the variable (i.e. there is no order // relationship between Sunny and Rain, one is not bigger nor smaller than the other, but are // just distinct). // Note: this example uses DataTables to represent the input data , but this is not required. var example = "Overtake"; Console.WriteLine(example); DataTable data = new DataTable(example); data.Columns.Add("Separation", typeof(String)); data.Columns.Add("Speed", typeof(String)); data.Columns.Add("OncomingSpeed", typeof(String)); data.Columns.Add("Result", typeof(String)); var shuffledInputs = GetInputs(100); for (int index = 0; index < shuffledInputs.Length; index++) { data.Rows.Add(shuffledInputs[index][0], shuffledInputs[index][1], shuffledInputs[index][2], shuffledInputs[index][3]); } // In order to try to learn a decision tree, we will first convert this problem to a more simpler // representation. Since all variables are categories, it does not matter if they are represented // as strings, or numbers, since both are just symbols for the event they represent. Since numbers // are more easily representable than text string, we will convert the problem to use a discrete // alphabet through the use of a Accord.Statistics.Filters.Codification codebook.</para> // A codebook effectively transforms any distinct possible value for a variable into an integer // symbol. For example, “Sunny” could as well be represented by the integer label 0, “Overcast” // by “1”, Rain by “2”, and the same goes by for the other variables. So:</para> // Create a new codification codebook to convert strings into integer symbols var codebook = new Codification(data); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToJagged <int>(new string[] { "Separation", "Speed", "OncomingSpeed", "Result" }); int[] outputs = symbols.ToArray <int>("Overtake"); string[] classNames = new string[] { "success", "fail" }; // For this task, in which we have only categorical variables, the simplest choice // to induce a decision tree is to use the ID3 algorithm by Quinlan. Let’s do it: // Create an ID3 algorithm var id3learning = new ID3Learning() { // Now that we already have our learning input/ouput pairs, we should specify our // decision tree. We will be trying to build a tree to predict the last column, entitled // “PlayTennis”. For this, we will be using the “Outlook”, “Temperature”, “Humidity” and // “Wind” as predictors (variables which will we will use for our decision). Since those // are categorical, we must specify, at the moment of creation of our tree, the // characteristics of each of those variables. So: new DecisionVariable("Separation", 150), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Speed", 150), // 3 possible values (Hot, mild, cool) new DecisionVariable("OncomingSpeed", 150), // 2 possible values (High, normal) new DecisionVariable("Result", 2) // 2 possible values (Weak, strong) }; // Learn the training instances! DecisionTree tree = id3learning.Learn(inputs, outputs); // The tree can now be queried for new examples through // its Decide method. For example, we can create a query int[] query = codebook.Transform(new[, ] { { "Separation", "150" }, { "Speed", "150" }, { "OncomingSpeed", "150" }, { "Result", "success" } }); // And then predict the label using int predicted = tree.Decide(query); var answer = codebook.Revert("Overtake", predicted); Console.WriteLine(""); Console.WriteLine(answer); double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs)); Console.WriteLine($"{error * 100:F10}"); DecisionSet rules = tree.ToRules(); var encodedRules = rules.ToString(); Console.WriteLine(encodedRules); Console.ReadKey(); // Keep the window open till a key is pressed }
static void Main(string[] args) { data = new DataTable("What should I do today?"); data.Columns.Add("Weather"); data.Columns.Add("Time of the day"); data.Columns.Add("Homework"); data.Columns.Add("Bored"); data.Columns.Add("Activity"); using (GenericParser parser = new GenericParser()) { parser.SetDataSource("C:\\Users\\Marija\\source\\repos\\IntSys2\\WhatToDo4.csv"); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; while (parser.Read()) { data.Rows.Add(parser[0].ToString(), parser[1].ToString(), parser[2].ToString(), parser[3].ToString(), parser[4].ToString()); } } codebook = new Codification(data); DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToArray <int>(data.Columns[0].ColumnName, data.Columns[1].ColumnName, data.Columns[2].ColumnName, data.Columns[3].ColumnName); int[] outputs = symbols.ToArray <int>(data.Columns[4].ColumnName); var id3learning = new ID3Learning() { new DecisionVariable(data.Columns[0].ColumnName, 3), new DecisionVariable(data.Columns[1].ColumnName, 2), new DecisionVariable(data.Columns[2].ColumnName, 2), new DecisionVariable(data.Columns[3].ColumnName, 2) }; DecisionTree id3Tree = id3learning.Learn(inputs, outputs); PrintTree(id3Tree.Root, "", id3Tree.Root.IsLeaf); int[] query = codebook.Transform(new string[, ] { { data.Columns[0].ColumnName, "Rainy" }, { data.Columns[1].ColumnName, "Daytime" }, { data.Columns[2].ColumnName, "No homework" }, { data.Columns[3].ColumnName, "Bored" } }); int predicted = id3Tree.Decide(query); string answer = codebook.Revert(data.Columns[4].ColumnName, predicted); Console.WriteLine("-------------------------"); Console.WriteLine("Id3 resenje: " + answer); Console.WriteLine("-------------------------"); //var c45learning = new C45Learning() //{ // new DecisionVariable(data.Columns[0].ColumnName, 3), // new DecisionVariable(data.Columns[1].ColumnName, 2), // new DecisionVariable(data.Columns[2].ColumnName, 2), // new DecisionVariable(data.Columns[3].ColumnName, 2) //}; //DecisionTree c45Tree = c45learning.Learn(inputs, outputs); //PrintTree(c45Tree.Root, "", c45Tree.Root.IsLeaf); //int c45predicted = c45Tree.Decide(query); //answer = codebook.Revert(data.Columns[4].ColumnName, c45predicted); //Console.WriteLine("-------------------------"); //Console.WriteLine("C45 resenje: " + answer); //Console.WriteLine("-------------------------"); Console.ReadLine(); }
public async Task EvaluateAnswer(long answerId) { var answer = await _context.Answers.FirstOrDefaultAsync(x => x.AnswerId == answerId); if (answer == null) { throw new Exception("Answer not found"); } var data = new DataTable("Define the disease"); var questions = (await GetQuestions()).ToArray(); var questionsLength = questions.Length; var answers = await GetAnswers(); foreach (var question in questions) { data.Columns.Add(new DataColumn(question.Text, typeof(string))); } data.Columns.Add(new DataColumn("Age category", typeof(byte))); data.Columns.Add(new DataColumn("Gender", typeof(bool))); data.Columns.Add(new DataColumn("Diagnosed Disease", typeof(string))); foreach (var trainningAnswer in answers) { var answerArr = trainningAnswer.AnswerData.Split(';'); if (answerArr.Length != questionsLength) { throw new Exception("Answers count must be equals questions count"); } var patient = data.NewRow(); for (var i = 0; i < answerArr.Length; i++) { patient[questions[i].Text] = answerArr[i]; } var userResponse = await RequestExecutor.ExecuteRequestAsync( MicroservicesEnum.User, RequestUrl.GetPatientById, new Parameter[] { new Parameter("patientId", (int)trainningAnswer.PatientId.Value, ParameterType.GetOrPost) }); var patientData = JsonConvert.DeserializeObject <MksResponse>(userResponse); if (!patientData.Success) { throw new Exception(patientData.Data); } var patientCtx = JsonConvert.DeserializeObject <Patients>(patientData.Data); patient["Age category"] = (byte)new AgeLimit((byte)Math.Round((DateTime.UtcNow - patientCtx.DateBirth).TotalDays / 365.2425)).Limit; patient["Gender"] = patientCtx.Gender; var diseaseResponseName = await RequestExecutor.ExecuteRequestAsync( MicroservicesEnum.Medical, RequestUrl.GetDiseaseNameById, new Parameter[] { new Parameter("diseaseId", trainningAnswer.DeseaseId.Value, ParameterType.GetOrPost) }); var diseaseNameResponse = JsonConvert.DeserializeObject <MksResponse>(diseaseResponseName); if (!diseaseNameResponse.Success) { throw new Exception(diseaseNameResponse.Data); } patient["Diagnosed Disease"] = JsonConvert.DeserializeObject <string>(diseaseNameResponse.Data); data.Rows.Add(patient); } var codification = new Codification(data); var codifiedData = codification.Apply(data); int[][] input = codifiedData.ToJagged <int>(questions.Select(x => x.Text).ToArray()); int[] predictions = codifiedData.ToArray <int>("Diagnosed Disease"); var decisionTreeLearningAlgorithm = new ID3Learning { }; var decisionTree = decisionTreeLearningAlgorithm.Learn(input, predictions); var answerArray = answer.AnswerData.Split(';'); if (answerArray.Length != questionsLength) { throw new Exception("Answers count must be equals questions count"); } var inputValues = new string[questions.Length, 2]; for (var i = 0; i < answerArray.Length; i++) { inputValues[i, 0] = questions[i].Text; inputValues[i, 1] = answerArray[i]; } var query = codification.Transform(inputValues); var result = decisionTree.Decide(query); var diagnosis = codification.Revert("Diagnosed Disease", result); var diseaseIdResponse = await RequestExecutor.ExecuteRequestAsync( MicroservicesEnum.Medical, RequestUrl.GetDiseaseIdByName, new Parameter[] { new Parameter("name", diagnosis, ParameterType.GetOrPost) }); var diseaseResponseId = JsonConvert.DeserializeObject <MksResponse>(diseaseIdResponse); if (!diseaseResponseId.Success) { throw new Exception(diseaseResponseId.Data); } answer.DeseaseId = long.Parse(diseaseResponseId.Data); await _context.SaveChangesAsync(); }