python實現(xiàn)線性回歸算法
本文用python實現(xiàn)線性回歸算法,供大家參考,具體內容如下
# -*- coding: utf-8 -*-'''Created on Fri Oct 11 19:25:11 2019'''from sklearn import datasets, linear_model # 引用 sklearn庫,主要為了使用其中的線性回歸模塊# 創(chuàng)建數(shù)據(jù)集,把數(shù)據(jù)寫入到numpy數(shù)組import numpy as np # 引用numpy庫,主要用來做科學計算import matplotlib.pyplot as plt # 引用matplotlib庫,主要用來畫圖data = np.array([[152,51],[156,53],[160,54],[164,55], [168,57],[172,60],[176,62],[180,65], [184,69],[188,72]])# 打印出數(shù)組的大小print(data.shape)# TODO 1. 實例化一個線性回歸的模型model=linear_model.LinearRegression()# TODO 2. 在x,y上訓練一個線性回歸模型。 如果訓練順利,則regr會存儲訓練完成之后的結果模型x,y=data[:,0].reshape(-1,1),data[:,1]regr=model.fit(x,y)# TODO 3. 畫出身高與體重之間的關系plt.scatter(x,y,color='red')# 畫出已訓練好的線條plt.plot(x, regr.predict(x), color=’blue’)# 畫x,y軸的標題plt.xlabel(’height (cm)’)plt.ylabel(’weight (kg)’)plt.show() # 展示# 利用已經(jīng)訓練好的模型去預測身高為163的人的體重print ('Standard weight for person with 163 is %.2f'% regr.predict([[163]]))

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. 詳解Android studio 動態(tài)fragment的用法2. 解決Android studio xml界面無法預覽問題3. 圖文詳解vue中proto文件的函數(shù)調用4. Spring Boot和Thymeleaf整合結合JPA實現(xiàn)分頁效果(實例代碼)5. php模擬實現(xiàn)斗地主發(fā)牌6. 什么是python的自省7. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現(xiàn)8. vue 使用localstorage實現(xiàn)面包屑的操作9. .Net Core使用Coravel實現(xiàn)任務調度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備