Пример #1
0
        public ActionResult <IEnumerable <float> > GetPrediction([FromQuery, Required] float?sepalLength,
                                                                 [FromQuery, Required] float?sepalWidth,
                                                                 [FromQuery, Required] float?petalLength,
                                                                 [FromQuery, Required] float?petalWidth)
        {
            var observation = new IrisObservation
            {
                SepalLength = sepalLength.Value,
                SepalWidth  = sepalWidth.Value,
                PetalLength = petalLength.Value,
                PetalWidth  = petalWidth.Value
            };

            var prediction = this.model.Predict(observation);

            return(prediction.Distances);
        }
Пример #2
0
        public void TestEP_Q_KMeansEntryPointAPI_06()
        {
            var iris = FileHelper.GetTestFile("iris.txt");

            using (var env = new ConsoleEnvironment())
            {
                var reader = new TextLoader(env,
                                            new TextLoader.Arguments()
                {
                    Separator = "\t",
                    HasHeader = true,
                    Column    = new[] {
                        new TextLoader.Column("Label", DataKind.R4, 0),
                        new TextLoader.Column("Sepal_length", DataKind.R4, 1),
                        new TextLoader.Column("Sepal_width", DataKind.R4, 2),
                        new TextLoader.Column("Petal_length", DataKind.R4, 3),
                        new TextLoader.Column("Petal_width", DataKind.R4, 4),
                    }
                });

                var pipeline = new ColumnConcatenatingEstimator(env, "Features", "Sepal_length", "Sepal_width", "Petal_length", "Petal_width")
                               .Append(new KMeansPlusPlusTrainer(env, "Features", clustersCount: 3));

                IDataView trainingDataView = reader.Read(new MultiFileSource(iris));
                var       model            = pipeline.Fit(trainingDataView);

                var obs = new IrisObservation()
                {
                    Sepal_length = 3.3f,
                    Sepal_width  = 1.6f,
                    Petal_length = 0.2f,
                    Petal_width  = 5.1f,
                };

                var prediction = model.MakePredictionFunction <IrisObservation, IrisPrediction>(env).Predict(obs);
                Assert.IsTrue(prediction.PredictedLabel != 0);

                var df          = Scikit.ML.DataManipulation.DataFrameIO.ReadCsv(iris, sep: '\t', dtypes: new ColumnType[] { NumberType.R4 });
                var prediction2 = model.MakePredictionFunctionDataFrame(env, df.Schema);
                var df2         = Scikit.ML.DataManipulation.DataFrameIO.ReadCsv(iris, sep: '\t', dtypes: new ColumnType[] { NumberType.R4 });
                var df3         = prediction2.Predict(df2);
                Assert.AreEqual(df.Shape[0], df3.Shape[0]);
            }
        }
Пример #3
0
        public void TestEP_Q_KMeansEntryPointAPI_04()
        {
            var iris = FileHelper.GetTestFile("iris.txt");

            var pipeline = new Legacy.LearningPipeline();

            pipeline.Add(new Legacy.Data.TextLoader(iris).CreateFrom <IrisObservation>(separator: '\t', useHeader: true));
            pipeline.Add(new Legacy.Transforms.ColumnConcatenator("Features", "Sepal_length", "Sepal_width"));
            pipeline.Add(new Legacy.Trainers.KMeansPlusPlusClusterer());
            var model = pipeline.Train <IrisObservation, IrisPrediction>();
            var obs   = new IrisObservation()
            {
                Sepal_length = 3.3f,
                Sepal_width  = 1.6f,
                Petal_length = 0.2f,
                Petal_width  = 5.1f,
            };
            var predictions = model.Predict(obs);

            Assert.IsTrue(predictions.PredictedLabel != 0);
        }