使用Python循环和Matplotlib绘制轮廓图

0 投票
1 回答
1969 浏览
提问于 2025-04-18 21:50

我搞不清楚是什么原因导致我无法得到这个成本函数的轮廓图。经过多次尝试,我得到了:

ValueError: zero-size array to reduction operation minimum which has no identity

如果我打印出J,它并没有给我任何值,只是一个满是nan的100x100数组。这是问题所在吗?J应该是充满成本值的,对吧?非常感谢任何帮助。

X,y,ComputeCost = defined earlier and 90% sure not the problem
theta_zero = np.linspace(-10,10,100)
theta_one = np.linspace(-1,4,100)
L,Q = np.meshgrid(theta_zero,theta_one)
J = np.zeros((len(theta_zero),len(theta_one)))

for i in range(0,len(theta_zero)):
    for j in range(0,len(theta_one)):
        t = DataFrame([theta_zero[i],theta_one[j]])
        J[i,j] = ComputeCost(X,y,t)

plt.contour(L,Q,J)

1 个回答

1

如果 J 里面全是 nan,那么问题出在你生成 J 的方法上,而不是在调用 contour() 的时候。

撰写回答