Пример #1
0
        /// <summary>
        /// KMeans <see cref="ClusteringCatalog"/> extension method.
        /// </summary>
        /// <param name="catalog">The regression catalog trainer object.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="options">Algorithm advanced settings.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained.  Note that this action cannot change the result in any way; it is only a way for the caller to
        /// be informed about what was learnt.</param>
        /// <returns>The predicted output.</returns>
        public static (Vector <float> score, Key <uint> predictedLabel) KMeans(this ClusteringCatalog.ClusteringTrainers catalog,
                                                                               Vector <float> features, Scalar <float> weights,
                                                                               KMeansPlusPlusTrainer.Options options,
                                                                               Action <KMeansModelParameters> onFit = null)
        {
            Contracts.CheckValueOrNull(onFit);
            Contracts.CheckValue(options, nameof(options));

            var rec = new TrainerEstimatorReconciler.Clustering(
                (env, featuresName, weightsName) =>
            {
                options.FeatureColumnName       = featuresName;
                options.ExampleWeightColumnName = weightsName;

                var trainer = new KMeansPlusPlusTrainer(env, options);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                else
                {
                    return(trainer);
                }
            }, features, weights);

            return(rec.Output);
        }
Пример #2
0
        /// <summary>
        /// KMeans <see cref="ClusteringCatalog"/> extension method.
        /// </summary>
        /// <param name="catalog">The clustering catalog trainer object.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="clustersCount">The number of clusters to use for KMeans.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained.  Note that this action cannot change the result in any way; it is only a way for the caller to
        /// be informed about what was learnt.</param>
        /// <returns>The predicted output.</returns>
        public static (Vector <float> score, Key <uint> predictedLabel) KMeans(this ClusteringCatalog.ClusteringTrainers catalog,
                                                                               Vector <float> features, Scalar <float> weights = null,
                                                                               int clustersCount = KMeansPlusPlusTrainer.Defaults.NumberOfClusters,
                                                                               Action <KMeansModelParameters> onFit = null)
        {
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(clustersCount > 1, nameof(clustersCount), "If provided, must be greater than 1.");
            Contracts.CheckValueOrNull(onFit);

            var rec = new TrainerEstimatorReconciler.Clustering(
                (env, featuresName, weightsName) =>
            {
                var options = new KMeansPlusPlusTrainer.Options
                {
                    FeatureColumnName       = featuresName,
                    NumberOfClusters        = clustersCount,
                    ExampleWeightColumnName = weightsName
                };

                var trainer = new KMeansPlusPlusTrainer(env, options);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                else
                {
                    return(trainer);
                }
            }, features, weights);

            return(rec.Output);
        }
        public void KMeansEstimator()
        {
            string featureColumn = "NumericFeatures";
            string weights       = "Weights";

            var reader = new TextLoader(Env, new TextLoader.Arguments
            {
                HasHeader = true,
                Separator = "\t",
                Column    = new[]
                {
                    new TextLoader.Column(featureColumn, DataKind.R4, new [] { new TextLoader.Range(1, 784) }),
                    new TextLoader.Column(weights, DataKind.R4, 0)
                }
            });
            var data = reader.Read(new MultiFileSource(GetDataPath(TestDatasets.mnistTiny28.trainFilename)));


            // Pipeline.
            var pipeline = new KMeansPlusPlusTrainer(Env, featureColumn, weightColumn: weights,
                                                     advancedSettings: s => { s.InitAlgorithm = KMeansPlusPlusTrainer.InitAlgorithm.KMeansParallel; });

            TestEstimatorCore(pipeline, data);

            Done();
        }
Пример #4
0
        /// <summary>
        /// KMeans <see cref="ClusteringContext"/> extension method.
        /// </summary>
        /// <param name="ctx">The regression context trainer object.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="clustersCount">The number of clusters to use for KMeans.</param>
        /// <param name="advancedSettings">Algorithm advanced settings.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained.  Note that this action cannot change the result in any way; it is only a way for the caller to
        /// be informed about what was learnt.</param>
        /// <returns>The predicted output.</returns>
        public static (Vector <float> score, Key <uint> predictedLabel) KMeans(this ClusteringContext.ClusteringTrainers ctx,
                                                                               Vector <float> features, Scalar <float> weights = null,
                                                                               int clustersCount = KMeansPlusPlusTrainer.Defaults.K,
                                                                               Action <KMeansPlusPlusTrainer.Arguments> advancedSettings = null,
                                                                               Action <KMeansPredictor> onFit = null)
        {
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(clustersCount > 1, nameof(clustersCount), "If provided, must be greater than 1.");
            Contracts.CheckValueOrNull(onFit);
            Contracts.CheckValueOrNull(advancedSettings);

            var rec = new TrainerEstimatorReconciler.Clustering(
                (env, featuresName, weightsName) =>
            {
                var trainer = new KMeansPlusPlusTrainer(env, featuresName, clustersCount, weightsName, advancedSettings);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                else
                {
                    return(trainer);
                }
            }, features, weights);

            return(rec.Output);
        }
        public void KMeansEstimator()
        {
            string featureColumn = "NumericFeatures";
            string weights       = "Weights";

            var reader = new TextLoader(Env, new TextLoader.Options
            {
                HasHeader = true,
                Separator = "\t",
                Columns   = new[]
                {
                    new TextLoader.Column(featureColumn, DataKind.R4, new [] { new TextLoader.Range(1, 784) }),
                    new TextLoader.Column(weights, DataKind.R4, 0)
                },
                AllowSparse = true
            });
            var data = reader.Read(GetDataPath(TestDatasets.mnistTiny28.trainFilename));


            // Pipeline.
            var pipeline = new KMeansPlusPlusTrainer(Env, new KMeansPlusPlusTrainer.Options
            {
                FeatureColumn = featureColumn,
                WeightColumn  = weights,
                InitAlgorithm = KMeansPlusPlusTrainer.InitAlgorithm.KMeansParallel,
            });

            TestEstimatorCore(pipeline, data);

            Done();
        }
Пример #6
0
        /// <summary>
        /// KMeans <see cref="ClusteringContext"/> extension method.
        /// </summary>
        /// <param name="ctx">The regression context trainer object.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="clustersCount">The number of clusters to use for KMeans.</param>
        /// <param name="advancedSettings">Algorithm advanced settings.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained.  Note that this action cannot change the result in any way; it is only a way for the caller to
        /// be informed about what was learnt.</param>
        /// <returns>The predicted output.</returns>
        public static (Vector <float> score, Key <uint> predictedLabel) KMeans(this ClusteringContext.ClusteringTrainers ctx,
                                                                               Vector <float> features, Scalar <float> weights = null,
                                                                               int clustersCount = KMeansPlusPlusTrainer.Defaults.K,
                                                                               Action <KMeansPlusPlusTrainer.Arguments> advancedSettings = null,
                                                                               Action <KMeansPredictor> onFit = null)
        {
            var rec = new TrainerEstimatorReconciler.Clustering(
                (env, featuresName, weightsName) =>
            {
                var trainer = new KMeansPlusPlusTrainer(env, featuresName, clustersCount, weightsName, advancedSettings);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                else
                {
                    return(trainer);
                }
            }, features, weights);

            return(rec.Output);
        }