【问题标题】:How can i solve the TypeError gotten while working with feature_engine如何解决使用 feature_engine 时遇到的 TypeError
【发布时间】:2020-08-05 00:43:23
【问题描述】:

我正在使用 feature_engine 来填充缺失值

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# from feature-engine
from feature_engine import missing_data_imputers as mdi

#Working with House Data and Feature Engine__Practice
cols_to_use = [
    'BsmtQual', 'FireplaceQu', 'LotFrontage', 'MasVnrArea', 'GarageYrBlt',
]
data = pd.read_csv(r'C:\Users\HP\Desktop\Hash\kaggle\Housing Project/train.csv', usecols=cols_to_use)

我创建了一个 mdi 实例来适应我的数据

imputer = mdi.MeanMedianImputer(imputation_method='median')
imputer.fit(data)

但是在调用转换方法时,它返回一个 TypeError,我找不到它发生的原因。

tmp = imputer.transform(data)

这是返回的错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-2f486acb96bd> in <module>
----> 1 tmp = imputer.transform(data)

~\Anaconda3\lib\site-packages\feature_engine\missing_data_imputers.py in transform(self, X)
    103     # Ugly work around to import the docstring for Sphinx, otherwise none of this is necessary
    104     def transform(self, X):
--> 105         X = super().transform(X)
    106         return X
    107 

~\Anaconda3\lib\site-packages\feature_engine\base_transformers.py in transform(self, X)
     35 
     36         # Check method fit has been called
---> 37         check_is_fitted(self)
     38 
     39         # check that input is a dataframe

TypeError: check_is_fitted() missing 1 required positional argument: 'attributes'

【问题讨论】:

  • 哪些变量类型是 ['BsmtQual', 'FireplaceQu', 'LotFrontage', 'MasVnrArea', 'GarageYrBlt'] ?通过检查文档,当您未将 variables 参数指定给 MedianMedianImputer 时,它会选择所有数字类型的变量。也许,您的变量需要转换为数字。
  • 就像你说的,feature_engine 会自动选择数值,当我使用 imputed.vatiables 检查已选择的特征时,它们都是数值特征。所以这不应该是它不起作用的原因。无论如何,谢谢。

标签: python pandas machine-learning scikit-learn feature-engineering


【解决方案1】:

通过查看您提供的堆栈跟踪,在我看来,feature_engine 与旧版本的 scikit-learn 之间不兼容。在旧版本(例如0.21)中,attributescheck_is_fitted 的强制参数,但在较新版本(例如0.23)中是可选参数:

如果None,如果存在以下划线结尾且不以双下划线开头的属性,则estimator 被认为是合适的。

【讨论】:

  • 就像你说的 Shovalt,这是一个版本错误。我卸载了 feature_engine 并再次安装并且工作正常。谢谢
  • 是的。已经解决了这种不兼容问题,错误不应再出现:)
猜你喜欢
  • 2023-01-29
  • 1970-01-01
  • 2017-12-27
  • 2012-09-18
  • 2021-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多