在计算时充当常数项
data.insert(0,'Ones',1)
#初始化X,y
cols=data.shape[1]#shape表示行列数,[0]行,[1]列
X=data.iloc[:,:-1]# 数据为二维数组...print(c)
#梯度下降算法
def gD(X,y,theta,alpha=0.01,iters=1000):
temp=np.array(np.zeros(theta.shape))#初始化参数矩阵...权重与迭代一万次的一万个损失值
final_theta,cost=gD(Xnp,ynp,theta)
final_cost=computeCost(Xnp,ynp,final_theta)#算出的cost...跟第一万次的cost一样
population=np.linspace(data.Population.min(),data.Population.max(),97)#人口数的一维数组,从小到大排列
profit...Population Size')
plt.show()
32.072733877455676
算法:基于梯度下降算法的线性回归是使用梯度下降算法进行收敛得到的最佳拟合参数,画出线性拟合的直线,数据集的点零散分布在平面内