• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

sklearn+python:线性回归案例

python 搞代码 4年前 (2022-01-08) 28次浏览 已收录 0个评论

今天小编就为大家分享一篇sklearn+python:线性回归案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

使用一阶线性方程预测波士顿房价

载入的数据是随sklearn一起发布的,来自boston 1993年之前收集的506个房屋的数据和价格。load_boston()用于载入数据。

 from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split import time from sklearn.linear_model import LinearRegression boston = load_boston() X = boston.data y = boston.ta<b style="color:transparent">来源gao@dai!ma.com搞$代^码网</b>rget print("X.shape:{}. y.shape:{}".format(X.shape, y.shape)) print('boston.feature_name:{}'.format(boston.feature_names)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3) model = LinearRegression() start = time.clock() model.fit(X_train, y_train) train_score = model.score(X_train, y_train) cv_score = model.score(X_test, y_test) print('time used:{0:.6f}; train_score:{1:.6f}, sv_score:{2:.6f}'.format((time.clock()-start), train_score, cv_score)) 

输出内容为:

 X.shape:(506, 13). y.shape:(506,) boston.feature_name:['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO' 'B' 'LSTAT'] time used:0.012403; train_score:0.723941, sv_score:0.794958

可以看到测试集上准确率并不高,应该是欠拟合。

使用多项式做线性回归

上面的例子是欠拟合的,说明模型太简单,无法拟合数据的情况。现在增加模型复杂度,引入多项式。

打个比方,如果原来的特征是[a, b]两个特征,

在degree为2的情况下, 多项式特征变为[1, a, b, a^2, ab, b^2]。degree为其它值的情况依次类推。

多项式特征相当于增加了数据和模型的复杂性,能够更好的拟合。

下面的代码使用Pipeline把多项式特征和线性回归特征连起来,最终测试degree在1、2、3的情况下的得分。

 from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split import time from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import Pipeline def polynomial_model(degree=1): polynomial_features = PolynomialFeatures(degree=degree, include_bias=False) linear_regression = LinearRegression(normalize=True) pipeline = Pipeline([('polynomial_features', polynomial_features), ('linear_regression', linear_regression)]) return pipeline boston = load_boston() X = boston.data y = boston.target print("X.shape:{}. y.shape:{}".format(X.shape, y.shape)) print('boston.feature_name:{}'.format(boston.feature_names)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3) for i in range(1,4): print( 'degree:{}'.format( i ) ) model = polynomial_model(degree=i) start = time.clock() model.fit(X_train, y_train) train_score = model.score(X_train, y_train) cv_score = model.score(X_test, y_test) print('time used:{0:.6f}; train_score:{1:.6f}, sv_score:{2:.6f}'.format((time.clock()-start), train_score, cv_score)) 

输出结果为:

 X.shape:(506, 13). y.shape:(506,) boston.feature_name:['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO' 'B' 'LSTAT'] degree:1 time used:0.003576; train_score:0.723941, sv_score:0.794958 degree:2 time used:0.030123; train_score:0.930547, sv_score:0.860465 degree:3 time used:0.137346; train_score:1.000000, sv_score:-104.429619

可以看到degree为1和上面不使用多项式是一样的。degree为3在训练集上的得分为1,在测试集上得分是负数,明显过拟合了。

所以最终应该选择degree为2的模型。

二阶多项式比一阶多项式好的多,但是测试集和训练集上的得分仍有不少差距,这可能是数据不够的原因,需要更多的讯据才能进一步提高模型的准确度。

正规方程解法和梯度下降的比较

除了梯度下降法来逼近最优解,也可以使用正规的方程解法直接计算出最终的解来。

根据吴恩达的课程,线性回归最优解为:

theta = (X^T * X)^-1 * X^T * y

其实两种方法各有优缺点:

梯度下降法:

缺点:需要选择学习率,需要多次迭代

优点:特征值很多(1万以上)时仍然能以不错的速度工作

正规方程解法:

优点:不需要设置学习率,不需要多次迭代

缺点:需要计算X的转置和逆,复杂度O3;特征值很多(1万以上)时特变慢

在分类等非线性计算中,正规方程解法并不适用,所以梯度下降法适用范围更广。

以上就是sklearn+python:线性回归案例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:sklearn+python:线性回归案例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址