要探索 两个变量之间的关系是否是线性的,最简单的方式就是绘制散点图,如果散点图能够相对均匀地分布在一条直线的两 端,则说明这两个变量之间的关系是线性的。
线性数据
一组数据由多个特征和标签组成。当这些 特征分别与标签存在线性关系的时候,我们就说这一组数据是线性数据。当特征矩阵中任意一个特征与标签之间的关 系需要使用三角函数,指数函数等函数来定义,则我们就说这种数据叫做“非线性数据”
线性回归在非线性数据上的表现
建立一个明显是非线性的数据集,并观察线性回归和决策树的而回 归在拟合非线性数据集时的表现:
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.linear_model import LinearRegression
- from sklearn.tree import DecisionTreeRegressor
-
- #创建需要拟合的数据集
- rnd = np.random.RandomState(42) #设置随机数种子
- X = rnd.uniform(-3, 3, size=100) #random.uniform,从输入的任意两个整数中取出size个随机数
- #生成y的思路:先使用NumPy中的函数生成一个sin函数图像,然后再人为添加噪音
- y = np.sin(X) + rnd.normal(size=len(X)) / 3 #random.normal,生成size个服从正态分布的随机数
- #使用散点图观察建立的数据集是什么样子
- plt.scatter(X, y,marker='o',c='k',s=20)
- plt.show()
- #为后续建模做准备:sklearn只接受二维以上数组作为特征矩阵的输入
- X.shape
- X = X.reshape(-1, 1)
-
- #使用原始数据进行建模
- #使用原始数据进行建模
- LinearR = LinearRegression().fit(X, y)
- TreeR = DecisionTreeRegressor(random_state=0).fit(X, y)
- #放置画布
- fig, ax1 = plt.subplots(1)
- #创建测试数据:一系列分布在横坐标上的点
- line = np.linspace(-3, 3, 1000, endpoint=False).reshape(-1, 1)
- #将测试数据带入predict接口,获得模型的拟合效果并进行绘制
- ax1.plot(line, LinearR.predict(line), linewidth=2, color='green',
- label="linear regression")
- ax1.plot(line, TreeR.predict(line), linewidth=2, color='red',
- label="decision tree")
- #将原数据上的拟合绘制在图像上
- ax1.plot(X[:, 0], y, 'o', c='k')
- #其他图形选项
- ax1.legend(loc="best")
- ax1.set_ylabel("Regression output")
- ax1.set_xlabel("Input feature")
- ax1.set_title("Result before discretization")
- plt.tight_layout()
- plt.show()
- #从这个图像来看,可以得出什么结果?
线性回归无法拟合出这条带噪音的正弦曲线的真实面貌,只能够模拟出大概的趋势,而决策树却 通过建立复杂的模型将几乎每个点都拟合出来了。可见,使用线性回归模型来拟合非线性数据的效果并不好,而决策 树这样的模型却拟合得太细致,但是相比之下,还是决策树的拟合效果更好一些。
线性模型可以用来拟合非线性数据,而非线性模型也可以用来拟合线性数据,更 神奇的是,有的算法没有模型也可以处理各类数据,而有的模型可以既可以是线性,也可以是非线性模型!
如果我们使用随机森林来拟合一条直线,那随机森林毫无疑问会过拟合,因为线性数 据对于非线性模型来说太过简单,很容易就把训练集上的 训练得很高,MSE训练的很低
线性模型若用来拟合非线性数据或者对非线性可分的数据进行分类,那通常都会表现糟糕。通常如果我 们已经发现数据属于非线性数据,或者数据非线性可分的数据,则我们不会选择使用线性模型来进行建模
于有一些模型来说,他们既可以处理线性模型又可以处理非线性模型,比如说强大的支持向量机。
总结:
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.linear_model import LinearRegression
- from sklearn.tree import DecisionTreeRegressor
-
- #创建需要拟合的数据集
- rnd = np.random.RandomState(42) #设置随机数种子
- X = rnd.uniform(-3, 3, size=100) #random.uniform,从输入的任意两个整数中取出size个随机数
- #生成y的思路:先使用NumPy中的函数生成一个sin函数图像,然后再人为添加噪音
- y = np.sin(X) + rnd.normal(size=len(X)) / 3 #random.normal,生成size个服从正态分布的随机数
- #使用散点图观察建立的数据集是什么样子
- plt.scatter(X, y,marker='o',c='k',s=20)
- plt.show()
- #为后续建模做准备:sklearn只接受二维以上数组作为特征矩阵的输入
- X.shape
- X = X.reshape(-1, 1)
-
- #使用原始数据进行建模
- #使用原始数据进行建模
- LinearR = LinearRegression().fit(X, y)
- TreeR = DecisionTreeRegressor(random_state=0).fit(X, y)
- #放置画布
- fig, ax1 = plt.subplots(1)
- #创建测试数据:一系列分布在横坐标上的点
- line = np.linspace(-3, 3, 1000, endpoint=False).reshape(-1, 1)
- #将测试数据带入predict接口,获得模型的拟合效果并进行绘制
- ax1.plot(line, LinearR.predict(line), linewidth=2, color='green',
- label="linear regression")
- ax1.plot(line, TreeR.predict(line), linewidth=2, color='red',
- label="decision tree")
- #将原数据上的拟合绘制在图像上
- ax1.plot(X[:, 0], y, 'o', c='k')
- #其他图形选项
- ax1.legend(loc="best")
- ax1.set_ylabel("Regression output")
- ax1.set_xlabel("Input feature")
- ax1.set_title("Result before discretization")
- plt.tight_layout()
- plt.show()
- from sklearn.preprocessing import KBinsDiscretizer
- #将数据分箱
- enc = KBinsDiscretizer(n_bins=10 #分几类?
- ,encode="onehot") #ordinal
- X_binned = enc.fit_transform(X)
- #encode模式"onehot":使用做哑变量方式做离散化
- #之后返回一个稀疏矩阵(m,n_bins),每一列是一个分好的类别
- #对每一个样本而言,它包含的分类(箱子)中它表示为1,其余分类中它表示为0
- X.shape
- X_binned
- #使用pandas打开稀疏矩阵
- import pandas as pd
- pd.DataFrame(X_binned.toarray()).head()
- #我们将使用分箱后的数据来训练模型,在sklearn中,测试集和训练集的结构必须保持一致,否则报错
- LinearR_ = LinearRegression().fit(X_binned, y)
- LinearR_.predict(line) #line作为测试集
- line.shape #测试
- X_binned.shape #训练
- #因此我们需要创建分箱后的测试集:按照已经建好的分箱模型将line分箱
- line_binned = enc.transform(line)
- line_binned.shape #分箱后的数据是无法进行绘图的
- line_binned
- LinearR_.predict(line_binned).shape
使用分箱数据进行建模和绘图
- #准备数据
- enc = KBinsDiscretizer(n_bins=10,encode="onehot")
- X_binned = enc.fit_transform(X)
- line_binned = enc.transform(line)
- #将两张图像绘制在一起,布置画布
- fig, (ax1, ax2) = plt.subplots(ncols=2
- , sharey=True #让两张图共享y轴上的刻度
- , figsize=(10, 4))
- #在图1中布置在原始数据上建模的结果
- ax1.plot(line, LinearR.predict(line), linewidth=2, color='green',
- label="linear regression")
- ax1.plot(line, TreeR.predict(line), linewidth=2, color='red',
- label="decision tree")
- ax1.plot(X[:, 0], y, 'o', c='k')
- ax1.legend(loc="best")
- ax1.set_ylabel("Regression output")
- ax1.set_xlabel("Input feature")
- ax1.set_title("Result before discretization")
- #使用分箱数据进行建模
- LinearR_ = LinearRegression().fit(X_binned, y)
- TreeR_ = DecisionTreeRegressor(random_state=0).fit(X_binned, y)
- #进行预测,在图2中布置在分箱数据上进行预测的结果
- ax2.plot(line #横坐标
- , LinearR_.predict(line_binned) #分箱后的特征矩阵的结果
- , linewidth=2
- , color='green'
- , linestyle='-'
- , label='linear regression')
- ax2.plot(line, TreeR_.predict(line_binned), linewidth=2, color='red',
- linestyle=':', label='decision tree')
- #绘制和箱宽一致的竖线
- ax2.vlines(enc.bin_edges_[0] #x轴
- , *plt.gca().get_ylim() #y轴的上限和下限
- , linewidth=1
- , alpha=.2)
- #将原始数据分布放置在图像上
- ax2.plot(X[:, 0], y, 'o', c='k')
- #其他绘图设定
- ax2.legend(loc="best")
- ax2.set_xlabel("Input feature")
- ax2.set_title("Result after discretization")
- plt.tight_layout()
- plt.show()
箱子数如何影响模型的结果
- enc = KBinsDiscretizer(n_bins=5,encode="onehot")
- X_binned = enc.fit_transform(X)
- line_binned = enc.transform(line)
- fig, ax2 = plt.subplots(1,figsize=(5,4))
- LinearR_ = LinearRegression().fit(X_binned, y)
- print(LinearR_.score(line_binned,np.sin(line)))
- TreeR_ = DecisionTreeRegressor(random_state=0).fit(X_binned, y)
- ax2.plot(line #横坐标
- , LinearR_.predict(line_binned) #分箱后的特征矩阵的结果
- , linewidth=2
- , color='green'
- , linestyle='-'
- , label='linear regression')
- ax2.plot(line, TreeR_.predict(line_binned), linewidth=2, color='red',
- linestyle=':', label='decision tree')
- ax2.vlines(enc.bin_edges_[0], *plt.gca().get_ylim(), linewidth=1, alpha=.2)
- ax2.plot(X[:, 0], y, 'o', c='k')
- ax2.legend(loc="best")
- ax2.set_xlabel("Input feature")
- ax2.set_title("Result after discretization")
- plt.tight_layout()
- plt.show()
如何选取最优的箱数
- from sklearn.model_selection import cross_val_score as CVS
- import numpy as np
- pred,score,var = [], [], []
- binsrange = [2,5,10,15,20,30]
- for i in binsrange:
- #实例化分箱类
- enc = KBinsDiscretizer(n_bins=i,encode="onehot")
- #转换数据
- X_binned = enc.fit_transform(X)
- line_binned = enc.transform(line)
- #建立模型
- LinearR_ = LinearRegression()
- #全数据集上的交叉验证
- cvresult = CVS(LinearR_,X_binned,y,cv=5)
- score.append(cvresult.mean())
- var.append(cvresult.var())
- #测试数据集上的打分结果
- pred.append(LinearR_.fit(X_binned,y).score(line_binned,np.sin(line)))
- #绘制图像
- plt.figure(figsize=(6,5))
- plt.plot(binsrange,pred,c="orange",label="test")
- plt.plot(binsrange,score,c="k",label="full data")
- plt.plot(binsrange,score+np.array(var)*0.5,c="red",linestyle="--",label = "var")
- plt.plot(binsrange,score-np.array(var)*0.5,c="red",linestyle="--")
- plt.legend()
- plt.show()
用于解决”线性回归只能处理线性数据“问题的手段,就是使用多项式回归对线性回归 进行改进
- from sklearn.preprocessing import PolynomialFeatures
- import numpy as np
- #如果原始数据是一维的
- X = np.arange(1,4).reshape(-1,1)
- X
- #二次多项式,参数degree控制多项式的次方
- poly = PolynomialFeatures(degree=2)
- #接口transform直接调用
- X_ = poly.fit_transform(X)
- X_
- X_.shape
- #三次多项式
- PolynomialFeatures(degree=3).fit_transform(X)
- from sklearn.preprocessing import PolynomialFeatures as PF
- from sklearn.linear_model import LinearRegression
- import numpy as np
- rnd = np.random.RandomState(42) #设置随机数种子
- X = rnd.uniform(-3, 3, size=100)
- y = np.sin(X) + rnd.normal(size=len(X)) / 3
- #将X升维,准备好放入sklearn中
- X = X.reshape(-1,1)
- #创建测试数据,均匀分布在训练集X的取值范围内的一千个点
- line = np.linspace(-3, 3, 1000, endpoint=False).reshape(-1, 1)
- #原始特征矩阵的拟合结果
- LinearR = LinearRegr
- ession().fit(X, y)
- #对训练数据的拟合
- LinearR.score(X,y)
- #对测试数据的拟合
- LinearR.score(line,np.sin(line))
- #多项式拟合,设定高次项
- d=5
- #进行高此项转换
- poly = PF(degree=d)
- X_ = poly.fit_transform(X)
- line_ = PF(degree=d).fit_transform(line)
- #训练数据的拟合
- LinearR_ = LinearRegression().fit(X_, y)
- LinearR_.score(X_,y)
- #测试数据的拟合
- LinearR_.score(line_,np.sin(line))
- import matplotlib.pyplot as plt
- d=5
- #和上面展示一致的建模流程
- LinearR = LinearRegression().fit(X, y)
- X_ = PF(degree=d).fit_transform(X)
- LinearR_ = LinearRegression().fit(X_, y)
- line = np.linspace(-3, 3, 1000, endpoint=False).reshape(-1, 1)
- line_ = PF(degree=d).fit_transform(line)
- #放置画布
- fig, ax1 = plt.subplots(1)
- #将测试数据带入predict接口,获得模型的拟合效果并进行绘制
- ax1.plot(line, LinearR.predict(line), linewidth=2, color='green'
- ,label="linear regression")
- ax1.plot(line, LinearR_.predict(line_), linewidth=2, color='red'
- ,label="Polynomial regression")
- #将原数据上的拟合绘制在图像上
- ax1.plot(X[:, 0], y, 'o', c='k')
- #其他图形选项
- ax1.legend(loc="best")
- ax1.set_ylabel("Regression output")
- ax1.set_xlabel("Input feature")
- ax1.set_title("Linear Regression ordinary vs poly")
- plt.tight_layout()
- plt.show()
- #来一起鼓掌,感叹多项式回归的神奇
- #随后可以试试看较低和较高的次方会发生什么变化
- #d=2
- #d=20
- import numpy as np
- from sklearn.preprocessing import PolynomialFeatures
- from sklearn.linear_model import LinearRegression
- X = np.arange(9).reshape(3, 3)
- X
- poly = PolynomialFeatures(degree=5).fit(X)
- #重要接口get_feature_names
- poly.get_feature_names()
- from sklearn.datasets import fetch_california_housing as fch
- import pandas as pd
- housevalue = fch()
- X = pd.DataFrame(housevalue.data)
- y = housevalue.target
- housevalue.feature_names
- X.columns = ["住户收入中位数","房屋使用年代中位数","平均房间数目"
- ,"平均卧室数目","街区人口","平均入住率","街区的纬度","街区的经度"]
- poly = PolynomialFeatures(degree=2).fit(X,y)
- poly.get_feature_names(X.columns)
- X_ = poly.transform(X)
- #在这之后,我们依然可以直接建立模型,然后使用线性回归的coef_属性来查看什么特征对标签的影响最大
- reg = LinearRegression().fit(X_,y)
- coef = reg.coef_
- [*zip(poly.get_feature_names(X.columns),reg.coef_)]
- #放到dataframe中进行排序
- coeff = pd.DataFrame([poly.get_feature_names(X.columns),reg.coef_.tolist()]).T
- coeff.columns = ["feature","coef"]
- coeff.sort_values(by="coef")
- #顺便可以查看一下多项式变化之后,模型的拟合效果如何了
- poly = PolynomialFeatures(degree=4).fit(X,y)
- X_ = poly.transform(X)
- reg = LinearRegression().fit(X,y)
- reg.score(X,y)
- from time import time
- time0 = time()
- reg_ = LinearRegression().fit(X_,y)
- print("R2:{}".format(reg_.score(X_,y)))
- print("time:{}".format(time()-time0))
- #假设使用其他模型?
- from sklearn.ensemble import RandomForestRegressor as RFR
- time0 = time()
- print("R2:{}".format(RFR(n_estimators=100).fit(X,y).score(X,y)))
- print("time:{}".format(time()-time0))
多项式回归通常被认为是非线性模型,但广义上它是一种特殊的线性模型,它能够帮助我们处理非线性数 据,是线性回归的一种进化。
线性回归进行多项式变化后被称为多项式回归,但这并不代表多项式变化只能够与线性回 归连用。