线性回归例子(Linear Regression Example)
2017-06-10 20:39
369 查看
原文地址:
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.
为了得到线性回归的二维图,这个示例只使用了糖尿病数据集的第一个特征。
线性回归试图绘制一条直线,使数据集中所有样本到直线上的距离的剩余平方和最小,并且与响应预测线性逼近。
输出:
Python 源码: plot_ols.py
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()
相关文章推荐
- Matlab实现线性回归和逻辑回归: Linear Regression & Logistic Regression
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- 【Machine Learning】单参数线性回归 Linear Regression with one variable
- 局部权重线性回归(Locally weighted linear regression)
- 机器学习:多变量线性回归(Linear Regression with Multiple Variables)
- Stanford ML - Linear regression with one variable 单变量线性回归
- Standford 机器学习—第一讲 Linear Regression with one variable(单变量线性回归)
- Linear regression with multiple variables(多特征值的线性回归)
- Locally weighted linear regression(局部加权线性回归)
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- 机器学习之2-多变量线性回归(Linear Regression with Multiple Variables)
- 第二讲.Linear Regression with multiple variable (多变量线性回归)
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- Matlab实现线性回归和逻辑回归: Linear Regression & Logistic Regression
- 线性回归(linear-regression)预测算法基本概念&C++实现
- 2 单变量线性回归(Linear Regression with One Varisble)
- 线性回归(linear_regression),多项式回归(polynomial regression)(Tensorflow实现)
- 机器学习之单变量线性回归(Linear Regression with One Variable)
- 局部加权线性回归(Locally weighted linear regression)