我在youtube上遇到了andrew Ng的课程并观看了以下video(2分03)
我尝试实现以下函数,以便在他的幻灯片中显示之后绘制它,但似乎我只得到一个斜率。此外,我试图将theta0和theta1绘制成JList作为网格图我不断得到一个错误的情节,任何有关如何获得与他的视频相同的情节的帮助是受欢迎的
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from fractions import Fraction
from matplotlib import cm
theta0=[]
theta1=[]
JList=[]
csv=np.genfromtxt('ex1.data', delimiter=",")
x=csv[:,0]
y=csv[:,1]
for a in np.arange(-50,50,1):
for b in np.arange(1,10,1):
theta0.append(a)
theta1.append(b)
result=0
for c in range(len(x)):
sum=float(a+(b*x[c])-(y[c]))
np.power(sum,2)
result+=float(sum)
Jt=0
Jt=Fraction(1,2*len(x))
Jt=Jt*result
JList.append(int(Jt))
fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y = np.meshgrid(theta1, theta0)
surf = ax.plot_surface(X, Y, JList, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
plt.show()
这是ex1.data
的示例6.1101,17.592
5.5277,9.1302
8.5186,13.662
7.0032,11.854
5.8598,6.8233
8.3829,11.886
7.4764,4.3483
8.5781,12
6.4862,6.5987
5.0546,3.8166
5.7107,3.2522
14.164,15.505
5.734,3.1551
8.4084,7.2258
5.6407,0.71618
5.3794,3.5129
6.3654,5.3048
5.1301,0.56077
6.4296,3.6518
7.0708,5.3893
6.1891,3.1386
20.27,21.767
5.4901,4.263
6.3261,5.1875
5.5649,3.0825
18.945,22.638
12.828,13.501
10.957,7.0467
13.176,14.692
22.203,24.147
5.2524,-1.22
6.5894,5.9966
9.2482,12.134
5.8918,1.8495
8.2111,6.5426
7.9334,4.5623
8.0959,4.1164
5.6063,3.3928
12.836,10.117
6.3534,5.4974
5.4069,0.55657
答案 0 :(得分:0)
它是线性的原因是由于我的错误np.power(sum,2)
应在sum=np.power(sum,2)
中修改。