Python 属性错误:模块“;sklearn.utils“;“没有属性”_joblib“;继承类'sklearn.employee.BaggingClassifier'时`

Python 属性错误:模块“;sklearn.utils“;“没有属性”_joblib“;继承类'sklearn.employee.BaggingClassifier'时`,python,machine-learning,scikit-learn,ensemble-learning,Python,Machine Learning,Scikit Learn,Ensemble Learning,我需要提取在sklearn.essemble.BaggingClassifier中训练的每个模型的概率。这样做的原因是为了估计XGBoostClassifier模型的不确定性 为此,我创建了一个继承自sklearn.employee.BaggingClassifier的扩展类,并添加了一个新方法来获得这些概率。请注意,此问题不同于 我在下面展示了迄今为止我已经实现的一段代码: 必要的模块 from sklearn.ensemble import BaggingClassifier from sk

我需要提取在
sklearn.essemble.BaggingClassifier
中训练的每个模型的概率。这样做的原因是为了估计XGBoostClassifier模型的不确定性

为此,我创建了一个继承自
sklearn.employee.BaggingClassifier
的扩展类,并添加了一个新方法来获得这些概率。请注意,此问题不同于

我在下面展示了迄今为止我已经实现的一段代码:

必要的模块

from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble.base import _partition_estimators
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted
import sklearn.utils as su
继承自
BaggingClassifier

class EBaggingClassifier(BaggingClassifier):
    """
    Extends the class BaggingClassifier fromsklearn

    """

    def __init__(self,
                 base_estimator=None,
                 n_estimators=10,
                 max_samples=1.0,
                 max_features=1.0,
                 bootstrap=True,
                 bootstrap_features=False,
                 oob_score=False,
                 warm_start=False,
                 n_jobs=1,
                 random_state=None,
                 verbose=0):

        super().__init__(
                 base_estimator,
                 n_estimators,
                 max_samples,
                 max_features,
                 bootstrap,
                 bootstrap_features,
                 oob_score,
                 warm_start,
                 n_jobs,
                 random_state,
                 verbose)
下面定义了允许计算每个估计器概率的新方法

    def predict_proball(self, X):
        """
        Computes the probability of each individual estimator

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

        Returns
        -------
        p : array of shape = [n_samples, n_classes]
            The class probabilities of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.
        """

        check_is_fitted(self, "classes_")
        # Check data
        X = check_array(
            X, accept_sparse=['csr', 'csc'], dtype=None,
            force_all_finite=False
        )

        if self.n_features_ != X.shape[1]:
            raise ValueError("Number of features of the model must "
                             "match the input. Model n_features is {0} and "
                             "input n_features is {1}."
                             "".format(self.n_features_, X.shape[1]))

        # Parallel loop
        n_jobs, n_estimators, starts = _partition_estimators(self.n_estimators,
                                                             self.n_jobs)

        all_proba = su._joblib.Parallel(n_jobs=n_jobs, verbose=self.verbose,
                             **self._parallel_args())(
            su._joblib.delayed(BaggingClassifier._parallel_predict_proba)(
                self.estimators_[starts[i]:starts[i + 1]],
                self.estimators_features_[starts[i]:starts[i + 1]],
                X,
                self.n_classes_)
            for i in range(n_jobs))

        return all_proba
我使用
XGBoostClassifier
作为基本估计器来实例化这个类:

base_estimator = XGBoostClassifier(**params)
estimator = EBaggingClassifier(base_estimator=base_estimator, max_samples=0.8, n_estimators=10)
然后使用
estimator.fit(X,y)
计算
estimator
,其中
X
y
pandas.DataFrame
对象。当我尝试运行
estimator.predict\u proball(X)
时,我得到

>>> estimator.predict_proball(X)
AttributeError: module 'sklearn.utils' has no attribute '_joblib'
有人知道为什么会这样吗?查看
BaggingClassifier
函数“sklearn.utils.\u joblib”应该可用

供参考:


问题在于您的
scikit学习版。版本
'0.19.2'
没有
\u作业库
,您可以参考。或者,您可以使用以下选项进行检查:

dir(su)
您需要更新
scikit-learn
,最新版本有
\u-joblib
,您可以参考

在版本
'0.20.2'
中可以看到以下内容:

>>> dir(su)
['Bunch', 'DataConversionWarning', 'IS_PYPY', 'Memory', 'Parallel', 'Sequence', 
'_IS_32BIT', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', 
'__loader__', '__name__', '__package__', '__path__', '__spec__', '_joblib', 
'_show_versions', 'as_float_array', 'assert_all_finite', 'axis0_safe_slice', 
'check_X_y', 'check_array', 'check_consistent_length', 'check_random_state', 
'check_symmetric', 'class_weight', 'column_or_1d', 'compute_class_weight', 
'compute_sample_weight', 'cpu_count', 'delayed', 'deprecate', 'deprecated', 
'deprecation', 'effective_n_jobs', 'fixes', 'gen_batches', 'gen_even_slices', 
'get_chunk_n_rows', 'get_config', 'hash', 'indexable', 'indices_to_mask', 
'is_scalar_nan', 'issparse', 'msg', 'murmurhash', 'murmurhash3_32', 'np', 
'numbers', 'parallel_backend', 'platform', 'register_parallel_backend', 
'resample', 'safe_indexing', 'safe_mask', 'safe_sqr', 'shuffle', 'struct', 
'tosequence', 'validation', 'warnings']
您可以按如下方式更新
scikit-learn

pip install -U scikit-learn

问题在于您的
scikit学习版。版本
'0.19.2'
没有
\u作业库
,您可以参考。或者,您可以使用以下选项进行检查:

dir(su)
您需要更新
scikit-learn
,最新版本有
\u-joblib
,您可以参考

在版本
'0.20.2'
中可以看到以下内容:

>>> dir(su)
['Bunch', 'DataConversionWarning', 'IS_PYPY', 'Memory', 'Parallel', 'Sequence', 
'_IS_32BIT', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', 
'__loader__', '__name__', '__package__', '__path__', '__spec__', '_joblib', 
'_show_versions', 'as_float_array', 'assert_all_finite', 'axis0_safe_slice', 
'check_X_y', 'check_array', 'check_consistent_length', 'check_random_state', 
'check_symmetric', 'class_weight', 'column_or_1d', 'compute_class_weight', 
'compute_sample_weight', 'cpu_count', 'delayed', 'deprecate', 'deprecated', 
'deprecation', 'effective_n_jobs', 'fixes', 'gen_batches', 'gen_even_slices', 
'get_chunk_n_rows', 'get_config', 'hash', 'indexable', 'indices_to_mask', 
'is_scalar_nan', 'issparse', 'msg', 'murmurhash', 'murmurhash3_32', 'np', 
'numbers', 'parallel_backend', 'platform', 'register_parallel_backend', 
'resample', 'safe_indexing', 'safe_mask', 'safe_sqr', 'shuffle', 'struct', 
'tosequence', 'validation', 'warnings']
您可以按如下方式更新
scikit-learn

pip install -U scikit-learn