public IActionResult YearAnalysis(RegressionModelInput input) { // Load the model MLContext mlContext = new MLContext(); ITransformer mlModel = mlContext.Model.Load(_hostingEnvironment.ContentRootPath + FileHelper.RegressionModelPath, out var modelInputSchema); var predictionEngine = mlContext.Model.CreatePredictionEngine <RegressionModelInput, RegressionModelOutput>(mlModel); //Input // Try model on sample data // Predict a whole year, starting with startDate DateTime startDate = new DateTime(2019, 1, 1); var predictedValues = new List <(string date, float calls)>(); var calls = GetCalls(); for (DateTime date = startDate; date < startDate.AddYears(1); date = date.AddDays(1)) { var dateString = date.ToString("MM'/'dd'/'yyyy 00:00:00"); var row = calls.Where(x => x.Date == date); string weatherConditions = row.FirstOrDefault().WeatherConditions; float callCount = 0; for (int i = 0; i < 24; i++) { var prediction = predictionEngine.Predict(new RegressionModelInput() { Hour = (float)i, Month = (float)date.Month, DayOfWeek = date.DayOfWeek.ToString(), WeatherConditions = weatherConditions }); callCount += prediction.Score; } predictedValues.Add((dateString, callCount)); } return(Json(new { Status = 1, result = predictedValues })); }
public IActionResult Analysis(RegressionModelInput input) { // Load the model MLContext mlContext = new MLContext(); ITransformer mlModel = mlContext.Model.Load(_hostingEnvironment.ContentRootPath + FileHelper.RegressionModelPath, out var modelInputSchema); var predictionEngine = mlContext.Model.CreatePredictionEngine <RegressionModelInput, RegressionModelOutput>(mlModel); var inputdate = DateTime.Parse(input.DayOfWeek); //Input // Try model on sample data // Predict values for each hour of this day var calls = GetCalls(); var actual = calls.Where(x => x.Date == new DateTime(2019, inputdate.Month, inputdate.Day)).Select(x => new ChartOutput() { x = Convert.ToInt32(x.Hour), y = x.Calls }); var predictedValues = new List <ChartOutput>(); for (int i = 0; i < 24; i++) { var prediction = predictionEngine.Predict(new RegressionModelInput() { Hour = (float)i, Month = inputdate.Month, DayOfWeek = inputdate.DayOfWeek.ToString(), WeatherConditions = input.WeatherConditions }); predictedValues.Add(new ChartOutput { x = i, y = Math.Round(prediction.Score) }); } return(Json(new { Status = 1, actual = actual.ToArray(), predicted = predictedValues.ToArray() })); }