首页 > 解决方案 > 使用 JamesSteinEncoder 时出现“is_categorical 已弃用”错误

问题描述

尝试适合 JamesSteinEncoder 时出现以下错误

encoder = JamesSteinEncoder().fit(X, y)

FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead
  elif pd.api.types.is_categorical(cols):

sklearn.__version__ :  '0.23.2'

代码:

speed = ['DE','AU','US','FR','GB','KR','AU']
lifespan = [1, 0, 1, 1, 1, 0, 1]
life = [1, 1, 0, 1, 1, 0, np.nan]
index = ['snail', 'pig', 'elephant',
         'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan,
                  'life':life}, index=index)

df['speed']= df['speed'].astype('category')

 from category_encoders import JamesSteinEncoder
    X = df['speed']
    y = df['lifespan']
    enc = JamesSteinEncoder().fit(X, y)

/Users/*/opt/anaconda3/envs/proj/lib/python3.7/site-packages/category_encoders/utils.py:21: FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead
  elif pd.api.types.is_categorical(cols):

标签: pythonmachine-learning

解决方案


python 的好处是您可以自己更改源代码。我做到了,你也可以这样做:

用 vim 打开文件,/Users/*/opt/anaconda3/envs/proj/lib/python3.7/sitepackages/category_encoders/utils.py 转到第 21 行更改

elif pd.api.types.is_categorical(cols):

elif pd.api.types.is_categorical_dtype(cols):

保存更改并退出 vim。你完成了。现在您已经为自己进行了更改,您将不会再收到此警告。


推荐阅读