转换数值sklearn python中的所有非数值列



我试图将所有非数字列转换为数字数据类型,但它通过了一个错误

TypeError:参数必须是字符串或数字

for column in clean_df.columns:
if clean_df[column].dtype == np.number:
continue
clean_df[column] = LabelEncoder.fit_transform(clean_df[column])

类型错误:fit_transform((缺少1个必需的位置参数:"y">

您可以将列转换为like(.toarray(((。它返回一个具有相同形状和相同表示数据的数组。

for column in clean_df.columns:
if clean_df[column].dtype == np.number:
continue
clean_df[column] = LabelEncoder().fit_transform(clean_df[column]).toarray()
import numpy as np
X = clean_df.select_dtypes(include=[np.object])
#For loop to loop one by one with col type object
for col in X.columns:
X[col]=pd.Categorical(X[col],categories=clean_df[col].dropna().unique())
#creating dummy variable
X_col = pd.get_dummies(X[col])
X = X.drop(col,axis=1)
X_col.columns = X_col.columns.tolist()
frames = [X_col, X] 
X = pd.concat(frames,axis=1)

你可以试试这个!

最新更新