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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.NET Core玩转机器学习

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

ML.NET?專門為.NET開發(fā)者提供了一套跨平臺的開源的機器學(xué)習(xí)框架。

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

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

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

  • 安裝.NET SDK?

    為了創(chuàng)建一個.NET應(yīng)用,首先下載 .NET SDK。??

  • 創(chuàng)建應(yīng)用

    使用如下命令初始化項目,創(chuàng)建一個控制臺應(yīng)用程序,目標(biāo)為myApp

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

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

    dotnet add package Microsoft.ML
  • 下載數(shù)據(jù)集

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

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

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

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

  • 編寫代碼

    打開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}");

    ? ? ? ? }

    ? ? }

    }

    運行應(yīng)用

    使用如下命令行運行程序:

    dotnet run

    在最后一行將輸出對花的預(yù)測結(jié)果,你可以修改傳給Predict函數(shù)各種鳶尾花的特征值看看有什么不同的結(jié)果。

    恭喜,你已經(jīng)跨入使用ML.NET進行機器學(xué)習(xí)的門檻了!

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


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

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。