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)
Posted shiyiandchuixue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sklearn-least squares(data set of diabetes)相关的知识,希望对你有一定的参考价值。
以上是关于sklearn-least squares(data set of diabetes)的主要内容,如果未能解决你的问题,请参考以下文章