码迷,mamicode.com
首页 > 其他好文 > 详细

sklearn 线性模型使用入门

时间:2015-04-25 10:40:58      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

 

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: \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

sklearn 线性模型使用入门

标签:

原文地址:http://www.cnblogs.com/jkmiao/p/4455399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!