分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

ML.NET 示例:聚类之鸢尾花

发布时间:2023-09-06 02:26责任编辑:彭小芳关键词:.NET

写在前面

准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正。
如果有朋友对此感兴趣,可以加入我:https://github.com/feiyun0112/machinelearning-samples.zh-cn
?

聚类鸢尾花数据

ML.NET 版本API 类型状态应用程序类型数据类型场景机器学习任务算法
v0.7动态 API最新版控制台应用程序.txt 文件聚类鸢尾花聚类K-means++

在这个介绍性示例中,您将看到如何使用ML.NET将不同类型鸢尾花划分为不同组。在机器学习的世界中,这个任务被称为群集

问题

为了演示聚类API的实际作用,我们将使用三种类型的鸢尾花:setosa、versicolor和versicolor。它们都存储在相同的数据集中。尽管这些花的类型是已知的,我们将不使用它,只对花的参数,如花瓣长度,花瓣宽度等运行聚类算法。这个任务是把所有的花分成三个不同的簇。我们期望不同类型的花属于不同的簇。

模型的输入使用下列鸢尾花参数:

  • petal length
  • petal width
  • sepal length
  • sepal width

ML 任务 - 聚类

聚类的一般问题是将一组对象分组,使得同一组中的对象彼此之间的相似性大于其他组中的对象。

其他一些聚类示例:

  • 将新闻文章分为不同主题:体育,政治,科技等。
  • 按购买偏好对客户进行分组。
  • 将数字图像划分为不同的区域以进行边界检测或物体识别。

聚类看起来类似于多类分类,但区别在于对于聚类任务,我们不知道过去数据的答案。 因此,没有“导师”/“主管”可以判断我们的算法的预测是对还是错。 这种类型的ML任务称为无监督学习

解决方案

要解决这个问题,首先我们将建立并训练ML模型。 然后我们将使用训练模型来预测鸢尾花的簇。

1. 建立模型

建立模型包括:上传数据(使用TextLoader加载iris-full.txt),转换数据以便ML算法(使用Concatenate)有效地使用,并选择学习算法(KMeans)。 所有这些步骤都存储在trainingPipeline中:

//Create the MLContext to share across components for deterministic resultsMLContext mlContext = new MLContext(seed: 1); ?//Seed set to any number so you have a deterministic environment// STEP 1: Common data loading configurationTextLoader textLoader = mlContext.Data.TextReader(new TextLoader.Arguments() ???????????????????????????????{ ???????????????????????????????????Separator = "\t", ???????????????????????????????????HasHeader = true, ???????????????????????????????????Column = new[] ???????????????????????????????????????????????{ ???????????????????????????????????????????????????new TextLoader.Column("Label", DataKind.R4, 0), ???????????????????????????????????????????????????new TextLoader.Column("SepalLength", DataKind.R4, 1), ???????????????????????????????????????????????????new TextLoader.Column("SepalWidth", DataKind.R4, 2), ???????????????????????????????????????????????????new TextLoader.Column("PetalLength", DataKind.R4, 3), ???????????????????????????????????????????????????new TextLoader.Column("PetalWidth", DataKind.R4, 4), ???????????????????????????????????????????????} ???????????????????????????????});IDataView fullData = textLoader.Read(DataPath);//STEP 2: Process data transformations in pipelinevar dataProcessPipeline = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth");// STEP 3: Create and train the model ????var trainer = mlContext.Clustering.Trainers.KMeans(features: "Features", clustersCount: 3);var trainingPipeline = dataProcessPipeline.Append(trainer);

2. 训练模型

训练模型是在给定数据上运行所选算法的过程。 要执行训练,您需要调用Fit()方法。

var trainedModel = trainingPipeline.Fit(trainingDataView);

3. 使用模型

在建立和训练模型之后,我们可以使用Predict()API来预测鸢尾花的簇,并计算从给定花参数到每个簇(簇的每个质心)的距离。

 ???????????????// Test with one sample text ????????????????var sampleIrisData = new IrisData() ???????????????{ ???????????????????SepalLength = 3.3f, ???????????????????SepalWidth = 1.6f, ???????????????????PetalLength = 0.2f, ???????????????????PetalWidth = 5.1f, ???????????????}; ???????????????// Create prediction engine related to the loaded trained model ???????????????var predFunction = trainedModel.MakePredictionFunction<IrisData, IrisPrediction>(mlContext); ???????????????//Score ???????????????var resultprediction = predFunction.Predict(sampleIrisData); ???????????????????????????????Console.WriteLine($"Cluster assigned for setosa flowers:" + resultprediction.SelectedClusterId);

ML.NET 示例:聚类之鸢尾花

原文地址:https://www.cnblogs.com/feiyun0112/p/10124828.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved