gpt4 book ai didi

python - 值错误 : bad input shape (2835, 18)

转载 作者:行者123 更新时间:2023-12-01 06:41:07 26 4
gpt4 key购买 nike

我是数据科学新手,我想根据分类数据进行分类。我希望在使用 K 均值算法之前执行此操作,但当我使用 fit_transform() 时,我收到此“错误 ValueError:错误的输入形状 (2835, 18)”,并且我不知道如何修复它。我希望有人能帮助我。

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

#load my data
myData = pd.read_excel('panelForOneHot.xlsx')
myData = myData.dropna()
myData.reset_index(drop = True, inplace = True)
myData

values = np.array(myData)
print(values)

#integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)

最佳答案

LabelEncoder() 需要一维数据。传递要编码的特定字段,如下所示。

# Import label encoder 
from sklearn import preprocessing

# label_encoder object knows how to understand word labels.
label_encoder = preprocessing.LabelEncoder()

# Encode labels in column 'species'.
df['species']= label_encoder.fit_transform(df['species'])

df['species'].unique()

如果您打算对所有列进行编码,

df.apply(LabelEncoder().fit_transform)

如果您打算对多列但不是全部进行编码,

from sklearn.compose import make_column_transformer
from sklearn.preprocessing import RobustScaler
from sklearn.preprocessing import OneHotEncoder

categorical_columns = ['country', 'gender']
numerical_columns = ['age']
column_trans = make_column_transformer(
(categorical_columns, OneHotEncoder(handle_unknown='ignore'),
(numerical_columns, RobustScaler())
column_trans.fit_transform(df)

关于python - 值错误 : bad input shape (2835, 18),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com