03-线性回归进阶

更高维度的线性回归

二维空间的线性回归,到一条直线熵

image-20191117223027199

三维空间的线性回归,到一个面上

image-20191117223056947

更高维度的线性回归,我们无法绘制和理解出来,只能是通过数学的方式计算对应的数值

实战2个参数的线性回归

汽车的加速度,排气量和油耗之间有线性的关系,请采用梯度下降分析出来

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
data = np.loadtxt("cars.csv",delimiter=',' ,usecols=(3,6,1),skiprows=1)

feature = data[:,0:2]
ones = np.ones((len(feature),1))
Feature = np.hstack((feature ,ones))
Label = data[:,-1:]
weight = np.ones((3,1))

bhistory = []
mhistory = []
msehistory = []
learningrate = 10


##关键代码
changeweight  = np.zeros((3,1))

def gradentdecent():
    global changeweight
    global weight,learningrate
    mse = np.sum(np.power((np.dot(Feature,weight)-Label),2))
    msehistory.append(mse)
    if len(msehistory)>=2:
        if(msehistory[-1]>msehistory[-2]):
            learningrate = learningrate /2
        else :
            learningrate = learningrate * 1.1

    change = np.dot(Feature.T,(np.dot(Feature,weight)-Label))
    ###关键代码
    changeweight = changeweight + change**2       
    weight = weight - learningrate* change/np.sqrt(changeweight)
    ###关键代码


for i in range(100000):
    gradentdecent()
    mhistory.append(weight[0][0])
    bhistory.append(weight[1][0])
print(weight)

使用框架sklearn 线性回归计算

1
from sklearn.linear_model import LinearRegression
1
model = LinearRegression()
1
2
feature = np.array([80,95,104,112,125,135])
label = np.array([200,230,245,274,259,262])
1
model.fit(feature.reshape(-1,1),label.reshape(-1,1))
1
model.coef_   #斜率
1
model.intercept_ #截距
1
model.predict([[80]])  #预测