【发布时间】:2019-10-22 08:56:38
【问题描述】:
我正在关注这里的教程: https://blog.hyperiondev.com/index.php/2019/02/18/machine-learning/
我有作者使用的完全相同的代码,但我仍然会在下面分享它......
train_data = scipy.io.loadmat('train_32x32.mat')
X = train_data['X']
y = train_data['y']
img_index = 24
X = X.reshape(X.shape[0]*X.shape[1]*X.shape[2],X.shape[3]).T
y = y.reshape(y.shape[0],)
X, y = shuffle(X, y, random_state=42)
clf = RandomForestClassifier(n_estimators=10, n_jobs=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf.fit(X_train, y_train) <-----------(MEMORY ERROR)
preds = clf.predict(X_test)
print("Accuracy:", accuracy_score(y_test,preds))
我使用的数据集基本上是一个数字字典和数字图片。每次我到达我上面指出的那一行时,我都会收到一个MemoryError。完整的错误回溯如下:
Traceback (most recent call last):
File "C:/Users/jack.walsh/Projects/img_recog/main.py", line 22, in <module>
clf.fit(X_train, y_train)
File "C:\Users\jack.walsh\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\ensemble\forest.py", line 249, in fit
X = check_array(X, accept_sparse="csc", dtype=DTYPE)
File "C:\Users\jack.walsh\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\validation.py", line 496, in check_array
array = np.asarray(array, dtype=dtype, order=order)
File "C:\Users\jack.walsh\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
return array(a, dtype, copy=False, order=order)
MemoryError
我与它并排运行资源监视器,并意识到我的已用内存从未超过 30%。告诉我如何在不改变结果的情况下解决这个问题!
X.shape = (73257, 3072)
X_train.shape = (51279, 3072)
我在这台机器上有 16GB RAM。
【问题讨论】:
-
X的大小/形状是多少? -
X.shape = (73257, 3072) X_train.shape = (51279, 3072)
-
你也可以在补丁中进行增量训练。 stackoverflow.com/questions/44060432/…
标签: python machine-learning scikit-learn scipy random-forest