public async System.Threading.Tasks.Task <IEnumerable <string> > GetRecommendationsForUserAsync(string user, string[] products, int recommendationsInPage) { var model = await PredictionModel.ReadAsync <SalesData, SalesPrediction>(modelLocation); // Create all possible SalesData objects between (unique) CustomerId x ProductId (many) var crossPredictions = from product in products select new SalesData { CustomerId = user, ProductId = product }; // Execute the recommendation model with previous generated data var predictions = model.Predict(crossPredictions).ToArray(); //Count how many recommended products the user gets (with more or less score..) var numberOfRecommendedProducts = predictions.Where(x => x.Recommendation.IsTrue == true).Select(x => x.Recommendation).Count(); //Count how many recommended products the user gets (with more than 0.7 score..) var RecommendedProductsOverThreshold = (from p in predictions orderby p.Score descending where p.Recommendation.IsTrue == true && p.Score > 0.7 select new SalesPrediction { ProductId = p.ProductId, Score = p.Score, Recommendation = p.Recommendation }); var numberOfRecommendedProductsOverThreshold = RecommendedProductsOverThreshold.Count(); // Return (recommendationsInPage) product Ids ordered by Score return(predictions .Where(p => p.Recommendation.IsTrue) .OrderByDescending(p => p.Score) .Select(p => p.ProductId) .Take(recommendationsInPage)); }
public static List <DataSolution> Predict(string NnModelPath, List <NnRow> dataset) { List <DataSolution> predictions = new List <DataSolution>(); if (string.IsNullOrWhiteSpace(NnModelPath)) { return(predictions); } var task = PredictionModel.ReadAsync <MLNetData, MLNetPredict>(NnModelPath); PredictionModel <MLNetData, MLNetPredict> model = task.Result; MLNetData[] testSet = Convert(dataset); List <MLNetPredict> preds = model.Predict(testSet).ToList(); if (dataset.Count != preds.Count) { throw new InvalidDataException(); } // Write out predictions to CSV for (int i = 0; i < dataset.Count; i++) { predictions.Add(new DataSolution() { ID_CPTE = dataset[i].id, Default = (int)preds[i].PredictedLabel }); } return(predictions); }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public string FunctionHandler(JObject jObjectInput, ILambdaContext context) { string input = jObjectInput["item"].ToObject <string>(); PredictionModel <SentimentData, SentimentPrediction> model = PredictionModel.ReadAsync <SentimentData, SentimentPrediction>(modelPath).Result; SentimentData data = new SentimentData { SentimentText = input, Sentiment = 0 }; SentimentPrediction prediction = model.Predict(data); DynamoDbSchema schematizedPrediction = new DynamoDbSchema { Feedback = input, Date = DateTime.UtcNow.ToString("MM/dd/yyyy"), Sentiment = prediction.Sentiment ? "positive" : "negative" }; string serializedPrediction = JsonConvert.SerializeObject(schematizedPrediction); string response = PostPredictionViaHttp(serializedPrediction); return(JsonConvert.SerializeObject(new Dictionary <string, string> { { "dynamoPayload", serializedPrediction }, { "dynamoResponse", response } })); // return PostPredicitionViaInvokeRequest(serializedPrediction); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, [Blob("models/model.zip", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream serializedModel, TraceWriter log) { // Workaround for Azure Functions Host if (typeof(Microsoft.ML.Runtime.Data.LoadTransform) == null || typeof(Microsoft.ML.Runtime.Learners.LinearClassificationTrainer) == null || typeof(Microsoft.ML.Runtime.Internal.CpuMath.SseUtils) == null || typeof(Microsoft.ML.Runtime.FastTree.FastTree) == null) { log.Error("Error loading ML.NET"); return(new StatusCodeResult(500)); } //Read incoming request body string requestBody = new StreamReader(req.Body).ReadToEnd(); log.Info(requestBody); //Bind request body to IrisData object IrisData data = JsonConvert.DeserializeObject <IrisData>(requestBody); //Load prediction model var model = PredictionModel.ReadAsync <IrisData, IrisPrediction>(serializedModel).Result; //Make prediction IrisPrediction prediction = model.Predict(data); //Return prediction return((IActionResult) new OkObjectResult(prediction.PredictedLabels)); }
public async void TrainSaveModelAndPredict() { var dataPath = GetDataPath(SentimentDataPath); var testDataPath = GetDataPath(SentimentDataPath); var pipeline = new LearningPipeline(); pipeline.Add(new TextLoader(dataPath).CreateFrom <SentimentData>()); pipeline.Add(MakeSentimentTextTransform()); pipeline.Add(new FastTreeBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 }); pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" }); var model = pipeline.Train <SentimentData, SentimentPrediction>(); var modelName = "trainSaveAndPredictdModel.zip"; DeleteOutputPath(modelName); await model.WriteAsync(modelName); var loadedModel = await PredictionModel.ReadAsync <SentimentData, SentimentPrediction>(modelName); var singlePrediction = loadedModel.Predict(new SentimentData() { SentimentText = "Not big fan of this." }); Assert.True(singlePrediction.Sentiment); }
static async Task Main(string[] args) { using (var source = new CancellationTokenSource()) { var model = await MemoriaAgent.Build(source.Token); var evaluation = await model.Evaluate(); Console.WriteLine($" Root Mean Square Error : {evaluation.Rms}"); Console.WriteLine($" Coef of determination : {evaluation.RSquared}"); var test = new Sell { Temperature = 7, Age = 72, TreatedProbability = 0.7741935483870968f, CityCode = 1 }; var prediction = model.Predict(test); Console.WriteLine($"Prediction is {prediction.Amount}"); Console.WriteLine($"Saving the model..."); await model.SaveAsync(source.Token); Console.WriteLine($"Reloading the model..."); var loadedModel = await PredictionModel.ReadAsync <Sell, SellPrediction>(Files.LoadStorePath()); prediction = loadedModel.Predict(test); Console.WriteLine($"Prediction using previous test data is {prediction.Amount}"); } }
/// <summary> /// Predict samples using saved model /// </summary> /// <param name="outputModelPath">Model file path</param> /// <returns></returns> public static async Task TestPrediction(string outputModelPath = "Pricing_fastTree_model.zip") { Console.WriteLine("*********************************"); Console.WriteLine("Testing pricing Unit Forecast model"); // Read the model that has been previously saved by the method SaveModel var model = await PredictionModel.ReadAsync <PriceData, PriceUnitPrediction>(outputModelPath); Console.WriteLine("** Testing Pricing 1 **"); // STEP 8: Use your model to make a prediction // You can change these numbers to test different predictions PriceData dataSample = new PriceData() { property_code = "building", Avdrags_code = "1", property_count = 2, floor_Price = 1.3f, payment_type = "CRD", Model_Price = 0 // predict it. actual = 7 }; //model.Predict() predicts the indicative price to the one provided above PriceUnitPrediction prediction = model.Predict(dataSample); Console.WriteLine("indicative Pricing: {0}, actual Pricing: 7", prediction.Model_Price); }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, TraceWriter log) { var r = new StreamReader(req.Body); log.Info("Prediction trigger function started..."); var content = await r.ReadToEndAsync(); log.Info(content); //if (typeof(Microsoft.ML.Runtime.Data.LoadTransform) == null || // typeof(Microsoft.ML.Runtime.Learners.LinearClassificationTrainer) == null || // typeof(Microsoft.ML.Runtime.Internal.CpuMath.SseUtils) == null) //{ // log.Info("Assemblies are NOT loaded correctly"); // return new BadRequestObjectResult("ML model failed to load"); //} var request = JsonConvert.DeserializeObject <PredictionRequest>(content); var model = await PredictionModel.ReadAsync <BankStatementLineItem, PredictedLabel>(PredictionModelWrapper.GetModel()); var predicted = model.Predict(BankStatementLineItem.ToBankStatementLineItem(request)); //return predicted != null // ? (ActionResult) new OkObjectResult(predicted.SubCategory) // : new BadRequestObjectResult("prediction failed"); return(new BadRequestObjectResult("no dice")); }
// gets appartment data and predicts a price public async Task <string> predict(int inCityID, int inRoomsNumber, int inSizeInMeters, bool inIsThereElivator, bool inFurnitureInculded, bool inIsRenovated) { // Get the city in which the apartment is located Models.City currCity = _context.City.Where(c => c.ID == inCityID).First(); // create an ApartmentData object from the given parameters ML.ApartmentData newExample = new ML.ApartmentData { cityID = inCityID, cityAvarageSalary = currCity.avarageSalary, region = (int)currCity.region, cityGraduatesPercent = currCity.GraduatesPercents, RoomsNumber = inRoomsNumber, sizeInMeters = inSizeInMeters, isThereElivator = inIsThereElivator, furnitureInculded = inFurnitureInculded, isRenovated = inIsRenovated }; //Read the model from the zip file var model = await PredictionModel.ReadAsync <ApartmentData, AppartmentPricePrediction>(ModelBuilder.MODEL_PATH); // Make the prediction ML.AppartmentPricePrediction prediction = model.Predict(newExample); return(((int)prediction.price).ToString()); }
public string Post([FromBody] IrisData instance) { var model = PredictionModel.ReadAsync <IrisData, IrisPrediction>("model.zip").Result; var prediction = model.Predict(instance); return(prediction.PredictedLabels); }
public async Task EvaluateAsync(string modelPath, string csvPath) { var model = await PredictionModel.ReadAsync <Product, ProductCategoryPrediction>(modelPath).ConfigureAwait(false); // To evaluate how good the model predicts values, the model is ran against new set // of data (test data) that was not involved in training. var testData = new TextLoader(csvPath).CreateFrom <Product>(useHeader: false, allowQuotedStrings: false, supportSparse: false); // ClassificationEvaluator performs evaluation for Multiclass Classification type of ML problems. var evaluator = new ClassificationEvaluator { OutputTopKAcc = 3 }; Console.WriteLine("=============== Evaluating model ==============="); var metrics = evaluator.Evaluate(model, testData); Console.WriteLine("Metrics:"); Console.WriteLine($" AccuracyMacro = {metrics.AccuracyMacro:0.####}, a value between 0 and 1, the closer to 1, the better"); Console.WriteLine($" AccuracyMicro = {metrics.AccuracyMicro:0.####}, a value between 0 and 1, the closer to 1, the better"); Console.WriteLine($" LogLoss = {metrics.LogLoss:0.####}, the closer to 0, the better"); Console.WriteLine($" LogLoss for class 1 = {metrics.PerClassLogLoss[0]:0.####}, the closer to 0, the better"); Console.WriteLine($" LogLoss for class 2 = {metrics.PerClassLogLoss[1]:0.####}, the closer to 0, the better"); Console.WriteLine($" LogLoss for class 3 = {metrics.PerClassLogLoss[2]:0.####}, the closer to 0, the better"); Console.WriteLine("=============== End evaluating ==============="); Console.WriteLine(); }
public static string GetPredictedResult(string buying, string maintenance, string doors, string persons, string lugboot, string safety) { buying = ValueMapper(buying); maintenance = ValueMapper(maintenance); doors = ValueMapper(doors); persons = ValueMapper(persons); lugboot = ValueMapper(lugboot); safety = ValueMapper(safety); CarEval carEval = new CarEval() { Buying = buying, Maintenance = maintenance, Doors = doors, Persons = persons, LugBoot = lugboot, Safety = safety }; // Load model string appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); string modelPath = Path.Combine(appPath, "CarEvaluator.zip"); // Read model var model = PredictionModel.ReadAsync <CarEval, CarEvalPredictor>(modelPath).Result; // Predict car value var prediction = model.Predict(carEval); // Return to the caller return(ValueMapper(prediction.Result)); }
internal override async Task <ReturnObj <PredictionModel <T, TK> > > LoadOrGenerateModelAsync <T, TK>(string trainingFileName) { PredictionModel <T, TK> model; if (File.Exists(ModelName)) { model = await PredictionModel.ReadAsync <T, TK>(ModelName); return(new ReturnObj <PredictionModel <T, TK> >(model)); } try { var pipeline = new LearningPipeline { new TextLoader(trainingFileName).CreateFrom <T>(separator: ','), new ColumnConcatenator("Features", "Features"), new FastTreeRegressor() }; model = pipeline.Train <T, TK>(); await model.WriteAsync(ModelName); } catch (Exception ex) { return(new ReturnObj <PredictionModel <T, TK> >(ex)); } return(new ReturnObj <PredictionModel <T, TK> >(model)); }
public async void PredictHousePriceModelTest() { string modelFilePath = GetOutputPath("PredictHousePriceModelTest.zip"); ModelHelper.WriteKcHousePriceModel(GetDataPath("kc_house_data.csv"), modelFilePath); PredictionModel <HousePriceData, HousePricePrediction> model = await PredictionModel.ReadAsync <HousePriceData, HousePricePrediction>(modelFilePath); HousePricePrediction prediction = model.Predict(new HousePriceData() { Bedrooms = 3, Bathrooms = 2, SqftLiving = 1710, SqftLot = 4697, Floors = 1.5f, Waterfront = 0, View = 0, Condition = 5, Grade = 6, SqftAbove = 1710, SqftBasement = 0, YearBuilt = 1941, YearRenovated = 0, Zipcode = 98002, Lat = 47.3048f, Long = -122.218f, SqftLiving15 = 1030, SqftLot15 = 4705 }); Assert.InRange(prediction.Price, 260_000, 330_000); }
public async Task <string> ClassifySentiment(string usersUtterance) { try { var data = new SentimentData() { SentimentText = usersUtterance }; var blobResult = _blobService.DownloadModelAsync().Result; if (blobResult != null) { var model = await PredictionModel.ReadAsync <SentimentData, SentimentPrediction>(blobResult); var classification = model.Predict(data); _logger.LogInfo($"Sentiment returned from model was: {classification.Sentiment}"); return($"{(classification.Sentiment ? "Positive" : "Negative")}"); } return(null); } catch (Exception ex) { _logger.LogException(ex); throw ex; } }
private async void ResultsGridView_ItemClick(object sender, ItemClickEventArgs e) { var gridItemClicked = (ResultGrigViewItem)e.ClickedItem; switch (gridItemClicked.Name) { case "ML.NET": var model = PredictionModel .ReadAsync <Fatalities, FatalitiesPrediction>("Model.zip").Result; var prediction = model.Predict(_fatality); ScoreTxtBlock.Text = prediction.InjurySeverity.ToString("N0"); var injSev = prediction.InjurySeverity.ToString("N"); LoadatOTextBlock(injSev); break; case "Azure Machine Learning Studio": var isConnected = CrossConnectivity.Current.IsConnected; if (isConnected) { var result = await new AzureMlPrediction(_azureMlFatality).InvokeRequestResponseService(); LoadatOTextBlock(result); } else { LoadatOTextBlock("Network Error"); } break; } }
public async Task <ActionResult> Index(List <int> ingredient, List <int> amount) { PredictionModel <ShakVector, ShakPrediction> model = await PredictionModel.ReadAsync <ShakVector, ShakPrediction>(_modelpath); ShakVector newVec = new ShakVector { TomatoAmount = amount[0], OnionAmount = amount[1], GarlicAmount = amount[2], BellPepperAmount = amount[3], EggsAmount = amount[4], PepperAmount = amount[5], SaltAmount = amount[6], BulgerianCheeseAmount = amount[7], PaprikaAmount = amount[8], WaterAmount = amount[9], TomatoResekAmount = amount[10], CuminAmount = amount[11], EggplantAmount = amount[12], TofuAmount = amount[13], FryingTimeBeforeTomatosMinutes = amount[14], CookingAfterEggsMinutes = amount[15], CookingAfterTomatosMinutes = amount[16] }; ShakPrediction prediction = model.Predict(newVec); return(View("Prediction", prediction.Rating)); }
/// <summary> /// Init <see cref="_model"/> : read model if exist, create it if not /// </summary> public void Init() { if (_ready.Task.IsCompleted) { return; } Task.Run(async() => { if (!ModelExist) { await Train(); } else { try { _model = await PredictionModel.ReadAsync <WindowData, RegionPrediction>(Constants.ModelPath); } catch (Exception) { File.Delete(Constants.ModelPath); await Train(); } } }).ContinueWith(obj => { _ready.SetResult(true); }); }
public async Task TrainModelAsync(string modelPath, string csvPath) { var model = await PredictionModel.ReadAsync <Product, ProductCategoryPrediction>(modelPath).ConfigureAwait(false); var predictions = new List <ProductPrediction>(); using (var s = File.Open(csvPath, FileMode.Open)) using (var r = new StreamReader(s)) { string line; while ((line = await r.ReadLineAsync().ConfigureAwait(false)) != null) { var prediction = Predict(model, line); if (prediction != null) { predictions.Add(prediction); } } } var client = this.CreateMongoClient(); var database = client.GetDatabase("tagger"); var collection = database.GetCollection <ProductPrediction>("prediction"); await collection.InsertManyAsync(predictions).ConfigureAwait(false); }
public static async Task <ExchangeRatePrediction> Predict(ExchangeRate trip) { PredictionModel <ExchangeRate, ExchangeRatePrediction> _model = await PredictionModel.ReadAsync <ExchangeRate, ExchangeRatePrediction>(_modelpath); var prediction = _model.Predict(trip); return(prediction); }
public static async Task <TaxiTripFarePrediction> Predict(TaxiTrip trip) { PredictionModel <TaxiTrip, TaxiTripFarePrediction> _model = await PredictionModel.ReadAsync <TaxiTrip, TaxiTripFarePrediction>(_modelpath); var prediction = _model.Predict(trip); return(prediction); }
public void Handle(string input) { var request = JsonConvert.DeserializeObject <SentimentRequest>(input); var phrases = request.Phrases; var model = PredictionModel.ReadAsync <SentimentData, SentimentPrediction>("sentiment.model").Result; Predict(model, phrases); }
//public static void Predict(PredictionModel<SentimentData, SentimentPrediction> model) public string Run(SentimentData SentimentData1) { var model = PredictionModel.ReadAsync <SentimentData, SentimentPrediction>(_modelpath.Result); SentimentData SentimentData2 = SentimentData1; var prediction = model.Predict(SentimentData1); return(Convert.ToString(prediction.Category)); }
static void Main(string[] args) { if (args.Length > 0) { var action = args[0]; if (action.ToLower() == "train") { Console.WriteLine("Enter path to training data file followed by <Enter>:"); _dataPath = Console.ReadLine(); Console.WriteLine("Enter path to testing data file followed by <Enter>:"); _testDataPath = Console.ReadLine(); Console.WriteLine("Enter path to model file <Enter>:"); _modelPath = Console.ReadLine(); //create empty model file if it doesn't already exist if (!File.Exists(_modelPath)) { File.CreateText(_modelPath); } //train the model var model = TrainModel(); //save the output file model.WriteAsync(_modelPath); Console.WriteLine("Model saved, starting evaluation . . ."); //evaluate the model Evaluate(model); } else if (action.ToLower() == "predict") { Console.WriteLine("Enter text to analyze for sentiment followed by <Enter>:"); var texttoanalyze = Console.ReadLine(); Console.WriteLine("Enter path to model file <Enter>:"); _modelPath = Console.ReadLine(); //open the model file and instantiate the model var model = PredictionModel.ReadAsync <SentimentData, SentimentPrediction>(_modelPath).Result; //run the prediction Predict(model, texttoanalyze); } else { throw new Exception("Must supply 'train' or 'predict' argument."); } } else { throw new Exception("Must supply 'train' or 'predict' argument."); } }
public string Get(string text) { var model = PredictionModel.ReadAsync <SentimentData, SentimentPrediction>("Model.zip").Result; var prediction = model.Predict(new SentimentData { SentimentText = text }); return(prediction.Sentiment ? "Positive" : "Negative"); }
/// <summary> /// Predicts the test data outcomes based on a model that can be /// loaded via path or be given via parameter to this method. /// /// Creates test data. /// Predicts sentiment based on test data. /// Combines test data and predictions for reporting. /// Displays the predicted results. /// </summary> /// <param name="model"></param> internal static async Task <PredictionModel <ClassificationData, ClassPrediction> > PredictAsync( string modelPath, IEnumerable <ClassificationData> predicts = null, PredictionModel <ClassificationData, ClassPrediction> model = null) { if (model == null) { new LightGbmArguments(); model = await PredictionModel.ReadAsync <ClassificationData, ClassPrediction>(modelPath); } if (predicts == null) // do we have input to predict a result? { return(model); } // Use the model to predict the positive or negative sentiment of the data. IEnumerable <ClassPrediction> predictions = model.Predict(predicts); Console.WriteLine(); Console.WriteLine("Classification Predictions"); Console.WriteLine("--------------------------"); // Builds pairs of (sentiment, prediction) IEnumerable <(ClassificationData sentiment, ClassPrediction prediction)> sentimentsAndPredictions = predicts.Zip(predictions, (sentiment, prediction) => (sentiment, prediction)); if (!model.TryGetScoreLabelNames(out var scoreClassNames)) { throw new Exception("Can't get score classes"); } foreach (var(sentiment, prediction) in sentimentsAndPredictions) { string textDisplay = sentiment.Text; if (textDisplay.Length > 80) { textDisplay = textDisplay.Substring(0, 75) + "..."; } string predictedClass = prediction.Class; Console.WriteLine("Prediction: {0}-{1} | Test: '{2}', Scores:", prediction.Class, predictedClass, textDisplay); for (var l = 0; l < prediction.Score.Length; ++l) { Console.Write($" {l}({scoreClassNames[l]})={prediction.Score[l]}"); } Console.WriteLine(); Console.WriteLine(); } Console.WriteLine(); return(model); }
private async Task <PredictionModel <ImageNetData, ImageNetPrediction> > LoadModel() { ConsoleWriteHeader("Read model"); Console.WriteLine($"Model location: {modelLocation}"); // Initialize TensorFlow engine TensorFlowUtils.Initialize(); return(await PredictionModel.ReadAsync <ImageNetData, ImageNetPrediction>(modelLocation)); }
/// <summary> /// 使用訓練好的預測模型檔進行預測 /// </summary> /// <param name="sampleData"></param> public static async Task PredictWithModelLoadedFromFile(IrisData sampleData = null) { // 載入之前訓練好的預測模型 var loadPredictionModel = await PredictionModel.ReadAsync <IrisData, IrisPrediction>(ModelPath); // 使用匯入的預測模型進行預測,若無 sampleData 則用預設測試樣本 var prediction = loadPredictionModel.Predict(sampleData ?? DefaultSampleData); Console.WriteLine($"預測的鳶尾花(Iris)類別: {prediction.PredictedLabel}"); }
async void readModel() { try { model = await PredictionModel <SentimentData, SentimentPrediction> .ReadAsync <SentimentData, SentimentPrediction>(_hostingEnvironment.WebRootPath + "/Model.zip"); } catch (Exception ex) { } }
/// <summary> /// 使用訓練好的預測模型檔進行預測 /// </summary> /// <param name="sampleData"></param> public static async Task PredictFromFile(MachineStatusData sampleData = null) { // 載入之前訓練好的預測模型 var loadPredictionModel = await PredictionModel.ReadAsync <MachineStatusData, MachineStatusPrediction>(ModelPath); // 使用匯入的預測模型進行預測,若無 sampleData 則用預設測試樣本 var prediction = loadPredictionModel.Predict(sampleData ?? DefaultSampleData); Console.WriteLine($"預測類別: {prediction.PredictedLabel}"); }