public Task <RegressionPrediction> Predict(RegressionData data) { return(Task.Run(() => { return _model.Predict(data); })); }
public static void InitRegressionPrediction() { // load the mlContext and data path regressionPredict.LoadData(mlContext, regressionData); // create the model, boolean to force model retrain regressionPredict.BuildAndTrainModel(); // evaluate the model against test data regressionPredict.Evaluate(); // test output with a test method var sample = new RegressionData { VendorId = "VTS", RateCode = "1", PassengerCount = 1, TripTime = 900, TripDistance = 8.63f, PaymentType = "CRD", FareAmount = 0 // To predict. Actual/Observed = 15.5 }; regressionPredict.TestModel(sample); }
public void Predict(MLContext mlContext, ITransformer model, RegressionData dataSample) { var predictionFunction = mlContext.Model.CreatePredictionEngine <RegressionData, RegressionPrediction>(model); var prediction = predictionFunction.Predict(dataSample); Console.WriteLine($"**********************************************************************"); // Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5"); Console.WriteLine($"Prediction: {prediction.Result:0.####}"); Console.WriteLine($"**********************************************************************"); }
public void TestModel(RegressionData sample) { // var taxiTripSample = new RegressionData() // { // Date = ("2/01/2023"), // VendorId = "VTS", // RateCode = "1", // PassengerCount = 1, // TripTime = 1140, // TripDistance = tripDistance, // PaymentType = "CRD", // FareAmount = 0 // To predict. Actual/Observed = 15.5 //}; var predictionFunction = mlContext.Model.CreatePredictionEngine <RegressionData, RegressionPrediction>(trainedModel); var prediction = predictionFunction.Predict(sample); Console.WriteLine($"**********************************************************************"); Console.WriteLine($"Prediction: {prediction.Result:0.####}"); Console.WriteLine($"**********************************************************************"); }
RegressionData calculateRegression(List<double> coords) { double xySum = 0; double xSquaredSum = 0; double ySquaredSum = 0; double ySum = 0; double xSum = 0; double mean; for (int i = 1; i < coords.Count; i++) { double x = i; double xSquared = x * x; double y = coords[i - 1]; double ySquared = y * y; double xy = x * y; xSum += x; ySum += y; xySum += xy; xSquaredSum += xSquared; ySquaredSum += ySquared; } double a = ( (ySum * xSquaredSum) - (xSum * xySum) ) / ( (coords.Count * xSquaredSum) - (xSum * xSum) ); double b = ((coords.Count * xySum) - (xSum * ySum) ) / ( ( coords.Count * xSquaredSum) - (xSum * xSum) ); //Determine regression line // y = a + bx List<double> yData = new List<double>(); for (int i = 1; i < coords.Count; i++) { double x = i; double y = a + (b * x); yData.Add(y); } RegressionData regData = new RegressionData(); regData.yVals = yData; //Calculate standard deviation of regression line ySum = 0; //First calculate sum for (int i = 1; i < yData.Count; i++) { ySum += yData[i - 1]; } mean = ySum / yData.Count; //Next calculate deviations List<double> deviations = new List<double>(); double devSquaredSum = 0; for (int i = 1; i < yData.Count; i++) { double dev = yData[i - 1] - mean; double devSquared = dev * dev; devSquaredSum += devSquared; } double standardDev = System.Math.Sqrt(devSquaredSum / (yData.Count - 1)); regData.a = a; regData.b = b; regData.standardDev = standardDev; regData.yVals = yData; return regData; }