试图可视化超参数及其结果,我无法在3d绘图中绘制它们
我试图构建一个函数作为:
PlotGridSearch(grid,xparam,yparam,zlabels):
称为
gs= GridSearchCV(DecisionTreeClassifier()
,HyperParams
, scoring='accuracy'
, cv=50).fit(train_data,train_labels)
PlotGridSearch(gs
,'param_max_depth'
,'param_max_leaf_nodes'
,'mean_test_score')
但我可以将mean_testrongcore列转换为必要的矩阵(二维arrray(,并使用从param_max_depth和param_max_leap_nodes 中提取的正确标签
有什么建议吗?
关于3D曲面matplotlib绘图,有一个很好的官方文档。
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
如果您在Jupyter Notebook中运行python,则可以使用%matplotlib notebook
使其具有交互式。
在你的情况下,如果你想得到正确的X、Y和Z,你可以从gs.cv_results_
得到值,即:
m = len(param_grid['max_depth'])
n = len(param_grid['max_leaf_nodes'])
X = np.reshape(gs.cv_results_['param_max_depth'].data,[n,m]) # do mind the order of reshape, it might diff
Y = np.reshape(gs.cv_results_['param_max_leaf_nodes'].data,[n,m])
Z = np.reshape(gs.cv_results_['mean_test_score'],[n,m])