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内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    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,执行它吧。

    打印出超参数和结果输出结果了呢。

0x03 后记

(PS: 0x02让我吃了,肚子好饿,早知道就不做机器学习了)

运行起来的效果还是可以的,但是我还是要吐槽比Python的麻烦多了。哼~ 不接受任何不服。

如果帮到了你就点个赞打个赏吧,我才不会谢谢你呢。