gpt4 book ai didi

c# - ml.net 中分类数据的聚类

转载 作者:行者123 更新时间:2023-11-30 09:42:32 25 4
gpt4 key购买 nike

我正在努力处理 ML.NET 中的分类数据集群。

var Predictor = mlContext.Model.CreatePredictionEngine(model) 行失败,并出现异常“System.InvalidOperationException: '不兼容的特征列类型:'Vector' 与 'Vector''”

我对机器学习很陌生,有人可以帮忙吗?

谢谢!

class Program
{
static void Main(string[] args)
{
var mlContext = new MLContext();
var samples = new[]
{
new DataPoint {Education = "0-5yrs", ZipCode = "98005"},
new DataPoint {Education = "0-5yrs", ZipCode = "98052"},
new DataPoint {Education = "6-11yrs", ZipCode = "98005"},
new DataPoint {Education = "6-11yrs", ZipCode = "98052"},
new DataPoint {Education = "11-15yrs", ZipCode = "98005"}
};

IDataView data = mlContext.Data.LoadFromEnumerable(samples);

var multiColumnKeyPipeline =
mlContext.Transforms.Categorical.OneHotEncoding(
new[]
{
new InputOutputColumnPair("Education"),
new InputOutputColumnPair("ZipCode")
});

IDataView transformedData =
multiColumnKeyPipeline.Fit(data).Transform(data);

string featuresColumnName = "Features";
var pipeline = mlContext.Transforms
.Concatenate(featuresColumnName, "Education", "ZipCode")
.Append(mlContext.Clustering.Trainers.KMeans(featuresColumnName, numberOfClusters: 2));
var model = pipeline.Fit(transformedData);
var predictor = mlContext.Model.CreatePredictionEngine<TransformedData, ClusterPredictionItem>(model);
}

private class DataPoint
{
public string Education { get; set; }

public string ZipCode { get; set; }
}

private class TransformedData
{
public float Education { get; set; }

public float ZipCode { get; set; }
}
internal class ClusterPredictionItem
{
}
}

最佳答案

我怀疑您会看到一些问题,因为如果您将 onehotencoding 和 trainer 合并到一个中,您会看到一些问题,因为您划分管道的方式并将实际训练基于从转换中产生的 IDataView,而不是管道的一部分您可以通过管道简化代码:

 IDataView data = mlContext.Data.LoadFromEnumerable(samples);
string featuresColumnName = "Features";
var pipeline = mlContext.Transforms.Categorical.OneHotEncoding(
new[]
{
new InputOutputColumnPair("Education"),
new InputOutputColumnPair("ZipCode")
}).Append(mlContext.Transforms.Concatenate("Features", "Education", "ZipCode"))
.Append(mlContext.Clustering.Trainers.KMeans(featuresColumnName, numberOfClusters: 2));
var model = pipeline.Fit(data);
var predictor = mlContext.Model.CreatePredictionEngine<DataPoint, ClusterPredictionItem>(model);

它应该可以正常工作。

关于c# - ml.net 中分类数据的聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56904969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com