Revize 66c3e0df
Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)
Server/ServerApp/Predictor/IPredictor.cs | ||
---|---|---|
13 | 13 |
|
14 | 14 |
void Fit(IEnumerable<ModelInput> trainInput); |
15 | 15 |
|
16 |
ModelOutput Predict(IEnumerable<ModelInput> input);
|
|
16 |
String[] Predict(IEnumerable<ModelInput> input);
|
|
17 | 17 |
} |
18 | 18 |
} |
Server/ServerApp/Predictor/ModelInput.cs | ||
---|---|---|
4 | 4 |
{ |
5 | 5 |
public class ModelInput |
6 | 6 |
{ |
7 |
[ColumnName("label"), LoadColumn(0)]
|
|
8 |
public string LabelCol { get; set; }
|
|
7 |
[ColumnName("Label"), LoadColumn(0)]
|
|
8 |
public string Label { get; set; } |
|
9 | 9 |
|
10 | 10 |
|
11 | 11 |
[ColumnName("temp"), LoadColumn(1)] |
12 |
public double Col1 { get; set; }
|
|
12 |
public float Temp { get; set; }
|
|
13 | 13 |
|
14 | 14 |
//[ColumnName("date"), LoadColumn(2)] |
15 | 15 |
//public string Col2 { get; set; } |
Server/ServerApp/Predictor/ModelOutput.cs | ||
---|---|---|
5 | 5 |
{ |
6 | 6 |
public class ModelOutput |
7 | 7 |
{ |
8 |
[ColumnName("PredictedLabel")]
|
|
8 |
[ColumnName("prediction")]
|
|
9 | 9 |
public String Prediction { get; set; } |
10 | 10 |
public float[] Score { get; set; } |
11 | 11 |
|
Server/ServerApp/Predictor/NaiveBayesClassifier.cs | ||
---|---|---|
24 | 24 |
public IEnumerable<ModelInput> ExtractModelInput(List<WeatherInfo> weatherInfos, List<ActivityInfo> activityInfos) |
25 | 25 |
{ |
26 | 26 |
return weatherInfos.Select(e => new ModelInput(){ |
27 |
Col1 = e.temp,
|
|
28 |
LabelCol = "Empty",
|
|
27 |
Temp = (float)e.temp,
|
|
28 |
Label = e.temp > 15.0 ? "Full" : "Empty",
|
|
29 | 29 |
}).ToList(); |
30 | 30 |
} |
31 | 31 |
|
32 | 32 |
public void Fit(IEnumerable<ModelInput> trainingData) |
33 | 33 |
{ |
34 | 34 |
IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainingData); |
35 |
var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey("label", "label")
|
|
36 |
.Append(mlContext.Transforms.CopyColumns("Features", "temp"))
|
|
37 |
.Append(mlContext.Transforms.NormalizeMinMax("Features", "Features")); |
|
35 |
var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label))
|
|
36 |
.Append(mlContext.Transforms.Concatenate("Features", new[] { "temp" })
|
|
37 |
.Append(mlContext.Transforms.NormalizeMinMax("Features", "Features")));
|
|
38 | 38 |
var trainer = mlContext.MulticlassClassification.Trainers.NaiveBayes(); |
39 |
var traininingPipeline = dataProcessPipeline.Append(trainer); |
|
39 |
var traininingPipeline = dataProcessPipeline.Append(trainer) |
|
40 |
.Append(mlContext.Transforms.Conversion.MapKeyToValue("prediction", "PredictedLabel")); |
|
40 | 41 |
|
41 | 42 |
this.model = traininingPipeline.Fit(trainingDataView); |
42 | 43 |
|
43 | 44 |
} |
44 | 45 |
|
45 |
public ModelOutput Predict(IEnumerable<ModelInput> input)
|
|
46 |
public String[] Predict(IEnumerable<ModelInput> input)
|
|
46 | 47 |
{ |
47 | 48 |
var data = mlContext.Data.LoadFromEnumerable(input); |
48 |
var prediction = model.Transform(data); |
|
49 |
var result = mlContext.Data.CreateEnumerable<ModelOutput>(prediction, reuseRowObject: false); |
|
50 |
return (ModelOutput)result; |
|
49 |
IDataView result = model.Transform(data); |
|
50 |
String[] prediction = result.GetColumn<String>("prediction").ToArray(); |
|
51 |
|
|
52 |
return prediction; |
|
51 | 53 |
} |
52 | 54 |
} |
53 | 55 |
} |
Server/ServerApp/Program.cs | ||
---|---|---|
16 | 16 |
NaiveBayesClassifier naiveBayesClassifier = new NaiveBayesClassifier(); |
17 | 17 |
IEnumerable<ModelInput> modelInput = naiveBayesClassifier.ExtractModelInput(p.weatherList, p.jisList); |
18 | 18 |
naiveBayesClassifier.Fit(modelInput); |
19 |
var testData = new ModelInput()
|
|
19 |
List<ModelInput> dataList = new List<ModelInput>()
|
|
20 | 20 |
{ |
21 |
Col1 = -40, |
|
21 |
new ModelInput() |
|
22 |
{ |
|
23 |
Temp = -40, |
|
24 |
} |
|
22 | 25 |
}; |
23 |
List<ModelInput> dataList = new List<ModelInput>(); |
|
24 |
dataList.Add(testData); |
|
25 | 26 |
var result = naiveBayesClassifier.Predict(dataList); |
26 | 27 |
|
27 |
Console.WriteLine($"Prediction: {result.Prediction}"); |
|
28 |
Console.WriteLine($"Predictions: "); |
|
29 |
foreach(var item in result) |
|
30 |
{ |
|
31 |
Console.WriteLine(item.ToString()); |
|
32 |
} |
|
28 | 33 |
|
29 | 34 |
Console.ReadLine(); |
30 | 35 |
} |
Server/ServerApp/ServerApp.csproj | ||
---|---|---|
45 | 45 |
<LangVersion>7.3</LangVersion> |
46 | 46 |
<ErrorReport>prompt</ErrorReport> |
47 | 47 |
<Prefer32Bit>true</Prefer32Bit> |
48 |
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
|
|
48 |
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
|
|
49 | 49 |
</PropertyGroup> |
50 | 50 |
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> |
51 | 51 |
<OutputPath>bin\x64\Release\</OutputPath> |
... | ... | |
57 | 57 |
<ErrorReport>prompt</ErrorReport> |
58 | 58 |
<Prefer32Bit>true</Prefer32Bit> |
59 | 59 |
</PropertyGroup> |
60 |
<PropertyGroup> |
|
61 |
<StartupObject>ServerApp.Program</StartupObject> |
|
62 |
</PropertyGroup> |
|
60 | 63 |
<ItemGroup> |
61 |
<Reference Include="Microsoft.ML, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
62 |
<HintPath>..\packages\Microsoft.ML.1.5.5\lib\netstandard2.0\Microsoft.ML.dll</HintPath> |
|
63 |
</Reference> |
|
64 |
<Reference Include="Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
65 |
<HintPath>..\packages\Microsoft.ML.1.5.5\lib\netstandard2.0\Microsoft.ML.Core.dll</HintPath> |
|
66 |
</Reference> |
|
67 |
<Reference Include="Microsoft.ML.CpuMath, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
68 |
<HintPath>..\packages\Microsoft.ML.CpuMath.1.5.5\lib\netstandard2.0\Microsoft.ML.CpuMath.dll</HintPath> |
|
69 |
</Reference> |
|
70 |
<Reference Include="Microsoft.ML.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
71 |
<HintPath>..\packages\Microsoft.ML.1.5.5\lib\netstandard2.0\Microsoft.ML.Data.dll</HintPath> |
|
72 |
</Reference> |
|
73 |
<Reference Include="Microsoft.ML.DataView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
74 |
<HintPath>..\packages\Microsoft.ML.DataView.1.5.5\lib\netstandard2.0\Microsoft.ML.DataView.dll</HintPath> |
|
75 |
</Reference> |
|
76 |
<Reference Include="Microsoft.ML.KMeansClustering, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
77 |
<HintPath>..\packages\Microsoft.ML.1.5.5\lib\netstandard2.0\Microsoft.ML.KMeansClustering.dll</HintPath> |
|
78 |
</Reference> |
|
79 |
<Reference Include="Microsoft.ML.PCA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
80 |
<HintPath>..\packages\Microsoft.ML.1.5.5\lib\netstandard2.0\Microsoft.ML.PCA.dll</HintPath> |
|
81 |
</Reference> |
|
82 |
<Reference Include="Microsoft.ML.StandardTrainers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
83 |
<HintPath>..\packages\Microsoft.ML.1.5.5\lib\netstandard2.0\Microsoft.ML.StandardTrainers.dll</HintPath> |
|
84 |
</Reference> |
|
85 |
<Reference Include="Microsoft.ML.Transforms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> |
|
86 |
<HintPath>..\packages\Microsoft.ML.1.5.5\lib\netstandard2.0\Microsoft.ML.Transforms.dll</HintPath> |
|
87 |
</Reference> |
|
88 |
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> |
|
89 |
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> |
|
90 |
</Reference> |
|
91 | 64 |
<Reference Include="System" /> |
92 | 65 |
<Reference Include="System.Core" /> |
93 | 66 |
<Reference Include="System.Xml.Linq" /> |
... | ... | |
175 | 148 |
</None> |
176 | 149 |
<None Include="packages.config" /> |
177 | 150 |
</ItemGroup> |
178 |
<ItemGroup /> |
|
151 |
<ItemGroup> |
|
152 |
<PackageReference Include="Microsoft.ML"> |
|
153 |
<Version>1.5.2</Version> |
|
154 |
</PackageReference> |
|
155 |
<PackageReference Include="System.Runtime.CompilerServices.Unsafe"> |
|
156 |
<Version>5.0.0</Version> |
|
157 |
</PackageReference> |
|
158 |
</ItemGroup> |
|
179 | 159 |
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
180 | 160 |
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> |
181 | 161 |
<PropertyGroup> |
Také k dispozici: Unified diff
re #8598
Update Server/ServerApp/ServerApp.csproj
fixed corrupted dependencies
working simple classification