[英]Memory error when using fit_transform with OneHotEncoder
我正在嘗試對我的數據集中的分類列進行熱編碼。 我正在使用以下 function:
def create_ohe(df, col):
le = LabelEncoder()
a = le.fit_transform(df_new[col]).reshape(-1,1)
ohe = OneHotEncoder(sparse=False)
column_names = [col + "_" + str(i) for i in le.classes_]
return (pd.DataFrame(ohe.fit_transform(a), columns=column_names))
在此循環中調用 function 時出現 MemoryError:
for column in categorical_columns:
temp_df = create_ohe(df_new, column)
temp = pd.concat([temp, temp_df], axis=1)
錯誤回溯:
MemoryError Traceback (most recent call last)
<ipython-input-40-9b241e8bf9e6> in <module>
1 for column in categorical_columns:
----> 2 temp_df = create_ohe(df_new, column)
3 temp = pd.concat([temp, temp_df], axis=1)
4 print("\nShape of final df after one hot encoding: ", temp.shape)
<ipython-input-34-1530423fdf06> in create_ohe(df, col)
8 ohe = OneHotEncoder(sparse=False)
9 column_names = [col + "_" + str(i) for i in le.classes_]
---> 10 return (pd.DataFrame(ohe.fit_transform(a), columns=column_names))
MemoryError:
Ah memory error means that either your computer is at the maximum use of your memory (RAM) or that python is at the maximum: Memory errors and list limits?
您可以嘗試拆分a = le.fit_transform(df_new[col]).reshape(-1,1)
方法。 嘗試運行b= le.fit(df_new[col])
以便將 label 編碼器與完整數據集相匹配,然后您可以拆分它,不要同時為每一行轉換它,也許這有幫助. 如果b= le.fit(df_new[col])
也不起作用,則您有 memory 問題,您可以將col
替換為列名。
fit_transform
是fit
和transform
的組合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.