在统计学中,线性回归是模拟标量响应与一个或多个解释变量(也称为因变量和自变量)之间关系的线性方法。一个解释变量的情况称为简单线性回归;对于多个变量,这个过程称为多重线性回归。 该术语不同于多变量线性回归,多变量线性回归预测多个相关因变量,而不是单个标量变量。
样子就是Y=Kx+b(就是这个样子,大佬们不要喷,我知道是矩阵格式,累了,不打那么多了)
对于线性回归而言,其主要的目的有三个
- 线性回归可用于将预测模型拟合到响应和解释变量的观测值数据集
- 对于一些标量数据而言,可以观察其是否有关联(看看关联性大不大啊,好写论文嘛有时候)
- 如果目标是解释响应变量中可归因于解释变量变化的变异,则可以使用线性回归分析来量化响应和解释变量之间关系的强度,特别是确定某些解释变量可能与响应完全没有线性关系,或确定解释变量的哪些子集可能包含有关响应的冗余信息。
目前,常用的方法有普通最小二乘,(这个就是高中学的那个东西,我忘了不知道怎么描述了。不懂得可以call高中数学老师)
最小角度回归:
最小角度回归(LARS)是一种高维数据的回归算法,由布拉德利·埃夫隆、特雷弗·哈斯蒂、伊恩·约翰斯通和罗伯特·蒂布希拉尼开发。LARS类似于正向逐步回归。在每一步,它都会发现该功能与目标最相关。当多个特征具有等效相关性时,它不会沿着同一特征继续,而是沿着特征之间的等角方向前进。(不重要这里,就是分析高纬数据的,比如博主的遥感数据中的高光谱数据的分析和时间顺序的一些挖掘就可以用)
LARS的优势在于:
在特征数量明显大于样本数量的情况下,它在数值上是有效的。
它在计算上与正向选择一样快,并且具有与普通最小二乘相同的复杂度顺序。
它产生一个完整的分段线性解路径,这在交叉验证或类似的调整模型尝试中非常有用。
如果两个特征与目标几乎等额相关,那么它们的系数应该以大约相同的速率增加。因此,该算法的行为与直觉预期的一样,并且更稳定。
它很容易被修改,为其他估计器(如套索)生成解决方案。
LARS方法的缺点包括:
由于LARS基于残留物的迭代改装,它似乎对噪音的影响特别敏感。魏斯伯格在埃夫隆等人的讨论部分详细讨论了这个问题。
废话不多说先看第一个–最小二乘法
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
fpath="/Users/longzekun/Documents/CSDN/coviddata.csv"
df=pd.read_csv(fpath,usecols=[2,36,38])
df.head()
location | people_fully_vaccinated | new_vaccinations_smoothed | |
---|---|---|---|
0 | Afghanistan | 0 | 0 |
1 | Afghanistan | 0 | 0 |
2 | Afghanistan | 0 | 0 |
3 | Afghanistan | 0 | 0 |
4 | Afghanistan | 0 | 0 |
df = df.drop_duplicates(keep="first")# only read the Initial item as for the data
df.index = range(len(df))# renew an index
print(df)
location hospital_beds_per_thousand human_development_index
0 Afghanistan 0.500 0.511
1 Africa 0.000 0.000
2 Albania 2.890 0.795
3 Algeria 1.900 0.748
4 Andorra 0.000 0.868
.. ... ... ...
225 Wallis and Futuna 0.000 0.000
226 World 2.705 0.737
227 Yemen 0.700 0.470
228 Zambia 2.000 0.584
229 Zimbabwe 1.700 0.571
[230 rows x 3 columns]
X=(df['hospital_beds_per_thousand'])
y=df['human_development_index']
plt.plot(X,y,'b.',markersize=8)
plt.axis([-1,X.max(),0.2,1])
plt.show()
x1=df.iloc[:, 1:2].values
y1=df.iloc[:, 2]
x1_train, x1_test, y1_train, y1_test = train_test_split(x1, y1, test_size = 0.33)
regressor = LinearRegression()
regressor.fit(x1_train, y1_train)
pred = regressor.predict(x1_test)
# The coefficients
print('Coefficients:', regressor.coef_)
#The interception
print('intercept:',regressor.intercept_)
#The R^2
r_sq = regressor.score(x1, y1)
print('coefficient of determination:', r_sq)
Coefficients: [0.06906763]
intercept: 0.44202562095030357
coefficient of determination: 0.2518700960850849
plt.scatter(x1_train, y1_train)
plt.plot(x1_train, regressor.predict(x1_train), color='yellow')
plt.title('Fit on training set')
plt.xlabel('X-Train')
plt.ylabel('Y-Train')
Text(0, 0.5, 'Y-Train')
plt.scatter(x1_test, y1_test, color='g')
plt.plot(x1_test, pred, color='b')
plt.title('Validation set')
plt.xlabel('X-Test')
plt.ylabel('Y-Test')
Text(0, 0.5, 'Y-Test')
test=pd.read_csv(fpath,usecols=[56,58])
test = test.drop_duplicates(keep="first")# only read the Initial item as for the data
test.index = range(len(test))
x='hospital_beds_per_thousand'
y='human_development_index'
test.plot.scatter(x,y, color='black')
plt.plot(test[x], regressor.predict(test.iloc[:,0:1].values), color='red')
plt.title('Linear Regression Ouput on Test Data Set')
plt.xlabel('X-Values')
plt.ylabel('Y-Values')
plt.show()
到这里看上去博主的数据还是可以的,但R^2不好,说明博主的这个选择数据之间关联性不是特别大。可以再找找其他的数据进行比较。