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

Linear Regression Example

时间:2018-01-29 21:33:50      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:plot   puts   org   ini   create   style   img   pos   函数   

 

代码主要来自:http://scikit-learn.org/stable/

误差函数: 采用最小二平方

 

代码如下:

print(__doc__)
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score

diabetes = datasets.load_diabetes()

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)

#Make predictions using the testing set
diabetes_y_pred = regr.predict(diabetes_x_test)

#The coefficients
print(Coefficients: \n, regr.coef_)

#The mean squared error
print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test,diabetes_y_pred))

# Explained variance score: 1 is perfect prediction
print(Variance score: %.2f % r2_score(diabetes_y_test,diabetes_y_pred))

#Plot outputs
plt.scatter(diabetes_x_test,diabetes_y_test, color=black)
plt.plot(diabetes_x_test,diabetes_y_pred,color=blue,linewidth=3)

plt.xticks(())
plt.yticks(())
plt.show()

 

执行结果如下:

技术分享图片

 

Linear Regression Example

标签:plot   puts   org   ini   create   style   img   pos   函数   

原文地址:https://www.cnblogs.com/ordili/p/8379203.html

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