作为有监督学习,分类问题是预測类别结果,而回归问题是预測一个连续的结果。
Pandas是一个用于数据探索、数据处理、数据分析的Python库
In [1]: import pandas as pd In [2]: # read csv file directly from a URL and save the results data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0) # display the first 5 rows data.head() Out[2]: TV Radio Newspaper Sales 1 230.1 37.8 69.2 22.1 2 44.5 39.3 45.1 10.4 3 17.2 45.9 69.3 9.3 4 151.5 41.3 58.5 18.5 5 180.8 10.8 58.4 12.9上面显示的结果类似一个电子表格,这个结构称为Pandas的数据帧(data frame)。
pandas的两个主要数据结构:Series和DataFrame:
Series类似于一维数组,它有一组数据以及一组与之相关的数据标签(即索引)组成。DataFrame是一个表格型的数据结构,它含有一组有序的列,每列能够是不同的值类型。DataFrame既有行索引也有列索引,它能够被看做由Series组成的字典。 In [3]: # display the last 5 rows data.tail() Out[3]: TV Radio Newspaper Sales 196 38.2 3.7 13.8 7.6 197 94.2 4.9 8.1 9.7 198 177.0 9.3 6.4 12.8 199 283.6 42.0 66.2 25.5 200 232.1 8.6 8.7 13.4 In [4]: # check the shape of the DataFrame(rows, colums) data.shape Out[4]: (200, 4)特征:
TV:对于一个给定市场中单一产品。用于电视上的广告费用(以千为单位)Radio:在广播媒体上投资的广告费用Newspaper:用于报纸媒体的广告费用响应:
Sales:相应产品的销量在这个案例中。我们通过不同的广告投入,预測产品销量。由于响应变量是一个连续的值,所以这个问题是一个回归问题。数据集一共同拥有200个观測值,每一组观測相应一个市场的情况。
In [5]: import seaborn as sns %matplotlib inline In [6]: # visualize the relationship between the features and the response using scatterplots sns.pairplot(data, x_vars=['TV','Radio','Newspaper'], y_vars='Sales', size=7, aspect=0.8) Out[6]: <seaborn.axisgrid.PairGrid at 0x82dd890>seaborn的pairplot函数绘制X的每一维度和相应Y的散点图。通过设置size和aspect參数来调节显示的大小和比例。能够从图中看出,TV特征和销量是有比較强的线性关系的,而Radio和Sales线性关系弱一些。Newspaper和Sales线性关系更弱。通过加入一个參数kind='reg'。seaborn能够加入一条最佳拟合直线和95%的置信带。
In [7]: sns.pairplot(data, x_vars=['TV','Radio','Newspaper'], y_vars='Sales', size=7, aspect=0.8, kind='reg') Out[7]: <seaborn.axisgrid.PairGrid at 0x83b76f0>长处:高速;没有调节參数;可轻易解释;可理解
缺点:相比其它复杂一些的模型,其预測准确率不是太高,由于它如果特征和响应之间存在确定的线性关系,这样的如果对于非线性的关系,线性回归模型显然不能非常好的对这样的数据建模。
线性模型表达式: y=β0+β1x1+β2x2+...+βnxn 当中
y是响应β0是截距β1是x1的系数,以此类推在这个案例中: y=β0+β1∗TV+β2∗Radio+...+βn∗Newspaper
y=2.88+0.0466∗TV+0.179∗Radio+0.00345∗Newspaper
怎样解释各个特征相应的系数的意义?
对于给定了Radio和Newspaper的广告投入,假设在TV广告上每多投入1个单位,相应销量将添加0.0466个单位更明白一点,添加其他两个媒体投入固定,在TV广告上没添加1000美元(由于单位是1000美元),销量将添加46.6(由于单位是1000)对于分类问题,评价測度是准确率,但这样的方法不适用于回归问题。
我们使用针对连续数值的评价測度(evaluation metrics)。
以下介绍三种经常使用的针对回归问题的评价測度
In [21]: # define true and predicted response values true = [100, 50, 30, 20] pred = [90, 50, 50, 30](1)平均绝对误差(Mean Absolute Error, MAE)
1n∑ni=1|yi−yi^|
(2)均方误差(Mean Squared Error, MSE)
1n∑ni=1(yi−yi^)2
(3)均方根误差(Root Mean Squared Error, RMSE)
1n∑ni=1(yi−yi^)2−−−−−−−−−−−−−√
In [24]: from sklearn import metrics import numpy as np # calculate MAE by hand print "MAE by hand:",(10 + 0 + 20 + 10)/4. # calculate MAE using scikit-learn print "MAE:",metrics.mean_absolute_error(true, pred) # calculate MSE by hand print "MSE by hand:",(10**2 + 0**2 + 20**2 + 10**2)/4. # calculate MSE using scikit-learn print "MSE:",metrics.mean_squared_error(true, pred) # calculate RMSE by hand print "RMSE by hand:",np.sqrt((10**2 + 0**2 + 20**2 + 10**2)/4.) # calculate RMSE using scikit-learn print "RMSE:",np.sqrt(metrics.mean_squared_error(true, pred)) MAE by hand: 10.0 MAE: 10.0 MSE by hand: 150.0 MSE: 150.0 RMSE by hand: 12.2474487139 RMSE: 12.2474487139在之前展示的数据中,我们看到Newspaper和销量之间的线性关系比較弱,如今我们移除这个特征。看看线性回归预測的结果的RMSE怎样?
In [27]: feature_cols = ['TV', 'Radio'] X = data[feature_cols] y = data.Sales X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) linreg.fit(X_train, y_train) y_pred = linreg.predict(X_test) print np.sqrt(metrics.mean_squared_error(y_test, y_pred)) 1.38790346994我们将Newspaper这个特征移除之后,得到RMSE变小了,说明Newspaper特征不适合作为预測销量的特征,于是。我们得到了新的模型。
我们还能够通过不同的特征组合得到新的模型,看看终于的误差是怎样的。
转载于:https://www.cnblogs.com/bhlsheji/p/5215680.html