sklearn 线性模型使用入门

 

LinearRegression fits a linear model with coefficients w = (w_1, ..., w_p) 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:

原理最小化:   \underset{w}{min\,} {|| X w - y||_2}^2

 
>>> 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: \n ',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()
View Code

 

 

 

转自:

http://scikit-learn.org/dev/auto_examples/linear_model/plot_ols.html#example-linear-model-plot-ols-py

posted on 2015-04-25 09:32  星空守望者--jkmiao  阅读(730)  评论(0编辑  收藏  举报