• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python validation.has_fit_parameter函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sklearn.utils.validation.has_fit_parameter函数的典型用法代码示例。如果您正苦于以下问题:Python has_fit_parameter函数的具体用法?Python has_fit_parameter怎么用?Python has_fit_parameter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了has_fit_parameter函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_has_fit_parameter

def test_has_fit_parameter():
    assert not has_fit_parameter(KNeighborsClassifier, "sample_weight")
    assert has_fit_parameter(RandomForestRegressor, "sample_weight")
    assert has_fit_parameter(SVR, "sample_weight")
    assert has_fit_parameter(SVR(), "sample_weight")

    class TestClassWithDeprecatedFitMethod:
        @deprecated("Deprecated for the purpose of testing has_fit_parameter")
        def fit(self, X, y, sample_weight=None):
            pass

    assert has_fit_parameter(TestClassWithDeprecatedFitMethod,
                             "sample_weight"), \
        "has_fit_parameter fails for class with deprecated fit method."
开发者ID:daniel-perry,项目名称:scikit-learn,代码行数:14,代码来源:test_validation.py


示例2: _validate_estimator

    def _validate_estimator(self):
        """Check the estimator and set the base_estimator_ attribute."""
        BaseWeightBoosting._validate_estimator(self,
            default=DecisionTreeClassifier(max_depth=1))

        if not has_fit_parameter(self.base_estimator_, "sample_weight"):
            raise ValueError("%s doesn't support sample_weight."
                             % self.base_estimator_.__class__.__name__)
开发者ID:gtkfi,项目名称:ArcSDM,代码行数:8,代码来源:weight_boosting.py


示例3: _validate_estimator

    def _validate_estimator(self):
        """Check the estimator and set the base_estimator_ attribute."""
        super(AdaBoostClassifier, self)._validate_estimator(
            default=DecisionTreeClassifier(max_depth=1))

        #  SAMME-R requires predict_proba-enabled base estimators
        if self.algorithm == 'SAMME.R':
            if not hasattr(self.base_estimator_, 'predict_proba'):
                raise TypeError(
                    "AdaBoostClassifier with algorithm='SAMME.R' requires "
                    "that the weak learner supports the calculation of class "
                    "probabilities with a predict_proba method.\n"
                    "Please change the base estimator or set "
                    "algorithm='SAMME' instead.")
        if not has_fit_parameter(self.base_estimator_, "sample_weight"):
            raise ValueError("%s doesn't support sample_weight."
                             % self.base_estimator_.__class__.__name__)
开发者ID:hwb2017,项目名称:scikit-learn,代码行数:17,代码来源:weight_boosting.py


示例4: fit

    def fit(self, X, y, sample_weight=None):
        """ Fit the model to data.
        Fit a separate model for each output variable.

        Parameters
        ----------
        X : (sparse) array-like, shape (n_samples, n_features)
            Data.

        y : (sparse) array-like, shape (n_samples, n_outputs)
            Multi-output targets. An indicator matrix turns on multilabel
            estimation.

        sample_weight : array-like, shape = (n_samples) or None
            Sample weights. If None, then samples are equally weighted.
            Only supported if the underlying regressor supports sample
            weights.

        Returns
        -------
        self : object
            Returns self.
        """

        if not hasattr(self.estimator, "fit"):
            raise ValueError("The base estimator should implement a fit method")

        # Ignore this because it chokes on nas
        # X, y = check_X_y(X, y, multi_output=True, accept_sparse=True)
        X, y = np.array(X), np.array(y)

        if y.ndim == 1:
            raise ValueError("y must have at least two dimensions for "
                             "multi target regression but has only one.")

        if (sample_weight is not None and
                not has_fit_parameter(self.estimator, 'sample_weight')):
            raise ValueError("Underlying regressor does not support"
                             " sample weights.")

        self.estimators_ = Parallel(n_jobs=self.n_jobs)(delayed(_fit_estimator)(
            self.estimator, X, y[:, i], sample_weight) for i in range(y.shape[1]))
        return self
开发者ID:eric-czech,项目名称:portfolio,代码行数:43,代码来源:multioutput.py


示例5: compute_cache_classifier_predictions

def compute_cache_classifier_predictions(X, y, sample_weights, estimator, population):
    support_predict_proba = hasattr(estimator, "predict_proba")
    support_sample_weight = has_fit_parameter(estimator, "sample_weight")
    for organizm in population:
        estimator.random_state = organizm.random_state
        if sample_weights and support_sample_weight:
            estimator.fit(
                X[organizm.genome_samples, :][:, organizm.genome_features],
                y[organizm.genome_samples],
                sample_weights[organizm.genome_samples],
            )
        else:
            estimator.fit(X[organizm.genome_samples, :][:, organizm.genome_features], y[organizm.genome_samples])
        if support_predict_proba:
            organizm.cache_predictions = estimator.predict_proba(X[:, organizm.genome_features])
        else:
            predictions = estimator.predict(X[:, organizm.genome_features])
            organizm.cache_predictions = np.zeros((predictions.shape[0], len(estimator.classes_)))
            for i in range(predictions.shape[0]):
                organizm.cache_predictions[i, predictions[i]] += 1
开发者ID:olologin,项目名称:CCEL,代码行数:20,代码来源:CCEL.py


示例6: test_has_fit_parameter

def test_has_fit_parameter():
    assert_false(has_fit_parameter(KNeighborsClassifier, "sample_weight"))
    assert_true(has_fit_parameter(RandomForestRegressor, "sample_weight"))
    assert_true(has_fit_parameter(SVR, "sample_weight"))
    assert_true(has_fit_parameter(SVR(), "sample_weight"))
开发者ID:Afey,项目名称:scikit-learn,代码行数:5,代码来源:test_validation.py


示例7: _check_sample_weight

 def _check_sample_weight(self):
     if not has_fit_parameter(self.base_estimator_, "sample_weight"):
         raise ValueError("%s doesn't support sample_weight."
                          % self.base_estimator_.__class__.__name__)
开发者ID:BobChew,项目名称:scikit-learn,代码行数:4,代码来源:weight_boosting.py


示例8: fit

    def fit(self, X, y, sample_weight=None):
        """ Fit the estimators.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vectors, where n_samples is the number of samples and
            n_features is the number of features.

        y : array-like, shape = [n_samples]
            Target values.

        sample_weight : array-like, shape = [n_samples] or None
            Sample weights. If None, then samples are equally weighted.
            Note that this is supported only if all underlying estimators
            support sample weights.

        Returns
        -------
        self : object
        """
        if isinstance(y, np.ndarray) and len(y.shape) > 1 and y.shape[1] > 1:
            raise NotImplementedError('Multilabel and multi-output'
                                      ' classification is not supported.')

        if self.voting not in ('soft', 'hard'):
            raise ValueError("Voting must be 'soft' or 'hard'; got (voting=%r)"
                             % self.voting)

        if self.estimators is None or len(self.estimators) == 0:
            raise AttributeError('Invalid `estimators` attribute, `estimators`'
                                 ' should be a list of (string, estimator)'
                                 ' tuples')

        if self.selectors is None or len(self.selectors) == 0:
            raise AttributeError('Invalid `selectors` attribute, `selectors`'
                                 ' should be a list of (string, np.array)'
                                 ' tuples')

        if len(self.selectors) != len(self.estimators):
            raise ValueError('Number of selectors and estimators must be equal'
                             '; got %d selectors, %d estimators'
                             % (len(self.selectors), len(self.estimators)))

        if not isinstance(self.weights, type(None)) and len(self.weights) != len(self.estimators):
            raise ValueError('Number of classifiers and weights must be equal'
                             '; got %d weights, %d estimators'
                             % (len(self.weights), len(self.estimators)))

        if sample_weight is not None:
            for name, step in self.estimators:
                if not has_fit_parameter(step, 'sample_weight'):
                    raise ValueError('Underlying estimator \'%s\' does not support'
                                     ' sample weights.' % name)

        self.le_ = LabelEncoder()
        self.le_.fit(y)
        self.classes_ = self.le_.classes_
        self.estimators_ = []

        transformed_y = self.le_.transform(y)
        self.estimators_ = Parallel(n_jobs=self.n_jobs)(
                delayed(_parallel_fit_estimator)(clone(clf), X[:, self.named_selectors[name]], transformed_y,
                    sample_weight)
                    for name, clf in self.estimators)

        return self
开发者ID:daniaki,项目名称:ppi_wrangler,代码行数:67,代码来源:learning.py


示例9: _parallel_build_ranking_estimators

def _parallel_build_ranking_estimators(n_estimators, ensemble, X, y, Q, sample_weight, seeds, verbose):
    """Private function used to build a batch of estimators within a job.
    Now it supports queries and querywise sampling.
    It also breaks the PEP8 line length constraint now"""
    # Retrieve settings
    n_samples, n_features = X.shape
    max_samples = ensemble.max_samples
    max_features = ensemble.max_features
    uQueries = np.unique(Q)

    sample_whole_queries = False
    if hasattr(ensemble, "sample_whole_queries"):
        sample_whole_queries = ensemble.sample_whole_queries

    if not isinstance(max_samples, (numbers.Integral, np.integer)) and (0.0 < max_samples <= 1.0):
        if sample_whole_queries:
            max_samples = int(max_samples * len(uQueries))
        else:
            max_samples = int(max_samples * n_samples)

    if not isinstance(max_features, (numbers.Integral, np.integer)) and (0.0 < max_features <= 1.0):
        max_features = int(max_features * n_features)

    bootstrap = ensemble.bootstrap

    bootstrap_features = ensemble.bootstrap_features
    support_sample_weight = has_fit_parameter(ensemble.base_estimator_, "sample_weight")

    # Build estimators
    estimators = []
    estimators_samples = []
    estimators_features = []

    for i in range(n_estimators):
        if verbose > 1:
            print("building estimator %d of %d" % (i + 1, n_estimators))

        random_state = check_random_state(seeds[i])
        seed = check_random_state(random_state.randint(MAX_INT))
        estimator = ensemble._make_estimator(append=False)

        try:  # Not all estimator accept a random_state
            estimator.set_params(random_state=seed)
        except ValueError:
            pass

        # Draw features
        if bootstrap_features:
            features = random_state.randint(0, n_features, max_features)
        else:
            features = sample_without_replacement(n_features, max_features, random_state=random_state)

        # Draw samples, using sample weights, and then fit
        if support_sample_weight:
            if sample_weight is None:
                curr_sample_weight = np.ones((n_samples,))
            else:
                curr_sample_weight = sample_weight.copy()

            if bootstrap:
                if sample_whole_queries:
                    Qindices = uQueries[random_state.randint(0, len(uQueries), max_samples)]
                    Qindices.sort()
                    indices = reduce(np.append, [np.where(Q == i) for i in Qindices])

                else:
                    indices = random_state.randint(0, n_samples, max_samples)
                sample_counts = bincount(indices, minlength=n_samples)
                curr_sample_weight *= sample_counts

            else:
                if sample_whole_queries:
                    notQindices = uQueries[random_state.randint(0, len(uQueries), len(uQueries) - max_samples)]
                    notQindices.sort()
                    not_indices = reduce(np.append, [np.where(Q == i) for i in Qindices])
                else:
                    not_indices = sample_without_replacement(
                        n_samples, n_samples - max_samples, random_state=random_state
                    )

                curr_sample_weight[not_indices] = 0

            estimator.fit(X[:, features], y, Q=Q, sample_weight=curr_sample_weight)
            samples = curr_sample_weight > 0.0

        # Draw samples, using a mask, and then fit
        else:
            if bootstrap:
                if sample_whole_queries:
                    Qindices = uQueries[random_state.randint(0, len(uQueries), max_samples)]
                    Qindices.sort()
                    indices = reduce(np.append, [np.where(Q == i) for i in Qindices])

                else:
                    indices = random_state.randint(0, n_samples, max_samples)
            else:
                if sample_whole_queries:
                    Qindices = uQueries[
                        sample_without_replacement(len(uQueries), max_samples, random_state=random_state)
                    ]
#.........这里部分代码省略.........
开发者ID:mindis,项目名称:mining_GIMMEREALPROBLEMPLS,代码行数:101,代码来源:bagged_ranking.py


示例10: fit

    def fit(self, X, y, sample_weight=None):
        random_state = check_random_state(self.random_state)

        # Convert data
        X, y = check_X_y(X, y, ["csr", "csc", "coo"])

        # Remap output
        n_samples, self.n_features_ = X.shape
        y = self._validate_y(y)

        # Check parameters
        self._validate_estimator()

        if isinstance(self.min_samples, (numbers.Integral, np.integer)):
            min_samples = self.min_samples
        else:  # float
            min_samples = int(self.min_samples * X.shape[0])

        if not (0 < min_samples <= X.shape[0]):
            raise ValueError("min_samples must be in (0, n_samples]")

        if isinstance(self.min_features, (numbers.Integral, np.integer)):
            min_features = self.min_features
        else:  # float
            min_features = int(self.min_features * self.n_features_)

        if not (0 < min_features <= self.n_features_):
            raise ValueError("min_features must be in (0, n_features]")

        if self.min_estimators <= 0:
            raise ValueError("min_estimators must be greater than 0")

        support_predict_proba = hasattr(self.base_estimator_, "predict_proba")
        support_sample_weight = has_fit_parameter(self.base_estimator_, "sample_weight")
        if not support_sample_weight and sample_weight is not None:
            raise ValueError("The base estimator doesn't support sample weight")

        self.estimators_ = []

        accuracy_per_generation = np.zeros((self.tmax,), dtype=float)
        _best_accuracy = 0
        _best_organizms = []
        _populations = []
        _contributions = []
        _estimator = self._make_estimator(append=False)
        _offsprings = [Organizm(n_samples, self.n_features_, self.ps, self.pf, random_state) for _ in range(self.N1)]

        def _append_population():
            population = [Organizm(n_samples, self.n_features_, self.ps, self.pf, random_state) for _ in range(self.N0)]
            _populations.append(population)
            _contributions.append(np.arange(self.d1, dtype=float))
            self.compute_population_predictions(X, y, sample_weight, _estimator, population)

        def _remove_population(idx):
            del _populations[idx]
            del _contributions[idx]

        def _compute_cache_accuracy_contribution(fitness_func, population):
            accuracy_without_target = fitness_func()
            for organizm in population:
                organizm.cache_accuracy = fitness_func(organizm)
                organizm.cache_contribution = organizm.cache_accuracy - accuracy_without_target

        def genchoices():
            res = set()
            while len(res) <= self.N1:
                first = random_state.randint(0, self.N0 - 1)
                second = random_state.randint(first + 1, self.N0)
                res.add((first, second))
            return res

        for _ in range(self.min_estimators):
            _append_population()

        for generation in range(self.tmax):
            idx = 0
            print("Generation #{0}".format(generation))
            while idx < len(_populations):
                population = _populations[idx]
                fitness_func = self.get_estimator_fitness_func(
                    [o[0] for i, o in enumerate(_populations) if i != idx], y, sample_weight
                )
                # crossover
                _compute_cache_accuracy_contribution(fitness_func, population)

                choices = genchoices()
                for offspring, (first, second) in zip(_offsprings, choices):
                    offspring.crossover(population[first], population[second])
                    offspring.mutation(self.pm)

                self.compute_population_predictions(X, y, sample_weight, _estimator, _offsprings)
                _compute_cache_accuracy_contribution(fitness_func, _offsprings)
                population.sort(reverse=True, key=lambda x: x.cache_accuracy)
                a = sorted(population[: self.N2] + _offsprings, reverse=True, key=lambda x: x.cache_accuracy)
                dead = population[self.N2 :]
                _populations[idx] = a[: self.N0]
                _offsprings = dead + a[self.N0 :]
                accuracy_per_generation[generation] = max(accuracy_per_generation[generation], a[0].cache_accuracy)
                print(
                    "Estimator #{0} from {1}, accuracy {2}, contribution {3}".format(
#.........这里部分代码省略.........
开发者ID:olologin,项目名称:CCEL,代码行数:101,代码来源:CCEL.py


示例11: _spark_build_estimators

def _spark_build_estimators(n_estimators, ensemble, X, y, sample_weight,
                               seeds, verbose):
    """Private function used to build a batch of estimators within a job."""
    print "building estimators"
    # Retrieve settings
    X = X.value
    y = y.value
    ensemble = ensemble
    sample_weight = sample_weight.value

    n_samples, n_features = X.shape
    max_samples = ensemble.max_samples
    max_features = ensemble.max_features

    if (not isinstance(max_samples, (numbers.Integral, np.integer)) and
            (0.0 < max_samples <= 1.0)):
        max_samples = int(max_samples * n_samples)

    if (not isinstance(max_features, (numbers.Integral, np.integer)) and
            (0.0 < max_features <= 1.0)):
        max_features = int(max_features * n_features)

    bootstrap = ensemble.bootstrap
    bootstrap_features = ensemble.bootstrap_features
    support_sample_weight = has_fit_parameter(ensemble.base_estimator_,
                                              "sample_weight")

    # Build estimators
    estimators = []
    estimators_samples = []
    estimators_features = []

    for i in range(n_estimators):
        if verbose > 1:
            print("building estimator %d of %d" % (i + 1, n_estimators))

        random_state = check_random_state(seeds[i])
        seed = check_random_state(random_state.randint(MAX_INT))
        estimator = ensemble._make_estimator(append=False)

        try:  # Not all estimator accept a random_state
            estimator.set_params(random_state=seed)
        except ValueError:
            pass

        # Draw features
        if bootstrap_features:
            features = random_state.randint(0, n_features, max_features)
        else:
            features = sample_without_replacement(n_features,
                                                  max_features,
                                                  random_state=random_state)

        # Draw samples, using sample weights, and then fit
        if support_sample_weight:
            if sample_weight is None:
                curr_sample_weight = np.ones((n_samples,))
            else:
                curr_sample_weight = sample_weight.copy()

            if bootstrap:
                indices = random_state.randint(0, n_samples, max_samples)
                sample_counts = bincount(indices, minlength=n_samples)
                curr_sample_weight *= sample_counts

            else:
                not_indices = sample_without_replacement(
                    n_samples,
                    n_samples - max_samples,
                    random_state=random_state)

                curr_sample_weight[not_indices] = 0

            estimator.fit(X[:, features], y, sample_weight=curr_sample_weight)
            samples = curr_sample_weight > 0.

        # Draw samples, using a mask, and then fit
        else:
            if bootstrap:
                indices = random_state.randint(0, n_samples, max_samples)
            else:
                indices = sample_without_replacement(n_samples,
                                                     max_samples,
                                                     random_state=random_state)

            sample_counts = bincount(indices, minlength=n_samples)

            estimator.fit((X[indices])[:, features], y[indices])
            samples = sample_counts > 0.

        estimators.append(estimator)
        estimators_samples.append(samples)
        estimators_features.append(features)

    return estimators, estimators_samples, estimators_features
开发者ID:xiaohan2012,项目名称:mynlp,代码行数:95,代码来源:multilabel_bagging.py



注:本文中的sklearn.utils.validation.has_fit_parameter函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python sklearn_pandas.DataFrameMapper类代码示例发布时间:2022-05-27
下一篇:
Python validation.check_random_state函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap