数据分析与机器学习之线性回归与逻辑回归(六) (8)
#梯度下降解决问题
import numpy as np
from mpl_toolkits.mplot3d.axes3d import Axes3D #导入3D包
import matplotlib.pyplot as plt
%matplotlib inline
theta0s = np.linspace(-2,2,100)
theta1s = np.linspace(-2,2,100)
COST = np.empty(shape=(100,100)) #空白填充(100,100)的数组
print(COST.shape) #(100, 100)
TOS,TIS = np.meshgrid(theta0s,theta1s)
print(TOS.shape,TIS.shape) # (100, 100) (100, 100)
#将标准归一化的数据替换新的数组并绘制
for i in range(100):
for j in range(100):
COST[i,j] = cost(TOS[0,i],TIS[j,0],pga.distance,pga.accuracy)
print(COST.shape) #(100, 100)
fig2 = plt.figure()
ax = fig2.gca(projection='3d')
ax.plot_surface(X=TOS,Y=TIS,Z=COST)
plt.show()
内容版权声明:除非注明,否则皆为本站原创文章。