@@ -9,7 +9,6 @@ using Microsoft.Data.DataView;
9
9
using Microsoft.ML.LightGBM;
10
10
11
11
12
-
13
12
namespace MyNamespace
14
13
{
15
14
class Program
@@ -41,30 +40,29 @@ namespace MyNamespace
41
40
// Data loading
42
41
IDataView trainingDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
43
42
path: TrainDataPath,
44
- hasHeader : true,
45
- separatorChar : ',',
46
- allowQuotedStrings : true,
47
- trimWhitespace : false ,
48
- supportSparse : true);
43
+ hasHeader: true,
44
+ separatorChar: ',',
45
+ allowQuotedStrings: true,
46
+ trimWhitespace: false,
47
+ supportSparse: true);
49
48
IDataView testDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
50
49
path: TestDataPath,
51
- hasHeader : true,
52
- separatorChar : ',',
53
- allowQuotedStrings : true,
54
- trimWhitespace : false ,
55
- supportSparse : true);
56
-
57
- // Common data process configuration with pipeline data transformations
50
+ hasHeader: true,
51
+ separatorChar: ',',
52
+ allowQuotedStrings: true,
53
+ trimWhitespace: false,
54
+ supportSparse: true);
58
55
59
- var dataProcessPipeline = mlContext.Transforms.Concatenate("Out",new []{"In"});
56
+ // Common data process configuration with pipeline data transformations
57
+ var dataProcessPipeline = mlContext.Transforms.Concatenate("Out", new[] { "In" });
60
58
61
59
// Set the training algorithm, then create and config the modelBuilder
62
- var trainer = mlContext.BinaryClassification.Trainers.LightGbm(new Options(){ NumLeaves=2, Booster= new Options.TreeBooster.Arguments(){}, LabelColumn= "Label",FeatureColumn= "Features"});
63
-
60
+ var trainer = mlContext.BinaryClassification.Trainers.LightGbm(new Options() { NumLeaves = 2, Booster = new Options.TreeBooster.Arguments() { }, LabelColumn = "Label", FeatureColumn = "Features" });
61
+ var trainingPipeline = dataProcessPipeline.Append(trainer);
64
62
65
63
// Train the model fitting to the DataSet
66
- var trainingPipeline = dataProcessPipeline.Append(trainer);
67
64
var trainedModel = trainingPipeline.Fit(trainingDataView);
65
+
68
66
// Evaluate the model and show accuracy stats
69
67
Console.WriteLine("===== Evaluating Model's accuracy with Test data =====");
70
68
var predictions = trainedModel.Transform(testDataView);
@@ -86,11 +84,11 @@ namespace MyNamespace
86
84
//Load data to test. Could be any test data. For demonstration purpose train data is used here.
87
85
IDataView trainingDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
88
86
path: TrainDataPath,
89
- hasHeader : true,
90
- separatorChar : ',',
91
- allowQuotedStrings : true,
92
- trimWhitespace : false ,
93
- supportSparse : true);
87
+ hasHeader: true,
88
+ separatorChar: ',',
89
+ allowQuotedStrings: true,
90
+ trimWhitespace: false,
91
+ supportSparse: true);
94
92
95
93
var sample = mlContext.CreateEnumerable<SampleObservation>(trainingDataView, false).First();
96
94
@@ -101,7 +99,7 @@ namespace MyNamespace
101
99
}
102
100
103
101
// Create prediction engine related to the loaded trained model
104
- var predEngine= trainedModel.CreatePredictionEngine<SampleObservation, SamplePrediction>(mlContext);
102
+ var predEngine = trainedModel.CreatePredictionEngine<SampleObservation, SamplePrediction>(mlContext);
105
103
106
104
//Score
107
105
var resultprediction = predEngine.Predict(sample);
@@ -115,29 +113,29 @@ namespace MyNamespace
115
113
116
114
public class SampleObservation
117
115
{
118
- [ColumnName("Label"), LoadColumn(0)]
119
- public bool Label{get; set;}
120
-
116
+ [ColumnName("Label"), LoadColumn(0)]
117
+ public bool Label { get; set; }
118
+
119
+
120
+ [ColumnName("col1"), LoadColumn(1)]
121
+ public float Col1 { get; set; }
122
+
123
+
124
+ [ColumnName("col2"), LoadColumn(0)]
125
+ public float Col2 { get; set; }
126
+
127
+
128
+ [ColumnName("col3"), LoadColumn(0)]
129
+ public string Col3 { get; set; }
121
130
122
- [ColumnName("col1"), LoadColumn(1)]
123
- public float Col1{get; set;}
124
-
125
131
126
- [ColumnName("col2"), LoadColumn(0)]
127
- public float Col2{get; set;}
128
-
132
+ [ColumnName("col4"), LoadColumn(0)]
133
+ public int Col4 { get; set; }
129
134
130
- [ColumnName("col3"), LoadColumn(0)]
131
- public string Col3{get; set;}
132
-
133
135
134
- [ColumnName("col4"), LoadColumn(0)]
135
- public int Col4{get; set;}
136
-
136
+ [ColumnName("col5"), LoadColumn(0)]
137
+ public uint Col5 { get; set; }
137
138
138
- [ColumnName("col5"), LoadColumn(0)]
139
- public uint Col5{get; set;}
140
-
141
139
142
140
}
143
141
0 commit comments