Unary linear regression

Source: Internet
Author: User
#-*-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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.