Revize 9fc5fa93
Přidáno uživatelem Roman Kalivoda před více než 3 roky(ů)
Server/ServerApp.sln | ||
---|---|---|
8 | 8 |
Global |
9 | 9 |
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
10 | 10 |
Debug|Any CPU = Debug|Any CPU |
11 |
Debug|x64 = Debug|x64 |
|
11 | 12 |
Release|Any CPU = Release|Any CPU |
13 |
Release|x64 = Release|x64 |
|
12 | 14 |
EndGlobalSection |
13 | 15 |
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
14 | 16 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
15 | 17 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Debug|Any CPU.Build.0 = Debug|Any CPU |
18 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Debug|x64.ActiveCfg = Debug|x64 |
|
19 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Debug|x64.Build.0 = Debug|x64 |
|
16 | 20 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Release|Any CPU.ActiveCfg = Release|Any CPU |
17 | 21 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Release|Any CPU.Build.0 = Release|Any CPU |
22 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Release|x64.ActiveCfg = Release|x64 |
|
23 |
{B49B2821-751C-4032-8637-B4E9282DCF06}.Release|x64.Build.0 = Release|x64 |
|
18 | 24 |
EndGlobalSection |
19 | 25 |
GlobalSection(SolutionProperties) = preSolution |
20 | 26 |
HideSolutionNode = FALSE |
Server/ServerApp/Predictor/IPredictor.cs | ||
---|---|---|
11 | 11 |
interface IPredictor |
12 | 12 |
{ |
13 | 13 |
|
14 |
void Fit(ModelInput trainInput);
|
|
14 |
void Fit(IEnumerable<ModelInput> trainInput);
|
|
15 | 15 |
|
16 |
void Predict(ModelInput testInput);
|
|
16 |
ModelOutput Predict(IEnumerable<ModelInput> input);
|
|
17 | 17 |
} |
18 | 18 |
} |
Server/ServerApp/Predictor/ModelInput.cs | ||
---|---|---|
1 |
namespace ServerApp.Predictor |
|
1 |
using Microsoft.ML.Data; |
|
2 |
|
|
3 |
namespace ServerApp.Predictor |
|
2 | 4 |
{ |
3 | 5 |
public class ModelInput |
4 | 6 |
{ |
5 |
protected class DataPoint |
|
6 |
{ |
|
7 |
public uint Label { get; set; } |
|
7 |
[ColumnName("label"), LoadColumn(0)] |
|
8 |
public string LabelCol { get; set; } |
|
9 |
|
|
10 |
|
|
11 |
[ColumnName("temp"), LoadColumn(1)] |
|
12 |
public double Col1 { get; set; } |
|
13 |
|
|
14 |
//[ColumnName("date"), LoadColumn(2)] |
|
15 |
//public string Col2 { get; set; } |
|
8 | 16 |
|
9 |
public float[] Features { get; set; }
|
|
10 |
} |
|
17 |
//[ColumnName("time"), LoadColumn(3)]
|
|
18 |
//public string Col3 { get; set; }
|
|
11 | 19 |
} |
12 | 20 |
} |
Server/ServerApp/Predictor/ModelOutput.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using Microsoft.ML.Data; |
|
3 |
|
|
4 |
namespace ServerApp.Predictor |
|
5 |
{ |
|
6 |
public class ModelOutput |
|
7 |
{ |
|
8 |
[ColumnName("PredictedLabel")] |
|
9 |
public String Prediction { get; set; } |
|
10 |
public float[] Score { get; set; } |
|
11 |
|
|
12 |
} |
|
13 |
} |
Server/ServerApp/Predictor/NaiveBayesClassifier.cs | ||
---|---|---|
4 | 4 |
using System.Text; |
5 | 5 |
using System.Threading.Tasks; |
6 | 6 |
using Microsoft.ML; |
7 |
using Microsoft.ML.Data; |
|
8 |
using ServerApp.Parser.OutputInfo; |
|
7 | 9 |
|
8 | 10 |
namespace ServerApp.Predictor |
9 | 11 |
{ |
... | ... | |
11 | 13 |
{ |
12 | 14 |
private MLContext mlContext; |
13 | 15 |
|
16 |
private ITransformer model; |
|
17 |
|
|
14 | 18 |
public NaiveBayesClassifier() |
15 | 19 |
{ |
16 | 20 |
mlContext = new MLContext(); |
17 | 21 |
|
18 | 22 |
} |
19 | 23 |
|
20 |
public void Fit(ModelInput trainingData) |
|
24 |
public IEnumerable<ModelInput> ExtractModelInput(List<WeatherInfo> weatherInfos, List<ActivityInfo> activityInfos) |
|
25 |
{ |
|
26 |
return weatherInfos.Select(e => new ModelInput(){ |
|
27 |
Col1 = e.temp, |
|
28 |
LabelCol = "Empty", |
|
29 |
}).ToList(); |
|
30 |
} |
|
31 |
|
|
32 |
public void Fit(IEnumerable<ModelInput> trainingData) |
|
21 | 33 |
{ |
22 |
throw new NotImplementedException(); |
|
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")); |
|
38 |
var trainer = mlContext.MulticlassClassification.Trainers.NaiveBayes(); |
|
39 |
var traininingPipeline = dataProcessPipeline.Append(trainer); |
|
40 |
|
|
41 |
this.model = traininingPipeline.Fit(trainingDataView); |
|
42 |
|
|
23 | 43 |
} |
24 | 44 |
|
25 |
public void Predict(ModelInput testData)
|
|
45 |
public ModelOutput Predict(IEnumerable<ModelInput> input)
|
|
26 | 46 |
{ |
27 |
throw new NotImplementedException(); |
|
47 |
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; |
|
28 | 51 |
} |
29 | 52 |
} |
30 | 53 |
} |
Server/ServerApp/Program.cs | ||
---|---|---|
1 | 1 |
using ServerApp.Parser.Parsers; |
2 |
using ServerApp.Predictor; |
|
2 | 3 |
using System; |
4 |
using System.Collections.Generic; |
|
3 | 5 |
|
4 | 6 |
namespace ServerApp |
5 | 7 |
{ |
... | ... | |
11 | 13 |
|
12 | 14 |
p.Parse(); |
13 | 15 |
|
16 |
NaiveBayesClassifier naiveBayesClassifier = new NaiveBayesClassifier(); |
|
17 |
IEnumerable<ModelInput> modelInput = naiveBayesClassifier.ExtractModelInput(p.weatherList, p.jisList); |
|
18 |
naiveBayesClassifier.Fit(modelInput); |
|
19 |
var testData = new ModelInput() |
|
20 |
{ |
|
21 |
Col1 = -40, |
|
22 |
}; |
|
23 |
List<ModelInput> dataList = new List<ModelInput>(); |
|
24 |
dataList.Add(testData); |
|
25 |
var result = naiveBayesClassifier.Predict(dataList); |
|
26 |
|
|
27 |
Console.WriteLine($"Prediction: {result.Prediction}"); |
|
28 |
|
|
14 | 29 |
Console.ReadLine(); |
15 | 30 |
} |
16 | 31 |
} |
Server/ServerApp/ServerApp.csproj | ||
---|---|---|
1 | 1 |
<?xml version="1.0" encoding="utf-8"?> |
2 | 2 |
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
3 |
<Import Project="..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.props" Condition="Exists('..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.props')" /> |
|
4 |
<Import Project="..\packages\Microsoft.ML.CpuMath.1.5.5\build\netstandard2.0\Microsoft.ML.CpuMath.props" Condition="Exists('..\packages\Microsoft.ML.CpuMath.1.5.5\build\netstandard2.0\Microsoft.ML.CpuMath.props')" /> |
|
3 | 5 |
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> |
4 | 6 |
<PropertyGroup> |
5 | 7 |
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
... | ... | |
12 | 14 |
<FileAlignment>512</FileAlignment> |
13 | 15 |
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> |
14 | 16 |
<Deterministic>true</Deterministic> |
17 |
<NuGetPackageImportStamp> |
|
18 |
</NuGetPackageImportStamp> |
|
15 | 19 |
</PropertyGroup> |
16 | 20 |
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
17 | 21 |
<PlatformTarget>AnyCPU</PlatformTarget> |
... | ... | |
32 | 36 |
<ErrorReport>prompt</ErrorReport> |
33 | 37 |
<WarningLevel>4</WarningLevel> |
34 | 38 |
</PropertyGroup> |
39 |
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> |
|
40 |
<DebugSymbols>true</DebugSymbols> |
|
41 |
<OutputPath>bin\x64\Debug\</OutputPath> |
|
42 |
<DefineConstants>DEBUG;TRACE</DefineConstants> |
|
43 |
<DebugType>full</DebugType> |
|
44 |
<PlatformTarget>x64</PlatformTarget> |
|
45 |
<LangVersion>7.3</LangVersion> |
|
46 |
<ErrorReport>prompt</ErrorReport> |
|
47 |
<Prefer32Bit>true</Prefer32Bit> |
|
48 |
<RestoreProjectStyle>PackageReference</RestoreProjectStyle> |
|
49 |
</PropertyGroup> |
|
50 |
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> |
|
51 |
<OutputPath>bin\x64\Release\</OutputPath> |
|
52 |
<DefineConstants>TRACE</DefineConstants> |
|
53 |
<Optimize>true</Optimize> |
|
54 |
<DebugType>pdbonly</DebugType> |
|
55 |
<PlatformTarget>x64</PlatformTarget> |
|
56 |
<LangVersion>7.3</LangVersion> |
|
57 |
<ErrorReport>prompt</ErrorReport> |
|
58 |
<Prefer32Bit>true</Prefer32Bit> |
|
59 |
</PropertyGroup> |
|
35 | 60 |
<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> |
|
36 | 91 |
<Reference Include="System" /> |
37 | 92 |
<Reference Include="System.Core" /> |
38 | 93 |
<Reference Include="System.Xml.Linq" /> |
... | ... | |
45 | 100 |
<ItemGroup> |
46 | 101 |
<Compile Include="Predictor\IPredictor.cs" /> |
47 | 102 |
<Compile Include="Predictor\ModelInput.cs" /> |
103 |
<Compile Include="Predictor\ModelOutput.cs" /> |
|
48 | 104 |
<Compile Include="Predictor\NaiveBayesClassifier.cs" /> |
49 |
<Compile Include="obj\Debug\.NETFramework,Version=v4.7.2.AssemblyAttributes.cs" /> |
|
50 | 105 |
<Compile Include="Parser\InputData\CsvDataLoader.cs" /> |
51 | 106 |
<Compile Include="Parser\InputData\JisInstance.cs" /> |
52 | 107 |
<Compile Include="Parser\InputData\LogInInstance.cs" /> |
... | ... | |
118 | 173 |
<None Include="data\weather\OD_ZCU_POCASI_12_2019.CSV"> |
119 | 174 |
<CopyToOutputDirectory>Always</CopyToOutputDirectory> |
120 | 175 |
</None> |
121 |
<None Include="obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache" /> |
|
122 |
<None Include="obj\Debug\ServerApp.csprojAssemblyReference.cache" /> |
|
123 |
</ItemGroup> |
|
124 |
<ItemGroup> |
|
125 |
<Folder Include="bin\Debug\" /> |
|
126 |
<Folder Include="bin\Release\" /> |
|
127 |
<Folder Include="obj\Debug\TempPE\" /> |
|
176 |
<None Include="packages.config" /> |
|
128 | 177 |
</ItemGroup> |
178 |
<ItemGroup /> |
|
129 | 179 |
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
180 |
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> |
|
181 |
<PropertyGroup> |
|
182 |
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> |
|
183 |
</PropertyGroup> |
|
184 |
<Error Condition="!Exists('..\packages\Microsoft.ML.CpuMath.1.5.5\build\netstandard2.0\Microsoft.ML.CpuMath.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.ML.CpuMath.1.5.5\build\netstandard2.0\Microsoft.ML.CpuMath.props'))" /> |
|
185 |
<Error Condition="!Exists('..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.props'))" /> |
|
186 |
<Error Condition="!Exists('..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.targets'))" /> |
|
187 |
</Target> |
|
188 |
<Import Project="..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.targets" Condition="Exists('..\packages\Microsoft.ML.1.5.5\build\netstandard2.0\Microsoft.ML.targets')" /> |
|
130 | 189 |
</Project> |
Také k dispozici: Unified diff
Draft Predictor
re #8598 @2h