public string Predict(List <double> marks) { try { if (marks.Count() < 10) { return("غير معلوم"); } if (Model == null) { if (!File.Exists("randomforest.xml")) { Learn("data.txt", 500, 266, 10); } Model = ClassificationForestModel.Load(() => new StreamReader(@"randomforest.xml")); } var lastTenMarks = marks.Skip(marks.Count - 10); F64Matrix observations = new F64Matrix(1, lastTenMarks.Count()); for (var j = 0; j < lastTenMarks.Count(); j++) { observations.At(0, j, lastTenMarks.ElementAt(j)); } var v = Model.Predict(observations).First(); Console.WriteLine("predict " + v); return(GetClass(v)); } catch (Exception e) { Console.Error.WriteLine(e.Message); return("غير معلوم"); } }
/// <summary> /// Converts a list of n LearningCurvePoint to a n by 3 matrix /// with columns SampleSize, TrainingScore and ValidationScore. /// </summary> /// <param name="points"></param> /// <returns></returns> public static F64Matrix ToF64Matrix(this List <LearningCurvePoint> points) { if (points.Count == 0) { throw new ArgumentException("There must be at least one element in the list to convert to a matrix"); } ; var matrix = new F64Matrix(points.Count, 3); for (int i = 0; i < points.Count; i++) { var point = points[i]; matrix.At(i, 0, point.SampleSize); matrix.At(i, 1, point.TrainingScore); matrix.At(i, 2, point.ValidationScore); } return(matrix); }
/// <summary> /// Transposes matrix. /// Output is saved in the provided matrix transposed. /// </summary> /// <param name="matrix"></param> /// <param name="transposed"></param> /// <returns></returns> public static void TransposeF64(F64Matrix matrix, F64Matrix transposed) { var cols = matrix.ColumnCount; var rows = matrix.RowCount; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transposed.At(j, i, matrix.At(i, j)); } } }
/// <summary> /// /// </summary> /// <param name="ensemblePredictions"></param> /// <param name="predictions"></param> public void Combine(F64Matrix ensemblePredictions, double[] predictions) { var cols = ensemblePredictions.ColumnCount; var rows = ensemblePredictions.RowCount; for (int i = 0; i < rows; i++) { var sum = 0.0; for (int j = 0; j < cols; j++) { sum += ensemblePredictions.At(i, j); } predictions[i] = sum / (double)cols; } }
static void Parse(string dataPath, F64Matrix observations, List <double> targets, F64Matrix testObservations, List <double> actualTargets, int FEATURES_COUNT, int LEARNING_ROWS_COUNT) { var lines = File.ReadAllLines(dataPath); int TOTAL_LINES = lines.Length; int i = 0; foreach (var line in lines) { var j = 0; var values = line.Trim().Split(' '); if (values.Length > FEATURES_COUNT) { foreach (var t in values) { if (j >= LEARNING_ROWS_COUNT) { actualTargets.Add(Normlize(Double.Parse(t))); } else { targets.Add(Normlize(Double.Parse(t))); } j++; } } else { foreach (var r in values) { if (i < LEARNING_ROWS_COUNT) { observations.At(i, j, Normlize(Double.Parse(r))); } else { testObservations.At(i - LEARNING_ROWS_COUNT, j, Normlize(Double.Parse(r))); } j++; } } i++; } }
/// <summary> /// Converts F64Matrix to float[][]. /// </summary> /// <param name="matrix"></param> /// <returns></returns> public static float[][] ToFloatJaggedArray(this F64Matrix matrix) { var rows = matrix.RowCount; var cols = matrix.ColumnCount; var jaggedArray = new float[rows][]; for (int row = 0; row < rows; row++) { var rowArray = new float[cols]; for (int col = 0; col < cols; col++) { rowArray[col] = (float)matrix.At(row, col); } jaggedArray[row] = rowArray; } return(jaggedArray); }
/// <summary> /// Converts F64Matrix to float[][]. /// </summary> /// <param name="matrix"></param> /// <param name="rowIndices"></param> /// <returns></returns> public static float[][] ToFloatJaggedArray(this F64Matrix matrix, int[] rowIndices) { var rows = rowIndices.Length; var cols = matrix.ColumnCount; var jaggedArray = new float[rows][]; for (int outputRow = 0; outputRow < rowIndices.Length; outputRow++) { var inputRow = rowIndices[outputRow]; var rowArray = new float[cols]; for (int col = 0; col < cols; col++) { rowArray[col] = (float)matrix.At(inputRow, col); } jaggedArray[outputRow] = rowArray; } return(jaggedArray); }
void FindBestSplit(F64Matrix observations, double[] residuals, double[] targets, double[] predictions, int[][] orderedElements, GBMTreeCreationItem parentItem, bool[] parentInSample, int featureIndex, ConcurrentBag <GBMSplitResult> results) { var bestSplit = new GBMSplit { Depth = parentItem.Depth, FeatureIndex = -1, SplitIndex = -1, SplitValue = -1, Cost = double.MaxValue, LeftConstant = -1, RightConstant = -1, SampleCount = parentItem.Values.Samples }; var bestLeft = GBMSplitInfo.NewEmpty(); var bestRight = GBMSplitInfo.NewEmpty(); var left = GBMSplitInfo.NewEmpty(); var right = parentItem.Values.Copy(NodePositionType.Right); var orderedIndices = orderedElements[featureIndex]; var j = NextAllowedIndex(0, orderedIndices, parentInSample); // No allowed sample or valid split left. if (j >= orderedIndices.Length || orderedIndices.Length == 1) { return; } var currentIndex = orderedIndices[j]; m_loss.UpdateSplitConstants(ref left, ref right, targets[currentIndex], residuals[currentIndex]); var previousValue = observations.At(currentIndex, featureIndex); while (right.Samples > 0) { j = NextAllowedIndex(j + 1, orderedIndices, parentInSample); currentIndex = orderedIndices[j]; var currentValue = observations.At(currentIndex, featureIndex); if (Math.Min(left.Samples, right.Samples) >= m_minimumSplitSize) { if (previousValue != currentValue) { var cost = left.Cost + right.Cost; if (cost < bestSplit.Cost) { bestSplit.FeatureIndex = featureIndex; bestSplit.SplitIndex = j; bestSplit.SplitValue = (previousValue + currentValue) * .5; bestSplit.LeftError = left.Cost; bestSplit.RightError = right.Cost; bestSplit.Cost = cost; bestSplit.CostImprovement = parentItem.Values.Cost - cost; bestSplit.LeftConstant = left.BestConstant; bestSplit.RightConstant = right.BestConstant; bestLeft = left.Copy(); bestRight = right.Copy(); } } } m_loss.UpdateSplitConstants(ref left, ref right, targets[currentIndex], residuals[currentIndex]); previousValue = currentValue; } if (bestSplit.FeatureIndex != -1) { results.Add(new GBMSplitResult { BestSplit = bestSplit, Left = bestLeft, Right = bestRight }); } }
/// <summary> /// Returns the agumented version of the data. Excluding the original. /// The each feature in the dataset must be scaled/normnalized between 0.0 and 1.0 /// before the method works. /// </summary> /// <param name="dataset"></param> /// <returns></returns> public F64Matrix Agument(F64Matrix dataset) { var orgCols = dataset.ColumnCount; var orgRows = dataset.RowCount; var augmentation = new F64Matrix(dataset.RowCount, dataset.ColumnCount); var indicesVisited = new HashSet <int>(); var sample = new double[orgCols]; var candidate = new double[orgCols]; indicesVisited.Clear(); for (int j = 0; j < orgRows; j++) { if (indicesVisited.Contains(j)) { continue; } dataset.Row(j, sample); var closestDistance = double.MaxValue; var closestIndex = -1; indicesVisited.Add(j); for (int f = 0; f < orgRows; f++) { if (indicesVisited.Contains(f)) { continue; } dataset.Row(f, candidate); var distance = GetDistance(sample, candidate); if (distance < closestDistance) { closestDistance = distance; closestIndex = f; } } if (closestIndex != -1) { dataset.Row(closestIndex, candidate); indicesVisited.Add(closestIndex); for (int h = 0; h < sample.Length; h++) { var sampleValue = sample[h]; var candiateValue = candidate[h]; if (m_random.NextDouble() <= m_probabilityParameter && m_probabilityParameter != 0.0) { var std = (sampleValue - candiateValue) / m_localVariance; augmentation.At(j, h, SampleRandom(candiateValue, std)); augmentation.At(closestIndex, h, SampleRandom(sampleValue, std)); } else // keep values { augmentation.At(j, h, sampleValue); augmentation.At(closestIndex, h, candiateValue); } } } } return(augmentation); }