python - OneHotEncoder 对象没有属性 get_feature_names_out

标签 python scikit-learn

我试图通过此链接了解 OneHotEncoder 和 get_dummies 之间的差异:enter link description here

当我编写完全相同的代码时,我收到一个错误,它显示

AttributeError: 'OneHotEncoder' object has no attribute 'get_feature_names_out'

这是代码:

import pandas as pd
import seaborn as sns
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

df = sns.load_dataset('tips')
df = df[['total_bill', 'tip', 'day', 'size']]

df.head(5)

X = df.drop('tip', axis=1)
y = df['tip']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

ohe = OneHotEncoder(handle_unknown='ignore', sparse=False, dtype='int')
ohe.fit(X_train[['day']])

def get_ohe(df):
    temp_df = pd.DataFrame(data=ohe.transform(df[['day']]), columns=ohe.get_feature_names_out())
    df.drop(columns=['day'], axis=1, inplace=True)
    df = pd.concat([df.reset_index(drop=True), temp_df], axis=1)
    return df

X_train = get_ohe(X_train)
X_test = get_ohe(X_test)

X_train.head()

我从 sklearn.preprocessing 模块检查了 OneHotEncoder,并且 get_feature_names_out() 方法存在并且并未弃用。我不知道为什么会收到此错误。

最佳答案

如果您使用的 scikit-learn 版本低于 1.0,则需要使用 get_feature_names方法。对于较新版本的 scikit-learn,get_feature_names_out会工作得很好。

关于python - OneHotEncoder 对象没有属性 get_feature_names_out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72274006/

相关文章:

python - 使用django上传时创建目录

python - 通过 RTI Connector 将序列/对象发布到 ROS2 应用程序

Python:如果所选列为空,则从 Pandas Dataframe 中删除行

multiple-regression - scikit_learn 回归总结

python - 在 Python 中进行测试 - 如何在使用 unittest 进行测试时使用 assertRaises?

apache-spark - 来自 spark-sklearn 的 GridSearchCV 的 best_score_ 参数不适用于版本 0.2.3

scikit-learn - sklearn 增量 Pca 大数据集

python - 来自 sklearn 的 Tfidfvectorizer - 如何获取矩阵

python - 在 scikit-learn 中查找和利用来自 PCA 的特征值和特征向量

python - 广播矩阵向量点积