带有10行代码的Python进行简单的线性回归

时间:2018-10-25 17:07:36

标签: linear-regression

我正在做机器学习的第一步。首先,我尝试创建一个简单的算法,例如两个变量的线性回归。因此,本手册(https://towardsdatascience.com/linear-regression-using-gradient-descent-in-10-lines-of-code-642f995339c0)是对此代码进行编码的最佳示例。当我传输此代码时,它不起作用。更正确的是,它打印出虚幻的回归参数。请帮我解决这个问题。下面的脚本。

x_1 = range(1,100)
y_1 = range(1,100)
N = float(len(y_1))
epochs=1000
m_current = b_current = 0
learning_rate=0.01
for i in range(epochs):
    for X,y in zip(x_1, y_1):
        y_current = (m_current * X) + b_current
        cost = (y-y_current)/N
        m_gradient = -(2/N)*(X * (y - y_current))
        b_gradient = -(2/N)*(y - y_current)
        m_current = m_current - (learning_rate * m_gradient)
        b_current = b_current - (learning_rate * b_gradient)
print(m_current)
print(b_current) 
print(cost)
/*print 
1.9999 i excpect 0.9999999 or 1
9.2333 i excpect 0.00000001 or 0
101.11 i excpect 0.1
*/

0 个答案:

没有答案