LinearRegression
fits a linear model with coefficients to minimize the residual sum of squares between the observed responses in the dataset, and the responses predicted by the linear approximation. Mathematically it solves a problem of the form:
原理最小化:
>>> from sklearn import linear_model >>> clf = linear_model.LinearRegression() >>> clf.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2]) LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False) >>> clf.coef_ array([ 0.5, 0.5])
完整代理例子
#!/usr/bin/env python # coding=utf-8 import matplotlib.pyplot as plt import numpy as np from sklearn import datasets,linear_model print(__doc__) # load dataset diabetes = datasets.load_diabetes() # use only one feature diabetes_x = diabetes.data[:,np.newaxis] diabetes_x_temp = diabetes_x[:,:,2] # split data into training/testing sets diabetes_x_train = diabetes_x_temp[:-20] diabetes_x_test = diabetes_x_temp[-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() regr.fit(diabetes_x_train,diabetes_y_train) # the coefficients print('coefficients: ',regr.coef_) # the mean square error print("Residual sum of squares:%.2f" % np.mean((regr.predict(diabetes_x_test)-diabetes_y_test)**2)) # 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.title("linear_model example") plt.xlabel("X") plt.ylabel("Y") # plt.xticks(()) # plt.yticks(()) plt.show()
转自:
http://scikit-learn.org/dev/auto_examples/linear_model/plot_ols.html#example-linear-model-plot-ols-py