版本要求:Visual Studio 2017 15.6 或者以后版本
本人上篇博客点这有介绍一种方法,今天介绍另一种方案,在使用上方便很多。
1 新建一个Console App(.NET Core) 我命名为regression1,在解决方案管理器中右击regression1,添加----->机器学习
2 选择场景 这里我们选Price Prediction。
在这里插入图片描述
3 添加数据集和选择预测的列
在这里插入图片描述
4 选择训练算法
这里选择的算法是FastTreeTweedieRegression。
在这里插入图片描述
5 举例5种评估训练算法
在这里插入图片描述
6 生成代码,在解决方案管理器中会出现
在这里插入图片描述
7 运用你模型
在解决方案管理器regression1项目中,将Program.cs代码替换成如下所示


        //Machine Learning model to load and use for predictions加载机器学习模型,并使用他来预测
        private const string MODEL_FILEPATH = @"MLModel.zip";

        //Dataset to use for predictions  用于预测的数据集
        private const string DATA_FILEPATH = @"C:\Users\yxz\Documents\xiaoxiong\taxi-fare-test.csv";
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext();// Load the model 加载模型

            // Training code used by ML.NET CLI and AutoML to generate the model 
            //ModelBuilder.CreateModel();

            ITransformer mlModel = mlContext.Model.Load(GetAbsolutePath(MODEL_FILEPATH), out DataViewSchema inputSchema);
            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
            // Create sample data to do a single prediction with it 创建示例数据来对其进行单个预测
            ModelInput sampleData = CreateSingleDataSample(mlContext, DATA_FILEPATH);

            // Try a single prediction 进行单个预测
            ModelOutput predictionResult = predEngine.Predict(sampleData);

            Console.WriteLine($"Single Prediction --> Actual value: {sampleData.Fare_amount} | Predicted value: {predictionResult.Score}");

            Console.WriteLine("=============== End of process, hit any key to finish ===============");
            Console.ReadKey();
        }
        public static string GetAbsolutePath(string relativePath)
        {
            FileInfo _dataRoot = new FileInfo(typeof(Program).Assembly.Location);
            string assemblyFolderPath = _dataRoot.Directory.FullName;

            string fullPath = Path.Combine(assemblyFolderPath, relativePath);

            return fullPath;
        }

        //********************
        // Method to load single row of data to try a single prediction 加载一行数据,进行单个预测
        // You can change this code and create your own sample data here (Hardcoded or from any source)你可以改变这里的代码,并创建你自己的数据集
        private static ModelInput CreateSingleDataSample(MLContext mlContext, string dataFilePath)
        {
            // Read dataset to get a single row for trying a prediction   读数据集,去单一的一行来进行预测       
            IDataView dataView = mlContext.Data.LoadFromTextFile<ModelInput>(
                                            path: dataFilePath,
                                            hasHeader: true,
                                            separatorChar: ',',
                                            allowQuoting: true,
                                            allowSparse: false);

            // Here (ModelInput object) you could provide new test data, hardcoded or from the end-user application, instead of the row from the file.
            //在这里(ModelInput对象),您可以提供新的测试数据,无论是硬编码的还是来自最终用户应用程序的,而不是来自文件的行。
            ModelInput sampleForPrediction = mlContext.Data.CreateEnumerable<ModelInput>(dataView, false)
                                                                        .First( );
            return sampleForPrediction;
        }

总结:相对我上篇博客来说,这个方案简单了很多,更有趣的是误差居然更小(应该是这个算法选的比较好),根据这个案例,你可以更具不同的数据做不同的回归预测。

还有一个问题:.目前NET Core 跟 .NET Formwork不兼容(所以就不能做成桌面应用),如何将上述模型坐成应用让其他人在没有安装VS软件的前提下就能使用,感谢知道的大神能够指点一二。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐