日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.NET Core玩转机器学习

發布時間:2023/12/4 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET Core玩转机器学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ML.NET?專門為.NET開發者提供了一套跨平臺的開源的機器學習框架。

ML.NET支持.NET開發者不需要過度專業的機器學習開發經驗,就能輕松地訓練自己的模型,并且嵌入到自己的應用中。一切盡在.NET之中。ML.NET早期是由Microsoft Research開發,近十年來逐步集成到一個大體系中被眾多Microsoft產品使用,如大家熟知的Windows、Bing、PowerPoint、Excel之類。

ML.NET的第一個預覽版提供了分類器(如文本分類、情感分析)和回歸(如價格預測)等實用的機器學習模型。第一版發布后在既有功能之上又新增了關于訓練模型的.NET API,使用這些模型進行預測,就像框架中算法、轉換、數據結構一類核心組件一樣的開發體驗。

接下來用個示例,一起進入快速上手的實踐中來。

  • 安裝.NET SDK?

    為了創建一個.NET應用,首先下載 .NET SDK。??

  • 創建應用

    使用如下命令初始化項目,創建一個控制臺應用程序,目標為myApp

    dotnet new console -o myAppcd myApp
  • 安裝ML.NET包

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

    dotnet add package Microsoft.ML
  • 下載數據集

    假設我們使用機器學習來預測鳶尾花的類型,比如有setosa、versicolor、virginica三種,基于特征有四種:花瓣長度、花瓣寬度, 萼片長度、萼片寬度。

    去UCI Machine Learning Repository: Iris Data Set下載一個現成的數據集,復制粘貼其中的數據到任何一個文本編輯器中,然后保存命名為iris-data.txtmyApp目錄中。

    粘貼完文本內容應該是如下格式,每一行表示不同鳶尾花的樣本,數值的部分從左到右依次是萼片長度、萼片寬度、花瓣長度、花瓣寬度,最后是鳶尾花的類型。

    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添加至項目中,需要進行如下配置確保運行時數據集文件在輸出的目錄中。

  • 編寫代碼

    打開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進行機器學習的門檻了!

    原文地址: https://www.cnblogs.com/Wddpct/p/9002242.html


    .NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

    總結

    以上是生活随笔為你收集整理的.NET Core玩转机器学习的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。