C#编程之.NET Core玩转机器学习
小标 2019-04-08 来源 : 阅读 2134 评论 0

摘要:本文主要向大家介绍了C#编程之.NET Core玩转机器学习,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了C#编程之.NET Core玩转机器学习,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

C#编程之.NET Core玩转机器学习

ML.NET 专门为.NET开发者提供了一套跨平台的开源的机器学习框架。

ML.NET支持.NET开发者不需要过度专业的机器学习开发经验,就能轻松地训练自己的模型,并且嵌入到自己的应用中。一切尽在.NET之中。ML.NET早期是由Microsoft Research开发,近十年来逐步集成到一个大体系中被众多Microsoft产品使用,如大家熟知的Windows、Bing、PowerPoint、Excel之类。

ML.NET的第一个预览版提供了分类器(如文本分类、情感分析)和回归(如价格预测)等实用的机器学习模型。第一版发布后在既有功能之上又新增了关于训练模型的.NET API,使用这些模型进行预测,就像框架中算法、转换、数据结构一类核心组件一样的开发体验。

接下来用个示例,一起进入快速上手的实践中来。

  1. 安装.NET SDK 

    为了创建一个.NET应用,首先下载 .NET SDK。  

  2. 创建应用

    使用如下命令初始化项目,创建一个控制台应用程序,目标为myApp:

    dotnet new console -o myAppcd myApp
  3. 安装ML.NET包

    使用如下命令安装Microsoft.ML包:

    dotnet add package Microsoft.ML
  4. 下载数据集

    假设我们使用机器学习来预测鸢尾花的类型,比如有setosa、versicolor、virginica三种,基于特征有四种:花瓣长度、花瓣宽度, 萼片长度、萼片宽度。

    去UCI Machine Learning Repository: Iris Data Set下载一个现成的数据集,复制粘贴其中的数据到任何一个文本编辑器中,然后保存命名为iris-data.txt到myApp目录中。

    粘贴完文本内容应该是如下格式,每一行表示不同鸢尾花的样本,数值的部分从左到右依次是萼片长度、萼片宽度、花瓣长度、花瓣宽度,最后是鸢尾花的类型。

    5.1,3.5,1.4,0.2,Iris-setosa
    4.9,3.0,1.4,0.2,Iris-setosa
    4.7,3.2,1.3,0.2,Iris-setosa
    ...

    如果是使用了Visual Studio,将iris-data.txt添加至项目中,需要进行如下配置确保运行时数据集文件在输出的目录中。

  5. C#编程之.NET Core玩转机器学习


  1. 编写代码

    打开Program.cs文件,输入以下代码:

using Microsoft.ML;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using System;
namespace myApp
{
    class Program
    {
        // STEP 1: Define your data structures
        // IrisData is used to provide training data, and as 
        // input for prediction operations
        // - First 4 properties are inputs/features used to predict the label
        // - Label is what you are predicting, and is only set when training
        public class IrisData
        {
            [Column(""0"")]
            public float SepalLength;
            [Column(""1"")]
            public float SepalWidth;
            [Column(""2"")]
            public float PetalLength;
            [Column(""3"")]
            public float PetalWidth;
            [Column(""4"")]
            [ColumnName(""Label"")]
            public string Label;
        }
        // IrisPrediction is the result returned from prediction operations
        public class IrisPrediction
        {
            [ColumnName(""PredictedLabel"")]
            public string PredictedLabels;
        }
        static void Main(string[] args)
        {
            // STEP 2: Create a pipeline and load your data
            var pipeline = new LearningPipeline();
            // If working in Visual Studio, make sure the 'Copy to Output Directory' 
            // property of iris-data.txt is set to 'Copy always'
            string dataPath = ""iris-data.txt"";
            pipeline.Add(new TextLoader<IrisData>(dataPath, separator: "",""));
            // STEP 3: Transform your data
            // Assign numeric values to text in the ""Label"" column, because only
            // numbers can be processed during model training
            pipeline.Add(new Dictionarizer(""Label""));
            // Puts all features into a vector
            pipeline.Add(new ColumnConcatenator(""Features"", ""SepalLength"", ""SepalWidth"", ""PetalLength"", ""PetalWidth""));
            // STEP 4: Add learner
            // Add a learning algorithm to the pipeline. 
            // This is a classification scenario (What type of iris is this?)
            pipeline.Add(new StochasticDualCoordinateAscentClassifier());
            // Convert the Label back into original text (after converting to number in step 3)
            pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = ""PredictedLabel"" });
            // STEP 5: Train your model based on the data set
            var model = pipeline.Train<IrisData, IrisPrediction>();
            // STEP 6: Use your model to make a prediction
            // You can change these numbers to test different predictions
            var prediction = model.Predict(new IrisData()
            {
                SepalLength = 3.3f,
                SepalWidth = 1.6f,
                PetalLength = 0.2f,
                PetalWidth = 5.1f,
            });
            Console.WriteLine($""Predicted flower type is: {prediction.PredictedLabels}"");
        }
    }
}

运行应用

使用如下命令行运行程序:

dotnet run

在最后一行将输出对花的预测结果,你可以修改传给Predict函数各种鸢尾花的特征值看看有什么不同的结果。

恭喜,你已经跨入使用ML.NET进行机器学习的门槛了!

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved