ML Practice1 Linear Regression

梯度下降法

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

filepath='/Users/husir/Desktop/ex1data1.txt'
datafile=open(filepath, 'r')
data=datafile.readlines()
#print(*data)
#print(len(data))

population=[float(x.split(',')[0]) for x in data]
profit=[float(x.split(',')[1]) for x in data]

plt.plot(population,profit,'rx')
plt.xlabel('Population of City in 10,000s');
plt.ylabel('Profit in $10,000s');
1.png
x=np.ones((97,1))
y=np.ones((97,1))
for i in range(97):
    x[i][0]=population[i]
    y[i][0]=profit[i]
x0=np.ones((97,1))
x1=np.hstack((x0,x))  # 把矩阵按行合并,即增加列
#print(x)

theta=[[1],[1]]
#print(theta)
iterations=1500 # 迭代次数
alpha=0.01 # 学习速率
for i in range(iterations):
    p=np.dot(x1,theta)-y
    q=p*x
    der0=sum(p)/97
    der1=sum(q)/97
    theta[0]-=alpha*der0
    theta[1]-=alpha*der1

plt.plot(population,profit,'rx')
plt.xlabel('Population of City in 10,000s');
plt.ylabel('Profit in $10,000s');
a=np.linspace(min(population),max(population),5)
predicty=theta[0]+theta[1]*a
plt.plot(a,predicty)
2.png

梯度下降法需要注意的一个点是,所有的theta参数都应同步更新

晚安!明天必7:30起!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容