为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
C#

0x00 前言

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

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

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

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


    webp

    新建项目



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

    webp

    添加包

    webp

    点击安装


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

    webp

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

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

    webp

    修改属性


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

    webp

    打印出超参数和结果

    1. OK,执行它吧。



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


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消