|
| 1 | +using Accord.MachineLearning.Bayes; |
| 2 | +using Accord.Math; |
| 3 | +using Accord.Math.Random; |
| 4 | +using Accord.Statistics.Filters; |
| 5 | +using Accord.Statistics.Models.Regression.Linear; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace AI.MachineLearning |
| 13 | +{ |
| 14 | + #region Linear Regression |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Class providing support for linear regression ML algorithms |
| 18 | + /// </summary> |
| 19 | + public class LinearRegression |
| 20 | + { |
| 21 | + #region public properties |
| 22 | + // dataset |
| 23 | + public double[] inputs { get; private set; } |
| 24 | + public double[] outputs { get; private set; } |
| 25 | + public double testValue { get; private set; } |
| 26 | + |
| 27 | + // regression |
| 28 | + public SimpleLinearRegression regression { get; private set; } |
| 29 | + public OrdinaryLeastSquares ols; |
| 30 | + |
| 31 | + // state & result |
| 32 | + public bool learned { get; private set; } |
| 33 | + public double result { get; private set; } |
| 34 | + #endregion |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Constructs a new LinearRegression machine. |
| 38 | + /// </summary> |
| 39 | + public LinearRegression(List<double> inputList, List<double> outputList) |
| 40 | + { |
| 41 | + // validation |
| 42 | + if (inputList == null || outputList == null) throw new ArgumentNullException("Neither the input list nor the output list can be NULL"); |
| 43 | + |
| 44 | + // initialise seed value |
| 45 | + Generator.Seed = new Random().Next(); |
| 46 | + |
| 47 | + // process input and output lists into arrays |
| 48 | + inputs = inputList.ToArray(); |
| 49 | + outputs = outputList.ToArray(); |
| 50 | + |
| 51 | + // set up linear regression using OLS |
| 52 | + regression = new SimpleLinearRegression(); |
| 53 | + ols = new OrdinaryLeastSquares(); |
| 54 | + |
| 55 | + // nulls |
| 56 | + testValue = new double(); |
| 57 | + result = new double(); |
| 58 | + this.learned = false; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Use the object's inputs and outputs to learn the model of the linear regression, using OrdinaryLeastSquares |
| 63 | + /// </summary> |
| 64 | + public LinearRegression Learn() |
| 65 | + { |
| 66 | + regression = this.ols.Learn(inputs, outputs); |
| 67 | + learned = true; |
| 68 | + |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Using the learned model, predict an output for the specified input |
| 74 | + /// </summary> |
| 75 | + /// <param name="test">The value to use as input for the prediction</param> |
| 76 | + /// <returns>The predicted value</returns> |
| 77 | + public double Predict(double test) |
| 78 | + { |
| 79 | + // don't predict if we haven't learned the model yet |
| 80 | + if (this.learned != true) throw new Exception("Cannot predict before the machine has learned."); |
| 81 | + |
| 82 | + // check we haven't already predicted for this input |
| 83 | + if (test == this.testValue && this.learned == true) return this.result; |
| 84 | + |
| 85 | + // predict |
| 86 | + this.testValue = test; |
| 87 | + this.result = this.regression.Transform(this.testValue); |
| 88 | + |
| 89 | + return this.result; |
| 90 | + } |
| 91 | + } |
| 92 | + #endregion |
| 93 | + |
| 94 | + #region Classifiers |
| 95 | + /// <summary> |
| 96 | + /// Class providing support for Naive Bayes classification machines. |
| 97 | + /// </summary> |
| 98 | + public class NaiveBayes |
| 99 | + { |
| 100 | + #region public properties |
| 101 | + // dataset |
| 102 | + public string[][] dataset { get; private set; } |
| 103 | + public string[] columns { get; private set; } |
| 104 | + public string outputColumn { get; private set; } |
| 105 | + public int[][] inputs; |
| 106 | + public int[] outputs; |
| 107 | + |
| 108 | + // classifier |
| 109 | + public Accord.MachineLearning.Bayes.NaiveBayes classifier; |
| 110 | + public NaiveBayesLearning learner; |
| 111 | + public Codification codebook { get; private set; } |
| 112 | + |
| 113 | + // state & result |
| 114 | + public bool learned { get; private set; } |
| 115 | + public string[] testValue { get; private set; } |
| 116 | + public string result { get; private set; } |
| 117 | + public double[] probs { get; private set; } |
| 118 | + #endregion |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Constructs a new NaiveBayes classification machine. |
| 122 | + /// </summary> |
| 123 | + public NaiveBayes(string[][] data, List<string> columnList, string outputColumn) |
| 124 | + { |
| 125 | + // validation |
| 126 | + if (data == null || columnList == null || outputColumn==null) throw new ArgumentNullException("Neither the input list nor the column list can be NULL"); |
| 127 | + |
| 128 | + // initialise seed value |
| 129 | + Generator.Seed = new Random().Next(); |
| 130 | + |
| 131 | + // process input and output lists into arrays |
| 132 | + this.dataset = data; |
| 133 | + this.columns = columnList.ToArray(); |
| 134 | + this.outputColumn = outputColumn; |
| 135 | + |
| 136 | + // Create a new codification codebook to |
| 137 | + // convert strings into discrete symbols |
| 138 | + this.codebook = new Codification(columns, this.dataset); |
| 139 | + |
| 140 | + // Extract input and output pairs to train |
| 141 | + int[][] symbols = this.codebook.Transform(this.dataset); |
| 142 | + this.inputs = symbols.Get(null, 0, -1); // Gets all rows, from 0 to the last (but not the last) |
| 143 | + this.outputs = symbols.GetColumn(-1); // Gets only the last column |
| 144 | + |
| 145 | + // Create a new Naive Bayes learning |
| 146 | + this.learner = new NaiveBayesLearning(); |
| 147 | + |
| 148 | + // nulls |
| 149 | + testValue = null; |
| 150 | + result = null; |
| 151 | + probs = null; |
| 152 | + this.learned = false; |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Use the object's inputs and outputs to learn the model of the linear regression, using OrdinaryLeastSquares |
| 157 | + /// </summary> |
| 158 | + public NaiveBayes Learn() |
| 159 | + { |
| 160 | + this.classifier = this.learner.Learn(inputs, outputs); |
| 161 | + this.learned = true; |
| 162 | + |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Using the learned model, predict an output for the specified input |
| 168 | + /// </summary> |
| 169 | + /// <param name="test">The value to use as input for the prediction</param> |
| 170 | + /// <returns>The predicted value</returns> |
| 171 | + public string Predict(string[] test) |
| 172 | + { |
| 173 | + // don't predict if we haven't learned the model yet |
| 174 | + if (this.learned != true) throw new Exception("Cannot predict before the machine has learned."); |
| 175 | + |
| 176 | + // check we haven't already predicted for this input |
| 177 | + if (test == this.testValue && this.learned == true) return this.result; |
| 178 | + |
| 179 | + // predict |
| 180 | + // First encode the test instance |
| 181 | + int[] instance = this.codebook.Transform(test); |
| 182 | + |
| 183 | + // Let us obtain the numeric output that represents the answer |
| 184 | + int codeword = this.classifier.Decide(instance); |
| 185 | + |
| 186 | + // Now let us convert the numeric output to an actual answer |
| 187 | + this.result = this.codebook.Revert(this.outputColumn, codeword); |
| 188 | + |
| 189 | + // We can also extract the probabilities for each possible answer |
| 190 | + this.probs = this.classifier.Probabilities(instance); |
| 191 | + |
| 192 | + return this.result; |
| 193 | + } |
| 194 | + } |
| 195 | + #endregion |
| 196 | + |
| 197 | + #region Helpers |
| 198 | + |
| 199 | + #endregion |
| 200 | + |
| 201 | +} |
0 commit comments