批梯度下降的 python 实现
来源:互联网 发布:淘宝便宜好吃的零食店 编辑:程序博客网 时间:2024/06/05 02:40
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 02 09:51:35 2015
@author: brian
"""
import numpy as np
if __name__ == "__main__":
x = []
y = []
fx = open('ex2x.dat','r')
for line in fx:
x.append(float(line))
fx.close()
fy = open('ex2y.dat','r')
for line in fy:
y.append(float(line))
fy.close()
m = len(y)
y = np.array(y)
y.shape = (50,1)
# Gradient descent
x = np.array([x])
x.shape = (m,1)
x = np.column_stack([np.ones([m,1]) ,x])
theta = np.zeros([2,1])
MAX_ITR = 1500
alpha = 0.07
i = 0
while i < MAX_ITR:
grad = (float(1)/m) * np.dot(x.T , ( np.dot(x , theta) - y))
theta = theta - alpha * grad
i = i+1
print theta
"""
Created on Sun Aug 02 09:51:35 2015
@author: brian
"""
import numpy as np
if __name__ == "__main__":
x = []
y = []
fx = open('ex2x.dat','r')
for line in fx:
x.append(float(line))
fx.close()
fy = open('ex2y.dat','r')
for line in fy:
y.append(float(line))
fy.close()
m = len(y)
y = np.array(y)
y.shape = (50,1)
# Gradient descent
x = np.array([x])
x.shape = (m,1)
x = np.column_stack([np.ones([m,1]) ,x])
theta = np.zeros([2,1])
MAX_ITR = 1500
alpha = 0.07
i = 0
while i < MAX_ITR:
grad = (float(1)/m) * np.dot(x.T , ( np.dot(x , theta) - y))
theta = theta - alpha * grad
i = i+1
print theta
0 0
- 批梯度下降的 python 实现
- 梯度下降的python实现
- 梯度下降算法的python实现
- 梯度下降算法的Python实现
- 梯度下降算法的python实现
- Python实现梯度下降法
- 梯度下降算法 Python实现
- 梯度下降的C#实现
- 逻辑回归-梯度下降法 python实现
- 梯度下降法及其Python实现
- 梯度下降原理及Python实现
- python实现随机梯度下降(SGD)
- python最优化-梯度下降实现
- 梯度下降算法及Python实现
- python实现随机梯度下降法
- 梯度下降原理及Python实现
- Python实现 线性回归(梯度下降)
- 梯度下降法及其Python实现
- Java构造方法
- 把vim打造成牛逼的C++ IDE
- MySQL日志表按月分区
- LeetCode之Search for a Range
- 停电。。。烦烦烦!!!
- 批梯度下降的 python 实现
- python中的while循环
- Java之集合类【ArrayList】【入门版,实例解析】
- springAOP模板
- MD5工具类 -- 文件MD5值 -- MD5加密
- 读书笔记-《用户体验设计》
- 冒泡排序--改进
- [HDU 1348] Wall 凸包周长
- VC6\VS2003 转换到VS2005 时可能会遇到的问题以及解决方法