コード例 #1
0
        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);
        }
コード例 #2
0
        public void TrainAndPredictHousePriceModelTest()
        {
            string dataPath = GetDataPath("kc_house_data.csv");

            var pipeline = new LearningPipeline();

            pipeline.Add(new TextLoader <HousePriceData>(dataPath, useHeader: true, separator: ","));

            pipeline.Add(new ColumnConcatenator(outputColumn: "NumericalFeatures",
                                                "SqftLiving", "SqftLot", "SqftAbove", "SqftBasement", "Lat", "Long", "SqftLiving15", "SqftLot15"));

            pipeline.Add(new ColumnConcatenator(outputColumn: "CategoryFeatures",
                                                "Bedrooms", "Bathrooms", "Floors", "Waterfront", "View", "Condition", "Grade", "YearBuilt", "YearRenovated", "Zipcode"));

            pipeline.Add(new CategoricalOneHotVectorizer("CategoryFeatures"));
            pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
                                                "NumericalFeatures", "CategoryFeatures"));
            pipeline.Add(new StochasticDualCoordinateAscentRegressor());

            PredictionModel <HousePriceData, HousePricePrediction> model = pipeline.Train <HousePriceData, HousePricePrediction>();

            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);

            string testDataPath = GetDataPath("kc_house_test.csv");
            var    testData     = new TextLoader <HousePriceData>(testDataPath, useHeader: true, separator: ",");

            var evaluator             = new RegressionEvaluator();
            RegressionMetrics metrics = evaluator.Evaluate(model, testData);

            Assert.InRange(metrics.L1, 85_000, 89_000);
            Assert.InRange(metrics.L2, 17_000_000_000, 19_000_000_000);
            Assert.InRange(metrics.Rms, 130_500, 135_000);
            Assert.InRange(metrics.LossFn, 17_000_000_000, 19_000_000_000);
            Assert.Equal(.8, metrics.RSquared, 1);
        }
コード例 #3
0
        public ActionResult <IEnumerable <string> > Get(string area, float approxSquFeet, float rooms, float bedrooms, float fullbath, float halfbath, string garageType, float garageSpaces)
        {
            var housePriceSample1 = new HouseData()
            {
                Area = area, BedRooms = bedrooms, BedRoomsBsmt = 0, FullBath = fullbath, HalfBath = halfbath, Rooms = rooms, ApproxSquFeet = approxSquFeet, GarageType = garageType, GarageSpaces = garageSpaces
            };
            var price = HousePricePrediction.PredictSinglePrice(housePriceSample1);

            return(new string[] { price });
        }
コード例 #4
0
        public ActionResult <IEnumerable <string> > Get()
        {
            var housePriceSample1 = new HouseData()
            {
                Area = "76", BedRooms = 3, BedRoomsBsmt = 0, FullBath = 2, HalfBath = 0, Rooms = 7, ApproxSquFeet = 1300, GarageType = "Attached", GarageSpaces = 2
            };
            //var price = HousePricePrediction.PredictSinglePrice(housePriceSample1, @"MLNETModels\housePriceModel.zip");
            var price = HousePricePrediction.PredictSinglePrice(housePriceSample1);

            return(new string[] { price });
        }
コード例 #5
0
        public ActionResult <float> Post([FromBody] HousePriceData data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            Console.WriteLine(data.Surface);
            HousePricePrediction predictedValue = _predictionEnginePool.Predict(modelName: "HousePriceModel", example: data);



            return(Ok(System.Text.Json.JsonSerializer.Serialize(predictedValue)));
        }
コード例 #6
0
        public ActionResult <IEnumerable <string> > Get()
        {
            var housePriceSample1 = new HouseData()
            {
                Area = "76", BedRooms = 3, BedRoomsBsmt = 0, FullBath = 2, HalfBath = 0, Rooms = 7, ApproxSquFeet = 1300, GarageType = "Attached", GarageSpaces = 2
            };

            HouseData[] hd = new HouseData[] { housePriceSample1 };

            var results = HousePricePrediction.PredictSinglePriceSet(hd, _dataTransformModelPath, _modelPath);

            return(new string[] { results[0].ToString() });
        }
コード例 #7
0
        public ActionResult <IEnumerable <string> > Get([FromQuery] string area, [FromQuery] float approxSquFeet, [FromQuery] float rooms, [FromQuery] float bedrooms, [FromQuery] float fullbath,
                                                        [FromQuery] float halfbath, [FromQuery] string garageType, [FromQuery] float garageSpaces)
        {
            var requestHousePriceFeatures = new HouseData()
            {
                Area = area, BedRooms = bedrooms, BedRoomsBsmt = 0, FullBath = fullbath, HalfBath = halfbath, Rooms = rooms, ApproxSquFeet = approxSquFeet, GarageType = garageType, GarageSpaces = garageSpaces
            };

            HouseData[] hd = new HouseData[] { requestHousePriceFeatures };

            var results = HousePricePrediction.PredictSinglePriceSet(hd, _dataTransformModelPath, _modelPath);

            return(new string[] { results[0].ToString() });
        }
コード例 #8
0
        public async Task ReadStrongTypeModelFromStream()
        {
            using (var memoryStream = new MemoryStream())
            {
                ModelHelper.WriteKcHousePriceModel(GetDataPath("kc_house_data.csv"), memoryStream);
                memoryStream.Position = 0;

                var model = await Legacy.PredictionModel.ReadAsync <HousePriceData, HousePricePrediction>(memoryStream);

                HousePricePrediction prediction = model.Predict(new HousePriceData()
                {
                    Bedrooms      = 3,
                    Bathrooms     = 1.75f,
                    SqftLiving    = 2450,
                    SqftLot       = 2691,
                    Floors        = 2,
                    Waterfront    = 0,
                    View          = 0,
                    Condition     = 3,
                    Grade         = 8,
                    SqftAbove     = 1750,
                    SqftBasement  = 700,
                    YearBuilt     = 1915,
                    YearRenovated = 0,
                    Zipcode       = 98119,
                    Lat           = 47.6386f,
                    Long          = -122.36f,
                    SqftLiving15  = 1760,
                    SqftLot15     = 3573
                });

                Assert.InRange(prediction.Price, 790_000, 850_000);


                var dataView = model.Predict(ModelHelper.GetKcHouseDataView(GetDataPath("kc_house_data.csv")));
                dataView.Schema.TryGetColumnIndex("Score", out int scoreColumn);
                using (var cursor = dataView.GetRowCursor((int col) => col == scoreColumn))
                {
                    var   scoreGetter = cursor.GetGetter <float>(scoreColumn);
                    float score       = 0;
                    cursor.MoveNext();
                    scoreGetter(ref score);
                    Assert.InRange(score, 100_000, 200_000);
                }

                Legacy.PredictionModel nonGenericModel;
                using (var anotherStream = new MemoryStream())
                {
                    await model.WriteAsync(anotherStream);

                    nonGenericModel = await Legacy.PredictionModel.ReadAsync(anotherStream);
                }

                dataView = nonGenericModel.Predict(ModelHelper.GetKcHouseDataView(GetDataPath("kc_house_data.csv")));
                using (var cursor = dataView.GetRowCursor((int col) => col == scoreColumn))
                {
                    var   scoreGetter = cursor.GetGetter <float>(scoreColumn);
                    float score       = 0;
                    cursor.MoveNext();
                    scoreGetter(ref score);
                    Assert.InRange(score, 100_000, 200_000);
                }
            }
        }