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

sklearn-least squares(data set of diabetes)

时间:2018-02-05 23:22:33      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:split   ssi   square   ini   link   learn   gre   red   int   

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model
from sklearn.metrics import mean_absolute_error,r2_score

# Load the dataset 糖尿病 看源码呗
diabetes = datasets.load_diabetes()

# use only one featrue np.newaxis:(:,) 变成(:,1)的矩阵,增加维数
diabetes_x = diabetes.data[:,np.newaxis,2]

# splite the data into training/testing sets
diabetes_x_train = diabetes_x[:-20]
diabetes_x_test = diabetes_x[-20:]
# splite 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()

# training the model
regr.fit(diabetes_x_train,diabetes_y_train)

# make predictions using the testiong set
diabetes_y_pred = regr.predict(diabetes_x_test)

# the cofficients
print(regr.coef_)
print(diabetes_y_pred)
print("mean squared error:%.2f"%mean_absolute_error(diabetes_x_test,diabetes_y_pred))
print("variance scor:%.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()

remark:
  >>>X[:, 1][:, np.newaxis] #索引数组的一列 如果没有[:,np.newaxis]索引的结果是一行,X[:, 1][:, np.newaxis]可以简化为:X[:, np.newaxis, 1]

sklearn-least squares(data set of diabetes)

标签:split   ssi   square   ini   link   learn   gre   red   int   

原文地址:https://www.cnblogs.com/shiyiandchuixue/p/8419399.html

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