简体   繁体   English

线性回归模型

[英]Linear regression model

Following is my implementation of linear regression using SGD but the line obtained is not the best fit.How can i improve this? 以下是我使用SGD实现的线性回归,但获得的线不是最合适的。我怎样才能改善这一点? 在此输入图像描述

import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np

style.use("fivethirtyeight")

x=[[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]
y=[[3],[5],[9],[9],[11],[13],[16],[17],[19],[21]]

X=np.array(x)
Y=np.array(y)

learning_rate=0.015


m=1
c=2
gues=[]

for i in range(len(x)):

    guess=m*x[i][0]+c
    error=guess-y[i][0]


    if error<0:

        m=m+abs(error)*x[i][0]*learning_rate
        c=c+abs(error)*learning_rate

    if error>0:

        m=m-abs(error)*x[i][0]*learning_rate
        c=c-abs(error)*learning_rate
    gues.append([guess])
t=np.array(gues)



plt.scatter(X,Y)
plt.plot(X,t)
plt.show()


from sklearn.linear_model import LinearRegression
var=LinearRegression()
var.fit(X,Y)
plt.scatter(X,Y)
plt.plot(X,var.predict(X))
plt.show()

Since I have to minimize error which is (guess-y) on taking partial derivative of error function wrt to m gives x and wrt c gives a constant. 因为我必须最小化误差,即误差函数wrt的偏导数得到的误差为m得到x和wrt c给出一个常数。

You're doing stochastic gradient descent, evaluating the fit at every data point. 您正在进行随机梯度下降,评估每个数据点的拟合度。 So the final m and c give you the parameters of the fitted relationship. 所以最后的mc给出了拟合关系的参数。 The line you're plotting is the 'evolution' of the fitted line. 您正在绘制的线是拟合线的“演变”。

Here's how I plotted it, with some other tweaks to your code as I figured out what you're doing: 以下是我绘制它的方式,以及对代码的一些其他调整,因为我弄清楚你在做什么:

import numpy as np
import matplotlib.pyplot as plt

X = np.array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
Y = np.array([ 3,  5,  9,  9, 11, 13, 16, 17, 19, 21])

learning_rate = 0.015
m = 1
c = 2

gues = []
for xi, yi in zip(X, Y):

    guess = m * xi + c
    error = guess - yi

    m = m - error * xi * learning_rate
    c = c - error * learning_rate

    gues.append(guess)

t = np.array(gues)

# Plot the modeled line.
y_hat = m * X + c
plt.figure(figsize=(10,5))
plt.plot(X, y_hat, c='red')

# Plot the data.
plt.scatter(X, Y)

# Plot the evolution of guesses.
plt.plot(X, t)
plt.show()

在此输入图像描述

The main mods I made in the code are: stepping over zipped X and Y so you can use then without indexing into them. 我在代码中做的主要修改是:踩过拉链XY这样你就可以使用它而不用索引。 I also made them 1D arrays for simplicity. 为简单起见,我也为它们制作了一维数组。 And if you use the gradient directly, without abs , you don't need different paths for +ve and -ve cases. 如果直接使用渐变,没有abs ,则不需要+ ve和-ve情况的不同路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM