0

我正在尝试将我的数据框拆分为标签(最后一列)和特征(其余列),以便我可以通过分类器运行它。

这是我到目前为止所做的:

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_train), (X_test, y_test)

但是,在运行此函数时,我的特征 (y) 似乎返回 690 x 12,而不是预期的一维数组。有关完整的错误消息,请参阅附图。

我会很感激任何关于我在哪里出错的指示......

我是一名 Python 初学者,这是我在堆栈上的第一篇文章,如果这篇文章的结构不恰当,敬请见谅。

4

0 回答 0