批梯度下降:指的是每下降一步,使用所有的训练集来计算梯度值。
单变量
# -*- coding:utf-8 -*-
# @Time:4/18/19 9:08 PM
# @Author:CIGA
# @Filename:02_batch_gradient_descent.py
# @Software:PyCharm
"""
批梯度下降batch_gradient_descent
批梯度下降:指的是每下降一步,使用所有的训练集来计算梯度值
单变量
"""
import numpy as np
a = np.random.rand(100, 1)
X = 2 * a
y = 4 + 3 * X + a
"""
a = np.array([[1, 2, 3],[7,8,9]])
b=np.array([[4,5,6],[1,2,3]])
a
Out[4]:
array([[1, 2, 3],
[7, 8, 9]])
b
Out[5]:
array([[4, 5, 6],
[1, 2, 3]])
c=np.c_[a,b]
c
Out[7]:
array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 1, 2, 3]])
"""
"""
>>> np.ones((2, 1))
array([[ 1.],
[ 1.]])
"""
X_b = np.c_[np.ones((100, 1)), X]
# 学习率
learning_rate = 0.1
# 迭代次数
n_iterations = 1000
# 样本个数
m = 100
# 1.初始化theta,w0...wn
"""
import numpy as np
a=np.random.randn(2,1)
a
array([[0.68050012],
[0.75656742]])
"""
theta = np.random.randn(2, 1)
count = 0
for iteration in range(n_iterations):
count += 1
# 2.求梯度
gradients = 1 / m * X_b.T.dot(X_b.dot(theta) - y)
# 3.应用公式调整theta值,theta_t + 1 = theta_t - gradients * learning_rate
theta = theta - learning_rate * gradients
print(theta)
多变量
# -*- coding:utf-8 -*-
# @Time:4/18/19 9:32 PM
# @Author:CIGA
# @Filename:02_batch_gradient_descent_mul.py
# @Software:PyCharm
"""
多变量
"""
import numpy as np
X1 = 2 * np.random.rand(100, 1)
X2 = 4 * np.random.rand(100, 1)
X3 = 6 * np.random.rand(100, 1)
y = 4 + 3 * X1 + 4 * X2 + 5 * X3 + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X1, X2, X3]
# print(X_b)
learning_rate = 0.1
# 通常在做机器学习的时候,一般不会等到他收敛,因为太浪费时间,所以会设置一个收敛次数
n_iterations = 1000
m = 100
# 1.初始化theta, w0...wn
theta = np.random.randn(4, 1)
count = 0
# 4.不会设置阈值,之间设置超参数,迭代次数,迭代次数到了,我们就认为收敛了
for iteration in range(n_iterations):
count += 1
# 2. 接着求梯度gradient
gradients = 1 / m * X_b.T.dot(X_b.dot(theta) - y)
# 3. 应用公式调整theta值, theta_t + 1 = theta_t - gradients * learning_rate
theta = theta - learning_rate * gradients
print(theta)