1. 程式人生 > >介紹ML.NET——面向.NET開發人員的機器學習庫

介紹ML.NET——面向.NET開發人員的機器學習庫

目錄

介紹

背景

二元分類問題

建立.NET應用程式並安裝ML.NET庫

使用程式碼

培訓資料

資料類

建立和訓練ML模型

評估模型

測試模型

興趣點


介紹

大多數常見的機器學習(ML)庫都是用Python編寫的,對.NET開發人員來說並不容易。ML.NET庫是ML庫和.NET應用程式之間的橋樑。

ML.NET是一個開源庫,可以直接在.NET應用程式中使用。在本文中,我將介紹如何在Visual Studio 2017中使用ML.NET庫(我正在使用VS 2017社群版)。

背景

二元分類問題

假設我們有兩個點(在二維空間中)是紅色和藍色的組,我們將根據此點的座標(

xy)來預測一個點是屬於該Red組還是屬於該Blue組。我們的培訓資料如下所示:

3 -2 Red
-2 3 Red
-1 -4 Red
2 3 Red
3 4 Red
-1 9 Blue
2 14 Blue
1 17 Blue
3 12 Blue
0 8 Blue

我們有十行點組。每行的兩個第一個值是每個點的座標(xy),第三個值是該點所屬的組。

因為我們只有兩個輸出,Blue或者Red,我們的問題是二元分類問題。解決二元分類問題有很多不同的ML技術,在本文中,我將使用Logistic Regression,因為它是最簡單的ML演算法。

建立.NET應用程式並安裝ML.NET

為簡單起見,我們將建立一個Console Application C#(.NET Framework)並命名它為MyFirstMLDOTNET。在Solution Explorer視窗中,我們還將Program.cs重新命名為MyFirstMLDOTNET.cs

https://www.codeproject.com/KB/AI/1268051/1.png

我們可以通過右鍵單擊MyFirstMLDOTNET專案並選擇Manage NuGet Packages 來安裝ML.NET 

https://www.codeproject.com/KB/AI/1268051/2.png

NuGet視窗中,我們選擇Browse選項卡並在Search欄位中輸入ML.NET '。最後,我們選擇Microsoft.ML並單擊

Install按鈕:

https://www.codeproject.com/KB/AI/1268051/3.png

單擊預覽更改中的確定,然後在許可證接受中單擊“ 我接受 ”。幾秒鐘後,Visual Studio將在輸出視窗中響應一條訊息:

https://www.codeproject.com/KB/AI/1268051/4.png

此時,如果我們嘗試執行我們的應用程式,我們可以收到如下錯誤訊息:

https://www.codeproject.com/KB/AI/1268051/5.png

通過右鍵單擊MyFirstMLDOTNET專案並選擇屬性” 來解決此錯誤。在Properties視窗中,我們選擇左側的Built選項,並在Plaform目標選項中將Any CPU更改為x64

https://www.codeproject.com/KB/AI/1268051/6.png

我們還需要選擇.NET Framework4.7版本(或更高版本),因為我們將遇到早期版本的一些錯誤。我們可以通過選擇左側的Application項並在Target framework項中選擇版本來選擇.NET Framework的版本。如果我們沒有4.7版本(或更高版本),我們可以選擇Install other frameworks,我們將被引導到Microsoft頁面下載並安裝.NET Framework包:

https://www.codeproject.com/KB/AI/1268051/7.png

到目前為止,我們可以嘗試再次執行我的應用程式,它是成功的。

使用程式碼

培訓資料

在建立ML模型之前,我們必須通過右鍵單擊MyFirstMLDOTNET專案並選擇Add> New Item 來建立訓練資料檔案,選擇Text File型別並在Name欄位中輸入myMLData.txt

https://www.codeproject.com/KB/AI/1268051/8.png

單擊“ 新增按鈕。在myMLData.txt視窗中,我們輸入(或複製上面)訓練資料:

3 -2 Red
-2 3 Red
-1 -4 Red
2 3 Red
3 4 Red
-1 9 Blue
2 14 Blue
1 17 Blue
3 12 Blue
0 8 Blue

單擊“ 儲存並關閉myMLData.txt視窗。

資料類

建立訓練資料檔案後,我們還需要建立資料類。類(命名為myData)定義訓練資料的結構(兩個座標(xy)和一個標籤(RedBlue))

public class myData
      {
          [Column(ordinal: "0", name: "XCoord")]
          public float x;
          [Column(ordinal: "1", name: "YCoord")]
          public float y;
          [Column(ordinal: "2", name: "Label")]
          public string Label;
      }

另一個類(命名為myPrediction)儲存預測資訊:

public class myPrediction
  {
            [ColumnName("PredictedLabel")]
            public string PredictedLabels;
  }

建立和訓練ML模型

我們可以建立ML模型並訓練它:

//creating a ML model
var pipeline = new LearningPipeline();
// loading the training data
string dataPath = "..\\..\\myMLData.txt";
pipeline.Add(new TextLoader(dataPath).CreateFrom<myData>(separator: ' '));
//convert string (Red or Blue) to number (0 or 1)
pipeline.Add(new Dictionarizer("Label"));
//combining the two predictor variables (XCoord and YCoord)
//into an aggregate (Features)
pipeline.Add(new ColumnConcatenator("Features", "XCoord", "YCoord"));
//using the Logistic Regression technique for a binary classification problem
pipeline.Add(new Logistic​Regression​Binary​Classifier());
pipeline.Add(new PredictedLabelColumnOriginalValueConverter()
       { PredictedLabelColumn = "PredictedLabel" });
//training the ML model
Console.WriteLine("\nStarting training \n");
var model = pipeline.Train<myData, myPrediction>();

評估模型

我們可以按如下方式評估我們的ML模型:

var testData = new TextLoader(dataPath).CreateFrom<myData>(separator: ' ');
var evaluator = new BinaryClassificationEvaluator();
var metrics = evaluator.Evaluate(model, testData);
double acc = metrics.Accuracy * 100;
Console.WriteLine("Model accuracy = " + acc.ToString("F2") + "%");

測試模型

最後,我們可以用一個新點測試我們的模型:

myData newPoint = new myData(){ x = 5f, y = -7f};
myPrediction prediction = model.Predict(newPoint);
string result = prediction.PredictedLabels;
Console.WriteLine("Prediction = " + result);

我們所有程式碼都在MyFirstMLDOTNET.cs檔案中:

using System;
using Microsoft.ML.Runtime.Api;
using System.Threading.Tasks;
using Microsoft.ML.Legacy;
using Microsoft.ML.Legacy.Data;
using Microsoft.ML.Legacy.Transforms;
using Microsoft.ML.Legacy.Trainers;
using Microsoft.ML.Legacy.Models;

namespace MyFirstMLDOTNET
{
    class MyFirstMLDOTNET
    {
        public class myData
        {
            [Column(ordinal: "0", name: "XCoord")]
            public float x;
            [Column(ordinal: "1", name: "YCoord")]
            public float y;
            [Column(ordinal: "2", name: "Label")]
            public string Label;
        }
        public class myPrediction
        {
            [ColumnName("PredictedLabel")]
            public string PredictedLabels;
        }

        static void Main(string[] args)
        {
            //creating a ML model
            var pipeline = new LearningPipeline();
            // loading the training data
            string dataPath = "..\\..\\myMLData.txt";
            pipeline.Add(new TextLoader(dataPath).CreateFrom<myData>(separator: ' '));
            //convert string (Red or Blue) to number (0 or 1)
            pipeline.Add(new Dictionarizer("Label"));
            //combining the two predictor variables (XCoord and YCoord)
            //into an aggregate (Features)
            pipeline.Add(new ColumnConcatenator("Features", "XCoord", "YCoord"));
            //using Logistic Regression technique for a binary classification problem
            pipeline.Add(new Logistic​Regression​Binary​Classifier());
            pipeline.Add(new PredictedLabelColumnOriginalValueConverter()
            { PredictedLabelColumn = "PredictedLabel" });
            //training and saving the ML model
            Console.WriteLine("\nStarting training \n");
            var model = pipeline.Train<myData, myPrediction>();
            //Evaluating the Model
            var testData = new TextLoader(dataPath).CreateFrom<myData>(separator: ' ');
            var evaluator = new BinaryClassificationEvaluator();
            var metrics = evaluator.Evaluate(model, testData);
            double acc = metrics.Accuracy * 100;
            Console.WriteLine("Model accuracy = " + acc.ToString("F2") + "%");
            //Predicting a new point (5,-7)
            myData newPoint = new myData()
            { x = 5f, y = -7f};
            myPrediction prediction = model.Predict(newPoint);
            string result = prediction.PredictedLabels;
            Console.WriteLine("Prediction = " + result);
            Console.WriteLine("\nEnd ML.NET demo");
            Console.ReadLine();
        }
    }
}

執行我們的應用程式並獲得如下所示的結果:

https://www.codeproject.com/KB/AI/1268051/9.png

興趣點

在本文中,我只介紹了ML.NET - .NET開發人員的機器學習庫 - 基本上。ML.NET仍在開發中,您可以通過此處的教程瞭解有關此庫的更多資訊。

 

原文地址:https://www.codeproject.com/Articles/1268051/Introducing-the-ML-NET-A-Machine-Learning-Library