手记

听说你要用C#做机器学习

0x00 前言

机器学习本身注重的是一种思想和方法,用什么语言开发完全是看应用领域了呢 。因为最近可能会有个 .NET 项目需要加入一些机器学习的功能,在网上溜达了一圈,好像没有多少文章关注到了微软前段时间发布的 ML.NET 。于是就在这里大概入门一下这个新框架吧。(PS: 真不如Python好用,(╬▔皿▔)凸)

注:下面演示的是一个简单的分类预测的例子,选自于官网的ML.NET 教程,和原文的操作过程略有差异。

0x01 10分钟快速上手(网速很重要)

  1. 打开VS2017,新建项目,选择 ".NET Core",名称 myApp (随便起一个,你开心就好)


    新建项目



  2. 添加 ML.NET 包,我现在的版本为0.6.0 (希望不要学tensorflow,严重质疑它现在再刷版本号)

    添加包

    点击安装


    点击安装,修改和授权都要选择同意呢。

    同意一下啦,不然...不然就不给你看了

  3. 下载数据集并复制到项目中,修改文件的属性复制到输出目录,选择永久输出。

    修改属性


  4. 修改Program.cs内容

    using Microsoft.ML;using Microsoft.ML.Data;using Microsoft.ML.Legacy;using Microsoft.ML.Trainers;using Microsoft.ML.Transforms;using Microsoft.ML.Runtime.Api;using Microsoft.ML.Legacy.Data;using Microsoft.ML.Legacy.Trainers;using Microsoft.ML.Legacy.Transforms;using System;using System.Threading;namespace myApp
    {    class Program
        {
            // 步骤 1: 定义数据结构
            // IrisData 用于提供训练数据, 以及用于预测操作的输入。
            // -前4属性是用于预测标签的输入/特征
            // -标签是你所预测的, 只有在训练时才设定
            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 是预测操作返回的结果
            public class IrisPrediction
            {
                [ColumnName("PredictedLabel")]            public string PredictedLabels;
            }        static void Main(string[] args)
            {            // STEP 2: 创建类并加载数据
                var pipeline = new LearningPipeline();            // 注意文件命名
                string dataPath = "iris.data.txt";
                pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','));            //步骤 3: 转换数据
                // 将数值分配给 "标签 " 列中的文本, 
                // 因为只有在模型训练过程中才能处理数字
    
                pipeline.Add(new Dictionarizer("Label"));            // 将所有特征放入向量中
                pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));            // 步骤 4: 添加学习者
                // 向类中添加学习算法。这是一个分类场景 (这是什么类型?)
                pipeline.Add(new StochasticDualCoordinateAscentClassifier());            // 将标签转换回原始文本 (在步骤3中转换为数字后)
                pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" });            // 步骤 5: 基于数据集对模型进行训练
                var model = pipeline.Train<IrisData, IrisPrediction>();            // 步骤 6: 使用您的模型进行预测
                // 您可以更改这些数字来测试不同的预测
                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}");
    
                Thread.Sleep(3000);
            }
            
        }
    }

    打印出超参数和结果

    1. OK,执行它吧。



作者:土豆豆一只
链接:https://www.jianshu.com/p/90609e1e2499


0人推荐
随时随地看视频
慕课网APP