public override double Forecast(double[] vector) { double result = 0; if (TrainedModel != null && vector != null) { Node[] node = new Node[vector.Length]; for (int i = 0; i < vector.Length; i++) { node[i] = new Node(); node[i].Index = i + 1; node[i].Value = vector[i]; } node = mRange.Transform(node); result = Prediction.Predict(TrainedModel, node); } return(result); }
/// <summary> /// Scales a problem using the provided range. This will not affect the parameter. /// </summary> /// <param name="prob">The problem to scale</param> /// <param name="range">The Range transform to use in scaling</param> /// <returns>The Scaled problem</returns> public static svm_problem Scale(this RangeTransform range, svm_problem prob) { svm_problem scaledProblem = new svm_problem() { l = prob.l, y = new double[prob.l], x = new svm_node[prob.l][] }; for (int i = 0; i < scaledProblem.l; i++) { scaledProblem.x[i] = new svm_node[prob.x[i].Length]; for (int j = 0; j < scaledProblem.x[i].Length; j++) { scaledProblem.x[i][j] = new svm_node() { index = prob.x[i][j].index, value = range.Transform(prob.x[i][j].value, prob.x[i][j].index) } } ; scaledProblem.y[i] = prob.y[i]; } return(scaledProblem); } }
void classify(object args) { Problem train = new Problem { X = _data.Select(o => new Node[] { new Node(1, o.Position.X), new Node(2, o.Position.Y) }).ToArray(), Y = _data.Select(o => o.Label).ToArray(), Count = _data.Count, MaxIndex = 2 }; Parameter param = args as Parameter; RangeTransform transform = RangeTransform.Compute(train); train = transform.Scale(train); Model model = Training.Train(train, param); int width = (int)plot.ActualWidth; int height = (int)plot.ActualHeight; byte[] pixels = new byte[width * height * 3]; int cWidth = (width >> SCALE) + 1; int cHeight = (height >> SCALE) + 1; int[,] labels = new int[cHeight, cWidth]; for (int r = 0, i = 0; r < cHeight; r++) { for (int c = 0; c < cWidth; c++, i++) { int rr = r << SCALE; int cc = c << SCALE; Node[] datum = new Node[] { new Node(1, cc), new Node(2, rr) }; datum = transform.Transform(datum); labels[r, c] = (int)model.Predict(datum); classifyPB.Dispatcher.Invoke(() => classifyPB.Value = (i * 100) / (cHeight * cWidth)); } } PixelFormat format = PixelFormats.Rgb24; for (int i = 0, r = 0; r < height; r++) { for (int c = 0; c < width; c++) { int label = labels[r >> SCALE, c >> SCALE]; Color color = _colors[label]; pixels[i++] = color.R; pixels[i++] = color.G; pixels[i++] = color.B; } } plot.Dispatcher.Invoke(() => { ImageBrush brush = new ImageBrush(BitmapSource.Create(width, height, 96, 96, format, null, pixels, width * 3)); brush.Stretch = Stretch.None; brush.AlignmentX = 0; brush.AlignmentY = 0; plot.Background = brush; }); classifyPB.Dispatcher.Invoke(() => classifyPB.Value = 0); }