2017-04-08 86 views
3

我有以下代码,其中我遍历2个参数的网格,以查看哪组参数将产生最佳结果。Python可视化优化参数

from sklearn.grid_search import ParameterGrid 

ar1= np.arange(1,10,0.1) 
ar2= np.arange(0.1,3,0.01) 
param_grid = {'p1': ar1, 'p2' : ar2} 
grid = ParameterGrid(param_grid) 
result=[] 
p1=[] 
p2=[] 

for params in grid: 
    r = getresult(params['p1'], params['p2']) 
    result.append(r) 
    p1.append(params['p1']) 
    p2.append(params['p2']) 

结果我得到3个阵列,一个与每次迭代和两个阵列(P1,P2)与所述相应的参数的结果。我现在想用matplotlib绘制这些数据来可视化结果在整个参数平面上的变化情况。

我尝试以下,但我得到了一个空白的情节:

fig = plt.figure() 
ax = fig.gca(projection='3d') 
ax.plot_surface(p1, p2, result) 

理想情况下,我想能够创建类似下面的情节。我怎样才能用matplotlib完成这个任务?

enter image description here

+0

@ ImportanceOfBevenErnest我尝试了以下,但我有一个空白图:fig = plt.figure()/ ax = fig.gca(projection ='3d')/ ax.plot_surface(p1,p2,result) –

回答

1

我结束了以下解决方案去:

fig = plt.figure() 
ax = fig.gca(projection='3d') 

ax.plot_trisurf(X, Y, Z, cmap=cm.jet, linewidth=0) 
fig.tight_layout() 
plt.show() 

上面得到所需的可视化,如下所示:

enter image description here

0

plot_surface要求输入阵列是二维的。正如我解释它,你的数组是1D。因此将它们重新塑造成2D可能是一个解决方案。

import numpy as np 
shape = (len(ar2), len(ar1)) 
p1 = np.array(p1).reshape(shape) 
p2 = np.array(p2).reshape(shape) 
result = result.reshape(shape) 

然后通过

fig = plt.figure() 
ax = fig.gca(projection='3d') 
ax.plot_surface(p1, p2, result) 

可能工作绘制它。 (我不能在此刻进行测试。)