private static void PredictSimple(string name, float age, string gender, PredictionFunction <AgeRange, AgeRangePrediction> predictionFunction) { var example = new AgeRange() { Age = age, Name = name, Gender = gender }; var prediction = predictionFunction.Predict(example); Console.WriteLine($"Name: {example.Name}\t Age: {example.Age:00}\t Gender: {example.Gender}\t >> Predicted Label: {prediction.PredictedLabel}"); }
public IrisPrediction Predict(double sl, double sw, double pl, double pw) { var obs = new IrisObservation() { Sepal_length = (float)sl, Sepal_width = (float)sw, Petal_length = (float)pl, Petal_width = (float)pw, }; return(_fct.Predict(obs)); }
private void ComparePredictions(PredictionFunction <IrisData, IrisPrediction> model) { IrisPrediction prediction = model.Predict(new IrisData() { SepalLength = 5.1f, SepalWidth = 3.3f, PetalLength = 1.6f, PetalWidth = 0.2f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 6.4f, SepalWidth = 3.1f, PetalLength = 5.5f, PetalWidth = 2.2f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 4.4f, SepalWidth = 3.1f, PetalLength = 2.5f, PetalWidth = 1.2f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); }
/// <summary> /// Use your model to make a prediction /// You can change these numbers to test different predictions /// </summary> /// <typeparam name="TSrc"></typeparam> /// <typeparam name="TDst"></typeparam> /// <param name="dataPath"></param> /// <param name="parameters"></param> /// <param name="inputData"></param> /// <returns></returns> public static TDst Predict <TSrc, TDst>(string dataPath, string[] parameters, TSrc inputData) where TSrc : class where TDst : class, new() { // Create a ML.NET environment MLContext mlContext = new MLContext(); IDataView trainingDataView = mlContext.LoadTrainingData <TSrc>(dataPath, inputData); EstimatorChain <KeyToValueMappingTransformer> learningPipeline = mlContext.GetLearningPipeline(parameters); TransformerChain <KeyToValueMappingTransformer> model = trainingDataView.TrainModel(learningPipeline); PredictionFunction <TSrc, TDst> prediction = model.MakePredictionFunction <TSrc, TDst>(mlContext); TDst result = prediction.Predict(inputData); return(result); }
private ImagePredictedLabelWithProbability PredictDataUsingModel(string imageName) { var inputImage = new ImageInputData { ImagePath = imageName }; var image1Probabilities = _model.Predict(inputImage).PredictedLabels; //Set a single label as predicted or even none if probabilities were lower than 70% var bestLabelPrediction = new ImagePredictedLabelWithProbability() { ImagePath = inputImage.ImagePath, }; (bestLabelPrediction.PredictedLabel, bestLabelPrediction.Probability) = ModelHelpers.GetBestLabel(_labels, image1Probabilities); return(bestLabelPrediction); }
public bool IsMalicious(byte[] data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (_predictionFunction == null) { LoadModel(); } var predictorData = new PEModelData(); var prediction = _predictionFunction.Predict(predictorData); return(prediction.IsMalicious); }
public static void VisualizeSomePredictions(MLContext mlContext, string modelName, string testDataLocation, PredictionFunction <DemandObservation, DemandPrediction> predFunction, int numberOfPredictions) { //Make a few prediction tests // Make the provided number of predictions and compare with observed data from the test dataset var testData = ReadSampleDataFromCsvFile(testDataLocation, numberOfPredictions); for (int i = 0; i < numberOfPredictions; i++) { //Score var resultprediction = predFunction.Predict(testData[i]); Common.ConsoleHelper.PrintRegressionPredictionVersusObserved(resultprediction.PredictedCount.ToString(), testData[i].Count.ToString()); } }
public TPrediction Predict(TData dataSample) { //Get PredictionEngine object from the Object Pool PredictionFunction <TData, TPrediction> predictionEngine = _predictionEnginePool.GetObject(); //Measure Predict() execution time var watch = System.Diagnostics.Stopwatch.StartNew(); //Predict TPrediction prediction = predictionEngine.Predict(dataSample); //Stop measuring time watch.Stop(); long elapsedMs = watch.ElapsedMilliseconds; //Release used PredictionEngine object into the Object Pool _predictionEnginePool.PutObject(predictionEngine); return(prediction); }
protected IEnumerable <ImageNetDataProbability> PredictDataUsingModel(string testLocation, string imagesFolder, string labelsLocation, PredictionFunction <ImageNetData, ImageNetPrediction> model) { Console.WriteLine($"Images folder: {imagesFolder}"); Console.WriteLine($"Training file: {testLocation}"); Console.WriteLine($"Labels file: {labelsLocation}"); var labels = File.ReadAllLines(labelsLocation); var testData = ImageNetData.ReadFromTsv(testLocation, imagesFolder); foreach (var sample in testData) { var probs = model.Predict(sample).PredictedLabels; var imageData = new ImageNetDataProbability() { ImagePath = sample.ImagePath, Label = sample.Label }; (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs); yield return(imageData); } }
public TPrediction PredictSingle(TObservation input) { return(PredictionFunction.Predict(input)); }
public TPrediction PredictSingle(TObservation input) { CheckTrainedModelIsLoaded(); return(PredictionFunction.Predict(input)); }
public float Post([FromBody] SearchData instance) { return(_predictionFn.Predict(instance).Estimate); }