# -*- 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();
'''

结果显示

最新文章

  1. 代码质量管理工具——SonarQube
  2. CA认证原理以及实现(上)
  3. Android开发环境配置
  4. 为什么X86汇编中的mov指令不支持内存到内存的寻址?
  5. ansible命令执行模块使用
  6. codeforces #310 div1 C
  7. C#日期时间格式化
  8. NOIP2010 机器翻译
  9. wlan0 Interface doesn't support scanning : Device or resource busy
  10. linux+Qt程序如何打包发布
  11. Logistic Regression(逻辑回归)(二)—深入理解
  12. fiddler导致页面确定按钮无法使用(测试遇到的问题经验)
  13. mvn mybatis-generator:generate postgresql
  14. hdu 5437Alisha’s Party(优先队列)
  15. one-hot编码理解
  16. React项目中那些奇怪的写法
  17. 洛谷P2845-Switching on the Lights 开关灯
  18. Chrome——F12 谷歌开发者工具详解
  19. javaScript之jQuery框架
  20. linux sumba服务器简单配置

热门文章

  1. AC日记——单词翻转 1.7 27
  2. 应用多个icon的对比
  3. PHP提升echo, printf, print, file_put_contents等输出方法的效率
  4. ACA烤箱菜单各项温度
  5. Oracle 11g Express
  6. PCL 库安装
  7. addShutdownHook的用法
  8. lecture10-模型的结合与全贝叶斯学习
  9. SQL基础之数据库快照
  10. keepalived+LVS 实现双机热备、负载均衡、失效转移 高性能 高可用 高伸缩性 服务器集群