예제 #1
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="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 ClusteringContext.ClusteringTrainers ctx,
                                                                               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.FeatureColumn = featuresName;
                options.WeightColumn  = weightsName != null ? Optional <string> .Explicit(DefaultColumnNames.Weight): Optional <string> .Implicit(DefaultColumnNames.Weight);

                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="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);
        }
예제 #3
0
        /// <summary>
        /// KMeans <see cref="ClusteringContext"/> extension method.
        /// </summary>
        /// <param name="ctx">The clustering 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="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.ClustersCount,
                                                                               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
                {
                    FeatureColumn = featuresName,
                    ClustersCount = clustersCount,
                    WeightColumn  = weightsName != null ? Optional <string> .Explicit(weightsName) : Optional <string> .Implicit(DefaultColumnNames.Weight)
                };

                var trainer = new KMeansPlusPlusTrainer(env, options);

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

            return(rec.Output);
        }
예제 #4
0
        /// <summary>
        /// Train a KMeans++ clustering algorithm.
        /// </summary>
        /// <param name="ctx">The clustering context trainer object.</param>
        /// <param name="options">Algorithm advanced options.</param>
        public static KMeansPlusPlusTrainer KMeans(this ClusteringContext.ClusteringTrainers ctx, KMeansPlusPlusTrainer.Options options)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            Contracts.CheckValue(options, nameof(options));

            var env = CatalogUtils.GetEnvironment(ctx);

            return(new KMeansPlusPlusTrainer(env, options));
        }
예제 #5
0
        /// <summary>
        /// Train a KMeans++ clustering algorithm.
        /// </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>
        public static KMeansPlusPlusTrainer KMeans(this ClusteringContext.ClusteringTrainers ctx,
                                                   string features,
                                                   string weights    = null,
                                                   int clustersCount = KMeansPlusPlusTrainer.Defaults.K,
                                                   Action <KMeansPlusPlusTrainer.Arguments> advancedSettings = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new KMeansPlusPlusTrainer(env, features, clustersCount, weights, advancedSettings));
        }
예제 #6
0
        /// <summary>
        /// Train a KMeans++ clustering algorithm.
        /// </summary>
        /// <param name="ctx">The clustering context trainer object.</param>
        /// <param name="featureColumn">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>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[KMeans](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/KMeans.cs)]
        /// ]]></format>
        /// </example>
        public static KMeansPlusPlusTrainer KMeans(this ClusteringContext.ClusteringTrainers ctx,
                                                   string featureColumn = DefaultColumnNames.Features,
                                                   string weights       = null,
                                                   int clustersCount    = KMeansPlusPlusTrainer.Defaults.ClustersCount)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            var options = new KMeansPlusPlusTrainer.Options
            {
                FeatureColumn = featureColumn,
                WeightColumn  = weights != null ? Optional <string> .Explicit(weights) : Optional <string> .Implicit(DefaultColumnNames.Weight),
                ClustersCount = clustersCount
            };

            return(new KMeansPlusPlusTrainer(env, options));
        }
예제 #7
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);
        }