#-*-coding:utf-8-*-#-----------------------unary linear regression----------------------------import Matplotlib.pyplot as Plt Import NumPy as NP from Sklearn import Datasets,linear_model from sklearn.metrics import Mean_squared_error,r2_score from
Matplotlib.font_manager Import Fontproperties font = fontproperties (fname=r "C:\WINDOWS\FONTS\SIMSUN.TTC", size=10) Import sys reload (SYS) sys.setdefaultencoding (' Utf-8 ') diabetes=datasets.load_diabetes () diabetes_x=diabetes.data[: , 2][:,np.newaxis] #newaxis将一维数组转换为二维数组 #将数据分割为测试和训练数据 diabetes_x_train=diabetes_x[:-20] #取从0到倒数第20列的数据 diabetes_x_ TEST=DIABETES_X[-20:] #取倒数第20列后的数据 diabetes_y_train=diabetes.target[:-20] diabetes_y_test=diabetes.target[-20:] " ' Diabetes_y_train=diabetes.target[:-20,np.newaxis] Diabetes_y_test=diabetes.target[-20:][:,np.newaxis] "Print Diabetes_x_train.shape print '---------------' Print Diabetes_y_train.shape Model=linear_model. Linearregression () Model.fit (Diabetes_x_train,diabetes_y_train) predict_y=model.predict (diabetes_x_test) print ' R-side: ', Model.score (diabetes_x_test,diabetes_y_test) plt.title (' Cancer prediction ', Fontproperties=font) plt.plot (Diabetes_x_test,diabetes_y_test, ' K. ') Plt.plot (diabetes_x_test,predict_y, ' G ') plt.show ()