专栏首页翻译scikit-learn CookbookFitting a line through data一条穿过数据的拟合直线

Fitting a line through data一条穿过数据的拟合直线

Now, we get to do some modeling! It's best to start simple; therefore, we'll look at linear regression first. Linear regression is the first, and therefore, probably the most fundamental model—a straight line through data.


Getting ready准备工作

The boston dataset is perfect to play around with regression. The boston dataset has the median home price of several areas in Boston. It also has other factors that might impact housing prices, for example, crime rate.


First, import the datasets model, then we can load the dataset:首先,载入数据集模型,然后我们载入数据。

from sklearn import datasets
boston = datasets.load_boston()

How to do it...怎么做

Actually, using linear regression in scikit-learn is quite simple. The API for linear regression is basically the same API you're now familiar with from the previous chapter.


First, import the LinearRegression object and create an object:首先,引入LinearRegression对象然后生成一个对象。

from sklearn.linear_model import LinearRegression
lr = LinearRegression()

Now, it's as easy as passing the independent and dependent variables to the fit method of LinearRegression :


lr.fit(boston.data, boston.target)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

Now, to get the predictions, do the following:现在为了得到预测值,做如下操作:

predictions = lr.predict(boston.data)

It's then probably a good idea to look at how close the predictions are to the actual data.We can use a histogram to look at the differences. These are called the residuals, as shown:


Let's take a look at the coefficients:让我们看一下相关系数:

array([-1.08011358e-01,  4.64204584e-02,  2.05586264e-02,  2.68673382e+00,
       -1.77666112e+01,  3.80986521e+00,  6.92224640e-04, -1.47556685e+00,
        3.06049479e-01, -1.23345939e-02, -9.52747232e-01,  9.31168327e-03,

So, going back to the data, we can see which factors have a negative relationship with the outcome, and also the factors that have a positive relationship. For example, and as expected,an increase in the per capita crime rate by town has a negative relationship with the price of a home in Boston. The per capita crime rate is the first coefficient in the regression.


How it works...它怎么做的

The basic idea of linear regression is to find the set of coefficients of that satisfy y =X β ,where X is the data matrix. It's unlikely that for the given values of X, we will find a set of coefficients that exactly satisfy the equation; an error term gets added if there is an inexact specification or measurement error. Therefore, the equation becomes y=X β+ε , where ε is assumed to be normally distributed and independent of the X values. Geometrically, this means that the error terms are perpendicular to X. It's beyond the scope of this book, but it might be worth it to prove E(X ε)= 0 to yourself.

线性回归最基本的思想就是找到系数矩阵满足y=Xβ,X数数据矩阵,这不大可能对于给出的X的值,我们能找到一个系数集合来完全满足方程,误差会因为不准确的说明或测量误差产生,所以,方程变为y=X β+ε,假定ε是正态分布且与X值独立,在几何学上误差是与X垂直的,这超出了本书的范围,但值得你自己证明一下。

In order to find the set of betas that map the X values to y, we minimize the error term.This is done by minimizing the residual sum of squares.This problem can be solved analytically, with the solution being .


β=(XT X)-XT y

There's more...扩展阅读

The LinearRegression object can automatically normalize (or scale) the inputs:


lr2 = LinearRegression(normalize=True)
lr2.fit(boston.data, boston.target)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=True)
>>> predictions2 = lr2.predict(boston.data)


原文作者:Trent Hauck


0 条评论
登录 后参与评论


  • Using stochastic gradient descent for regression使用随机梯度下降进行回归分析

    In this recipe, we'll get our first taste of stochastic gradient descent. We'll ...

  • Evaluating the linear regression model评估线性回归模型

    In this recipe, we'll look at how well our regression fits the underlying data. ...

  • Regression model evaluation回归模型评估

    We learned about quantifying the error in classification, now we'll discuss quan...

  • Python Decorators


  • 机器学习实战(二) - 单变量线性回归Model and Cost Function1 模型概述 - Model Representation2 代价函数 - Cost Function3 代价函数(

    To establish notation for future use, we’ll use

  • Palabos Tutorial 2/3:Understanding the multi-block structure

    The code structure of Palabos programs is driven by the duality between atomic-b...

  • log4j conversion pattern各格式含义

    Dylan Liu
  • Oops错误

    在at91rm9200下写了一个spi的驱动,加载后,运行测试程序时,蹦出这么个吓人的东西: Unable to handle kernel paging r...

  • Java开发人员常用的服务配置(Nginx、Tomcat、JVM、Mysql、Redis)

  • RFC2616-HTTP1.1-Methods(方法规定部分—单词注释版)