簡體   English   中英

在擬合scikit-learn決策樹和隨機森林分類器時的MemoryError

[英]MemoryError when fitting scikit-learn Decision Tree and Random Forest Classifiers

我有一個擁有86k行,5個功能和1個目標列的pandas DataFrame。 我正在嘗試使用70%的DataFrame訓練DecisionTreeClassifier作為訓練數據,我從fit方法得到一個MemoryError。 我已經嘗試更改一些參數,但我真的不知道是什么導致錯誤,所以我不知道如何處理它。 我在Windows 10上有8GB的RAM。

train, test = train_test_split(data, test_size = 0.3)
X_train = train.iloc[:, 1:-1] # first column is not a feature
y_train = train.iloc[:, -1]
X_test = test.iloc[:, 1:-1]
y_test = test.iloc[:, -1]

DT = DecisionTreeClassifier()
DT.fit(X_train, y_train)
dt_predictions = DT.predict(X_test)

錯誤

File (...), line 97, in <module>
DT.fit(X_train, y_train)
File "(...)\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\tree\tree.py", line 790, in fit
X_idx_sorted=X_idx_sorted)
File "(...)\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\tree\tree.py", line 362, in fit
builder.build(self.tree_, X, y, sample_weight, X_idx_sorted)
File "sklearn\trewe\_tree.pyx", line 145, in sklearn.tree._tree.DepthFirstTreeBuilder.build
File "sklearn\tree\_tree.pyx", line 244, in sklearn.tree._tree.DepthFirstTreeBuilder.build
File "sklearn\tree\_tree.pyx", line 735, in sklearn.tree._tree.Tree._add_node
File "sklearn\tree\_tree.pyx", line 707, in sklearn.tree._tree.Tree._resize_c
File "sklearn\tree\_utils.pyx", line 39, in sklearn.tree._utils.safe_realloc
MemoryError: could not allocate 671612928 bytes

當我嘗試使用RandomForestClassifier時,會發生同樣的錯誤,總是在進行擬合的行中。 我怎么解決這個問題?

我一直在遇到同樣的問題。 確保您處理的是分類問題,而不是回歸問題。 如果目標列是連續的,則可能需要使用http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html而不是RandomForestClassifier。

暫無
暫無

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

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