-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathRegressionPage.xaml.cs
More file actions
183 lines (154 loc) · 6.12 KB
/
RegressionPage.xaml.cs
File metadata and controls
183 lines (154 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Mvvm.Services;
using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Linq;
using Windows.UI.Xaml.Controls;
using XamlBrewer.Uwp.MachineLearningSample.Models;
using XamlBrewer.Uwp.MachineLearningSample.ViewModels;
namespace XamlBrewer.Uwp.MachineLearningSample
{
public sealed partial class RegressionPage : Page
{
public RegressionPage()
{
this.InitializeComponent();
this.DataContext = new RegressionPageViewModel();
Loaded += Page_Loaded;
RegressionTrainersCombo.ItemsSource = new[] { "Gam", "LightGbm", "Ols", "Sdca" };
RegressionTrainersCombo.SelectedIndex = 0;
}
private RegressionPageViewModel ViewModel => DataContext as RegressionPageViewModel;
private async void Page_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
DatasetBox.IsChecked = false;
SettingUpBox.IsChecked = false;
TrainingBox.IsChecked = false;
TestingBox.IsChecked = false;
PlottingBox.IsChecked = false;
RestartButton.IsEnabled = false;
DraftSlider.IsEnabled = false;
AgeSlider.IsEnabled = false;
WinsSlider.IsEnabled = false;
BoxSlider.IsEnabled = false;
BusyIndicator.Visibility = Windows.UI.Xaml.Visibility.Visible;
BusyIndicator.Resume();
// Prepare the input files
DatasetBox.IsChecked = true;
var trainingDataPath = await MlDotNet.FilePath(@"ms-appx:///Data/2017-18_NBA_salary.csv");
// Read training data
var trainingData = await ViewModel.Load(trainingDataPath);
// Configure data transformations.
SettingUpBox.IsChecked = true;
// Create and train the model
TrainingBox.IsChecked = true;
await ViewModel.BuildAndTrain(RegressionTrainersCombo.SelectedItem.ToString());
// Save the model.
await ViewModel.Save("regressionModel.zip");
// Visual evaluation of the model.
TestingBox.IsChecked = true;
var predictions = await ViewModel.PredictTrainingData();
var result = predictions.OrderBy((p) => p.Salary).ToList();
// Diagram
PlottingBox.IsChecked = true;
var foreground = OxyColors.SteelBlue;
var plotModel = new PlotModel
{
PlotAreaBorderThickness = new OxyThickness(1, 0, 0, 1),
PlotAreaBorderColor = foreground,
TextColor = foreground,
TitleColor = foreground,
SubtitleColor = foreground,
LegendPosition = LegendPosition.TopCenter,
LegendOrientation = LegendOrientation.Horizontal
};
var axisX = new LinearAxis
{
Position = AxisPosition.Bottom,
Title = "Test Data",
TextColor = foreground,
TicklineColor = foreground,
TitleColor = foreground
};
plotModel.Axes.Add(axisX);
var axisY = new LinearAxis
{
Title = "Salary",
TextColor = foreground,
TicklineColor = foreground,
TitleColor = foreground
};
plotModel.Axes.Add(axisY);
var realSeries = new ScatterSeries
{
Title = "Real",
MarkerType = MarkerType.Circle,
MarkerSize = 2,
MarkerFill = OxyColors.SteelBlue
};
plotModel.Series.Add(realSeries);
var predictedSeries = new ScatterSeries
{
Title = "Predicted",
MarkerType = MarkerType.Circle,
MarkerSize = 2,
MarkerFill = OxyColors.Firebrick
};
plotModel.Series.Add(predictedSeries);
for (int i = 0; i < result.Count; i++)
{
realSeries.Points.Add(new ScatterPoint(i, result[i].Salary));
predictedSeries.Points.Add(new ScatterPoint(i, result[i].Score));
}
// Just to put an entry in the Legend.
var singlePredictionSeries = new ScatterSeries
{
Title = "Single Prediction",
MarkerType = MarkerType.Circle,
MarkerSize = 2,
MarkerFill = OxyColors.Green
};
plotModel.Series.Add(singlePredictionSeries);
Diagram.Model = plotModel;
Slider_ValueChanged(this, null);
BusyIndicator.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
BusyIndicator.Pause();
RestartButton.IsEnabled = true;
DraftSlider.IsEnabled = true;
AgeSlider.IsEnabled = true;
WinsSlider.IsEnabled = true;
BoxSlider.IsEnabled = true;
}
private async void Slider_ValueChanged(object sender, Windows.UI.Xaml.Controls.Primitives.RangeBaseValueChangedEventArgs e)
{
if (DraftSlider == null || AgeSlider == null || WinsSlider == null || BoxSlider == null)
{
return;
}
// Predict
var result = await ViewModel.Predict(new RegressionData
{
NBA_DraftNumber = (float)DraftSlider.Value,
Age = (float)AgeSlider.Value,
Ws = (float)WinsSlider.Value,
Bmp = (float)BoxSlider.Value
});
var annotation = new LineAnnotation
{
X = 0,
Y = result.Score,
Type = LineAnnotationType.Horizontal,
Color = OxyColors.Green,
LineStyle = LineStyle.Solid
};
Diagram.Model.Annotations.Clear();
Diagram.Model.Annotations.Add(annotation);
Diagram.InvalidatePlot();
}
private void RegressionTrainersCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}