├── .gitignore ├── LICENSE ├── MLNetExamples ├── AnomalyDetection │ ├── AnomalyDetection.csproj │ ├── AnomalyResult.cs │ ├── EnergyData.cs │ ├── EnergyPrediction.cs │ ├── Program.cs │ └── energy_hourly.csv ├── AutoML │ ├── AutoML.csproj │ ├── HousingData.cs │ ├── Program.cs │ └── housing.csv ├── AutoMLRanking │ ├── AutoMLRanking.csproj │ ├── Program.cs │ ├── RankingData.cs │ └── ranking.tsv ├── AzureFunction │ ├── .gitignore │ ├── AzureFunction.csproj │ ├── HousingData.cs │ ├── HousingPredict.cs │ ├── HousingPrediction.cs │ ├── HousingRetrain.cs │ ├── Startup.cs │ └── host.json ├── BagOfWords │ ├── BagOfWords.csproj │ ├── Input.cs │ ├── Output.cs │ └── Program.cs ├── BinaryClassification │ ├── BinaryClassification.csproj │ ├── Program.cs │ ├── TitanicData.cs │ ├── TitanicPrediction.cs │ └── titanic.csv ├── BinaryData │ ├── BinaryData.csproj │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── Program.cs │ └── housing.csv ├── BookRecommendations │ ├── Book.cs │ ├── BookRating.cs │ ├── BookRatingPrediction.cs │ ├── BookRecommendations.csproj │ ├── Data │ │ ├── bookfeatures.csv │ │ └── ratings.csv │ └── Program.cs ├── CustomTransform │ ├── CustomTransform.csproj │ ├── InputData.cs │ ├── NewData.cs │ ├── OutputData.cs │ └── Program.cs ├── CustomVisionOnnx │ ├── BoundingBox.cs │ ├── CustomVisionOnnx.csproj │ ├── Model │ │ ├── labels.txt │ │ └── model.onnx │ ├── Program.cs │ ├── WineInput.cs │ ├── WineModel.cs │ ├── WinePredictions.cs │ └── test │ │ ├── red-test.jpg │ │ └── white.jpg ├── DataPrepRowsColumns │ ├── DataPrepRowsColumns.csproj │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── Program.cs │ └── housing.csv ├── DatabaseData │ ├── DatabaseData.csproj │ ├── Program.cs │ ├── SalaryData.cs │ ├── SalaryData.csv │ ├── SalaryPrediction.cs │ └── config.json ├── DatabaseLoader │ ├── DatabaseLoader.csproj │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── Program.cs │ └── config.json ├── DeepNeuralNetwork │ ├── DeepNeuralNetwork.csproj │ ├── ImageData.cs │ ├── ImagePrediction.cs │ └── Program.cs ├── DeepNeuralNetworkUpdate │ ├── DeepNeuralNetworkUpdate.csproj │ ├── ImageData.cs │ ├── ImageModelInput.cs │ ├── ImagePrediction.cs │ └── Program.cs ├── EntityFrameworkData │ ├── EntityFrameworkData.csproj │ ├── MLNetExampleContext.cs │ ├── Program.cs │ ├── SalaryData.cs │ ├── SalaryData.csv │ └── SalaryPrediction.cs ├── EventHubPredict │ ├── EventHubPredict.csproj │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── Model │ │ └── housing-model.zip │ └── Program.cs ├── ExpressionTransform │ ├── ExpressionOutput.cs │ ├── ExpressionTransform.csproj │ ├── Program.cs │ └── SalaryInput.cs ├── FSharpRegression │ ├── FSharpRegression.fsproj │ ├── Program.fs │ ├── SalaryTest.csv │ └── SalaryTrain.csv ├── FeatureImportance │ ├── FeatureImportance.csproj │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── Program.cs │ └── housing.csv ├── InferColumns │ ├── InferColumns.csproj │ ├── Program.cs │ └── housing.csv ├── LargeFeatures │ ├── LargeFeatures.csproj │ ├── Program.cs │ ├── SensorData.cs │ ├── SensorPrediction.cs │ └── features.csv ├── MLNetExamples.sln ├── ModelExplainability │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── ModelExplainability.csproj │ ├── Program.cs │ └── housing.csv ├── NGrams │ ├── Input.cs │ ├── NGrams.csproj │ ├── Output.cs │ └── Program.cs ├── NormalizeText │ ├── Input.cs │ ├── NormalizeText.csproj │ ├── Output.cs │ └── Program.cs ├── Notebooks │ ├── Bank Analysis and ML.NET Classifier.ipynb │ ├── Dataframe with AutoML.ipynb │ ├── Intro to DataFrame.ipynb │ ├── ML.NET Event Hub Producer.ipynb │ ├── ML.NET Filter Transforms.ipynb │ ├── NimbusML │ │ ├── Regression.ipynb │ │ └── housing.csv │ ├── Plotting in C# with XPlot.ipynb │ └── housing.csv ├── NullValues │ ├── HousingData.cs │ ├── MissingData.cs │ ├── NullValues.csproj │ ├── Program.cs │ ├── ReplacedValues.cs │ └── housing.csv ├── Onnx │ ├── Onnx.csproj │ ├── Program.cs │ ├── SalaryData.cs │ ├── SalaryData.csv │ └── SalaryPrediction.cs ├── PredictDNN │ ├── ImageData.cs │ ├── ImagePrediction.cs │ ├── PredictDNN.csproj │ └── Program.cs ├── PredictionBot │ ├── AdapterWithErrorHandler.cs │ ├── Bots │ │ └── HousingBot.cs │ ├── Controllers │ │ └── BotController.cs │ ├── DeploymentTemplates │ │ ├── new-rg-parameters.json │ │ ├── preexisting-rg-parameters.json │ │ ├── template-with-new-rg.json │ │ └── template-with-preexisting-rg.json │ ├── Dialogs │ │ └── HousingPredictionDialog.cs │ ├── MLModel │ │ └── housing-model.zip │ ├── Models │ │ ├── HousingData.cs │ │ └── HousingPrediction.cs │ ├── PredictionBot.csproj │ ├── Program.cs │ ├── README.md │ ├── Startup.cs │ ├── appsettings.Development.json │ ├── appsettings.json │ └── wwwroot │ │ └── default.htm ├── Ranking │ ├── Program.cs │ ├── Ranking.csproj │ ├── RankingData.cs │ ├── RankingPrediction.cs │ └── ranking.tsv ├── RetrainModel │ ├── HousingData.cs │ ├── Program.cs │ ├── RetrainModel.csproj │ └── retrain_housing_data.csv ├── ScaleData │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── Program.cs │ ├── ScaleData.csproj │ └── housing.csv ├── SeedClustering │ ├── Program.cs │ ├── SeedClustering.csproj │ ├── SeedData.cs │ ├── SeedPrediction.cs │ └── Seed_Data.csv ├── SelectAndShuffle │ ├── HousingData.cs │ ├── HousingPrediction.cs │ ├── Program.cs │ ├── SelectAndShuffle.csproj │ └── housing.csv ├── SentimentAnalysis │ ├── Program.cs │ ├── SentimentAnalysis.csproj │ ├── SentimentData.cs │ ├── SentimentPrediction.cs │ └── stock_data.csv ├── SimpleRegression │ ├── Program.cs │ ├── README.md │ ├── SalaryData.cs │ ├── SalaryData.csv │ ├── SalaryPrediction.cs │ └── SimpleRegression.csproj ├── SimpleRegressionUpdate │ ├── Program.cs │ ├── SalaryData.cs │ ├── SalaryData.csv │ ├── SalaryPrediction.cs │ └── SimpleRegressionUpdate.csproj ├── StopWords │ ├── Program.cs │ ├── StopWords.csproj │ ├── TextData.cs │ └── TextTokens.cs ├── TextTransferLearning │ ├── DataStructures │ │ ├── FixedLengthVector.cs │ │ ├── MovieReview.cs │ │ ├── MovieReviewSentiment.cs │ │ └── VariableLengthVector.cs │ ├── Program.cs │ └── TextTransferLearning.csproj ├── TimeSeriesForecast │ ├── EnergyData.cs │ ├── EnergyForecast.cs │ ├── Program.cs │ ├── TimeSeriesForecast.csproj │ └── energy_hourly.csv ├── Tokenization │ ├── Program.cs │ ├── SentimentData.cs │ ├── SentimentTokens.cs │ └── Tokenization.csproj ├── TransferLearning │ ├── ImageData.cs │ ├── ImagePrediction.cs │ ├── InceptionSettings.cs │ ├── Program.cs │ ├── TransferLearning.csproj │ ├── images │ │ ├── book.jpg │ │ ├── cup.jpg │ │ ├── cup2.jpg │ │ └── sunflower.jpg │ └── labels.csv └── WordEmbeddings │ ├── Program.cs │ ├── TextFeatures.cs │ ├── TextInput.cs │ └── WordEmbeddings.csproj └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jon Wood 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MLNetExamples/AnomalyDetection/AnomalyDetection.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MLNetExamples/AnomalyDetection/AnomalyResult.cs: -------------------------------------------------------------------------------- 1 | namespace AnomalyDetection 2 | { 3 | public class AnomalyResult 4 | { 5 | public bool PredictedLabel { get; set; } 6 | public float Score { get; set; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /MLNetExamples/AnomalyDetection/EnergyData.cs: -------------------------------------------------------------------------------- 1 |  2 | using Microsoft.ML.Data; 3 | using System; 4 | 5 | namespace AnomalyDetection 6 | { 7 | public class EnergyData 8 | { 9 | [LoadColumn(0)] 10 | public DateTime Date { get; set; } 11 | 12 | [LoadColumn(1)] 13 | public float Energy { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MLNetExamples/AnomalyDetection/EnergyPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace AnomalyDetection 4 | { 5 | public class EnergyPrediction 6 | { 7 | [VectorType(2)] 8 | public double[] Prediction { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/AnomalyDetection/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System; 4 | using System.Linq; 5 | 6 | namespace AnomalyDetection 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var data = context.Data.LoadFromTextFile("./energy_hourly.csv", 15 | hasHeader: true, 16 | separatorChar: ','); 17 | 18 | var pipeline = context.Transforms.DetectSpikeBySsa(nameof(EnergyPrediction.Prediction), nameof(EnergyData.Energy), 19 | confidence: 98, trainingWindowSize: 90, seasonalityWindowSize: 30, pvalueHistoryLength: 30); 20 | 21 | var transformedData = pipeline.Fit(data).Transform(data); 22 | 23 | var predictions = context.Data.CreateEnumerable(transformedData, reuseRowObject: false).ToList(); 24 | 25 | var energy = data.GetColumn("Energy").ToArray(); 26 | var date = data.GetColumn("Date").ToArray(); 27 | 28 | Console.WriteLine("Anomalies:"); 29 | for (int i = 0; i < predictions.Count(); i++) 30 | { 31 | if (predictions[i].Prediction[0] == 1) 32 | { 33 | Console.WriteLine("{0}\t{1:0.0000}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 34 | date[i], energy[i], predictions[i].Prediction[0], predictions[i].Prediction[1], predictions[i].Prediction[2]); 35 | } 36 | } 37 | 38 | Console.ReadLine(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MLNetExamples/AutoML/AutoML.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MLNetExamples/AutoML/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace AutoML 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity{ get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/AutoML/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.AutoML; 3 | using Microsoft.ML.Data; 4 | using System; 5 | 6 | namespace AutoML 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var trainData = context.Data.LoadFromTextFile("./housing.csv", hasHeader: true, separatorChar: ','); 15 | 16 | var settings = new RegressionExperimentSettings 17 | { 18 | MaxExperimentTimeInSeconds = 20, 19 | OptimizingMetric = RegressionMetric.MeanAbsoluteError 20 | }; 21 | 22 | var labelColumnInfo = new ColumnInformation() 23 | { 24 | LabelColumnName = "Label" 25 | }; 26 | 27 | var progress = new Progress>(p => 28 | { 29 | if (p.ValidationMetrics != null) 30 | { 31 | Console.WriteLine($"Current Result - {p.TrainerName}, {p.ValidationMetrics.RSquared}, {p.ValidationMetrics.MeanAbsoluteError}"); 32 | } 33 | }); 34 | 35 | var experiment = context.Auto().CreateRegressionExperiment(settings); 36 | 37 | var result = experiment.Execute(trainData, labelColumnInfo, progressHandler: progress); 38 | 39 | Console.WriteLine(Environment.NewLine); 40 | Console.WriteLine("Best run:"); 41 | Console.WriteLine($"Trainer name - {result.BestRun.TrainerName}"); 42 | Console.WriteLine($"RSquared - {result.BestRun.ValidationMetrics.RSquared}"); 43 | Console.WriteLine($"MAE - {result.BestRun.ValidationMetrics.MeanAbsoluteError}"); 44 | 45 | Console.ReadLine(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MLNetExamples/AutoMLRanking/AutoMLRanking.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MLNetExamples/AutoMLRanking/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using Microsoft.ML; 4 | using Microsoft.ML.AutoML; 5 | using Microsoft.ML.Data; 6 | 7 | namespace AutoMLRanking 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var context = new MLContext(); 14 | 15 | var data = context.Data.LoadFromTextFile("./ranking.tsv", separatorChar: '\t'); 16 | 17 | var trainTestSplit = context.Data.TrainTestSplit(data, testFraction: 0.2); 18 | 19 | var settings = new RankingExperimentSettings 20 | { 21 | MaxExperimentTimeInSeconds = 300, 22 | OptimizingMetric = RankingMetric.Ndcg, 23 | }; 24 | 25 | var experiment = context.Auto().CreateRankingExperiment(settings); 26 | 27 | var progressHandler = new Progress>(ph => 28 | { 29 | if (ph.ValidationMetrics != null) 30 | { 31 | Console.WriteLine($"Current trainer - {ph.TrainerName} with nDCG {ph.ValidationMetrics.NormalizedDiscountedCumulativeGains.Average()}"); 32 | } 33 | }); 34 | 35 | var results = experiment.Execute(trainTestSplit.TrainSet, validationData: trainTestSplit.TestSet, 36 | progressHandler: progressHandler); 37 | 38 | var bestRun = results.BestRun; 39 | 40 | var metrics = bestRun.ValidationMetrics.NormalizedDiscountedCumulativeGains; 41 | 42 | Console.WriteLine(Environment.NewLine); 43 | Console.WriteLine($"Best model {bestRun.TrainerName} - with nDCG {metrics.Average()}"); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MLNetExamples/AutoMLRanking/RankingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace AutoMLRanking 4 | { 5 | public class RankingData 6 | { 7 | [LoadColumn(0)] 8 | public float Label { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float GroupId { get; set; } 12 | 13 | [LoadColumn(2, 133)] 14 | [VectorType(133)] 15 | public float[] Features { get; set; } 16 | } 17 | } -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # Azure Functions localsettings file 5 | local.settings.json 6 | 7 | # User-specific files 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | bld/ 24 | [Bb]in/ 25 | [Oo]bj/ 26 | [Ll]og/ 27 | 28 | # Visual Studio 2015 cache/options directory 29 | .vs/ 30 | # Uncomment if you have tasks that create the project's static files in wwwroot 31 | #wwwroot/ 32 | 33 | # MSTest test Results 34 | [Tt]est[Rr]esult*/ 35 | [Bb]uild[Ll]og.* 36 | 37 | # NUNIT 38 | *.VisualState.xml 39 | TestResult.xml 40 | 41 | # Build Results of an ATL Project 42 | [Dd]ebugPS/ 43 | [Rr]eleasePS/ 44 | dlldata.c 45 | 46 | # DNX 47 | project.lock.json 48 | project.fragment.lock.json 49 | artifacts/ 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # NCrunch 117 | _NCrunch_* 118 | .*crunch*.local.xml 119 | nCrunchTemp_* 120 | 121 | # MightyMoose 122 | *.mm.* 123 | AutoTest.Net/ 124 | 125 | # Web workbench (sass) 126 | .sass-cache/ 127 | 128 | # Installshield output folder 129 | [Ee]xpress/ 130 | 131 | # DocProject is a documentation generator add-in 132 | DocProject/buildhelp/ 133 | DocProject/Help/*.HxT 134 | DocProject/Help/*.HxC 135 | DocProject/Help/*.hhc 136 | DocProject/Help/*.hhk 137 | DocProject/Help/*.hhp 138 | DocProject/Help/Html2 139 | DocProject/Help/html 140 | 141 | # Click-Once directory 142 | publish/ 143 | 144 | # Publish Web Output 145 | *.[Pp]ublish.xml 146 | *.azurePubxml 147 | # TODO: Comment the next line if you want to checkin your web deploy settings 148 | # but database connection strings (with potential passwords) will be unencrypted 149 | #*.pubxml 150 | *.publishproj 151 | 152 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 153 | # checkin your Azure Web App publish settings, but sensitive information contained 154 | # in these scripts will be unencrypted 155 | PublishScripts/ 156 | 157 | # NuGet Packages 158 | *.nupkg 159 | # The packages folder can be ignored because of Package Restore 160 | **/packages/* 161 | # except build/, which is used as an MSBuild target. 162 | !**/packages/build/ 163 | # Uncomment if necessary however generally it will be regenerated when needed 164 | #!**/packages/repositories.config 165 | # NuGet v3's project.json files produces more ignoreable files 166 | *.nuget.props 167 | *.nuget.targets 168 | 169 | # Microsoft Azure Build Output 170 | csx/ 171 | *.build.csdef 172 | 173 | # Microsoft Azure Emulator 174 | ecf/ 175 | rcf/ 176 | 177 | # Windows Store app package directories and files 178 | AppPackages/ 179 | BundleArtifacts/ 180 | Package.StoreAssociation.xml 181 | _pkginfo.txt 182 | 183 | # Visual Studio cache files 184 | # files ending in .cache can be ignored 185 | *.[Cc]ache 186 | # but keep track of directories ending in .cache 187 | !*.[Cc]ache/ 188 | 189 | # Others 190 | ClientBin/ 191 | ~$* 192 | *~ 193 | *.dbmdl 194 | *.dbproj.schemaview 195 | *.jfm 196 | *.pfx 197 | *.publishsettings 198 | node_modules/ 199 | orleans.codegen.cs 200 | 201 | # Since there are multiple workflows, uncomment next line to ignore bower_components 202 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 203 | #bower_components/ 204 | 205 | # RIA/Silverlight projects 206 | Generated_Code/ 207 | 208 | # Backup & report files from converting an old project file 209 | # to a newer Visual Studio version. Backup files are not needed, 210 | # because we have git ;-) 211 | _UpgradeReport_Files/ 212 | Backup*/ 213 | UpgradeLog*.XML 214 | UpgradeLog*.htm 215 | 216 | # SQL Server files 217 | *.mdf 218 | *.ldf 219 | 220 | # Business Intelligence projects 221 | *.rdl.data 222 | *.bim.layout 223 | *.bim_*.settings 224 | 225 | # Microsoft Fakes 226 | FakesAssemblies/ 227 | 228 | # GhostDoc plugin setting file 229 | *.GhostDoc.xml 230 | 231 | # Node.js Tools for Visual Studio 232 | .ntvs_analysis.dat 233 | 234 | # Visual Studio 6 build log 235 | *.plg 236 | 237 | # Visual Studio 6 workspace options file 238 | *.opt 239 | 240 | # Visual Studio LightSwitch build output 241 | **/*.HTMLClient/GeneratedArtifacts 242 | **/*.DesktopClient/GeneratedArtifacts 243 | **/*.DesktopClient/ModelManifest.xml 244 | **/*.Server/GeneratedArtifacts 245 | **/*.Server/ModelManifest.xml 246 | _Pvt_Extensions 247 | 248 | # Paket dependency manager 249 | .paket/paket.exe 250 | paket-files/ 251 | 252 | # FAKE - F# Make 253 | .fake/ 254 | 255 | # JetBrains Rider 256 | .idea/ 257 | *.sln.iml 258 | 259 | # CodeRush 260 | .cr/ 261 | 262 | # Python Tools for Visual Studio (PTVS) 263 | __pycache__/ 264 | *.pyc -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/AzureFunction.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | netcoreapp2.1 4 | v2 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PreserveNewest 17 | 18 | 19 | PreserveNewest 20 | Never 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace AzureFunction 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity{ get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/HousingPredict.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Threading.Tasks; 4 | using Microsoft.AspNetCore.Mvc; 5 | using Microsoft.Azure.WebJobs; 6 | using Microsoft.Azure.WebJobs.Extensions.Http; 7 | using Microsoft.AspNetCore.Http; 8 | using Microsoft.Extensions.Logging; 9 | using Newtonsoft.Json; 10 | using Microsoft.Extensions.ML; 11 | 12 | namespace AzureFunction 13 | { 14 | public class HousingPredict 15 | { 16 | private readonly PredictionEnginePool _predictionEnginePool; 17 | 18 | public HousingPredict(PredictionEnginePool predictionEnginePool) 19 | { 20 | _predictionEnginePool = predictionEnginePool; 21 | } 22 | 23 | [FunctionName("HousingPredict")] 24 | public async Task Run( 25 | [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, 26 | ILogger log) 27 | { 28 | log.LogInformation("C# HTTP trigger function processed a request."); 29 | 30 | string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); 31 | var housingData = JsonConvert.DeserializeObject(requestBody); 32 | 33 | var prediction = _predictionEnginePool.Predict(housingData); 34 | 35 | return new OkObjectResult(prediction); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace AzureFunction 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedPrice { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/HousingRetrain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.Azure.WebJobs; 6 | using Microsoft.Azure.WebJobs.Host; 7 | using Microsoft.Extensions.Logging; 8 | using Microsoft.ML; 9 | using Microsoft.ML.Trainers; 10 | using Microsoft.WindowsAzure.Storage; 11 | 12 | namespace AzureFunction 13 | { 14 | public class HousingRetrain 15 | { 16 | [FunctionName("HousingRetrain")] 17 | public async Task Run([BlobTrigger("input/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log) 18 | { 19 | log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); 20 | string blobData; 21 | 22 | // Download model files from Blob Storage, if needed 23 | string trainerFilePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "models/housing-trainer.zip"); 24 | string pipelineFilePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "models/housing-data-prep.zip"); 25 | 26 | var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process); 27 | 28 | var storageAccount = CloudStorageAccount.Parse(connectionString); 29 | 30 | var client = storageAccount.CreateCloudBlobClient(); 31 | 32 | var container = client.GetContainerReference("models"); 33 | 34 | var dataModel = container.GetBlockBlobReference("housing-data-prep.zip"); 35 | var trainingModel = container.GetBlockBlobReference("housing-trainer.zip"); 36 | 37 | if (!File.Exists(pipelineFilePath)) 38 | { 39 | await dataModel.DownloadToFileAsync(pipelineFilePath, FileMode.Create); 40 | } 41 | 42 | if (!File.Exists(trainerFilePath)) 43 | { 44 | await trainingModel.DownloadToFileAsync(trainerFilePath, FileMode.Create); 45 | } 46 | 47 | // Load models into ML Context 48 | var context = new MLContext(); 49 | 50 | DataViewSchema modelSchema, pipelineSchema; 51 | 52 | var trainerModel = context.Model.Load(trainerFilePath, out modelSchema); 53 | var dataPrepModel = context.Model.Load(pipelineFilePath, out pipelineSchema); 54 | 55 | var originalModelParams = 56 | ((ISingleFeaturePredictionTransformer)trainerModel).Model as PoissonRegressionModelParameters; 57 | 58 | // Read and parse blob data 59 | using (var reader = new StreamReader(myBlob)) 60 | { 61 | blobData = reader.ReadToEnd(); 62 | } 63 | 64 | var parsedData = blobData 65 | .Split('\n') 66 | .Skip(1) 67 | .Select(line => line.Split(',')) 68 | .TakeWhile(row => !string.IsNullOrWhiteSpace(row[0])) 69 | .Select(row => new HousingData 70 | { 71 | Longitude = float.Parse(row[0]), 72 | Latitude = float.Parse(row[1]), 73 | HousingMedianAge = float.Parse(row[2]), 74 | TotalRooms = float.Parse(row[3]), 75 | TotalBedrooms = float.Parse(row[4]), 76 | Population = float.Parse(row[5]), 77 | Households = float.Parse(row[6]), 78 | MedianIncome = float.Parse(row[7]), 79 | MedianHouseValue = float.Parse(row[8]), 80 | OceanProximity = row[9] 81 | }); 82 | 83 | // Load new data and build new model based off original parameters 84 | var newData = context.Data.LoadFromEnumerable(parsedData); 85 | 86 | var newDataTransformed = dataPrepModel.Transform(newData); 87 | 88 | var retrainedModel = context.Regression.Trainers.LbfgsPoissonRegression() 89 | .Fit(newDataTransformed, originalModelParams); 90 | 91 | // Compare model params 92 | var newModelParams = retrainedModel.Model as PoissonRegressionModelParameters; 93 | 94 | var weightDiffs = originalModelParams.Weights.Zip( 95 | newModelParams.Weights, (original, updated) => original - updated).ToArray(); 96 | 97 | Console.WriteLine("Original\tRetrained\tDifference"); 98 | for (int i = 0; i < weightDiffs.Count(); i++) 99 | { 100 | Console.WriteLine($"{originalModelParams.Weights[i]}\t{newModelParams.Weights[i]}\t{weightDiffs[i]}"); 101 | } 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/Startup.cs: -------------------------------------------------------------------------------- 1 | using AzureFunction; 2 | using Microsoft.Azure.Functions.Extensions.DependencyInjection; 3 | using Microsoft.Extensions.ML; 4 | using Microsoft.WindowsAzure.Storage; 5 | using System; 6 | 7 | [assembly: FunctionsStartup(typeof(Startup))] 8 | namespace AzureFunction 9 | { 10 | class Startup : FunctionsStartup 11 | { 12 | public override void Configure(IFunctionsHostBuilder builder) 13 | { 14 | var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process); 15 | 16 | var storageAccount = CloudStorageAccount.Parse(connectionString); 17 | 18 | var client = storageAccount.CreateCloudBlobClient(); 19 | 20 | var container = client.GetContainerReference("models"); 21 | 22 | var model = container.GetBlockBlobReference("housing-model.zip"); 23 | 24 | var uri = model.Uri.AbsoluteUri; 25 | 26 | builder.Services.AddPredictionEnginePool() 27 | .FromUri(uri); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MLNetExamples/AzureFunction/host.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0" 3 | } -------------------------------------------------------------------------------- /MLNetExamples/BagOfWords/BagOfWords.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /MLNetExamples/BagOfWords/Input.cs: -------------------------------------------------------------------------------- 1 | namespace BagOfWords 2 | { 3 | public class Input 4 | { 5 | public string Text { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/BagOfWords/Output.cs: -------------------------------------------------------------------------------- 1 | namespace BagOfWords 2 | { 3 | internal class Output 4 | { 5 | public float[] BagOfWords { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /MLNetExamples/BagOfWords/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace BagOfWords 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var data = new List 15 | { 16 | new Input { Text = "I really enjoy programming in C#. And I really like it." } 17 | }; 18 | 19 | var dataView = context.Data.LoadFromEnumerable(data); 20 | 21 | var bagWordsPipeline = context.Transforms.Text.ProduceWordBags( 22 | "BagOfWords", 23 | "Text", 24 | ngramLength: 1, 25 | useAllLengths: false, 26 | weighting: Microsoft.ML.Transforms.Text.NgramExtractingEstimator.WeightingCriteria.Tf 27 | ); 28 | 29 | var bagWordsTransform = bagWordsPipeline.Fit(dataView); 30 | var bagWordsDataView = bagWordsTransform.Transform(dataView); 31 | 32 | var predictionEngine = context.Model.CreatePredictionEngine(bagWordsTransform); 33 | 34 | var prediction = predictionEngine.Predict(data[0]); 35 | 36 | VBuffer> slotNames = default; 37 | bagWordsDataView.Schema["BagOfWords"].GetSlotNames(ref slotNames); 38 | 39 | var bagOfWordColumn = bagWordsDataView.GetColumn>(bagWordsDataView.Schema["BagOfWords"]); 40 | var slots = slotNames.GetValues(); 41 | 42 | Console.Write("NGrams: "); 43 | foreach (var featureRow in bagOfWordColumn) 44 | { 45 | foreach (var item in featureRow.Items()) 46 | { 47 | Console.Write($"{slots[item.Key]} "); 48 | } 49 | 50 | Console.WriteLine(); 51 | } 52 | 53 | Console.Write("Word Counts: "); 54 | for (int i = 0; i < prediction.BagOfWords.Length; i++) 55 | { 56 | Console.Write($"{prediction.BagOfWords[i]:F4} "); 57 | } 58 | 59 | Console.WriteLine(Environment.NewLine); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryClassification/BinaryClassification.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryClassification/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Runtime.Data; 3 | using Microsoft.ML.Transforms; 4 | using System; 5 | using System.Linq; 6 | 7 | namespace BinaryClassification 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var context = new MLContext(); 14 | 15 | var textLoader = context.Data.TextReader(new TextLoader.Arguments() 16 | { 17 | Separator = ",", 18 | HasHeader = true, 19 | Column = new[] 20 | { 21 | new TextLoader.Column("PassengerId", DataKind.R4, 0), 22 | new TextLoader.Column("Label", DataKind.Bool, 1), 23 | new TextLoader.Column("Pclass", DataKind.R4, 2), 24 | new TextLoader.Column("Name", DataKind.Text, 3), 25 | new TextLoader.Column("Sex", DataKind.Text, 4), 26 | new TextLoader.Column("Age", DataKind.R4, 5), 27 | new TextLoader.Column("SibSp", DataKind.R4, 6), 28 | new TextLoader.Column("Parch", DataKind.R4, 7), 29 | new TextLoader.Column("Ticket", DataKind.Text, 8), 30 | new TextLoader.Column("Fare", DataKind.R4, 9), 31 | new TextLoader.Column("Cabin", DataKind.Text, 10), 32 | new TextLoader.Column("Embarked", DataKind.Text, 11) 33 | } 34 | }); 35 | 36 | IDataView data = textLoader.Read("titanic.csv"); 37 | 38 | var (trainData, testData) = context.BinaryClassification.TrainTestSplit(data, testFraction: 0.2); 39 | 40 | var pipeline = context.Transforms.Concatenate("Text", "Name", "Sex", "Embarked") 41 | .Append(context.Transforms.Text.FeaturizeText("Text", "TextFeatures")) 42 | .Append(context.Transforms.Concatenate("Features", "TextFeatures", "Pclass", "Age", "Fare", "SibSp", "Parch")) 43 | .Append(context.BinaryClassification.Trainers.LogisticRegression("Label", "Features")); 44 | 45 | Console.WriteLine("Cross validating..."); 46 | 47 | var crossValidateResults = context.BinaryClassification.CrossValidate(testData, pipeline); 48 | 49 | var averageAuc = crossValidateResults.Average(i => i.metrics.Auc); 50 | 51 | Console.WriteLine($"Average AUC - {averageAuc}"); 52 | 53 | var model = pipeline.Fit(trainData); 54 | 55 | var predictionFunction = model.MakePredictionFunction(context); 56 | 57 | var prediction = predictionFunction.Predict(new TitanicData { Sex = "F" }); 58 | 59 | Console.WriteLine($"Prediction - {prediction.Prediction}"); 60 | 61 | Console.ReadLine(); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryClassification/TitanicData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Runtime.Api; 2 | 3 | namespace BinaryClassification 4 | { 5 | public class TitanicData 6 | { 7 | [Column("0")] 8 | public float PassengerId; 9 | 10 | [Column("1", name: "Label")] 11 | public bool HasSurvived; 12 | 13 | [Column("2")] 14 | public float Pclass; 15 | 16 | [Column("3")] 17 | public string Name; 18 | 19 | [Column("4")] 20 | public string Sex; 21 | 22 | [Column("5")] 23 | public float Age; 24 | 25 | [Column("6")] 26 | public float SibSp; 27 | 28 | [Column("7")] 29 | public float Parch; 30 | 31 | [Column("8")] 32 | public string Ticket; 33 | 34 | [Column("9")] 35 | public float Fare; 36 | 37 | [Column("10")] 38 | public string Cabin; 39 | 40 | [Column("11")] 41 | public string Embarked; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryClassification/TitanicPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Runtime.Api; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace BinaryClassification 7 | { 8 | public class TitanicPrediction 9 | { 10 | [ColumnName("PredictedLabel")] 11 | public bool Prediction; 12 | 13 | public float Score; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryData/BinaryData.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | 23 | PreserveNewest 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryData/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace BinaryData 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity{ get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryData/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace BinaryData 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedPrice { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/BinaryData/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System.IO; 3 | 4 | namespace BinaryData 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var context = new MLContext(); 11 | 12 | var data = context.Data.LoadFromTextFile("./housing.csv", hasHeader: true, separatorChar: ','); 13 | 14 | using (var stream = new FileStream("./housing_binary.idv", FileMode.Create)) 15 | { 16 | context.Data.SaveAsBinary(data, stream); 17 | } 18 | 19 | var binaryData = context.Data.LoadFromBinary("./housing_binary.idv"); 20 | 21 | var preview = binaryData.Preview(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MLNetExamples/BookRecommendations/Book.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace BookRecommendations 6 | { 7 | public class Book 8 | { 9 | public int BookId { get; set; } 10 | public string BookTitle { get; set; } 11 | public string Author { get; set; } 12 | public string Genre1 { get; set; } 13 | public string Genre2 { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MLNetExamples/BookRecommendations/BookRating.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace BookRecommendations 6 | { 7 | public class BookRating 8 | { 9 | public float Label; 10 | 11 | public float user; 12 | 13 | public float bookid; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MLNetExamples/BookRecommendations/BookRatingPrediction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace BookRecommendations 6 | { 7 | public class BookRatingPrediction 8 | { 9 | public float Label; 10 | 11 | public float Score; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/BookRecommendations/BookRecommendations.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | PreserveNewest 20 | 21 | 22 | PreserveNewest 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /MLNetExamples/BookRecommendations/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Runtime.Data; 3 | using Microsoft.ML.Trainers; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.IO; 7 | using System.Linq; 8 | 9 | namespace BookRecommendations 10 | { 11 | class Program 12 | { 13 | public static string dataLocation = "./Data"; 14 | public static int bookPredictionId = 34941133; 15 | 16 | static void Main(string[] args) 17 | { 18 | var trainingDataPath = $"{dataLocation}/ratings.csv"; 19 | 20 | var context = new MLContext(); 21 | 22 | var reader = context.Data.TextReader(new TextLoader.Arguments() { 23 | Separator = ",", 24 | HasHeader = true, 25 | Column = new[] 26 | { 27 | new TextLoader.Column("Label", DataKind.R4, 0), 28 | new TextLoader.Column("user", DataKind.R4, 1), 29 | new TextLoader.Column("bookid", DataKind.R4, 2), 30 | } 31 | }); 32 | 33 | IDataView data = reader.Read(trainingDataPath); 34 | 35 | var (trainData, testData) = context.BinaryClassification.TrainTestSplit(data, testFraction: 0.2); 36 | 37 | var pipeline = context.Transforms.Categorical.MapValueToKey("user", "userIdEncoded") 38 | .Append(context.Transforms.Categorical.MapValueToKey("bookid", "bookIdEncoded")) 39 | .Append(new MatrixFactorizationTrainer(context, "Label", "userIdEncoded", "bookIdEncoded", 40 | advancedSettings: s => { s.NumIterations = 20; s.K = 100; })); 41 | 42 | Console.WriteLine("Training recommender" + Environment.NewLine); 43 | var model = pipeline.Fit(trainData); 44 | 45 | var prediction = model.Transform(testData); 46 | var metrics = context.Regression.Evaluate(prediction); 47 | 48 | Console.WriteLine($"Model metrics: RMS - {metrics.Rms} R^2 - {metrics.RSquared}" + Environment.NewLine); 49 | 50 | var predictionFunc = model.MakePredictionFunction(context); 51 | 52 | var bookPrediction = predictionFunc.Predict(new BookRating { 53 | user = 99, 54 | bookid = bookPredictionId 55 | }); 56 | 57 | var bookData = LoadBookData(); 58 | 59 | Console.WriteLine($"Predicted rating - {Math.Round(bookPrediction.Score, 1)} for book {bookData.FirstOrDefault(b => b.BookId == bookPredictionId).BookTitle}"); 60 | 61 | Console.ReadLine(); 62 | } 63 | 64 | private static IList LoadBookData() 65 | { 66 | var result = new List(); 67 | 68 | var reader = File.OpenRead($"{dataLocation}/bookfeatures.csv"); 69 | 70 | var isHeader = true; 71 | var line = String.Empty; 72 | 73 | using (var streamReader = new StreamReader(reader)) 74 | { 75 | while(!streamReader.EndOfStream) 76 | { 77 | if (isHeader) 78 | { 79 | line = streamReader.ReadLine(); 80 | isHeader = false; 81 | } 82 | 83 | line = streamReader.ReadLine(); 84 | var data = line.Split(','); 85 | 86 | var book = new Book 87 | { 88 | BookId = int.Parse(data[3].ToString()), 89 | BookTitle = data[8].ToString(), 90 | Author = data[1].ToString(), 91 | Genre1 = data[4].ToString(), 92 | Genre2 = data[5].ToString() 93 | }; 94 | 95 | result.Add(book); 96 | } 97 | } 98 | 99 | return result; 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /MLNetExamples/CustomTransform/CustomTransform.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MLNetExamples/CustomTransform/InputData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CustomTransform 4 | { 5 | public class InputData 6 | { 7 | public DateTime Date { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /MLNetExamples/CustomTransform/NewData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CustomTransform 4 | { 5 | public class NewData 6 | { 7 | public bool IsWeekend { get; set; } 8 | public DateTime Date { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/CustomTransform/OutputData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace CustomTransform 6 | { 7 | public class MappingOutput 8 | { 9 | public bool IsWeekend { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /MLNetExamples/CustomTransform/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace CustomTransform 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var mlContext = new MLContext(); 12 | 13 | var sampleData = new List 14 | { 15 | new InputData { Date = new DateTime(2019, 7, 19) }, 16 | new InputData { Date = new DateTime(2019, 7, 6) }, 17 | new InputData { Date = new DateTime(2019, 7, 2) }, 18 | new InputData { Date = new DateTime(2019, 7, 14) }, 19 | }; 20 | 21 | var data = mlContext.Data.LoadFromEnumerable(sampleData); 22 | 23 | Action mapping = (input, output) => 24 | output.IsWeekend = input.Date.DayOfWeek == DayOfWeek.Saturday || input.Date.DayOfWeek == DayOfWeek.Sunday; 25 | 26 | var pipeline = mlContext.Transforms.CustomMapping(mapping, "customMap"); 27 | 28 | var transformedData = pipeline.Fit(data).Transform(data); 29 | 30 | var enumerableData = mlContext.Data.CreateEnumerable(transformedData, reuseRowObject: true); 31 | 32 | foreach (var row in enumerableData) 33 | { 34 | Console.WriteLine($"{row.Date} - {row.IsWeekend}"); 35 | } 36 | 37 | Console.ReadLine(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/BoundingBox.cs: -------------------------------------------------------------------------------- 1 | using System.Drawing; 2 | 3 | namespace CustomVisionOnnx 4 | { 5 | public class BoundingBoxDimensions 6 | { 7 | public float X { get; set; } 8 | public float Y { get; set; } 9 | public float Height { get; set; } 10 | public float Width { get; set; } 11 | } 12 | 13 | public class BoundingBox 14 | { 15 | public BoundingBoxDimensions Dimensions { get; set; } 16 | 17 | public string Label { get; set; } 18 | 19 | public float Confidence { get; set; } 20 | 21 | public RectangleF Rect 22 | { 23 | get { return new RectangleF(Dimensions.X, Dimensions.Y, Dimensions.Width, Dimensions.Height); } 24 | } 25 | 26 | public Color BoxColor { get; set; } 27 | 28 | public string Description => $"{Label} ({(Confidence * 100).ToString("0")}%)"; 29 | 30 | private static readonly Color[] classColors = new Color[] 31 | { 32 | Color.Khaki, Color.Fuchsia, Color.Silver, Color.RoyalBlue, 33 | Color.Green, Color.DarkOrange, Color.Purple, Color.Gold, 34 | Color.Red, Color.Aquamarine, Color.Lime, Color.AliceBlue, 35 | Color.Sienna, Color.Orchid, Color.Tan, Color.LightPink, 36 | Color.Yellow, Color.HotPink, Color.OliveDrab, Color.SandyBrown, 37 | Color.DarkTurquoise 38 | }; 39 | 40 | public static Color GetColor(int index) => index < classColors.Length ? classColors[index] : classColors[index % classColors.Length]; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/CustomVisionOnnx.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PreserveNewest 17 | 18 | 19 | PreserveNewest 20 | 21 | 22 | PreserveNewest 23 | 24 | 25 | PreserveNewest 26 | 27 | 28 | PreserveNewest 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/Model/labels.txt: -------------------------------------------------------------------------------- 1 | red 2 | white -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/Model/model.onnx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/CustomVisionOnnx/Model/model.onnx -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/WineInput.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Transforms.Image; 2 | using System.Drawing; 3 | 4 | namespace CustomVisionOnnx 5 | { 6 | public struct ImageSettings 7 | { 8 | public const int imageHeight = 416; 9 | public const int imageWidth = 416; 10 | } 11 | 12 | public class WineInput 13 | { 14 | [ImageType(ImageSettings.imageHeight, ImageSettings.imageWidth)] 15 | public Bitmap Image { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/WineModel.cs: -------------------------------------------------------------------------------- 1 | namespace CustomVisionOnnx 2 | { 3 | public class WineModel 4 | { 5 | public float[] Labels { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/WinePredictions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace CustomVisionOnnx 4 | { 5 | public class WinePredictions 6 | { 7 | [ColumnName("model_outputs0")] 8 | public float[] PredictedLabels { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/test/red-test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/CustomVisionOnnx/test/red-test.jpg -------------------------------------------------------------------------------- /MLNetExamples/CustomVisionOnnx/test/white.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/CustomVisionOnnx/test/white.jpg -------------------------------------------------------------------------------- /MLNetExamples/DataPrepRowsColumns/DataPrepRowsColumns.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | 23 | PreserveNewest 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /MLNetExamples/DataPrepRowsColumns/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DataPrepRowsColumns 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity{ get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/DataPrepRowsColumns/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DataPrepRowsColumns 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedPrice { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/DataPrepRowsColumns/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | 4 | namespace DataPrepRowsColumns 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var context = new MLContext(); 11 | 12 | var data = context.Data.LoadFromTextFile("./housing.csv", 13 | hasHeader: true, separatorChar: ','); 14 | 15 | // Select columns 16 | var selectCols = context.Transforms.SelectColumns("HousingMedianAge", "TotalBedrooms"); 17 | var selectColsTransform = selectCols.Fit(data).Transform(data); 18 | 19 | //DisplayColumns(selectColsTransform); 20 | 21 | // Drop columns 22 | var dropCols = context.Transforms.DropColumns("Latitude", "Longitude"); 23 | var dropColsTransforms = dropCols.Fit(data).Transform(data); 24 | 25 | //DisplayColumns(dropColsTransforms); 26 | 27 | // Shuffle rows 28 | //DisplayColumns(data); 29 | 30 | //Console.WriteLine("*********************************"); 31 | 32 | var shuffleRows = context.Data.ShuffleRows(data, seed: 42); 33 | 34 | //DisplayColumns(shuffleRows); 35 | 36 | // Take rows 37 | var takeRows = context.Data.TakeRows(data, 2); 38 | 39 | //DisplayColumns(takeRows); 40 | 41 | // Filter rows 42 | var filterRows = context.Data.FilterRowsByColumn(data, "Population", 43 | lowerBound: 0, upperBound: 1000); 44 | 45 | DisplayColumns(filterRows); 46 | 47 | Console.ReadLine(); 48 | } 49 | 50 | private static void DisplayColumns(IDataView data) 51 | { 52 | var preview = data.Preview(maxRows: 5); 53 | 54 | string previewData = ""; 55 | 56 | for (int i = 0; i < preview.RowView.Length; i++) 57 | { 58 | foreach (var item in preview.RowView[i].Values) 59 | { 60 | previewData += $"{item.Key}: {item.Value} "; 61 | } 62 | 63 | Console.WriteLine("----------------------------------"); 64 | Console.WriteLine(previewData); 65 | previewData = ""; 66 | } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseData/DatabaseData.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | PreserveNewest 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseData/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Configuration; 2 | using Microsoft.ML; 3 | using Microsoft.ML.Data; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Data.SqlClient; 7 | using System.IO; 8 | using System.Linq; 9 | 10 | namespace DatabaseData 11 | { 12 | class Program 13 | { 14 | private static string _connectionString; 15 | 16 | static void Main(string[] args) 17 | { 18 | var builder = new ConfigurationBuilder() 19 | .SetBasePath(Directory.GetCurrentDirectory()) 20 | .AddJsonFile("config.json"); 21 | 22 | var configuration = builder.Build(); 23 | _connectionString = configuration["connectionString"]; 24 | 25 | var fileData = ReadFromFile("./SalaryData.csv"); 26 | 27 | AddDataToDatabase(fileData); 28 | 29 | var dbData = ReadFromDatabase(); 30 | 31 | var context = new MLContext(); 32 | 33 | var mlData = context.Data.ReadFromEnumerable(dbData); 34 | 35 | var (trainData, testData) = context.Regression.TrainTestSplit(mlData, testFraction: 0.2); 36 | 37 | var preview = trainData.Preview(maxRows: 5); 38 | 39 | var pipeline = context.Transforms.Concatenate("Features", "YearsExperience") 40 | .Append(context.Transforms.CopyColumns(("Label", "Salary"))) 41 | .Append(context.Regression.Trainers.FastTree()); 42 | 43 | var model = pipeline.Fit(trainData); 44 | 45 | var prediction = model.Transform(testData); 46 | 47 | var metrics = context.Regression.Evaluate(prediction); 48 | 49 | var predictionFunc = model.CreatePredictionEngine(context); 50 | 51 | var salaryPrediction = predictionFunc.Predict(new SalaryData { YearsExperience = 11 }); 52 | 53 | Console.WriteLine($"Prediction - {salaryPrediction.PredictedSalary}"); 54 | Console.ReadLine(); 55 | } 56 | 57 | private static IEnumerable ReadFromDatabase() 58 | { 59 | var data = new List(); 60 | 61 | using (var conn = new SqlConnection(_connectionString)) 62 | { 63 | conn.Open(); 64 | 65 | var selectCommand = "SELECT YearsOfExperience, Salary FROM mlnetExample.dbo.SalaryData"; 66 | 67 | var sqlCommand = new SqlCommand(selectCommand, conn); 68 | 69 | var reader = sqlCommand.ExecuteReader(); 70 | 71 | while(reader.Read()) 72 | { 73 | data.Add(new SalaryData 74 | { 75 | YearsExperience = float.Parse(reader.GetValue(0).ToString()), 76 | Salary = float.Parse(reader.GetValue(1).ToString()) 77 | }); 78 | } 79 | } 80 | 81 | return data; 82 | } 83 | 84 | private static void AddDataToDatabase(IEnumerable data) 85 | { 86 | using (var conn = new SqlConnection(_connectionString)) 87 | { 88 | conn.Open(); 89 | 90 | var insertCommand = "INSERT INTO mlnetExample.dbo.SalaryData VALUES (@years, @salary);"; 91 | var selectCommand = "SELECT COUNT(*) From mlnetExample.dbo.SalaryData"; 92 | 93 | var selectSqlCommand = new SqlCommand(selectCommand, conn); 94 | 95 | var results = (int)selectSqlCommand.ExecuteScalar(); 96 | 97 | if (results > 0) 98 | { 99 | var deleteCommand = "DELETE FROM mlnetExample.dbo.SalaryData"; 100 | 101 | var deleteSqlCommand = new SqlCommand(deleteCommand, conn); 102 | 103 | deleteSqlCommand.ExecuteNonQuery(); 104 | } 105 | 106 | foreach (var item in data) 107 | { 108 | var command = new SqlCommand(insertCommand, conn); 109 | 110 | command.Parameters.AddWithValue("@years", item.YearsExperience); 111 | command.Parameters.AddWithValue("@salary", item.Salary); 112 | 113 | command.ExecuteNonQuery(); 114 | } 115 | } 116 | } 117 | 118 | private static IEnumerable ReadFromFile(string filePath) 119 | { 120 | var data = File.ReadAllLines(filePath) 121 | .Skip(1) 122 | .Select(l => l.Split(',')) 123 | .Select(i => new SalaryData 124 | { 125 | YearsExperience = float.Parse(i[0]), 126 | Salary = float.Parse(i[1]) 127 | }); 128 | 129 | return data; 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseData/SalaryData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DatabaseData 4 | { 5 | public class SalaryData 6 | { 7 | public float YearsExperience; 8 | 9 | public float Salary; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseData/SalaryData.csv: -------------------------------------------------------------------------------- 1 | YearsExperience,Salary 2 | 1.1,39343.00 3 | 1.3,46205.00 4 | 1.5,37731.00 5 | 2.0,43525.00 6 | 2.2,39891.00 7 | 2.9,56642.00 8 | 3.0,60150.00 9 | 3.2,54445.00 10 | 3.2,64445.00 11 | 3.7,57189.00 12 | 3.9,63218.00 13 | 4.0,55794.00 14 | 4.0,56957.00 15 | 4.1,57081.00 16 | 4.5,61111.00 17 | 4.9,67938.00 18 | 5.1,66029.00 19 | 5.3,83088.00 20 | 5.9,81363.00 21 | 6.0,93940.00 22 | 6.8,91738.00 23 | 7.1,98273.00 24 | 7.9,101302.00 25 | 8.2,113812.00 26 | 8.7,109431.00 27 | 9.0,105582.00 28 | 9.5,116969.00 29 | 9.6,112635.00 30 | 10.3,122391.00 31 | 10.5,121872.00 32 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseData/SalaryPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DatabaseData 4 | { 5 | public class SalaryPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedSalary; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseData/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "connectionString": "{SQL connection string}" 3 | } 4 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseLoader/DatabaseLoader.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseLoader/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DbLoader 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8)] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseLoader/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DbLoader 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedHouseValue { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseLoader/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Configuration; 2 | using Microsoft.ML; 3 | using Microsoft.ML.Data; 4 | using System; 5 | using System.Data; 6 | using System.Data.Common; 7 | using System.Data.SqlClient; 8 | using System.IO; 9 | using System.Linq; 10 | 11 | namespace DbLoader 12 | { 13 | class Program 14 | { 15 | static void Main(string[] args) 16 | { 17 | var builder = new ConfigurationBuilder() 18 | .SetBasePath(Directory.GetCurrentDirectory()) 19 | .AddJsonFile("config.json"); 20 | 21 | var configuration = builder.Build(); 22 | var connectionString = configuration["connectionString"]; 23 | 24 | var loaderColumns = new DatabaseLoader.Column[] 25 | { 26 | new DatabaseLoader.Column() { Name = "Longitude", Type = DbType.Single }, 27 | new DatabaseLoader.Column() { Name = "Latitude", Type = DbType.Single }, 28 | new DatabaseLoader.Column() { Name = "HousingMedianAge", Type = DbType.Single }, 29 | new DatabaseLoader.Column() { Name = "TotalRooms", Type = DbType.Single }, 30 | new DatabaseLoader.Column() { Name = "TotalBedrooms", Type = DbType.Single }, 31 | new DatabaseLoader.Column() { Name = "Population", Type = DbType.Single }, 32 | new DatabaseLoader.Column() { Name = "Households", Type = DbType.Single }, 33 | new DatabaseLoader.Column() { Name = "MedianIncome", Type = DbType.Single }, 34 | new DatabaseLoader.Column() { Name = "MedianHouseValue", Type = DbType.Single }, 35 | new DatabaseLoader.Column() { Name = "OceanProximity", Type = DbType.String } 36 | }; 37 | 38 | var connection = new SqlConnection(connectionString); 39 | var factory = DbProviderFactories.GetFactory(connection); 40 | 41 | var context = new MLContext(); 42 | 43 | var loader = context.Data.CreateDatabaseLoader(loaderColumns); 44 | 45 | var dbSource = new DatabaseSource(factory, connectionString, 46 | "SELECT * FROM salarydb.dbo.Housing"); 47 | 48 | var data = loader.Load(dbSource); 49 | 50 | var preview = data.Preview(); 51 | 52 | var testTrainSplit = context.Data.TrainTestSplit(data, testFraction: 0.2); 53 | 54 | var features = data.Schema 55 | .Select(col => col.Name) 56 | .Where(colName => colName != "MedianHouseValue" && colName != "OceanProximity") 57 | .ToArray(); 58 | 59 | var pipeline = context.Transforms.Text.FeaturizeText("Text", "OceanProximity") 60 | .Append(context.Transforms.Concatenate("Features", features)) 61 | .Append(context.Transforms.Concatenate("Features", "Text")) 62 | .Append(context.Regression.Trainers.LbfgsPoissonRegression(featureColumnName: "Features", labelColumnName: "MedianHouseValue")); 63 | 64 | var model = pipeline.Fit(testTrainSplit.TestSet); 65 | 66 | var predictionFunc = context.Model.CreatePredictionEngine(model); 67 | 68 | var prediction = predictionFunc.Predict(new HousingData 69 | { 70 | Longitude = -122.25f, 71 | Latitude = 37.85f, 72 | HousingMedianAge = 55.0f, 73 | TotalRooms = 1627.0f, 74 | TotalBedrooms = 235.0f, 75 | Population = 322.0f, 76 | Households = 120.0f, 77 | MedianIncome = 8.3014f, 78 | OceanProximity = "NEAR BAY" 79 | }); 80 | 81 | Console.WriteLine($"Prediction - {prediction.PredictedHouseValue}"); 82 | 83 | Console.ReadLine(); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /MLNetExamples/DatabaseLoader/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "connectionString": "{SQL connection string}" 3 | } 4 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetwork/DeepNeuralNetwork.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7.1 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetwork/ImageData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DeepNeuralNetwork 4 | { 5 | public class ImageData 6 | { 7 | [LoadColumn(0)] 8 | public string ImagePath { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public string Label { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetwork/ImagePrediction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DeepNeuralNetwork 6 | { 7 | public class ImagePrediction 8 | { 9 | public float[] Score { get; set; } 10 | 11 | public uint PredictedLabel { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetwork/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System; 4 | using System.IO; 5 | using System.Linq; 6 | 7 | namespace DeepNeuralNetwork 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var imagesFolder = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "images"); 14 | 15 | var files = Directory.GetFiles(imagesFolder, "*", SearchOption.AllDirectories); 16 | 17 | var images = files.Select(file => new ImageData 18 | { 19 | ImagePath = file, 20 | Label = Directory.GetParent(file).Name 21 | }); 22 | 23 | var context = new MLContext(); 24 | 25 | var imageData = context.Data.LoadFromEnumerable(images); 26 | var imageDataShuffled = context.Data.ShuffleRows(imageData); 27 | 28 | var testTrainData = context.Data.TrainTestSplit(imageDataShuffled, testFraction: 0.2); 29 | 30 | var validationData = context.Transforms.Conversion.MapValueToKey("LabelKey", "Label", keyOrdinality: Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality.ByValue) 31 | .Fit(testTrainData.TestSet) 32 | .Transform(testTrainData.TestSet); 33 | 34 | var pipeline = context.Transforms.Conversion.MapValueToKey("LabelKey", "Label", keyOrdinality: Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality.ByValue) 35 | .Append(context.Model.ImageClassification( 36 | "ImagePath", 37 | "LabelKey", 38 | arch: Microsoft.ML.Transforms.ImageClassificationEstimator.Architecture.ResnetV2101, 39 | epoch: 100, 40 | batchSize: 10, 41 | metricsCallback: Console.WriteLine, 42 | validationSet: validationData)); 43 | 44 | var model = pipeline.Fit(testTrainData.TrainSet); 45 | 46 | var predicions = model.Transform(testTrainData.TestSet); 47 | 48 | var metrics = context.MulticlassClassification.Evaluate(predicions, labelColumnName: "LabelKey", predictedLabelColumnName: "PredictedLabel"); 49 | 50 | Console.WriteLine(Environment.NewLine); 51 | Console.WriteLine($"Log loss - {metrics.LogLoss}"); 52 | 53 | var predictionEngine = context.Model.CreatePredictionEngine(model); 54 | 55 | var testImagesFolder = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "test"); 56 | 57 | var testFiles = Directory.GetFiles(testImagesFolder, "*", SearchOption.AllDirectories); 58 | 59 | var testImages = testFiles.Select(file => new ImageData 60 | { 61 | ImagePath = file 62 | }); 63 | 64 | VBuffer> keys = default; 65 | predictionEngine.OutputSchema["LabelKey"].GetKeyValues(ref keys); 66 | 67 | var originalLabels = keys.DenseValues().ToArray(); 68 | 69 | Console.WriteLine(Environment.NewLine); 70 | 71 | foreach (var image in testImages) 72 | { 73 | var prediction = predictionEngine.Predict(image); 74 | 75 | var labelIndex = prediction.PredictedLabel; 76 | 77 | Console.WriteLine($"Image : {Path.GetFileName(image.ImagePath)}, Score : {prediction.Score.Max()}, Predicted Label : {originalLabels[labelIndex]}"); 78 | } 79 | 80 | context.Model.Save(model, imageData.Schema, "./dnn_model.zip"); 81 | 82 | Console.ReadLine(); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetworkUpdate/ImageData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace DeepNeuralNetworkUpdate 4 | { 5 | public class ImageData 6 | { 7 | public string ImagePath { get; set; } 8 | 9 | public string Label { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetworkUpdate/ImageModelInput.cs: -------------------------------------------------------------------------------- 1 | namespace DeepNeuralNetworkUpdate 2 | { 3 | public class ImageModelInput 4 | { 5 | public byte[] Image { get; set; } 6 | 7 | public uint LabelAsKey { get; set; } 8 | 9 | public string ImagePath { get; set; } 10 | 11 | public string Label { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetworkUpdate/ImagePrediction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DeepNeuralNetworkUpdate 6 | { 7 | public class ImagePrediction 8 | { 9 | public string ImagePath { get; set; } 10 | 11 | public string Label { get; set; } 12 | 13 | public string PredictedLabel { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MLNetExamples/DeepNeuralNetworkUpdate/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using Microsoft.ML.Vision; 4 | using System; 5 | using System.IO; 6 | using System.Linq; 7 | 8 | namespace DeepNeuralNetworkUpdate 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | var imagesFolder = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "images"); 15 | 16 | var files = Directory.GetFiles(imagesFolder, "*", SearchOption.AllDirectories); 17 | 18 | var images = files.Select(file => new ImageData 19 | { 20 | ImagePath = file, 21 | Label = Directory.GetParent(file).Name 22 | }); 23 | 24 | var context = new MLContext(); 25 | 26 | var imageData = context.Data.LoadFromEnumerable(images); 27 | var imageDataShuffled = context.Data.ShuffleRows(imageData); 28 | 29 | var testTrainData = context.Data.TrainTestSplit(imageDataShuffled, testFraction: 0.2); 30 | 31 | var validationData = context.Transforms.Conversion.MapValueToKey("LabelKey", "Label", keyOrdinality: Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality.ByValue) 32 | .Append(context.Transforms.LoadRawImageBytes("Image", imagesFolder, "ImagePath")) 33 | .Fit(testTrainData.TestSet) 34 | .Transform(testTrainData.TestSet); 35 | 36 | var imagesPipeline = context.Transforms.Conversion.MapValueToKey("LabelKey", "Label", keyOrdinality: Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality.ByValue) 37 | .Append(context.Transforms.LoadRawImageBytes("Image", imagesFolder, "ImagePath")); 38 | 39 | var imageDataModel = imagesPipeline.Fit(testTrainData.TrainSet); 40 | 41 | var imageDataView = imageDataModel.Transform(testTrainData.TrainSet); 42 | 43 | var options = new ImageClassificationTrainer.Options() 44 | { 45 | Arch = ImageClassificationTrainer.Architecture.ResnetV250, 46 | Epoch = 100, 47 | BatchSize = 20, 48 | LearningRate = 0.01f, 49 | LabelColumnName = "LabelKey", 50 | FeatureColumnName = "Image", 51 | ValidationSet = validationData 52 | }; 53 | 54 | var pipeline = context.MulticlassClassification.Trainers.ImageClassification(options) 55 | .Append(context.Transforms.Conversion.MapKeyToValue("PredictedLabel")); 56 | 57 | var model = pipeline.Fit(imageDataView); 58 | 59 | var predictionEngine = context.Model.CreatePredictionEngine(model); 60 | 61 | var testImagesFolder = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "test"); 62 | 63 | var testFiles = Directory.GetFiles(testImagesFolder, "*", SearchOption.AllDirectories); 64 | 65 | var testImages = testFiles.Select(file => new ImageModelInput 66 | { 67 | ImagePath = file 68 | }); 69 | 70 | Console.WriteLine(Environment.NewLine); 71 | 72 | var testImagesData = context.Data.LoadFromEnumerable(testImages); 73 | 74 | var testImageDataView = imagesPipeline.Fit(testImagesData).Transform(testImagesData); 75 | 76 | var predictions = model.Transform(testImageDataView); 77 | 78 | var testPredictions = context.Data.CreateEnumerable(predictions, reuseRowObject: false); 79 | 80 | foreach (var prediction in testPredictions) 81 | { 82 | var labelIndex = prediction.PredictedLabel; 83 | 84 | Console.WriteLine($"Image: {Path.GetFileName(prediction.ImagePath)}, Predicted Label: {prediction.PredictedLabel}"); 85 | } 86 | 87 | context.Model.Save(model, imageData.Schema, "./dnn_model.zip"); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /MLNetExamples/EntityFrameworkData/EntityFrameworkData.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers 15 | 16 | 17 | 18 | 19 | 20 | 21 | PreserveNewest 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /MLNetExamples/EntityFrameworkData/MLNetExampleContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace EntityFrameworkData 4 | { 5 | public partial class MLNetExampleContext : DbContext 6 | { 7 | public DbSet Salaries { get; set; } 8 | 9 | public MLNetExampleContext(DbContextOptions options) 10 | : base(options) 11 | { 12 | } 13 | 14 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 15 | { 16 | if (!optionsBuilder.IsConfigured) 17 | { 18 | optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=EFProviders.InMemory;Trusted_Connection=True;ConnectRetryCount=0"); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /MLNetExamples/EntityFrameworkData/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.ML; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | 8 | namespace EntityFrameworkData 9 | { 10 | class Program 11 | { 12 | private static IEnumerable data; 13 | 14 | static void Main(string[] args) 15 | { 16 | var fileData = ReadFromFile("./SalaryData.csv"); 17 | 18 | data = WriteAndReadToDatabase(fileData); 19 | 20 | var context = new MLContext(); 21 | 22 | var mlData = context.Data.LoadFromEnumerable(data); 23 | 24 | var trainTestData = context.Regression.TrainTestSplit(mlData); 25 | 26 | var pipeline = context.Transforms.Concatenate("Features", "YearsExperience") 27 | .Append(context.Transforms.CopyColumns(("Label", "Salary"))) 28 | .Append(context.Regression.Trainers.FastTree()); 29 | 30 | var model = pipeline.Fit(trainTestData.TrainSet); 31 | 32 | var prediction = model.Transform(trainTestData.TestSet); 33 | 34 | var metrics = context.Regression.Evaluate(prediction); 35 | 36 | var predictionFunc = model.CreatePredictionEngine(context); 37 | 38 | var salaryPrediction = predictionFunc.Predict(new SalaryData { YearsExperience = 11 }); 39 | 40 | Console.WriteLine($"Prediction - {salaryPrediction.PredictedSalary}"); 41 | Console.ReadLine(); 42 | } 43 | 44 | private static IEnumerable WriteAndReadToDatabase(IEnumerable fileData) 45 | { 46 | var options = new DbContextOptionsBuilder() 47 | .UseInMemoryDatabase(databaseName: "TestData") 48 | .Options; 49 | 50 | using (var context = new MLNetExampleContext(options)) 51 | { 52 | foreach (var item in fileData) 53 | { 54 | context.Salaries.Add(item); 55 | } 56 | 57 | var count = context.SaveChanges(); 58 | 59 | return context.Salaries.ToList(); 60 | } 61 | } 62 | 63 | private static IEnumerable ReadFromFile(string filePath) 64 | { 65 | var data = File.ReadAllLines(filePath) 66 | .Skip(1) 67 | .Select(l => l.Split(',')) 68 | .Select(i => new SalaryData 69 | { 70 | YearsExperience = float.Parse(i[0]), 71 | Salary = float.Parse(i[1]) 72 | }); 73 | 74 | return data; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /MLNetExamples/EntityFrameworkData/SalaryData.cs: -------------------------------------------------------------------------------- 1 | namespace EntityFrameworkData 2 | { 3 | public class SalaryData 4 | { 5 | public int SalaryDataId { get; set; } 6 | public float YearsExperience { get; set; } 7 | 8 | public float Salary { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/EntityFrameworkData/SalaryData.csv: -------------------------------------------------------------------------------- 1 | YearsExperience,Salary 2 | 1.1,39343.00 3 | 1.3,46205.00 4 | 1.5,37731.00 5 | 2.0,43525.00 6 | 2.2,39891.00 7 | 2.9,56642.00 8 | 3.0,60150.00 9 | 3.2,54445.00 10 | 3.2,64445.00 11 | 3.7,57189.00 12 | 3.9,63218.00 13 | 4.0,55794.00 14 | 4.0,56957.00 15 | 4.1,57081.00 16 | 4.5,61111.00 17 | 4.9,67938.00 18 | 5.1,66029.00 19 | 5.3,83088.00 20 | 5.9,81363.00 21 | 6.0,93940.00 22 | 6.8,91738.00 23 | 7.1,98273.00 24 | 7.9,101302.00 25 | 8.2,113812.00 26 | 8.7,109431.00 27 | 9.0,105582.00 28 | 9.5,116969.00 29 | 9.6,112635.00 30 | 10.3,122391.00 31 | 10.5,121872.00 32 | -------------------------------------------------------------------------------- /MLNetExamples/EntityFrameworkData/SalaryPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace EntityFrameworkData 4 | { 5 | public class SalaryPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedSalary; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/EventHubPredict/EventHubPredict.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | preview 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /MLNetExamples/EventHubPredict/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace EventHubPredict 5 | { 6 | public class HousingData 7 | { 8 | [LoadColumn(0)] 9 | [JsonPropertyName("longitude")] 10 | public float Longitude { get; set; } 11 | 12 | [LoadColumn(1)] 13 | [JsonPropertyName("latitude")] 14 | public float Latitude { get; set; } 15 | 16 | [LoadColumn(2)] 17 | [JsonPropertyName("housing_median_age")] 18 | public float HousingMedianAge { get; set; } 19 | 20 | [LoadColumn(3)] 21 | [JsonPropertyName("total_rooms")] 22 | public float TotalRooms { get; set; } 23 | 24 | [LoadColumn(4)] 25 | [JsonPropertyName("total_bedrooms")] 26 | public float TotalBedrooms { get; set; } 27 | 28 | [LoadColumn(5)] 29 | [JsonPropertyName("population")] 30 | public float Population { get; set; } 31 | 32 | [LoadColumn(6)] 33 | [JsonPropertyName("households")] 34 | public float Households { get; set; } 35 | 36 | [LoadColumn(7)] 37 | [JsonPropertyName("median_income")] 38 | public float MedianIncome { get; set; } 39 | 40 | [LoadColumn(8), ColumnName("Label")] 41 | public float MedianHouseValue { get; set; } 42 | 43 | [LoadColumn(9)] 44 | [JsonPropertyName("ocean_proximity")] 45 | public string OceanProximity { get; set; } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /MLNetExamples/EventHubPredict/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace EventHubPredict 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedHouseValue { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/EventHubPredict/Model/housing-model.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/EventHubPredict/Model/housing-model.zip -------------------------------------------------------------------------------- /MLNetExamples/EventHubPredict/Program.cs: -------------------------------------------------------------------------------- 1 | using Azure.Messaging.EventHubs; 2 | using Azure.Messaging.EventHubs.Consumer; 3 | using Azure.Messaging.EventHubs.Processor; 4 | using Azure.Storage.Blobs; 5 | using Microsoft.ML; 6 | using System; 7 | using System.Text; 8 | using System.Text.Json; 9 | using System.Threading.Tasks; 10 | 11 | namespace EventHubPredict 12 | { 13 | class Program 14 | { 15 | private const string EventHubConnectionString = "Event hub connection string"; 16 | private const string EventHubName = "mlneteh "; 17 | private const string blobStorageConnectionString = "Blob storage connection string"; 18 | private const string blobContainerName = "events"; 19 | 20 | static async Task Main(string[] args) 21 | { 22 | string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; 23 | 24 | var storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName); 25 | 26 | var processor = new EventProcessorClient(storageClient, consumerGroup, EventHubConnectionString, EventHubName); 27 | 28 | processor.ProcessEventAsync += ProcessEventHandler; 29 | processor.ProcessErrorAsync += ProcessErrorHandler; 30 | 31 | await processor.StartProcessingAsync(); 32 | 33 | await Task.Delay(TimeSpan.FromSeconds(5)); 34 | 35 | await processor.StopProcessingAsync(); 36 | } 37 | 38 | private static Task ProcessErrorHandler(ProcessErrorEventArgs arg) 39 | { 40 | Console.WriteLine($"\tPartition '{ arg.PartitionId}': an unhandled exception was encountered."); 41 | Console.WriteLine(arg.Exception.Message); 42 | 43 | return Task.CompletedTask; 44 | } 45 | 46 | private static Task ProcessEventHandler(ProcessEventArgs arg) 47 | { 48 | var payload = Encoding.UTF8.GetString(arg.Data.Body.ToArray()); 49 | 50 | var data = JsonSerializer.Deserialize(payload); 51 | 52 | var context = new MLContext(); 53 | 54 | var model = context.Model.Load("./Model/housing-model.zip", out DataViewSchema inputSchema); 55 | 56 | var predictionEngine = context.Model.CreatePredictionEngine(model, inputSchema: inputSchema); 57 | 58 | var prediction = predictionEngine.Predict(data); 59 | 60 | Console.WriteLine($"Prediction is {prediction.PredictedHouseValue}"); 61 | 62 | return Task.CompletedTask; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /MLNetExamples/ExpressionTransform/ExpressionOutput.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace ExpressionTransform 6 | { 7 | public class ExpressionOutput 8 | { 9 | public int TeamsManagedOutput { get; set; } 10 | public float SquareRootOutput { get; set; } 11 | public string ToLowerOutput { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/ExpressionTransform/ExpressionTransform.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MLNetExamples/ExpressionTransform/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace ExpressionTransform 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var context = new MLContext(); 12 | 13 | var inputData = new List 14 | { 15 | new SalaryInput { IsManager = false, YearsExperience = 1f, Title = "Developer" }, 16 | new SalaryInput { IsManager = true, YearsExperience = 9f, Title = "Director", NumberOfTeamsManaged = 2 }, 17 | new SalaryInput { IsManager = false, YearsExperience = 4f, Title = "Analyst" } 18 | }; 19 | 20 | var data = context.Data.LoadFromEnumerable(inputData); 21 | 22 | var expressions = context.Transforms.Expression("SquareRootOutput", "(x) => sqrt(x)", "YearsExperience") 23 | .Append(context.Transforms.Expression("TeamsManagedOutput", "(x, y) => x ? y : 0", nameof(SalaryInput.IsManager), nameof(SalaryInput.NumberOfTeamsManaged))) 24 | .Append(context.Transforms.Expression("ToLowerOutput", "(x) => lower(x)", nameof(SalaryInput.Title))); 25 | 26 | var expressionsTransformed = expressions.Fit(data).Transform(data); 27 | 28 | var expressionsData = context.Data.CreateEnumerable(expressionsTransformed, 29 | reuseRowObject: false); 30 | 31 | foreach (var expression in expressionsData) 32 | { 33 | Console.WriteLine($"Square Root - {expression.SquareRootOutput}"); 34 | Console.WriteLine($"Teams Managed - {expression.TeamsManagedOutput}"); 35 | Console.WriteLine($"To Lower - {expression.ToLowerOutput}"); 36 | Console.WriteLine(Environment.NewLine); 37 | } 38 | 39 | Console.ReadLine(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MLNetExamples/ExpressionTransform/SalaryInput.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace ExpressionTransform 6 | { 7 | public class SalaryInput 8 | { 9 | public float YearsExperience; 10 | 11 | public bool IsManager; 12 | 13 | public string Title; 14 | 15 | public int NumberOfTeamsManaged; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /MLNetExamples/FSharpRegression/FSharpRegression.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | x64 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /MLNetExamples/FSharpRegression/Program.fs: -------------------------------------------------------------------------------- 1 | open System 2 | open Microsoft.ML 3 | open Microsoft.ML.Data 4 | 5 | let trainFile = "./SalaryTrain.csv" 6 | let testFile = "./SalaryTest.csv" 7 | 8 | type SalaryData = { 9 | [] 10 | YearsExperience: float32; 11 | 12 | [] 13 | Salary: float32; 14 | } 15 | 16 | [] 17 | type SalaryPrediction = { 18 | [] 19 | PredictedSalary: float32; 20 | } 21 | 22 | [] 23 | let main argv = 24 | let context = MLContext() 25 | 26 | let loadDataByPath path = 27 | context.Data.LoadFromTextFile(path, hasHeader=true, separatorChar=',') 28 | 29 | //let trainData = context.Data.LoadFromTextFile(trainFile, hasHeader=true, separatorChar=',') 30 | //let testData = context.Data.LoadFromTextFile(testFile, hasHeader=true, separatorChar=',') 31 | 32 | let trainData = loadDataByPath trainFile 33 | let testData = loadDataByPath testFile 34 | 35 | let pipeline = 36 | EstimatorChain() 37 | .Append(context.Transforms.Concatenate("Features", "YearsExperience")) 38 | .Append(context.Transforms.CopyColumns(("Label", "Salary"))) 39 | .Append(context.Regression.Trainers.LbfgsPoissonRegression()) 40 | 41 | printfn "Training model..." 42 | let model = pipeline.Fit trainData 43 | 44 | let predictions = model.Transform testData 45 | 46 | printfn "Evaluating model..." 47 | let metrics = context.Regression.Evaluate(predictions, "Label", "Score") 48 | 49 | printfn "RMS - %.2f" metrics.RootMeanSquaredError 50 | printfn "R^2 - %.2f" metrics.RSquared 51 | 52 | let predictionFunc = context.Model.CreatePredictionEngine(model) 53 | 54 | let salaryPrediction = { 55 | YearsExperience = 8.0f 56 | Salary = 0.0f 57 | } 58 | 59 | let prediction = predictionFunc.Predict(salaryPrediction) 60 | 61 | printfn "Prediction - %.2f" prediction.PredictedSalary 62 | 63 | Console.ReadLine() |> ignore 64 | 65 | 0 -------------------------------------------------------------------------------- /MLNetExamples/FSharpRegression/SalaryTest.csv: -------------------------------------------------------------------------------- 1 | YearsExperience,Salary 2 | 2.0,43525.0 3 | 3.0,60150.0 4 | 3.2,54445.0 5 | 3.2,64445.0 6 | 3.9,63218.0 7 | 4.1,57081.0 8 | 5.1,66029.0 9 | 7.1,98273.0 10 | 8.2,113812.0 11 | -------------------------------------------------------------------------------- /MLNetExamples/FSharpRegression/SalaryTrain.csv: -------------------------------------------------------------------------------- 1 | YearsExperience,Salary 2 | 2.2,39891.0 3 | 5.9,81363.0 4 | 10.5,121872.0 5 | 9.0,105582.0 6 | 7.9,101302.0 7 | 10.3,122391.0 8 | 4.5,61111.0 9 | 1.1,39343.0 10 | 4.9,67938.0 11 | 4.0,56957.0 12 | 8.7,109431.0 13 | 1.5,37731.0 14 | 1.3,46205.0 15 | 5.3,83088.0 16 | 9.5,116969.0 17 | 6.8,91738.0 18 | 9.6,112635.0 19 | 4.0,55794.0 20 | 3.7,57189.0 21 | 2.9,56642.0 22 | 6.0,93940.0 23 | -------------------------------------------------------------------------------- /MLNetExamples/FeatureImportance/FeatureImportance.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MLNetExamples/FeatureImportance/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace FeatureImportance 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/FeatureImportance/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace FeatureImportance 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedHouseValue { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/FeatureImportance/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Linq; 4 | 5 | namespace FeatureImportance 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var context = new MLContext(); 12 | 13 | var data = context.Data.LoadFromTextFile("./housing.csv", hasHeader: true, separatorChar: ','); 14 | 15 | var preview = data.Preview(); 16 | 17 | var features = data.Schema 18 | .Select(col => col.Name) 19 | .Where(colName => colName != "Label" && colName != "OceanProximity") 20 | .ToArray(); 21 | 22 | var pipeline = context.Transforms.Text.FeaturizeText("Text", "OceanProximity") 23 | .Append(context.Transforms.Concatenate("Features", features)) 24 | .Append(context.Regression.Trainers.LbfgsPoissonRegression()); 25 | 26 | var model = pipeline.Fit(data); 27 | var transformedData = model.Transform(data); 28 | 29 | // Get weights of model 30 | var linearModel = model.LastTransformer.Model; 31 | 32 | var weights = linearModel.Weights; 33 | 34 | Console.WriteLine("Weights:"); 35 | var weightsResult = ""; 36 | 37 | foreach (var weight in weights) 38 | { 39 | weightsResult += $"{weight} "; 40 | } 41 | 42 | Console.WriteLine(weightsResult); 43 | Console.WriteLine(Environment.NewLine); 44 | 45 | // Get global feature importance 46 | var lastTransformer = model.LastTransformer; 47 | 48 | var featureImportance = context.Regression.PermutationFeatureImportance(lastTransformer, transformedData); 49 | 50 | Console.WriteLine("Global feature importance:"); 51 | for (int i = 0; i < featureImportance.Count(); i++) 52 | { 53 | Console.WriteLine($"Feature - {features[i]}: Difference in RMS - {featureImportance[i].RootMeanSquaredError.Mean}"); 54 | } 55 | 56 | Console.WriteLine(Environment.NewLine); 57 | 58 | // Get feature importance for each row 59 | var featureContribution = context.Transforms.CalculateFeatureContribution(lastTransformer, normalize: false); 60 | 61 | var featureContributionResults = featureContribution.Fit(transformedData).Transform(transformedData); 62 | 63 | var topTenRows = context.Data.TakeRows(featureContributionResults, 10); 64 | 65 | var scoringEnumerator = context.Data.CreateEnumerable(topTenRows, reuseRowObject: true); 66 | 67 | Console.WriteLine("Row feature importance:"); 68 | Console.WriteLine("Households - Housing Median Age - Median Income - Ocean Proximity"); 69 | var globalResults = ""; 70 | foreach (var row in scoringEnumerator) 71 | { 72 | globalResults += $"{row.Households} - {row.HousingMedianAge} = {row.MedianIncome} - {row.OceanProximity}\n"; 73 | } 74 | 75 | Console.WriteLine(globalResults); 76 | 77 | Console.ReadLine(); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /MLNetExamples/InferColumns/InferColumns.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/InferColumns/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.AutoML; 3 | using System; 4 | 5 | namespace InferColumns 6 | { 7 | class Program 8 | { 9 | private static string FILE_PATH = "./housing.csv"; 10 | private static string LABEL_NAME = "median_house_value"; 11 | 12 | static void Main(string[] args) 13 | { 14 | var context = new MLContext(); 15 | 16 | // Inferring with column information 17 | //var columnInfo = new ColumnInformation 18 | //{ 19 | // LabelColumnName = LABEL_NAME 20 | //}; 21 | //var inference = context.Auto().InferColumns(FILE_PATH, columnInfo, separatorChar: ','); 22 | 23 | // Inferring with label column index 24 | //var inference = context.Auto().InferColumns(FILE_PATH, labelColumnIndex: 8, hasHeader: true, separatorChar: ','); 25 | 26 | // Inferring with label column name 27 | var inference = context.Auto().InferColumns(FILE_PATH, labelColumnName: LABEL_NAME, separatorChar: ','); 28 | 29 | var loader = context.Data.CreateTextLoader(inference.TextLoaderOptions); 30 | 31 | var data = loader.Load(FILE_PATH); 32 | 33 | var split = context.Data.TrainTestSplit(data, testFraction: 0.2); 34 | 35 | var experimentSettings = new RegressionExperimentSettings 36 | { 37 | MaxExperimentTimeInSeconds = 60, 38 | OptimizingMetric = RegressionMetric.RSquared 39 | }; 40 | 41 | var experimentResult = context.Auto() 42 | .CreateRegressionExperiment(experimentSettings) 43 | .Execute(split.TrainSet, labelColumnName: LABEL_NAME); 44 | 45 | var predictions = experimentResult.BestRun.Model.Transform(split.TestSet); 46 | 47 | var metrics = context.Regression.Evaluate(predictions, LABEL_NAME); 48 | 49 | Console.WriteLine($"R^2: {metrics.RSquared}"); 50 | Console.Write(Environment.NewLine); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /MLNetExamples/LargeFeatures/LargeFeatures.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/LargeFeatures/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System; 4 | using System.Linq; 5 | 6 | namespace LargeFeatures 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var data = context.Data.LoadFromTextFile("./features.csv", hasHeader: false, separatorChar: ','); 15 | 16 | //data.GetColumn 17 | 18 | var pipeline = context.Transforms.Conversion.MapValueToKey("Label", "Gas") 19 | .Append(context.MulticlassClassification.Trainers.LbfgsMaximumEntropy()); 20 | 21 | var model = pipeline.Fit(data); 22 | 23 | var predictions = model.Transform(data); 24 | 25 | var metrics = context.MulticlassClassification.Evaluate(predictions); 26 | 27 | Console.WriteLine($"Log loss - {metrics.LogLoss}"); 28 | 29 | Console.ReadLine(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MLNetExamples/LargeFeatures/SensorData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace LargeFeatures 4 | { 5 | internal class SensorData 6 | { 7 | [LoadColumn(0)] 8 | public float ExperienceNumber { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public string Batch { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float AcetoneConcentration { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float EthanolConcentration { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public string Gas { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public string Lab { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public string ColorCode { get; set; } 27 | 28 | [LoadColumn(7, 431)] 29 | [VectorType(431)] 30 | public float[] Features { get; set; } 31 | } 32 | } -------------------------------------------------------------------------------- /MLNetExamples/LargeFeatures/SensorPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace LargeFeatures 4 | { 5 | public class SensorPrediction 6 | { 7 | [ColumnName("PredictedLabel")] 8 | public bool Prediction; 9 | 10 | public float Score; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /MLNetExamples/ModelExplainability/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace ModelExplainability 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/ModelExplainability/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace ModelExplainability 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedHouseValue { get; set; } 9 | 10 | public float[] FeatureContributions { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /MLNetExamples/ModelExplainability/ModelExplainability.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/ModelExplainability/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Linq; 4 | 5 | namespace ModelExplainability 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var context = new MLContext(); 12 | 13 | var data = context.Data.LoadFromTextFile("./housing.csv", hasHeader: true, separatorChar: ','); 14 | 15 | var features = data.Schema 16 | .Select(col => col.Name) 17 | .Where(colName => colName != "Label" && colName != "OceanProximity") 18 | .ToArray(); 19 | 20 | var pipeline = context.Transforms.Text.FeaturizeText("Text", "OceanProximity") 21 | .Append(context.Transforms.Concatenate("Features", features)) 22 | .Append(context.Regression.Trainers.LbfgsPoissonRegression()); 23 | 24 | var model = pipeline.Fit(data); 25 | var transformedData = model.Transform(data); 26 | 27 | // Get weights of model 28 | var linearModel = model.LastTransformer.Model; 29 | 30 | var weights = linearModel.Weights; 31 | 32 | Console.WriteLine("Weights:"); 33 | 34 | for (int i = 0; i < features.Length; i++) 35 | { 36 | Console.WriteLine($"Feature {features[i]} has weight {weights[i]}"); 37 | } 38 | 39 | Console.WriteLine(Environment.NewLine); 40 | 41 | // Get global feature importance 42 | var lastTransformer = model.LastTransformer; 43 | 44 | var featureImportance = context.Regression.PermutationFeatureImportance(lastTransformer, transformedData); 45 | 46 | Console.WriteLine("Global feature importance:"); 47 | for (int i = 0; i < featureImportance.Count(); i++) 48 | { 49 | Console.WriteLine($"Feature - {features[i]}: Difference in RMS - {featureImportance[i].RootMeanSquaredError.Mean}"); 50 | } 51 | 52 | Console.WriteLine(Environment.NewLine); 53 | 54 | // Get feature importance for each row 55 | var firstRow = model.Transform(context.Data.TakeRows(transformedData, 1)); 56 | 57 | var featureContribution = context.Transforms.CalculateFeatureContribution(lastTransformer, normalize: false); 58 | 59 | var featureContributionTransformer = featureContribution.Fit(firstRow); 60 | 61 | var featureContributionPipeline = model.Append(featureContributionTransformer); 62 | 63 | var predictionEngine = context.Model.CreatePredictionEngine(featureContributionPipeline); 64 | 65 | var sampleData = new HousingData 66 | { 67 | Longitude = -122.25f, 68 | Latitude = 37.85f, 69 | HousingMedianAge = 55.0f, 70 | TotalRooms = 1627.0f, 71 | TotalBedrooms = 235.0f, 72 | Population = 322.0f, 73 | Households = 120.0f, 74 | MedianIncome = 8.3014f, 75 | OceanProximity = "NEAR BAY" 76 | }; 77 | 78 | var prediction = predictionEngine.Predict(sampleData); 79 | 80 | Console.WriteLine("Row feature importance:"); 81 | 82 | for (int i = 0; i < prediction.FeatureContributions.Length; i++) 83 | { 84 | Console.WriteLine($"Feature {features[i]} has feature contribution of {prediction.FeatureContributions[i]}"); 85 | } 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /MLNetExamples/NGrams/Input.cs: -------------------------------------------------------------------------------- 1 | namespace NGrams 2 | { 3 | public class Input 4 | { 5 | public string Text { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/NGrams/NGrams.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MLNetExamples/NGrams/Output.cs: -------------------------------------------------------------------------------- 1 | namespace NGrams 2 | { 3 | internal class Output 4 | { 5 | public float[] NGrams { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /MLNetExamples/NGrams/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace NGrams 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var context = new MLContext(); 14 | 15 | var data = new List 16 | { 17 | new Input { Text = "I really enjoy being in jazz band." }, 18 | new Input { Text = "But I'm done for the day and am heading home." } 19 | }; 20 | 21 | var dataView = context.Data.LoadFromEnumerable(data); 22 | 23 | var nGramPipeline = context.Transforms.Text.TokenizeIntoWords("Tokens", nameof(Input.Text)) 24 | .Append(context.Transforms.Conversion.MapValueToKey("Tokens") 25 | .Append(context.Transforms.Text.ProduceNgrams( 26 | "NGrams", 27 | "Tokens", 28 | ngramLength: 2, 29 | useAllLengths: false, 30 | weighting: Microsoft.ML.Transforms.Text.NgramExtractingEstimator.WeightingCriteria.Tf))); 31 | 32 | var fitData = nGramPipeline.Fit(dataView); 33 | var dataTransformed = fitData.Transform(dataView); 34 | 35 | var preview = dataTransformed.Preview(); 36 | 37 | VBuffer> slotNames = default; 38 | dataTransformed.Schema["NGrams"].GetSlotNames(ref slotNames); 39 | 40 | var nGramsColumn = dataTransformed.GetColumn>(dataTransformed.Schema["NGrams"]); 41 | var slots = slotNames.GetValues(); 42 | 43 | Console.WriteLine("NGrams"); 44 | 45 | foreach (var row in nGramsColumn) 46 | { 47 | foreach (var item in row.Items()) 48 | { 49 | Console.WriteLine($"{slots[item.Key]} "); 50 | } 51 | 52 | Console.WriteLine(); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /MLNetExamples/NormalizeText/Input.cs: -------------------------------------------------------------------------------- 1 | namespace NormalizeText 2 | { 3 | internal class Input 4 | { 5 | public string Text { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /MLNetExamples/NormalizeText/NormalizeText.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MLNetExamples/NormalizeText/Output.cs: -------------------------------------------------------------------------------- 1 | namespace NormalizeText 2 | { 3 | public class Output 4 | { 5 | public string NormalizedText { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/NormalizeText/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Security.Principal; 5 | 6 | namespace NormalizeText 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var emptyData = context.Data.LoadFromEnumerable(new List()); 15 | 16 | var normalizedPipeline = context.Transforms.Text.NormalizeText("NormalizedText", "Text", 17 | Microsoft.ML.Transforms.Text.TextNormalizingEstimator.CaseMode.Upper, keepDiacritics: false, keepPunctuations: false, keepNumbers: true); 18 | 19 | var normalizeTransformer = normalizedPipeline.Fit(emptyData); 20 | 21 | var predictionEngine = context.Model.CreatePredictionEngine(normalizeTransformer); 22 | 23 | var text = new Input { Text = "Whisk the batter for the crêpe, then let it sit for 5 minutes." }; 24 | 25 | var normalizedText = predictionEngine.Predict(text); 26 | 27 | Console.WriteLine($"Original text - {text.Text}"); 28 | Console.WriteLine(Environment.NewLine); 29 | Console.WriteLine($"Normalized text - {normalizedText.NormalizedText}"); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MLNetExamples/NullValues/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace NullValues 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8)] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/NullValues/MissingData.cs: -------------------------------------------------------------------------------- 1 | namespace NullValues 2 | { 3 | internal class MissingData : HousingData 4 | { 5 | public bool[] MissingValues { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /MLNetExamples/NullValues/NullValues.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/NullValues/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace NullValues 7 | { 8 | class Program 9 | { 10 | private static readonly Dictionary MISSING_INDEXES = new Dictionary(); 11 | 12 | static void Main(string[] args) 13 | { 14 | var context = new MLContext(); 15 | 16 | var data = context.Data.LoadFromTextFile("./housing.csv", hasHeader: true, separatorChar: ','); 17 | 18 | var columns = data.Schema 19 | .Select(col => col.Name) 20 | .Where(colName => colName != "Label" && colName != "OceanProximity") 21 | .ToArray(); 22 | 23 | // Indicate missing values 24 | var nullTransform = context.Transforms.Concatenate("Features", columns) 25 | .Append(context.Transforms.IndicateMissingValues("MissingValues", "Features")); 26 | 27 | var nullValues = nullTransform.Fit(data).Transform(data); 28 | 29 | var nullData = context.Data.CreateEnumerable(nullValues, 30 | reuseRowObject: false).ToArray(); 31 | 32 | for (int i = 0; i < nullData.Length; i++) 33 | { 34 | if (nullData[i].MissingValues.Any(a => a == true)) 35 | { 36 | var missingIndexes = nullData[i].MissingValues.Select((v, idx) => v ? idx : -1 ) 37 | .Where(idx => idx != -1) 38 | .ToArray(); 39 | 40 | foreach (var index in missingIndexes) 41 | { 42 | var feature = columns[index]; 43 | MISSING_INDEXES.Add(i, index); 44 | 45 | Console.WriteLine($"Feature {feature} in row {i + 1} has missing value"); 46 | } 47 | } 48 | } 49 | 50 | // Replace missing values 51 | var replaceTransform = context.Transforms.Concatenate("Features", columns) 52 | .Append(context.Transforms.ReplaceMissingValues("NewValues", "Features", 53 | Microsoft.ML.Transforms.MissingValueReplacingEstimator.ReplacementMode.Mean)); 54 | 55 | var replacedValues = replaceTransform.Fit(data).Transform(data); 56 | 57 | var replacedData = context.Data.CreateEnumerable(replacedValues, 58 | reuseRowObject: false).ToArray(); 59 | 60 | for (int i = 0; i < replacedData.Count(); i++) 61 | { 62 | foreach (var index in MISSING_INDEXES) 63 | { 64 | if (i == index.Key) 65 | { 66 | Console.WriteLine($"New value - {replacedData[i].NewValues[index.Value]}"); 67 | } 68 | } 69 | } 70 | 71 | Console.ReadLine(); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /MLNetExamples/NullValues/ReplacedValues.cs: -------------------------------------------------------------------------------- 1 | namespace NullValues 2 | { 3 | internal class ReplacedValues : HousingData 4 | { 5 | public float[] NewValues { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /MLNetExamples/Onnx/Onnx.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PreserveNewest 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /MLNetExamples/Onnx/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System.IO; 4 | 5 | namespace ExportToOnnx 6 | { 7 | class Program 8 | { 9 | private static readonly string MODEL_NAME = "model.onnx"; 10 | 11 | static void Main(string[] args) 12 | { 13 | var context = new MLContext(); 14 | 15 | var textLoader = context.Data.CreateTextLoader(new[] 16 | { 17 | new TextLoader.Column("YearsExperience", DataKind.Single, 0), 18 | new TextLoader.Column("Salary", DataKind.Single, 1), 19 | }, 20 | hasHeader: true, 21 | separatorChar: ','); 22 | 23 | var data = textLoader.Load("./SalaryData.csv"); 24 | 25 | var trainTestData = context.Data.TrainTestSplit(data); 26 | 27 | var pipeline = context.Transforms.Concatenate("Features", "YearsExperience") 28 | .Append(context.Regression.Trainers.Sdca(labelColumnName: "Salary")); 29 | 30 | ITransformer model = pipeline.Fit(trainTestData.TrainSet); 31 | 32 | using (var stream = File.Create(MODEL_NAME)) 33 | { 34 | context.Model.ConvertToOnnx(model, data, stream); 35 | } 36 | 37 | var estimator = context.Transforms.ApplyOnnxModel(MODEL_NAME); 38 | 39 | var newModel = estimator.Fit(trainTestData.TestSet); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MLNetExamples/Onnx/SalaryData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SimpleRegression 4 | { 5 | public class SalaryData 6 | { 7 | [LoadColumn(0)] 8 | public float YearsExperience; 9 | 10 | [LoadColumn(1)] 11 | public float Salary; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/Onnx/SalaryData.csv: -------------------------------------------------------------------------------- 1 | YearsExperience,Salary 2 | 1.1,39343.00 3 | 1.3,46205.00 4 | 1.5,37731.00 5 | 2.0,43525.00 6 | 2.2,39891.00 7 | 2.9,56642.00 8 | 3.0,60150.00 9 | 3.2,54445.00 10 | 3.2,64445.00 11 | 3.7,57189.00 12 | 3.9,63218.00 13 | 4.0,55794.00 14 | 4.0,56957.00 15 | 4.1,57081.00 16 | 4.5,61111.00 17 | 4.9,67938.00 18 | 5.1,66029.00 19 | 5.3,83088.00 20 | 5.9,81363.00 21 | 6.0,93940.00 22 | 6.8,91738.00 23 | 7.1,98273.00 24 | 7.9,101302.00 25 | 8.2,113812.00 26 | 8.7,109431.00 27 | 9.0,105582.00 28 | 9.5,116969.00 29 | 9.6,112635.00 30 | 10.3,122391.00 31 | 10.5,121872.00 32 | -------------------------------------------------------------------------------- /MLNetExamples/Onnx/SalaryPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SimpleRegression 4 | { 5 | public class SalaryPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedSalary; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/PredictDNN/ImageData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace PredictDNN 4 | { 5 | public class ImageData 6 | { 7 | [LoadColumn(0)] 8 | public string ImagePath { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public string Label { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/PredictDNN/ImagePrediction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace PredictDNN 6 | { 7 | public class ImagePrediction 8 | { 9 | public float[] Score { get; set; } 10 | 11 | public uint PredictedLabel { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/PredictDNN/PredictDNN.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7.1 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /MLNetExamples/PredictDNN/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using Microsoft.WindowsAzure.Storage; 4 | using Microsoft.WindowsAzure.Storage.Blob; 5 | using System; 6 | using System.IO; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace PredictDNN 11 | { 12 | class Program 13 | { 14 | static string connectionString = ""; 15 | 16 | static async Task Main(string[] args) 17 | { 18 | var context = new MLContext(); 19 | 20 | var testImagesFolder = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "images"); 21 | 22 | var testFiles = Directory.GetFiles(testImagesFolder, "*", SearchOption.AllDirectories); 23 | 24 | var testImages = testFiles.Select(file => new ImageData 25 | { 26 | ImagePath = file 27 | }); 28 | 29 | var model = context.Model.Load("./dnn_model.zip", out var inputSchema); 30 | 31 | var predictionEngine = context.Model.CreatePredictionEngine(model); 32 | 33 | VBuffer> keys = default; 34 | predictionEngine.OutputSchema["LabelKey"].GetKeyValues(ref keys); 35 | 36 | var originalLabels = keys.DenseValues().ToArray(); 37 | 38 | Console.WriteLine(Environment.NewLine); 39 | 40 | foreach (var image in testImages) 41 | { 42 | var prediction = predictionEngine.Predict(image); 43 | 44 | Console.WriteLine($"Image : {Path.GetFileName(image.ImagePath)}, Score : {prediction.Score.Max()}, Predicted Label : {originalLabels[prediction.PredictedLabel]}"); 45 | } 46 | 47 | var storageAccount = CloudStorageAccount.Parse(connectionString); 48 | 49 | var client = storageAccount.CreateCloudBlobClient(); 50 | 51 | var container = client.GetContainerReference("images"); 52 | 53 | var images = await container.ListBlobsSegmentedAsync(null); 54 | 55 | foreach (CloudBlockBlob image in images.Results) 56 | { 57 | var blob = container.GetBlockBlobReference(image.Name); 58 | 59 | await blob.DownloadToFileAsync($"./{image.Name}", FileMode.Create); 60 | 61 | var newImage = new ImageData 62 | { 63 | ImagePath = $"./{image.Name}" 64 | }; 65 | 66 | var prediction = predictionEngine.Predict(newImage); 67 | 68 | Console.WriteLine($"Image : {image.Name}, Score : {prediction.Score.Max()}, Predicted Label : {originalLabels[prediction.PredictedLabel]}"); 69 | } 70 | 71 | Console.ReadLine(); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/AdapterWithErrorHandler.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT License. 3 | // 4 | // Generated with Bot Builder V4 SDK Template for Visual Studio CoreBot v4.6.2 5 | 6 | using Microsoft.Bot.Builder.Integration.AspNet.Core; 7 | using Microsoft.Bot.Builder.TraceExtensions; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace PredictionBot 12 | { 13 | public class AdapterWithErrorHandler : BotFrameworkHttpAdapter 14 | { 15 | public AdapterWithErrorHandler(IConfiguration configuration, ILogger logger) 16 | : base(configuration, logger) 17 | { 18 | OnTurnError = async (turnContext, exception) => 19 | { 20 | // Log any leaked exception from the application. 21 | logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}"); 22 | 23 | // Send a message to the user 24 | await turnContext.SendActivityAsync("The bot encountered an error or bug."); 25 | await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code."); 26 | 27 | // Send a trace activity, which will be displayed in the Bot Framework Emulator 28 | await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError"); 29 | }; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/Bots/HousingBot.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT License. 3 | // 4 | // Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2 5 | 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.Bot.Builder; 9 | using Microsoft.Bot.Builder.Dialogs; 10 | using Microsoft.Bot.Schema; 11 | using Microsoft.Extensions.ML; 12 | 13 | namespace PredictionBot.Bots 14 | { 15 | public class HousingBot : ActivityHandler where T : Dialog 16 | { 17 | protected readonly Dialog Dialog; 18 | protected readonly BotState ConversationState; 19 | protected readonly BotState UserState; 20 | 21 | public HousingBot(ConversationState conversationState, UserState userState, T dialog) 22 | { 23 | Dialog = dialog; 24 | ConversationState = conversationState; 25 | UserState = userState; 26 | } 27 | 28 | protected override async Task OnMessageActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) 29 | { 30 | await Dialog.RunAsync(turnContext, ConversationState.CreateProperty(nameof(DialogState)), cancellationToken); 31 | } 32 | 33 | public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default) 34 | { 35 | await base.OnTurnAsync(turnContext, cancellationToken); 36 | 37 | await ConversationState.SaveChangesAsync(turnContext, force: false, cancellationToken); 38 | await UserState.SaveChangesAsync(turnContext, force: false, cancellationToken); 39 | } 40 | 41 | protected override Task OnEndOfConversationActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) 42 | { 43 | return base.OnEndOfConversationActivityAsync(turnContext, cancellationToken); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/Controllers/BotController.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT License. 3 | // 4 | // Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2 5 | 6 | using System.Threading.Tasks; 7 | using Microsoft.AspNetCore.Mvc; 8 | using Microsoft.Bot.Builder; 9 | using Microsoft.Bot.Builder.Integration.AspNet.Core; 10 | 11 | namespace PredictionBot.Controllers 12 | { 13 | // This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot 14 | // implementation at runtime. Multiple different IBot implementations running at different endpoints can be 15 | // achieved by specifying a more specific type for the bot constructor argument. 16 | [Route("api/messages")] 17 | [ApiController] 18 | public class BotController : ControllerBase 19 | { 20 | private readonly IBotFrameworkHttpAdapter Adapter; 21 | private readonly IBot Bot; 22 | 23 | public BotController(IBotFrameworkHttpAdapter adapter, IBot bot) 24 | { 25 | Adapter = adapter; 26 | Bot = bot; 27 | } 28 | 29 | [HttpPost, HttpGet] 30 | public async Task PostAsync() 31 | { 32 | // Delegate the processing of the HTTP POST to the adapter. 33 | // The adapter will invoke the bot. 34 | await Adapter.ProcessAsync(Request, Response, Bot); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/DeploymentTemplates/new-rg-parameters.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", 3 | "contentVersion": "1.0.0.0", 4 | "parameters": { 5 | "groupLocation": { 6 | "value": "" 7 | }, 8 | "groupName": { 9 | "value": "" 10 | }, 11 | "appId": { 12 | "value": "" 13 | }, 14 | "appSecret": { 15 | "value": "" 16 | }, 17 | "botId": { 18 | "value": "" 19 | }, 20 | "botSku": { 21 | "value": "" 22 | }, 23 | "newAppServicePlanName": { 24 | "value": "" 25 | }, 26 | "newAppServicePlanSku": { 27 | "value": { 28 | "name": "S1", 29 | "tier": "Standard", 30 | "size": "S1", 31 | "family": "S", 32 | "capacity": 1 33 | } 34 | }, 35 | "newAppServicePlanLocation": { 36 | "value": "" 37 | }, 38 | "newWebAppName": { 39 | "value": "" 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/DeploymentTemplates/preexisting-rg-parameters.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", 3 | "contentVersion": "1.0.0.0", 4 | "parameters": { 5 | "appId": { 6 | "value": "" 7 | }, 8 | "appSecret": { 9 | "value": "" 10 | }, 11 | "botId": { 12 | "value": "" 13 | }, 14 | "botSku": { 15 | "value": "" 16 | }, 17 | "newAppServicePlanName": { 18 | "value": "" 19 | }, 20 | "newAppServicePlanSku": { 21 | "value": { 22 | "name": "S1", 23 | "tier": "Standard", 24 | "size": "S1", 25 | "family": "S", 26 | "capacity": 1 27 | } 28 | }, 29 | "appServicePlanLocation": { 30 | "value": "" 31 | }, 32 | "existingAppServicePlan": { 33 | "value": "" 34 | }, 35 | "newWebAppName": { 36 | "value": "" 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/MLModel/housing-model.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/PredictionBot/MLModel/housing-model.zip -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/Models/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace PredictionBot 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/Models/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace PredictionBot 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedHouseValue { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/PredictionBot.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.1 5 | latest 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | Always 19 | 20 | 21 | 22 | 23 | 24 | PreserveNewest 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT License. 3 | // 4 | // Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2 5 | 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Logging; 9 | 10 | namespace PredictionBot 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateWebHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 20 | WebHost.CreateDefaultBuilder(args) 21 | .ConfigureLogging(logging => 22 | { 23 | logging.AddConsole(); 24 | logging.AddDebug(); 25 | }) 26 | .UseStartup(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/README.md: -------------------------------------------------------------------------------- 1 | # PredictionBot 2 | 3 | Bot Framework v4 echo bot sample. 4 | 5 | This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. 6 | 7 | ## Prerequisites 8 | 9 | - [.NET Core SDK](https://dotnet.microsoft.com/download) version 2.1 10 | 11 | ```bash 12 | # determine dotnet version 13 | dotnet --version 14 | ``` 15 | 16 | ## To try this sample 17 | 18 | - In a terminal, navigate to `PredictionBot` 19 | 20 | ```bash 21 | # change into project folder 22 | cd # PredictionBot 23 | ``` 24 | 25 | - Run the bot from a terminal or from Visual Studio, choose option A or B. 26 | 27 | A) From a terminal 28 | 29 | ```bash 30 | # run the bot 31 | dotnet run 32 | ``` 33 | 34 | B) Or from Visual Studio 35 | 36 | - Launch Visual Studio 37 | - File -> Open -> Project/Solution 38 | - Navigate to `PredictionBot` folder 39 | - Select `PredictionBot.csproj` file 40 | - Press `F5` to run the project 41 | 42 | ## Testing the bot using Bot Framework Emulator 43 | 44 | [Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. 45 | 46 | - Install the Bot Framework Emulator version 4.5.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) 47 | 48 | ### Connect to the bot using Bot Framework Emulator 49 | 50 | - Launch Bot Framework Emulator 51 | - File -> Open Bot 52 | - Enter a Bot URL of `http://localhost:3978/api/messages` 53 | 54 | ## Deploy the bot to Azure 55 | 56 | To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions. 57 | 58 | ## Further reading 59 | 60 | - [Bot Framework Documentation](https://docs.botframework.com) 61 | - [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) 62 | - [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) 63 | - [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) 64 | - [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) 65 | - [.NET Core CLI tools](https://docs.microsoft.com/en-us/dotnet/core/tools/?tabs=netcore2x) 66 | - [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) 67 | - [Azure Portal](https://portal.azure.com) 68 | - [Language Understanding using LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/) 69 | - [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) 70 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/Startup.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT License. 3 | // 4 | // Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2 5 | 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.AspNetCore.Mvc; 9 | using Microsoft.Bot.Builder; 10 | using Microsoft.Bot.Builder.Integration.AspNet.Core; 11 | using Microsoft.Extensions.Configuration; 12 | using Microsoft.Extensions.DependencyInjection; 13 | using Microsoft.Extensions.ML; 14 | using PredictionBot.Bots; 15 | using PredictionBot.Dialogs; 16 | 17 | namespace PredictionBot 18 | { 19 | public class Startup 20 | { 21 | public Startup(IConfiguration configuration) 22 | { 23 | Configuration = configuration; 24 | } 25 | 26 | public IConfiguration Configuration { get; } 27 | 28 | // This method gets called by the runtime. Use this method to add services to the container. 29 | public void ConfigureServices(IServiceCollection services) 30 | { 31 | services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); 32 | 33 | // Create the Bot Framework Adapter with error handling enabled. 34 | services.AddSingleton(); 35 | 36 | services.AddSingleton(); 37 | 38 | services.AddSingleton(); 39 | 40 | services.AddSingleton(); 41 | 42 | services.AddSingleton(); 43 | 44 | // Create the bot as a transient. In this case the ASP Controller is expecting an IBot. 45 | services.AddTransient>(); 46 | 47 | services.AddPredictionEnginePool() 48 | .FromFile("./MLModel/housing-model.zip", watchForChanges: false); 49 | } 50 | 51 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 52 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 53 | { 54 | if (env.IsDevelopment()) 55 | { 56 | app.UseDeveloperExceptionPage(); 57 | } 58 | else 59 | { 60 | app.UseHsts(); 61 | } 62 | 63 | app.UseDefaultFiles(); 64 | app.UseStaticFiles(); 65 | 66 | app.UseMvc(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /MLNetExamples/PredictionBot/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "MicrosoftAppId": "", 3 | "MicrosoftAppPassword": "" 4 | } 5 | -------------------------------------------------------------------------------- /MLNetExamples/Ranking/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace Ranking 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var context = new MLContext(); 14 | 15 | var data = context.Data.LoadFromTextFile("./ranking.tsv", separatorChar: '\t'); 16 | 17 | var split = context.Data.TrainTestSplit(data, testFraction: 0.2); 18 | 19 | var secondSplit = context.Data.TrainTestSplit(split.TestSet); 20 | 21 | //var validation = secondSplit.TrainSet; 22 | 23 | var sampleInput = secondSplit.TestSet; 24 | 25 | var rankingPipeline = context.Transforms.Conversion.MapValueToKey("Label") 26 | .Append(context.Transforms.Conversion.Hash("GroupId", "GroupId")) 27 | .Append(context.Ranking.Trainers.LightGbm()); 28 | 29 | var model = rankingPipeline.Fit(split.TrainSet); 30 | 31 | var predictions = model.Transform(split.TestSet); 32 | 33 | var options = new RankingEvaluatorOptions 34 | { 35 | DcgTruncationLevel = 5 36 | }; 37 | 38 | var metrics = context.Ranking.Evaluate(predictions, options); 39 | 40 | var ndcg = metrics.NormalizedDiscountedCumulativeGains.Average(); 41 | 42 | Console.WriteLine($"nDGC - {ndcg}"); 43 | Console.Write(Environment.NewLine); 44 | 45 | var batchPredictions = model.Transform(sampleInput); 46 | 47 | var newPredictions = context.Data.CreateEnumerable(batchPredictions, reuseRowObject: false); 48 | 49 | Console.WriteLine("Scores:"); 50 | foreach (var prediction in newPredictions) 51 | { 52 | Console.WriteLine($"{prediction.RelevanceScore}"); 53 | } 54 | 55 | //Console.WriteLine($"Relevance - {prediction.Score}"); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /MLNetExamples/Ranking/Ranking.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MLNetExamples/Ranking/RankingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace Ranking 4 | { 5 | public class RankingData 6 | { 7 | [LoadColumn(0), ColumnName("Label")] 8 | public float Relevance { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float GroupId { get; set; } 12 | 13 | [LoadColumn(2, 133)] 14 | [VectorType(133)] 15 | public float[] Features { get; set; } 16 | } 17 | } -------------------------------------------------------------------------------- /MLNetExamples/Ranking/RankingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace Ranking 4 | { 5 | internal class RankingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float RelevanceScore { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /MLNetExamples/RetrainModel/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace RetrainModel 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity{ get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/RetrainModel/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Trainers; 3 | using Microsoft.WindowsAzure.Storage; 4 | using System; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Threading.Tasks; 8 | 9 | namespace RetrainModel 10 | { 11 | class Program 12 | { 13 | static async Task Main(string[] args) 14 | { 15 | string blobData; 16 | var connectionString = ""; 17 | 18 | string trainerFilePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "models/housing-trainer.zip"); 19 | string pipelineFilePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "models/housing-data-prep.zip"); 20 | string modelDirectory = Path.Combine(pipelineFilePath, ".."); 21 | 22 | var storageAccount = CloudStorageAccount.Parse(connectionString); 23 | 24 | var client = storageAccount.CreateCloudBlobClient(); 25 | 26 | var container = client.GetContainerReference("models"); 27 | 28 | var dataModel = container.GetBlockBlobReference("housing-data-prep.zip"); 29 | var trainingModel = container.GetBlockBlobReference("housing-trainer.zip"); 30 | 31 | if (!File.Exists(pipelineFilePath)) 32 | { 33 | if (!Directory.Exists(modelDirectory)) 34 | { 35 | Directory.CreateDirectory(modelDirectory); 36 | } 37 | 38 | await dataModel.DownloadToFileAsync(pipelineFilePath, FileMode.Create); 39 | } 40 | 41 | if (!File.Exists(trainerFilePath)) 42 | { 43 | await trainingModel.DownloadToFileAsync(trainerFilePath, FileMode.Create); 44 | } 45 | 46 | var context = new MLContext(); 47 | 48 | DataViewSchema modelSchema, pipelineSchema; 49 | 50 | var trainerModel = context.Model.Load(trainerFilePath, out modelSchema); 51 | var dataPrepModel = context.Model.Load(pipelineFilePath, out pipelineSchema); 52 | 53 | var originalModelParams = 54 | ((ISingleFeaturePredictionTransformer)trainerModel).Model as PoissonRegressionModelParameters; 55 | 56 | var parsedData = File.ReadAllLines("./retrain_housing_data.csv") 57 | .Skip(1) 58 | .Select(line => line.Split(',')) 59 | .TakeWhile(row => !string.IsNullOrWhiteSpace(row[0])) 60 | .Select(row => new HousingData 61 | { 62 | Longitude = float.Parse(row[0]), 63 | Latitude = float.Parse(row[1]), 64 | HousingMedianAge = float.Parse(row[2]), 65 | TotalRooms = float.Parse(row[3]), 66 | TotalBedrooms = float.Parse(row[4]), 67 | Population = float.Parse(row[5]), 68 | Households = float.Parse(row[6]), 69 | MedianIncome = float.Parse(row[7]), 70 | MedianHouseValue = float.Parse(row[8]), 71 | OceanProximity = row[9] 72 | }); 73 | 74 | // Load new data and build new model based off original parameters 75 | var newData = context.Data.LoadFromEnumerable(parsedData); 76 | 77 | var newDataTransformed = dataPrepModel.Transform(newData); 78 | 79 | var retrainedModel = context.Regression.Trainers.LbfgsPoissonRegression() 80 | .Fit(newDataTransformed, originalModelParams); 81 | 82 | // Compare model params 83 | var newModelParams = retrainedModel.Model as PoissonRegressionModelParameters; 84 | 85 | var weightDiffs = originalModelParams.Weights.Zip( 86 | newModelParams.Weights, (original, updated) => original - updated).ToArray(); 87 | 88 | Console.WriteLine("Original\tRetrained\tDifference"); 89 | for (int i = 0; i < weightDiffs.Count(); i++) 90 | { 91 | Console.WriteLine($"{originalModelParams.Weights[i]}\t{newModelParams.Weights[i]}\t{weightDiffs[i]}"); 92 | } 93 | 94 | Console.ReadLine(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /MLNetExamples/RetrainModel/RetrainModel.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | latest 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PreserveNewest 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /MLNetExamples/RetrainModel/retrain_housing_data.csv: -------------------------------------------------------------------------------- 1 | longitude,latitude,housing_median_age,total_rooms,total_bedrooms,population,households,median_income,median_house_value,ocean_proximity 2 | -122.29,37.73,8.0,4004.0,2281.0,1300.0,55.0,4.4,110500,INLAND 3 | -120.72,35.33,11.0,1602.0,838.0,148.0,991.0,2.433,88320,NEAR BAY 4 | -121.99,36.42,33.0,258.0,77.0,2100.0,279.0,2.69,92310,<1H OCEAN 5 | -119.88,38.1,18.0,4911.0,211.0,1721.0,375.0,5.111,202700,NEAR OCEAN 6 | -------------------------------------------------------------------------------- /MLNetExamples/ScaleData/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace ScaleData 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/ScaleData/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace ScaleData 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedHouseValue { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/ScaleData/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Linq; 4 | 5 | namespace ScaleData 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var context = new MLContext(); 12 | 13 | var data = context.Data.LoadFromTextFile("./housing.csv", hasHeader: true, separatorChar: ','); 14 | 15 | var columns = data.Schema 16 | .Select(col => col.Name) 17 | .Where(col => col != "Label" && col != "OceanProximity") 18 | .Select(col => new InputOutputColumnPair(col, col)) 19 | .ToArray(); 20 | 21 | var scaling = context.Transforms.NormalizeMinMax(columns); 22 | 23 | var dataScaled = scaling.Fit(data).Transform(data); 24 | 25 | var features = columns.Select(col => col.OutputColumnName).ToArray(); 26 | 27 | var preview = dataScaled.Preview(); 28 | 29 | var pipeline = context.Transforms.Text.FeaturizeText("Text", "OceanProximity") 30 | .Append(context.Transforms.Concatenate("Features", features)) 31 | .Append(context.Transforms.Concatenate("Features", "Text")) 32 | .Append(context.Regression.Trainers.LbfgsPoissonRegression()); 33 | 34 | var model = pipeline.Fit(dataScaled); 35 | 36 | var predictionFunc = context.Model.CreatePredictionEngine(model); 37 | 38 | var prediction = predictionFunc.Predict(new HousingData 39 | { 40 | Longitude = -122.25f, 41 | Latitude = 37.85f, 42 | HousingMedianAge = 55.0f, 43 | TotalRooms = 1627.0f, 44 | TotalBedrooms = 235.0f, 45 | Population = 322.0f, 46 | Households = 120.0f, 47 | MedianIncome = 8.3014f, 48 | OceanProximity = "NEAR BAY" 49 | }); 50 | 51 | Console.WriteLine($"Prediction - {prediction.PredictedHouseValue}"); 52 | 53 | Console.ReadLine(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /MLNetExamples/ScaleData/ScaleData.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/SeedClustering/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Data.DataView; 2 | using Microsoft.ML; 3 | using Microsoft.ML.Data; 4 | using System; 5 | 6 | namespace SeedClustering 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var dataLocation = "./Seed_Data.csv"; 13 | 14 | var context = new MLContext(); 15 | 16 | var textLoader = context.Data.CreateTextLoader(new[] 17 | { 18 | new TextLoader.Column("A", DataKind.Single, 0), 19 | new TextLoader.Column("P", DataKind.Single, 1), 20 | new TextLoader.Column("C", DataKind.Single, 2), 21 | new TextLoader.Column("LK", DataKind.Single, 3), 22 | new TextLoader.Column("WK", DataKind.Single, 4), 23 | new TextLoader.Column("A_Coef", DataKind.Single, 5), 24 | new TextLoader.Column("LKG", DataKind.Single, 6), 25 | new TextLoader.Column("Label", DataKind.Single, 7) 26 | }, 27 | hasHeader: true, 28 | separatorChar: ','); 29 | 30 | IDataView data = textLoader.Load(dataLocation); 31 | 32 | var trainTestData = context.Clustering.TrainTestSplit(data, testFraction: 0.2); 33 | 34 | var pipeline = context.Transforms.Concatenate("Features", "A", "P", "C", "LK", "WK", "A_Coef", "LKG") 35 | .Append(context.Clustering.Trainers.KMeans(featureColumnName: "Features", clustersCount: 3)); 36 | 37 | var preview = trainTestData.TrainSet.Preview(); 38 | 39 | var model = pipeline.Fit(trainTestData.TrainSet); 40 | 41 | var predictions = model.Transform(trainTestData.TestSet); 42 | 43 | var metrics = context.Clustering.Evaluate(predictions, score: "Score", features: "Features"); 44 | 45 | Console.WriteLine($"Average minimum score: {metrics.AvgMinScore}"); 46 | 47 | var predictionFunc = model.CreatePredictionEngine(context); 48 | 49 | var prediction = predictionFunc.Predict(new SeedData 50 | { 51 | A = 13.89F, 52 | P = 15.33F, 53 | C = 0.862F, 54 | LK = 5.42F, 55 | WK = 3.311F, 56 | A_Coef = 2.8F, 57 | LKG = 5 58 | }); 59 | 60 | Console.WriteLine($"Prediction - {prediction.SelectedClusterId}"); 61 | Console.ReadLine(); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /MLNetExamples/SeedClustering/SeedClustering.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/SeedClustering/SeedData.cs: -------------------------------------------------------------------------------- 1 | namespace SeedClustering 2 | { 3 | public class SeedData 4 | { 5 | public float A; 6 | public float P; 7 | public float C; 8 | public float LK; 9 | public float WK; 10 | public float A_Coef; 11 | public float LKG; 12 | public float Label; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MLNetExamples/SeedClustering/SeedPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SeedClustering 4 | { 5 | public class SeedPrediction 6 | { 7 | [ColumnName("PredictedLabel")] 8 | public uint SelectedClusterId; 9 | [ColumnName("Score")] 10 | public float[] Distance; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /MLNetExamples/SelectAndShuffle/HousingData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SelectAndShuffle 4 | { 5 | public class HousingData 6 | { 7 | [LoadColumn(0)] 8 | public float Longitude { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Latitude { get; set; } 12 | 13 | [LoadColumn(2)] 14 | public float HousingMedianAge { get; set; } 15 | 16 | [LoadColumn(3)] 17 | public float TotalRooms { get; set; } 18 | 19 | [LoadColumn(4)] 20 | public float TotalBedrooms { get; set; } 21 | 22 | [LoadColumn(5)] 23 | public float Population { get; set; } 24 | 25 | [LoadColumn(6)] 26 | public float Households { get; set; } 27 | 28 | [LoadColumn(7)] 29 | public float MedianIncome { get; set; } 30 | 31 | [LoadColumn(8), ColumnName("Label")] 32 | public float MedianHouseValue { get; set; } 33 | 34 | [LoadColumn(9)] 35 | public string OceanProximity{ get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MLNetExamples/SelectAndShuffle/HousingPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SelectAndShuffle 4 | { 5 | public class HousingPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedPrice { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/SelectAndShuffle/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | 4 | namespace SelectAndShuffle 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var context = new MLContext(); 11 | 12 | var data = context.Data.LoadFromTextFile("./housing.csv", hasHeader: true, separatorChar: ','); 13 | 14 | // Select columns 15 | var selectCols = context.Transforms.SelectColumns("HousingMedianAge", "TotalBedrooms"); 16 | var selectColsTransform = selectCols.Fit(data).Transform(data); 17 | 18 | //DisplayColumns(selectColsTransform); 19 | 20 | // Drop columns 21 | var dropCols = context.Transforms.DropColumns("Latitude", "Longitude"); 22 | var dropColsTransform = dropCols.Fit(data).Transform(data); 23 | 24 | //DisplayColumns(dropColsTransform); 25 | 26 | // Shuffle rows 27 | //DisplayColumns(data); 28 | 29 | var shuffleRows = context.Data.ShuffleRows(data, seed: 42); 30 | 31 | //DisplayColumns(shuffleRows); 32 | 33 | // Take rows 34 | var takeRows = context.Data.TakeRows(data, 2); 35 | 36 | //DisplayColumns(takeRows); 37 | 38 | // Filter rows 39 | var filterRows = context.Data.FilterRowsByColumn(data, "Population", lowerBound: 0, upperBound: 1000); 40 | 41 | DisplayColumns(filterRows); 42 | 43 | Console.ReadLine(); 44 | } 45 | 46 | private static void DisplayColumns(IDataView data) 47 | { 48 | var preview = data.Preview(maxRows: 5); 49 | 50 | string previewData = ""; 51 | 52 | for (int i = 0; i < preview.RowView.Length; i++) 53 | { 54 | foreach (var item in preview.RowView[i].Values) 55 | { 56 | previewData += $"{item.Key}: {item.Value} "; 57 | } 58 | 59 | Console.WriteLine("----------------------------------"); 60 | Console.WriteLine(previewData); 61 | previewData = ""; 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /MLNetExamples/SelectAndShuffle/SelectAndShuffle.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/SentimentAnalysis/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | 4 | namespace SentimentAnalysis 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var context = new MLContext(); 11 | 12 | var data = context.Data.LoadFromTextFile("stock_data.csv", hasHeader: true, separatorChar: ',', allowQuoting: true); 13 | 14 | var pipeline = context.Transforms.Expression("Label", "(x) => x == 1 ? true : false", "Sentiment") 15 | .Append(context.Transforms.Text.FeaturizeText("Features", nameof(SentimentData.Text))) 16 | .Append(context.BinaryClassification.Trainers.SdcaLogisticRegression()); 17 | 18 | var model = pipeline.Fit(data); 19 | 20 | var predictionEngine = context.Model.CreatePredictionEngine(model); 21 | 22 | var prediction = predictionEngine.Predict(new SentimentData { Text = "I would buy MSFT shares." }); 23 | 24 | Console.WriteLine($"Prediction - {prediction.Prediction} with probability - {prediction.Probability}"); 25 | 26 | var newPrediction = predictionEngine.Predict(new SentimentData { Text = "TWTR may close at a low today." }); 27 | 28 | Console.WriteLine($"Prediction - {newPrediction.Prediction} with probability - {newPrediction.Probability}"); 29 | 30 | var anotherPrediction = predictionEngine.Predict(new SentimentData { Text = "TSLA is at an all time high." }); 31 | 32 | switch (anotherPrediction.Probability) 33 | { 34 | case float p when p < .5: 35 | Console.WriteLine($"TSLA sentiment is negative with probability {p}"); 36 | break; 37 | case float p when p >= .5 && p <= .7: 38 | Console.WriteLine($"TSLA sentiment is neutral with probability {p}"); 39 | break; 40 | case float p when p > .7: 41 | Console.WriteLine($"TSLA sentiment is positive with probability {p}"); 42 | break; 43 | default: 44 | break; 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MLNetExamples/SentimentAnalysis/SentimentAnalysis.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/SentimentAnalysis/SentimentData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SentimentAnalysis 4 | { 5 | public class SentimentData 6 | { 7 | [LoadColumn(0)] 8 | public string Text { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public float Sentiment { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/SentimentAnalysis/SentimentPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SentimentAnalysis 4 | { 5 | public class SentimentPrediction 6 | { 7 | [ColumnName("PredictedLabel")] 8 | public bool Prediction { get; set; } 9 | 10 | public float Probability { get; set; } 11 | 12 | public float Score { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegression/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Runtime.Data; 3 | using Microsoft.ML.StaticPipe; 4 | using Microsoft.ML.Trainers; 5 | using Microsoft.ML.Transforms; 6 | using System; 7 | 8 | namespace SimpleRegression 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | var context = new MLContext(); 15 | var reader = TextLoader.CreateReader(context, ctx => ( 16 | YearsExperience: ctx.LoadFloat(0), 17 | Salary: ctx.LoadFloat(1) 18 | ), hasHeader: true, separator: ','); 19 | 20 | var data = reader.Read(new MultiFileSource("SalaryData.csv")); 21 | 22 | var pipeline = reader.MakeNewEstimator() 23 | .Append(r => ( 24 | r.Salary, 25 | Prediction: context.Regression.Trainers.Sdca(label: r.Salary, features: r.YearsExperience.AsVector()) 26 | )); 27 | 28 | var model = pipeline.Fit(data).AsDynamic; 29 | 30 | var predictionFunc = model.MakePredictionFunction(context); 31 | 32 | var prediction = predictionFunc.Predict(new SalaryData { YearsExperience = 8 }); 33 | 34 | Console.WriteLine($"Predicted salary - {String.Format("{0:C}", prediction.PredictedSalary)}"); 35 | 36 | Console.Read(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegression/README.md: -------------------------------------------------------------------------------- 1 | ## Simple Linear Regression 2 | 3 | This example uses a small data set that is generously provided by [Super DataScience](https://www.superdatascience.com/) to predict salary based off years of experience. -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegression/SalaryData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Runtime.Api; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace SimpleRegression 7 | { 8 | public class SalaryData 9 | { 10 | [Column("0")] 11 | public float YearsExperience; 12 | 13 | [Column("1", name: "Label")] 14 | public float Salary; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegression/SalaryData.csv: -------------------------------------------------------------------------------- 1 | YearsExperience,Salary 2 | 1.1,39343.00 3 | 1.3,46205.00 4 | 1.5,37731.00 5 | 2.0,43525.00 6 | 2.2,39891.00 7 | 2.9,56642.00 8 | 3.0,60150.00 9 | 3.2,54445.00 10 | 3.2,64445.00 11 | 3.7,57189.00 12 | 3.9,63218.00 13 | 4.0,55794.00 14 | 4.0,56957.00 15 | 4.1,57081.00 16 | 4.5,61111.00 17 | 4.9,67938.00 18 | 5.1,66029.00 19 | 5.3,83088.00 20 | 5.9,81363.00 21 | 6.0,93940.00 22 | 6.8,91738.00 23 | 7.1,98273.00 24 | 7.9,101302.00 25 | 8.2,113812.00 26 | 8.7,109431.00 27 | 9.0,105582.00 28 | 9.5,116969.00 29 | 9.6,112635.00 30 | 10.3,122391.00 31 | 10.5,121872.00 32 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegression/SalaryPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Runtime.Api; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace SimpleRegression 7 | { 8 | public class SalaryPrediction 9 | { 10 | [ColumnName("Score")] 11 | public float PredictedSalary; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegression/SimpleRegression.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegressionUpdate/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | 4 | namespace SimpleRegressionUpdate 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var context = new MLContext(); 11 | 12 | // Load data 13 | var trainData = context.Data.LoadFromTextFile("./SalaryData.csv", hasHeader: true, separatorChar: ','); 14 | 15 | // Build model 16 | var pipeline = context.Transforms.Concatenate("Features", "YearsExperience") 17 | .Append(context.Regression.Trainers.LbfgsPoissonRegression()); 18 | 19 | var model = pipeline.Fit(trainData); 20 | 21 | // Evaluate 22 | var predictions = model.Transform(trainData); 23 | 24 | var metrics = context.Regression.Evaluate(predictions); 25 | 26 | Console.WriteLine($"R^2 - {metrics.RSquared}"); 27 | 28 | // Predict 29 | var newData = new SalaryData 30 | { 31 | YearsExperience = 1.1f 32 | }; 33 | 34 | var predictionFunc = context.Model.CreatePredictionEngine(model); 35 | 36 | var prediction = predictionFunc.Predict(newData); 37 | 38 | Console.WriteLine($"Prediction - {prediction.PredictedSalary}"); 39 | 40 | Console.ReadLine(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegressionUpdate/SalaryData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SimpleRegressionUpdate 4 | { 5 | public class SalaryData 6 | { 7 | [LoadColumn(0)] 8 | public float YearsExperience; 9 | 10 | [LoadColumn(1), ColumnName("Label")] 11 | public float Salary; 12 | } 13 | } -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegressionUpdate/SalaryData.csv: -------------------------------------------------------------------------------- 1 | YearsExperience,Salary 2 | 1.1,39343.00 3 | 1.3,46205.00 4 | 1.5,37731.00 5 | 2.0,43525.00 6 | 2.2,39891.00 7 | 2.9,56642.00 8 | 3.0,60150.00 9 | 3.2,54445.00 10 | 3.2,64445.00 11 | 3.7,57189.00 12 | 3.9,63218.00 13 | 4.0,55794.00 14 | 4.0,56957.00 15 | 4.1,57081.00 16 | 4.5,61111.00 17 | 4.9,67938.00 18 | 5.1,66029.00 19 | 5.3,83088.00 20 | 5.9,81363.00 21 | 6.0,93940.00 22 | 6.8,91738.00 23 | 7.1,98273.00 24 | 7.9,101302.00 25 | 8.2,113812.00 26 | 8.7,109431.00 27 | 9.0,105582.00 28 | 9.5,116969.00 29 | 9.6,112635.00 30 | 10.3,122391.00 31 | 10.5,121872.00 32 | -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegressionUpdate/SalaryPrediction.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace SimpleRegressionUpdate 4 | { 5 | public class SalaryPrediction 6 | { 7 | [ColumnName("Score")] 8 | public float PredictedSalary { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /MLNetExamples/SimpleRegressionUpdate/SimpleRegressionUpdate.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MLNetExamples/StopWords/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace StopWords 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var emptyData = new List(); 15 | 16 | var data = context.Data.LoadFromEnumerable(emptyData); 17 | 18 | var tokenization = context.Transforms.Text.TokenizeIntoWords("Tokens", "Text", separators: new[] { ' ', '.', ',' }) 19 | .Append(context.Transforms.Text.RemoveDefaultStopWords("Tokens", "Tokens", 20 | Microsoft.ML.Transforms.Text.StopWordsRemovingEstimator.Language.English)); 21 | 22 | var stopWordsModel = tokenization.Fit(data); 23 | 24 | var engine = context.Model.CreatePredictionEngine(stopWordsModel); 25 | 26 | var newText = engine.Predict(new TextData { Text = "This is a test sentence, and it is a long one." }); 27 | 28 | PrintTokens(newText); 29 | 30 | var customTokenization = context.Transforms.Text.TokenizeIntoWords("Tokens", "Text", separators: new[] { ' ', '.', ',' }) 31 | .Append(context.Transforms.Text.RemoveStopWords("Tokens", "Tokens", new[] { "and", "a" })); 32 | 33 | var customStopWordsModel = customTokenization.Fit(data); 34 | 35 | var customEngine = context.Model.CreatePredictionEngine(customStopWordsModel); 36 | 37 | var newCustomText = customEngine.Predict(new TextData { Text = "This is a test sentence, and it is a long one." }); 38 | 39 | PrintTokens(newCustomText); 40 | 41 | Console.ReadLine(); 42 | } 43 | 44 | private static void PrintTokens(TextTokens tokens) 45 | { 46 | Console.WriteLine(Environment.NewLine); 47 | 48 | var sb = new StringBuilder(); 49 | 50 | foreach (var token in tokens.Tokens) 51 | { 52 | sb.AppendLine(token); 53 | } 54 | 55 | Console.WriteLine(sb.ToString()); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /MLNetExamples/StopWords/StopWords.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MLNetExamples/StopWords/TextData.cs: -------------------------------------------------------------------------------- 1 | namespace StopWords 2 | { 3 | public class TextData 4 | { 5 | public string Text { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/StopWords/TextTokens.cs: -------------------------------------------------------------------------------- 1 | namespace StopWords 2 | { 3 | public class TextTokens 4 | { 5 | public string[] Tokens { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/TextTransferLearning/DataStructures/FixedLengthVector.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace TextTransferLearning.DataStructures 7 | { 8 | public class FixedLengthVector 9 | { 10 | [VectorType(600)] 11 | public int[] Features { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/TextTransferLearning/DataStructures/MovieReview.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TextTransferLearning.DataStructures 6 | { 7 | public class MovieReview 8 | { 9 | public string Review { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /MLNetExamples/TextTransferLearning/DataStructures/MovieReviewSentiment.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace TextTransferLearning.DataStructures 4 | { 5 | public class MovieReviewSentiment 6 | { 7 | [VectorType(2)] 8 | public float[] Prediction { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/TextTransferLearning/DataStructures/VariableLengthVector.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace TextTransferLearning.DataStructures 4 | { 5 | public class VariableLengthVector 6 | { 7 | [VectorType] 8 | public int[] VariableLengthFeatures { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/TextTransferLearning/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Data; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using TextTransferLearning.DataStructures; 7 | 8 | namespace TextTransferLearning 9 | { 10 | class Program 11 | { 12 | static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, "Model"); 13 | 14 | static void Main(string[] args) 15 | { 16 | var context = new MLContext(); 17 | 18 | var tensorFlowModel = context.Model.LoadTensorFlowModel(_modelPath); 19 | 20 | DataViewSchema schema = tensorFlowModel.GetModelSchema(); 21 | 22 | Console.WriteLine(" =============== TensorFlow Model Schema =============== "); 23 | var featuresType = (VectorDataViewType)schema["Features"].Type; 24 | 25 | Console.WriteLine($"Name: Features, Type: {featuresType.ItemType.RawType}, Size: ({featuresType.Dimensions[0]})"); 26 | var predictionType = (VectorDataViewType)schema["Prediction/Softmax"].Type; 27 | 28 | Console.WriteLine($"Name: Prediction/Softmax, Type: {predictionType.ItemType.RawType}, Size: ({predictionType.Dimensions[0]})"); 29 | 30 | // Run here to show tensorflow error 31 | 32 | var wordLookup = context.Data.LoadFromTextFile(Path.Combine(_modelPath, "imdb_word_index.csv"), 33 | columns: new[] 34 | { 35 | new TextLoader.Column("Words", DataKind.String, 0), 36 | new TextLoader.Column("Ids", DataKind.Int32, 1), 37 | }, 38 | separatorChar: ','); 39 | 40 | Action ResizeFeaturesAction = (s, f) => 41 | { 42 | var features = s.VariableLengthFeatures; 43 | Array.Resize(ref features, 600); 44 | f.Features = features; 45 | }; 46 | 47 | var pipeline = context.Transforms.Text.TokenizeIntoWords("Tokens", "Review") 48 | .Append(context.Transforms.Conversion.MapValue("VariableLengthFeatures", wordLookup, wordLookup.Schema["Words"], wordLookup.Schema["Ids"], "Tokens")) 49 | .Append(context.Transforms.CustomMapping(ResizeFeaturesAction, "Resize")) 50 | .Append(tensorFlowModel.ScoreTensorFlowModel("Prediction/Softmax", "Features")) 51 | .Append(context.Transforms.CopyColumns("Prediction", "Prediction/Softmax")); 52 | 53 | var data = context.Data.LoadFromEnumerable(new List()); 54 | 55 | var model = pipeline.Fit(data); 56 | 57 | var predictionEngine = context.Model.CreatePredictionEngine(model); 58 | 59 | var review = new MovieReview 60 | { 61 | Review = "I like the action" 62 | }; 63 | 64 | var prediction = predictionEngine.Predict(review); 65 | 66 | Console.WriteLine(Environment.NewLine); 67 | Console.WriteLine("Number of classes: {0}", prediction.Prediction.Length); 68 | Console.WriteLine("Sentiment? {0}", prediction.Prediction[1] > 0.5 ? "Positive" : "Negative"); 69 | 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /MLNetExamples/TextTransferLearning/TextTransferLearning.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PreserveNewest 17 | 18 | 19 | PreserveNewest 20 | 21 | 22 | PreserveNewest 23 | 24 | 25 | PreserveNewest 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /MLNetExamples/TimeSeriesForecast/EnergyData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | using System; 3 | 4 | namespace TimeSeriesForecast 5 | { 6 | public class EnergyData 7 | { 8 | [LoadColumn(0)] 9 | public DateTime Date { get; set; } 10 | 11 | [LoadColumn(1)] 12 | public float Energy { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MLNetExamples/TimeSeriesForecast/EnergyForecast.cs: -------------------------------------------------------------------------------- 1 | namespace TimeSeriesForecast 2 | { 3 | public class EnergyForecast 4 | { 5 | public float[] Forecast { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/TimeSeriesForecast/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Transforms.TimeSeries; 3 | using System; 4 | 5 | namespace TimeSeriesForecast 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var context = new MLContext(); 12 | 13 | var data = context.Data.LoadFromTextFile("./energy_hourly.csv", 14 | hasHeader: true, separatorChar: ','); 15 | 16 | var pipeline = context.Forecasting.ForecastBySsa( 17 | nameof(EnergyForecast.Forecast), 18 | nameof(EnergyData.Energy), 19 | windowSize: 5, 20 | seriesLength: 10, 21 | trainSize: 100, 22 | horizon: 4); 23 | 24 | var model = pipeline.Fit(data); 25 | 26 | var forecastingEngine = model.CreateTimeSeriesEngine(context); 27 | 28 | var forecasts = forecastingEngine.Predict(); 29 | 30 | foreach (var forecast in forecasts.Forecast) 31 | { 32 | Console.WriteLine(forecast); 33 | } 34 | 35 | Console.ReadLine(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MLNetExamples/TimeSeriesForecast/TimeSeriesForecast.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MLNetExamples/Tokenization/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Tokenization 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var emptyData = new List(); 15 | 16 | var data = context.Data.LoadFromEnumerable(emptyData); 17 | 18 | var tokenization = context.Transforms.Text.TokenizeIntoWords("Tokens", "Text", separators: new[] { ' ', '.', ',' }); 19 | 20 | var tokenModel = tokenization.Fit(data); 21 | 22 | var engine = context.Model.CreatePredictionEngine(tokenModel); 23 | 24 | var tokens = engine.Predict(new SentimentData { Text = "This is a test sentence, and it is a long one." }); 25 | 26 | PrintTokens(tokens); 27 | 28 | var charTokenization = context.Transforms.Text.TokenizeIntoCharactersAsKeys("Tokens", "Text", useMarkerCharacters: false) 29 | .Append(context.Transforms.Conversion.MapKeyToValue("Tokens")); 30 | 31 | var charTokenModel = charTokenization.Fit(data); 32 | 33 | var charEngine = context.Model.CreatePredictionEngine(charTokenModel); 34 | 35 | var charTokens = charEngine.Predict(new SentimentData { Text = "This is a test sentence, and it is a long one." }); 36 | 37 | PrintTokens(charTokens); 38 | 39 | Console.ReadLine(); 40 | } 41 | 42 | private static void PrintTokens(SentimentTokens tokens) 43 | { 44 | Console.WriteLine(Environment.NewLine); 45 | 46 | var sb = new StringBuilder(); 47 | 48 | foreach (var token in tokens.Tokens) 49 | { 50 | sb.AppendLine(token); 51 | } 52 | 53 | Console.WriteLine(sb.ToString()); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /MLNetExamples/Tokenization/SentimentData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace Tokenization 4 | { 5 | public class SentimentData 6 | { 7 | [LoadColumn(0)] 8 | public bool Sentiment { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public string Text { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/Tokenization/SentimentTokens.cs: -------------------------------------------------------------------------------- 1 | namespace Tokenization 2 | { 3 | public class SentimentTokens 4 | { 5 | public string[] Tokens { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/Tokenization/Tokenization.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/ImageData.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML.Data; 2 | 3 | namespace TransferLearning 4 | { 5 | public class ImageData 6 | { 7 | [LoadColumn(0)] 8 | public string ImagePath { get; set; } 9 | 10 | [LoadColumn(1)] 11 | public string Label { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/ImagePrediction.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace TransferLearning 3 | { 4 | public class ImagePrediction : ImageData 5 | { 6 | public float[] Score { get; set; } 7 | 8 | public string PredictedLabelValue { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/InceptionSettings.cs: -------------------------------------------------------------------------------- 1 | namespace TransferLearning 2 | { 3 | public struct InceptionSettings 4 | { 5 | public const int ImageHeight = 224; 6 | public const int IMageWidth = 224; 7 | public const float Mean = 117; 8 | public const float Scale = 1; 9 | public const bool ChannelsList = true; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using System; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | namespace TransferLearning 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var data = context.Data.LoadFromTextFile("./labels.csv", separatorChar: ','); 15 | 16 | var preview = data.Preview(); 17 | 18 | var pipeline = context.Transforms.Conversion.MapValueToKey("LabelKey", "Label") 19 | .Append(context.Transforms.LoadImages("input", "images", nameof(ImageData.ImagePath))) 20 | .Append(context.Transforms.ResizeImages("input", InceptionSettings.IMageWidth, InceptionSettings.ImageHeight, "input")) 21 | .Append(context.Transforms.ExtractPixels("input", interleavePixelColors: InceptionSettings.ChannelsList, 22 | offsetImage: InceptionSettings.Mean)) 23 | .Append(context.Model.LoadTensorFlowModel("./model/tensorflow_inception_graph.pb") 24 | .ScoreTensorFlowModel(new[] { "softmax2_pre_activation" }, new[] { "input" }, addBatchDimensionInput: true)) 25 | .Append(context.MulticlassClassification.Trainers.LbfgsMaximumEntropy("LabelKey", "softmax2_pre_activation")) 26 | .Append(context.Transforms.Conversion.MapKeyToValue("PredictedLabelValue", "PredictedLabel")); 27 | 28 | var model = pipeline.Fit(data); 29 | 30 | var imageData = File.ReadAllLines("./labels.csv") 31 | .Select(l => l.Split(',')) 32 | .Select(l => new ImageData { ImagePath = Path.Combine(Environment.CurrentDirectory, "images", l[0]) }); 33 | 34 | var imageDataView = context.Data.LoadFromEnumerable(imageData); 35 | 36 | var predictions = model.Transform(imageDataView); 37 | 38 | var imagePredictions = context.Data.CreateEnumerable(predictions, reuseRowObject: false, ignoreMissingColumns: true); 39 | 40 | // Evaluate 41 | Console.WriteLine("\n------------Evaluate-----------------"); 42 | 43 | var evalPredictions = model.Transform(data); 44 | 45 | var metrics = context.MulticlassClassification.Evaluate(evalPredictions, labelColumnName: "LabelKey", 46 | predictedLabelColumnName: "PredictedLabel"); 47 | 48 | // Log loss should be close to 0 for accurate predictions 49 | Console.WriteLine($"Log Loss - {metrics.LogLoss}"); 50 | Console.WriteLine($"Per class Log Loss - {String.Join(',', metrics.PerClassLogLoss.Select(l => l.ToString()))}"); 51 | 52 | // Predict batch 53 | Console.WriteLine("\n------------Batch predictions-----------------"); 54 | 55 | foreach (var prediction in imagePredictions) 56 | { 57 | Console.WriteLine($"Image - {prediction.ImagePath} is predicted as {prediction.PredictedLabelValue} " + 58 | $"with a score of {prediction.Score.Max()}"); 59 | } 60 | 61 | // Predict single 62 | var predictionFunc = context.Model.CreatePredictionEngine(model); 63 | 64 | var singlePrediction = predictionFunc.Predict(new ImageData 65 | { 66 | ImagePath = Path.Combine(Environment.CurrentDirectory, "images", "cup2.jpg") 67 | }); 68 | 69 | Console.WriteLine("\n------------Single prediction-----------------"); 70 | Console.WriteLine($"Image {Path.GetFileName(singlePrediction.ImagePath)} was predicted as a {singlePrediction.PredictedLabelValue} " + 71 | $"with a score of {singlePrediction.Score.Max()}"); 72 | 73 | Console.ReadLine(); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/TransferLearning.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PreserveNewest 17 | 18 | 19 | PreserveNewest 20 | 21 | 22 | PreserveNewest 23 | 24 | 25 | PreserveNewest 26 | 27 | 28 | PreserveNewest 29 | 30 | 31 | PreserveNewest 32 | 33 | 34 | PreserveNewest 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/images/book.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/TransferLearning/images/book.jpg -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/images/cup.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/TransferLearning/images/cup.jpg -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/images/cup2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/TransferLearning/images/cup2.jpg -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/images/sunflower.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwood803/MLNetExamples/02606bf06fecb559bde0bcbf0908b2494a933e77/MLNetExamples/TransferLearning/images/sunflower.jpg -------------------------------------------------------------------------------- /MLNetExamples/TransferLearning/labels.csv: -------------------------------------------------------------------------------- 1 | book.jpg,book 2 | cup.jpg,cup 3 | sunflower.jpg,flower -------------------------------------------------------------------------------- /MLNetExamples/WordEmbeddings/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.ML; 2 | using Microsoft.ML.Transforms.Text; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace WordEmbeddings 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var context = new MLContext(); 13 | 14 | var emptyData = context.Data.LoadFromEnumerable(new List()); 15 | 16 | var embeddingsPipline = context.Transforms.Text.NormalizeText("Text", null, keepDiacritics: false, keepPunctuations: false, keepNumbers: false) 17 | .Append(context.Transforms.Text.TokenizeIntoWords("Tokens", "Text")) 18 | .Append(context.Transforms.Text.ApplyWordEmbedding("Features", "Tokens", WordEmbeddingEstimator.PretrainedModelKind.SentimentSpecificWordEmbedding)); 19 | 20 | var embeddingTransformer = embeddingsPipline.Fit(emptyData); 21 | 22 | var predictionEngine = context.Model.CreatePredictionEngine(embeddingTransformer); 23 | 24 | var dogData = new TextInput { Text = "Dog" }; 25 | 26 | var prediction = predictionEngine.Predict(dogData); 27 | 28 | Console.WriteLine($"Number of Features: {prediction.Features.Length}"); 29 | 30 | // Print the embedding vector. 31 | Console.WriteLine("Features: "); 32 | foreach (var feature in prediction.Features) 33 | { 34 | Console.Write($"{feature:F4} "); 35 | } 36 | 37 | Console.WriteLine(Environment.NewLine); 38 | 39 | var catData = new TextInput { Text = "Cat" }; 40 | 41 | var catPrediction = predictionEngine.Predict(catData); 42 | 43 | Console.WriteLine($"Number of Features: {catPrediction.Features.Length}"); 44 | 45 | // Print the embedding vector. 46 | Console.WriteLine("Features: "); 47 | foreach (var feature in catPrediction.Features) 48 | { 49 | Console.Write($"{feature:F4} "); 50 | } 51 | 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /MLNetExamples/WordEmbeddings/TextFeatures.cs: -------------------------------------------------------------------------------- 1 | namespace WordEmbeddings 2 | { 3 | public class TextFeatures 4 | { 5 | public float[] Features { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/WordEmbeddings/TextInput.cs: -------------------------------------------------------------------------------- 1 | namespace WordEmbeddings 2 | { 3 | public class TextInput 4 | { 5 | public string Text { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MLNetExamples/WordEmbeddings/WordEmbeddings.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ML.NET Examples 2 | 3 | A collection of examples for ML.NET. 4 | --------------------------------------------------------------------------------