public static void Evaluate(MLContext context, string outputModelPath = "famoccupancy_fastTreeTweedie.zip") { ConsoleWriteHeader("Evaluate family room occupancy Model"); ITransformer trainedModel; using (var stream = File.OpenRead(outputModelPath)) { trainedModel = context.Model.Load(stream); } var predictionFunct = trainedModel.MakePredictionFunction <OccupancyTrainer, OccupancyPrediction>(context); Console.WriteLine("** Testing sample date **"); // Build sample data var dataSample = new OccupancyTrainer() { Date = DateTime.Parse("08-12-2018"), Occupied = 0, TotalRoom = 22, Prev = 0, }; // Predict sample data var prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 09-12-2018, - Real value: 7, Predicted Forecast: {prediction.Score}"); Console.WriteLine("\n\n=============== Attempt to predict the next 14 days occupancy ==============="); float ps = 0; float oed = 7; var datey = DateTime.Parse("09-12-2018"); for (int i = 0; i < 14; i++) { dataSample = new OccupancyTrainer() { Date = datey, Occupied = oed, TotalRoom = 22, Prev = ps, }; prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date to predict: {dataSample.Date.AddDays(1)}, - Predicted Forecast: {prediction.Score}"); datey = datey.AddDays(1); ps = oed; oed = prediction.Score; } }
private static IList <OccupancyTrainer> GetOccupancyData() { IList <OccupancyTrainer> occupancyList = new List <OccupancyTrainer>(); using (var context = new AppContext()) { var dblist = context.Occupancy.ToList(); foreach (var item in dblist) { OccupancyTrainer modelOcc = new OccupancyTrainer(); modelOcc.Date = item.Date; modelOcc.Occupied = item.RoomOccupied; modelOcc.TotalRoom = item.TotalRoom; modelOcc.Next = GetNextDayValue(item.Date); modelOcc.Prev = GetPrevDayValue(item.Date); occupancyList.Add(modelOcc); } } return(occupancyList); }
public static void Evaluate(MLContext context, string outputModelPath = "occupancy_fastTreeTweedie.zip") { ConsoleWriteHeader("Evaluate Model"); ITransformer trainedModel; using (var stream = File.OpenRead(outputModelPath)) { trainedModel = context.Model.Load(stream); } var predictionFunct = trainedModel.MakePredictionFunction <OccupancyTrainer, OccupancyPrediction>(context); Console.WriteLine("** Testing sample date **"); // Build sample data var dataSample = new OccupancyTrainer() { Date = DateTime.Parse("25-10-2018"), Occupied = 40, TotalRoom = 71, Prev = 51, }; // Predict sample data var prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 26-10-2018, - Real value: 40, Predicted Forecast: {prediction.Score}"); dataSample = new OccupancyTrainer() { Date = DateTime.Parse("26-10-2018"), Occupied = 40, TotalRoom = 71, Prev = 40, }; // Predict sample data prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 27-10-2018, - Real value: 44, Predicted Forecast: {prediction.Score}"); dataSample = new OccupancyTrainer() { Date = DateTime.Parse("05-12-2018"), Occupied = 25, TotalRoom = 71, Prev = 28, }; // Predict sample data prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 06-12-2018, - Real value: 14, Predicted Forecast: {prediction.Score}"); dataSample = new OccupancyTrainer() { Date = DateTime.Parse("06-12-2018"), Occupied = 14, TotalRoom = 71, Prev = 25, }; // Predict sample data prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 07-12-2018, - Real value: 16, Predicted Forecast: {prediction.Score}"); dataSample = new OccupancyTrainer() { Date = DateTime.Parse("07-12-2018"), Occupied = 16, TotalRoom = 71, Prev = 14, }; // Predict sample data prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 08-12-2018, - Real value: 13, Predicted Forecast: {prediction.Score}"); dataSample = new OccupancyTrainer() { Date = DateTime.Parse("08-12-2018"), Occupied = 13, TotalRoom = 71, Prev = 16, }; // Predict sample data prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 09-12-2018, - Real value: 27, Predicted Forecast: {prediction.Score}"); dataSample = new OccupancyTrainer() { Date = DateTime.Parse("09-12-2018"), Occupied = 27, TotalRoom = 71, Prev = 13, }; prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date: {dataSample.Date}, date to predict: 10-12-2018, - Predicted Forecast: {prediction.Score}"); Console.WriteLine("\n\n=============== Attempt to predict the next 14 days occupancy ==============="); var appcontext = new AppContext(); var latestDate = appcontext.Occupancy.Max(r => r.Date); var latestItem = appcontext.Occupancy.Where(i => i.Date == latestDate).FirstOrDefault(); var previousItem = appcontext.Occupancy.Where(i => i.Date == latestDate.AddDays(-1)).FirstOrDefault(); float ps = previousItem.RoomOccupied; float oed = latestItem.RoomOccupied; var datey = latestItem.Date; for (int i = 0; i < 14; i++) { dataSample = new OccupancyTrainer() { Date = datey, Occupied = oed, TotalRoom = latestItem.TotalRoom, Prev = ps, }; prediction = predictionFunct.Predict(dataSample); Console.WriteLine($"Date to predict: {dataSample.Date.AddDays(1)}, - Predicted Forecast: {prediction.Score}"); datey = datey.AddDays(1); ps = oed; oed = prediction.Score; } }