问题描述
我试图将我的数据框拆分为标签(最后一列)和特征(其余列),以便我可以通过分类器运行它。
这是我目前所做的:
def data_preprocess(df):
df3 = df.copy()
*#Convert the non-numeric data into numeric using sklearn's labelEncoder*
numeric_columns = []
object_cat_columns = []
for i,j in df3.dtypes.items():
if (j != object):
numeric_columns.append(i)
else:
object_cat_columns.append(i)
df3[numeric_columns] = df3[numeric_columns].apply(pd.to_numeric,errors='coerce')
df3[numeric_columns] = df3[numeric_columns].fillna(df3[numeric_columns].mean())
df3[object_cat_columns] = df3[object_cat_columns].replace(to_replace='?',value = np.nan)
df3[object_cat_columns] = df3[object_cat_columns].fillna(df3[object_cat_columns].mode().iloc[0])
le = preprocessing.LabelEncoder()
df3[non_numeric] = le.fit_transform(df3[non_numeric])
*Split the data into features and labels*
y = df3.iloc[:,-1:].values
X = df3[:,:-1].values
*Standardise the features using sklearn's MinMaxScaler*
scaler = preprocessing.MinMaxScaler()
X_scaled = scaler.fit_transform(X)
*Split the data into 80% training and 20% testing data*
X_train,X_test,y_train,y_test = train_test_split(X_scaled,y,test_size=0.2,random_state=42)
*Function should return two tuples of the form (X_train,y_train),(X_test,y_test)*
return (X_train,y_test)
但是,在运行此函数时,我的特征 (y) 似乎返回了 690 x 12,而不是预期的一维数组。有关完整的错误消息,请参阅所附图片。
我很感激任何关于我哪里出错的提示......
我是一名 Python 初学者,这是我在堆栈上的第一篇文章,如果这篇文章结构不合理,请见谅。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)