簡體   English   中英

轉換數字sklearn python中的所有非數字列

[英]Convert all non numeric column in numeric sklearn python

我正在嘗試將所有非數字列轉換為數字數據類型,但它通過一個錯誤

類型錯誤:參數必須是字符串或數字

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)

你可以試試這個!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM