예제 #1
0
        // Use model for single prediction
        public static void UseModelForSinglePrediction(MLContext mlContext, ITransformer model)
        {
            // <SnippetPredictionEngine>
            Console.WriteLine("=============== Making a prediction ===============");
            var predictionEngine = mlContext.Model.CreatePredictionEngine <MovieRating, MovieRatingPrediction>(model);
            // </SnippetPredictionEngine>

            // Create test input & make single prediction
            // <SnippetMakeSinglePrediction>
            var testInput = new MovieRating {
                userId = 6, movieId = 10
            };

            var movieRatingPrediction = predictionEngine.Predict(testInput);

            // </SnippetMakeSinglePrediction>

            // <SnippetPrintResults>
            if (Math.Round(movieRatingPrediction.Score, 1) > 3.5)
            {
                Console.WriteLine("Movie " + testInput.movieId + " is recommended for user " + testInput.userId);
            }
            else
            {
                Console.WriteLine("Movie " + testInput.movieId + " is not recommended for user " + testInput.userId);
            }
            // </SnippetPrintResults>
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string movieData = req.Query["movieData"];

            if (String.IsNullOrWhiteSpace(movieData))
            {
                return(new BadRequestObjectResult("Please pass a name on the query string"));
            }

            // Create test input & make single prediction
            String[] values = movieData.Split(":");
            var      movieRatingTestInput = new MovieRating
            {
                userId  = Int32.Parse(values[0]),
                movieId = Int32.Parse(values[1])
            };

            // Create MLContext to be shared across the model creation workflow objects
            MLContext mlContext = new MLContext();

            // Load data
            (IDataView trainingDataView, IDataView testDataView) = LoadData(mlContext);

            // Build & train model
            ITransformer model = BuildAndTrainModel(mlContext, trainingDataView);

            // Evaluate quality of model
            EvaluateModel(mlContext, testDataView, model);

            // Use model to try a single prediction (one row of data)
            string result = UseModelForSinglePrediction(mlContext, model, movieRatingTestInput);

            // Save model
            SaveModel(mlContext, trainingDataView.Schema, model);

            return((ActionResult) new OkObjectResult(result));
        }
예제 #3
0
        public static void UseModelForSinglePrediction(MLContext mlContext, ITransformer model)
        {
            Console.WriteLine("=============== Making a prediction ==============");
            var predictionEngine = mlContext.Model.CreatePredictionEngine <MovieRating, MovieRatingPrediction>(model);

            var testInput = new MovieRating {
                userId = 6, movieId = 10
            };

            var movieRatingPrediction = predictionEngine.Predict(testInput);

            Console.WriteLine("Score: " + movieRatingPrediction.Score.ToString());
            if (Math.Round(movieRatingPrediction.Score, 1) > 3.5)
            {
                Console.WriteLine("Movie: " + testInput.movieId + " is recommended for user " + testInput.userId);
            }
            else
            {
                Console.WriteLine("Movie: " + testInput.movieId + " is not recommended for user " + testInput.userId);
            }
        }
예제 #4
0
        public static void UseModelForSinglePrediction(MLContext mlcontext, ITransformer model)
        {
            Console.WriteLine("=============== Making a prediction ===============");

            // PredictionEngine - a convenience API that performs prediction on single instance of data
            var predictionEngine = mlcontext.Model.CreatePredictionEngine <MovieRating, MovieRatingPrediction>(model);

            // instance of MovieRating called testInput, pass to Prediction Engine
            // determine if movieId 10 should be recommended to userId 6
            var testInput = new MovieRating {
                userId = 6, movieId = 10
            };

            var movieRatingPrediction = predictionEngine.Predict(testInput);

            if (Math.Round(movieRatingPrediction.Score, 1) > 3.5)
            {
                Console.WriteLine("Movie " + testInput.movieId + " is recommended for user " + testInput.userId);
            }
            else
            {
                Console.WriteLine("Movie " + testInput.movieId + " is not recommended for user " + testInput.userId);
            }
        }
        // Use model for single prediction
        public static String UseModelForSinglePrediction(MLContext mlContext, ITransformer model, MovieRating movieRatingTestInput)
        {
            String result = String.Empty;

            Console.WriteLine("=============== Making a prediction ===============");
            var predictionEngine = mlContext.Model.CreatePredictionEngine <MovieRating, MovieRatingPrediction>(model);

            var movieRatingPrediction = predictionEngine.Predict(movieRatingTestInput);

            if (Math.Round(movieRatingPrediction.Score, 1) > 3.5)
            {
                result = String.Concat(movieRatingTestInput.userId, ":", movieRatingTestInput.movieId, ":", true);
            }
            else
            {
                result = String.Concat(movieRatingTestInput.userId, ":", movieRatingTestInput.movieId, ":", false);
            }

            Console.WriteLine(result);
            return(result);
        }