【问题标题】:TypeError: check_is_fitted() missing 1 required positional argument: 'attributes'类型错误:check_is_fitted() 缺少 1 个必需的位置参数:“属性”
【发布时间】:2020-06-11 09:48:54
【问题描述】:

我正在尝试运行官方教程给出的代码。但是,我遇到了错误

TypeError: check_is_fitted() 缺少 1 个必需的位置参数:'attributes'

from sklearn.utils.estimator_checks import check_estimator
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from sklearn.utils.multiclass import unique_labels
from sklearn.metrics import euclidean_distances


class TemplateClassifier(BaseEstimator, ClassifierMixin):
    def __init__(self, demo_param='demo'):
        self.demo_param = demo_param

    def fit(self, X, y):

        # Check that X and y have correct shape
        X, y = check_X_y(X, y)
        # Store the classes seen during fit
        self.classes_ = unique_labels(y)

        self.X_ = X
        self.y_ = y
        # Return the classifier
        return self

    def predict(self, X):

        # Check is fit had been called
        check_is_fitted(self)

        # Input validation
        X = check_array(X)

        closest = np.argmin(euclidean_distances(X, self.X_), axis=1)
        return self.y_[closest]

if __name__ == '__main__':
    check_estimator(TemplateClassifier)

【问题讨论】:

  • 它看起来像文档中的错误以供参考scikit-learn.org/stable/developers/develop.html
  • 我认为你的 scikit-learn 版本没有更新。使用最新版本 0.23,代码在我的机器上运行良好。
  • 谢谢,我的版本是0.21,升级版本解决了。
  • 函数check_is_fitted()的意思是检查属性。它就像 check_is_fitted(self, ['classes_','X_'])。属性 classes_ 和 X_ 将存在并具有值的位置

标签: python scikit-learn


【解决方案1】:

将 scikit-learn 更新到 0.23 对我来说效果很好。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2021-08-05
相关资源
最近更新 更多