无法重塑数组 - 绘制 GridSearchCV
Cannot reshape array - plot GridSearchCV
代码很简单,有几个与此相关的问题,但我对 python 的了解几乎为零,所以我不知道这是如何工作的。我正在尝试绘制我的 GridSearchCV 结果。
阅读文档没有帮助:https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
clf = GridSearchCV(estimator=svm.SVC(),
param_grid={'C': [1, 10], 'gamma': [0.001, 0.0001], 'kernel': ('linear', 'rbf')}, cv=10, n_jobs=None)
clf.fit(X_train, Y_train)
scores = [x[1] for x in clf.cv_results_]
print np.array(scores).shape # outputs: (33L,)
scores = np.array(scores).reshape(len([1, 10]), len([0.001, 0.0001]))
for ind, i in enumerate([1, 10]):
plt.plot([0.001, 0.0001], scores[ind])
plt.legend()
plt.xlabel('Gamma')
plt.ylabel('Mean score')
plt.show()
输出错误:
scores = np.array(scores).reshape(len([1, 10]), len([0.001, 0.0001]))
ValueError: cannot reshape array of size 33 into shape (2,2)
为什么会发生这种情况,我该如何解决?
想通了。首先,为了绘制我的 GridSearchCV,我需要访问 clf.cv_results
数组中的 mean_test_score
字段。
这使得要打印的数据大小为8,提示如下错误:
scores = np.array(scores).reshape(len([1, 10]), len([0.001, 0.0001]))
ValueError: cannot reshape array of size 8 into shape (2,2)
稍微修改一下代码后,它看起来应该可以正常工作:
scores = clf.cv_results_['mean_test_score'].reshape(len([1, 10]), len([0.001, 0.0006, 0.0003, 0.0001]))
for ind, i in enumerate([1, 10]):
plt.plot([0.001, 0.0006, 0.0003, 0.0001], scores[ind])
plt.legend()
plt.xlabel('Gamma')
plt.ylabel('Mean score')
plt.show()
这使得 8 长度的数据被绘制在一个 2x4 矩阵中,具有 8 个数据的容量,这是我过去犯的错误 2*4 != 33(有点愚蠢的问题,但是我缺乏关于它如何工作的基本知识。
代码很简单,有几个与此相关的问题,但我对 python 的了解几乎为零,所以我不知道这是如何工作的。我正在尝试绘制我的 GridSearchCV 结果。 阅读文档没有帮助:https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
clf = GridSearchCV(estimator=svm.SVC(),
param_grid={'C': [1, 10], 'gamma': [0.001, 0.0001], 'kernel': ('linear', 'rbf')}, cv=10, n_jobs=None)
clf.fit(X_train, Y_train)
scores = [x[1] for x in clf.cv_results_]
print np.array(scores).shape # outputs: (33L,)
scores = np.array(scores).reshape(len([1, 10]), len([0.001, 0.0001]))
for ind, i in enumerate([1, 10]):
plt.plot([0.001, 0.0001], scores[ind])
plt.legend()
plt.xlabel('Gamma')
plt.ylabel('Mean score')
plt.show()
输出错误:
scores = np.array(scores).reshape(len([1, 10]), len([0.001, 0.0001]))
ValueError: cannot reshape array of size 33 into shape (2,2)
为什么会发生这种情况,我该如何解决?
想通了。首先,为了绘制我的 GridSearchCV,我需要访问 clf.cv_results
数组中的 mean_test_score
字段。
这使得要打印的数据大小为8,提示如下错误:
scores = np.array(scores).reshape(len([1, 10]), len([0.001, 0.0001]))
ValueError: cannot reshape array of size 8 into shape (2,2)
稍微修改一下代码后,它看起来应该可以正常工作:
scores = clf.cv_results_['mean_test_score'].reshape(len([1, 10]), len([0.001, 0.0006, 0.0003, 0.0001]))
for ind, i in enumerate([1, 10]):
plt.plot([0.001, 0.0006, 0.0003, 0.0001], scores[ind])
plt.legend()
plt.xlabel('Gamma')
plt.ylabel('Mean score')
plt.show()
这使得 8 长度的数据被绘制在一个 2x4 矩阵中,具有 8 个数据的容量,这是我过去犯的错误 2*4 != 33(有点愚蠢的问题,但是我缺乏关于它如何工作的基本知识。