线性回归&&code
2024-09-28 17:25:53
# -*- coding: utf-8 -*- import numpy as np
import matplotlib.pyplot as plt
from certifi import __main__ def cost(x,y,theta=np.zeros((2,1))):
m=len(y);
J=1.0/(2*m)*sum((x.dot(theta).flatten()-y)**2);
return J; def gradientDesc(x,y,theat=np.zeros((2,1)),alpha=0.001,iterations=1500):
m=len(y)
J=[]
for i in xrange(iterations):
a=theat[0][0]-alpha*(1.0/m)*sum((x.dot(theat).flatten()-y)*x[:,0]);
b=theat[1][0]-alpha*(1.0/m)*sum((x.dot(theat).flatten()-y)*1);
theat[0][0],theat[1][0]=a,b
print theat[0][0], theat[1][0]
print cost(x, y, theat); return theat; if __name__=="__main__":
x=np.array([[9,1],[15,1],[25,1],[14,1],[10,1],[18,1]]);
y=np.array([39,56,93,61,50,75]);
ans=gradientDesc(x, y);
xx=[1,30]
yy=[ans[0][0]*1+ans[1][0],ans[0][0]*30+ans[1][0]]
plt.plot(xx,yy)
plt.scatter(x[:,0],y)
plt.show() print 'end' #显示数据
'''
plt.scatter(x,y);
plt.show();
'''
结果显示
最新文章
- 代码质量管理工具——SonarQube
- CA认证原理以及实现(上)
- Android开发环境配置
- 为什么X86汇编中的mov指令不支持内存到内存的寻址?
- ansible命令执行模块使用
- codeforces #310 div1 C
- C#日期时间格式化
- NOIP2010 机器翻译
- wlan0 Interface doesn't support scanning : Device or resource busy
- linux+Qt程序如何打包发布
- Logistic Regression(逻辑回归)(二)—深入理解
- fiddler导致页面确定按钮无法使用(测试遇到的问题经验)
- mvn mybatis-generator:generate postgresql
- hdu 5437Alisha’s Party(优先队列)
- one-hot编码理解
- React项目中那些奇怪的写法
- 洛谷P2845-Switching on the Lights 开关灯
- Chrome——F12 谷歌开发者工具详解
- javaScript之jQuery框架
- linux sumba服务器简单配置