聚集在ml.net中的分类数据上

时间:2019-07-05 14:27:03

标签: c# machine-learning ml.net

我正在努力在ML.NET中对分类数据进行聚类。

var预测变量= mlContext.Model.CreatePredictionEngine(model)行失败,出现异常“ System.InvalidOperationException:'不兼容的功能列类型:'Vector'与'Vector'“

我刚接触ml,有人可以协助吗?

谢谢!

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
    {
    }
}

1 个答案:

答案 0 :(得分:1)

我怀疑您看到了一些问题,因为如果您将onehotencoding和Trainer合并在一个管道中,那么您将管道分成两部分并以转换后的IDataView为基础进行实际培训,而无需成为管道的一部分管道,您可以简化代码:

        <OrchestrationStep Order="5" Type="ClaimsExchange">
          <ClaimsExchanges>
            <ClaimsExchange Id="WriteLastLogonTime" TechnicalProfileReferenceId="Custom-TP-AAD-WriteLastLoginDateUsingObjectId" />
          </ClaimsExchanges>
        </OrchestrationStep>

它应该正常工作。