Permalink
64cfcc5 Oct 1, 2018
1 contributor

Users who have contributed to this file

86 lines (70 sloc) 4.29 KB
using System;
using System.Linq;
using System.Runtime.InteropServices;
using BikeSharingDemand.BikeSharingDemandData;
using BikeSharingDemand.Helpers;
using BikeSharingDemand.Model;
using Microsoft.ML.Legacy;
using Microsoft.ML.Legacy.Models;
using Microsoft.ML.Legacy.Trainers;
namespace BikeSharingDemand
{
class Program
{
static void Main(string[] args)
{
var trainingDataLocation = @"Data/hour_train.csv";
var testDataLocation = @"Data/hour_test.csv";
var modelEvaluator = new ModelEvaluator();
var fastTreeModel = new ModelBuilder(trainingDataLocation, new FastTreeRegressor()).BuildAndTrain();
var fastTreeMetrics = modelEvaluator.Evaluate(fastTreeModel, testDataLocation);
PrintMetrics("Fast Tree", fastTreeMetrics);
var fastForestModel = new ModelBuilder(trainingDataLocation, new FastForestRegressor()).BuildAndTrain();
var fastForestMetrics = modelEvaluator.Evaluate(fastForestModel, testDataLocation);
PrintMetrics("Fast Forest", fastForestMetrics);
var poissonModel = new ModelBuilder(trainingDataLocation, new PoissonRegressor()).BuildAndTrain();
var poissonMetrics = modelEvaluator.Evaluate(poissonModel, testDataLocation);
PrintMetrics("Poisson", poissonMetrics);
var gradientDescentModel = new ModelBuilder(trainingDataLocation, new OnlineGradientDescentRegressor()).BuildAndTrain();
var gradientDescentMetrics = modelEvaluator.Evaluate(gradientDescentModel, testDataLocation);
PrintMetrics("Online Gradient Descent", gradientDescentMetrics);
var fastTreeTweedieModel = new ModelBuilder(trainingDataLocation, new FastTreeTweedieRegressor()).BuildAndTrain();
var fastTreeTweedieMetrics = modelEvaluator.Evaluate(fastTreeTweedieModel, testDataLocation);
PrintMetrics("Fast Tree Tweedie", fastTreeTweedieMetrics);
var additiveModel = new ModelBuilder(trainingDataLocation, new GeneralizedAdditiveModelRegressor()).BuildAndTrain();
var additiveMetrics = modelEvaluator.Evaluate(additiveModel, testDataLocation);
PrintMetrics("Generalized Additive Model", additiveMetrics);
var stochasticDualCorordinateAscentModel = new ModelBuilder(trainingDataLocation, new StochasticDualCoordinateAscentRegressor()).BuildAndTrain();
var stochasticDualCorordinateAscentMetrics = modelEvaluator.Evaluate(stochasticDualCorordinateAscentModel, testDataLocation);
PrintMetrics("Stochastic Dual Coordinate Ascent", stochasticDualCorordinateAscentMetrics);
VisualizeTenPredictionsForTheModel(fastTreeTweedieModel, testDataLocation);
fastTreeTweedieModel.WriteAsync(@".\Model.zip");
Console.ReadLine();
}
private static void PrintMetrics(string name, RegressionMetrics metrics)
{
Console.WriteLine($"*************************************************");
Console.WriteLine($"* Metrics for {name} ");
Console.WriteLine($"*------------------------------------------------");
Console.WriteLine($"* R2 Score: {metrics.RSquared:0.##}");
Console.WriteLine($"* Absolute loss: {metrics.L1:#.##}");
Console.WriteLine($"* Squared loss: {metrics.L2:#.##}");
Console.WriteLine($"* RMS loss: {metrics.Rms:#.##}");
Console.WriteLine($"*************************************************");
}
private static void VisualizeTenPredictionsForTheModel(
PredictionModel<BikeSharingDemandSample, BikeSharingDemandPrediction> model,
string testDataLocation)
{
var testData = new BikeSharingDemandsCsvReader().GetDataFromCsv(testDataLocation).ToList();
for (int i = 0; i < 10; i++)
{
var prediction = model.Predict(testData[i]);
Console.WriteLine($"-------------------------------------------------");
Console.WriteLine($"Predicted : {prediction.PredictedCount}");
Console.WriteLine($"Actual: {testData[i].Count}");
Console.WriteLine($"-------------------------------------------------");
}
}
}
}