线性回归例子(Linear Regression Example)

原文地址:
http://sklearn.lzjqsdd.com/auto_examples/linear_model/plot_ols.html#example-linear-model-plot-ols-py

This example uses the only the first feature of the diabetes dataset, in order to illustrate a two-dimensional plot of this regression technique. The straight line can be seen in the plot, showing how linear regression attempts to draw a straight line that will best minimize the residual sum of squares between the observed responses in the dataset, and the responses predicted by the linear approximation.

为了得到线性回归的二维图,这个示例只使用了糖尿病数据集的第一个特征。
线性回归试图绘制一条直线,使数据集中所有样本到直线上的距离的剩余平方和最小,并且与响应预测线性逼近。

diabetes英 [,daɪə'biːtiːz]美 [,daɪə'bitiz]
n. 糖尿病;多尿症

illustrate英 ['ɪləstreɪt]美 ['ɪləstret]
vi. 举例
vt. 阐明,举例说明;图解

two-dimensional英 [,tu:dɪ'menʃənəl]美 [,tʊdaɪ'mɛnʃənəl]
adj. 二维的;缺乏深度的

two-dimensional plot
双向图

residual
英 [rɪˈzɪdjuəl]   美 [rɪˈzɪdʒuəl]  
adj.
残留的;残余的
n.
剩余;残渣

sum of squares
平方和

residual sum of squares
剩余平方和

linear approximation
[数] 线性近似,线性逼近

The coefficients, the residual sum of squares and the variance score are also calculated.
系数、剩余平方和 、方差分也计算。

coefficients
n. [数] 系数(coefficient的复数形式)

这里写图片描述

输出:

Coefficients:
 [ 938.23786125]
Residual sum of squares: 2548.07
Variance score: 0.47

Python 源码: plot_ols.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
=========================================================
Linear Regression Example
=========================================================
This example uses the only the first feature of the `diabetes` dataset, in
order to illustrate a two-dimensional plot of this regression technique. The
straight line can be seen in the plot, showing how linear regression attempts
to draw a straight line that will best minimize the residual sum of squares
between the observed responses in the dataset, and the responses predicted by
the linear approximation.

The coefficients, the residual sum of squares and the variance score are also
calculated.

"""
#print(__doc__)


# Code source: Jaques Grobler
# License: BSD 3 clause


import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model

# Load the diabetes dataset
diabetes = datasets.load_diabetes()


# Use only one feature
diabetes_X = diabetes.data[:, np.newaxis, 2]

# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]

# Split the targets into training/testing sets
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]

# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets
regr.fit(diabetes_X_train, diabetes_y_train)

# The coefficients
print('Coefficients: \n', regr.coef_)
# The mean square error
print("Residual sum of squares: %.2f"
      % np.mean((regr.predict(diabetes_X_test) - diabetes_y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(diabetes_X_test, diabetes_y_test))

# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')
plt.plot(diabetes_X_test, regr.predict(diabetes_X_test), color='blue',
         linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值