监督学习算法初识

监督学习算法初识

简而言之,就是通过已知数据的拟合出一条线性线,来预测数据的变化
随机梯度下降法(Stochastic Gradient Descent,SGD):
随机梯度下降法不同于批量梯度下降,随机梯度下降是每次迭代使用一个样本来对参数进行更新。
监督学习算法初识_第1张图片通过对参数的不断拟合,从而找出最优的解。

import numpy as np
import matplotlib.pyplot as plt

x1 = np.array([2104, 1600, 2400, 1416, 3000])
x2 = np.array([3, 3, 3, 2, 4])
# x = np.array([[2104, 3], [1600, 3], [2400, 3], [1416, 2], [3000, 4]])
t = np.array([400, 330, 369, 232, 540])

alpha = 0.3
m0 = np.random.random()
m1 = np.random.random()
m2 = np.random.random()
# m = np.array([m1, m2])

eps = 0.00001
e0 = 7
e1 = 7
e2 = 7
i = 0

while e0 >= eps or e1 >= eps or e2 >= eps:

    e0 = np.sum(m1*x1[i] + m2 * x2[i] + m0 - t[i])
    e1 = e0 * np.sum(x1[i])
    e2 = e0 * np.sum(x2[i])
    m0 = m0 - alpha * e0
    m1 = m1 - alpha * e1
    m2 = m2 - alpha * e2
    # plt.title("Matplotlib demo")
    # plt.xlabel("m1 axis caption")
    # plt.ylabel("t axis caption")
    # plt.plot(m1, "ob")

print(m0, m1, m2)
# plt.show()

输出结果:

78.38201053871096 163447.40804949705 233.93818994118612

你可能感兴趣的:(学习学习,算法,机器学习)