Python使用sklearn實(shí)現(xiàn)的各種回歸算法示例
本文實(shí)例講述了Python使用sklearn實(shí)現(xiàn)的各種回歸算法。分享給大家供大家參考,具體如下:
使用sklearn做各種回歸
基本回歸:線性、決策樹、SVM、KNN
集成方法:隨機(jī)森林、Adaboost、GradientBoosting、Bagging、ExtraTrees
1. 數(shù)據(jù)準(zhǔn)備
為了實(shí)驗(yàn)用,我自己寫了一個(gè)二元函數(shù),y=0.5*np.sin(x1)+ 0.5*np.cos(x2)+0.1*x1+3。其中x1的取值范圍是0~50,x2的取值范圍是-10~10,x1和x2的訓(xùn)練集一共有500個(gè),測試集有100個(gè)。其中,在訓(xùn)練集的上加了一個(gè)-0.5~0.5的噪聲。生成函數(shù)的代碼如下:
def f(x1, x2): y = 0.5 * np.sin(x1) + 0.5 * np.cos(x2) + 0.1 * x1 + 3 return y def load_data(): x1_train = np.linspace(0,50,500) x2_train = np.linspace(-10,10,500) data_train = np.array([[x1,x2,f(x1,x2) + (np.random.random(1)-0.5)] for x1,x2 in zip(x1_train, x2_train)]) x1_test = np.linspace(0,50,100)+ 0.5 * np.random.random(100) x2_test = np.linspace(-10,10,100) + 0.02 * np.random.random(100) data_test = np.array([[x1,x2,f(x1,x2)] for x1,x2 in zip(x1_test, x2_test)]) return data_train, data_test
其中訓(xùn)練集(y上加有-0.5~0.5的隨機(jī)噪聲)和測試集(沒有噪聲)的圖像如下:

2. scikit-learn的簡單使用
scikit-learn非常簡單,只需實(shí)例化一個(gè)算法對(duì)象,然后調(diào)用fit()函數(shù)就可以了,fit之后,就可以使用predict()函數(shù)來預(yù)測了,然后可以使用score()函數(shù)來評(píng)估預(yù)測值和真實(shí)值的差異,函數(shù)返回一個(gè)得分。
完整程式化代碼為:
import numpy as np
import matplotlib.pyplot as plt
###########1.數(shù)據(jù)生成部分##########
def f(x1, x2):
y = 0.5 * np.sin(x1) + 0.5 * np.cos(x2) + 3 + 0.1 * x1
return y
def load_data():
x1_train = np.linspace(0,50,500)
x2_train = np.linspace(-10,10,500)
data_train = np.array([[x1,x2,f(x1,x2) + (np.random.random(1)-0.5)] for x1,x2 in zip(x1_train, x2_train)])
x1_test = np.linspace(0,50,100)+ 0.5 * np.random.random(100)
x2_test = np.linspace(-10,10,100) + 0.02 * np.random.random(100)
data_test = np.array([[x1,x2,f(x1,x2)] for x1,x2 in zip(x1_test, x2_test)])
return data_train, data_test
train, test = load_data()
x_train, y_train = train[:,:2], train[:,2] #數(shù)據(jù)前兩列是x1,x2 第三列是y,這里的y有隨機(jī)噪聲
x_test ,y_test = test[:,:2], test[:,2] # 同上,不過這里的y沒有噪聲
###########2.回歸部分##########
def try_different_method(model):
model.fit(x_train,y_train)
score = model.score(x_test, y_test)
result = model.predict(x_test)
plt.figure()
plt.plot(np.arange(len(result)), y_test,'go-',label='true value')
plt.plot(np.arange(len(result)),result,'ro-',label='predict value')
plt.title('score: %f'%score)
plt.legend()
plt.show()
###########3.具體方法選擇##########
####3.1決策樹回歸####
from sklearn import tree
model_DecisionTreeRegressor = tree.DecisionTreeRegressor()
####3.2線性回歸####
from sklearn import linear_model
model_LinearRegression = linear_model.LinearRegression()
####3.3SVM回歸####
from sklearn import svm
model_SVR = svm.SVR()
####3.4KNN回歸####
from sklearn import neighbors
model_KNeighborsRegressor = neighbors.KNeighborsRegressor()
####3.5隨機(jī)森林回歸####
from sklearn import ensemble
model_RandomForestRegressor = ensemble.RandomForestRegressor(n_estimators=20)#這里使用20個(gè)決策樹
####3.6Adaboost回歸####
from sklearn import ensemble
model_AdaBoostRegressor = ensemble.AdaBoostRegressor(n_estimators=50)#這里使用50個(gè)決策樹
####3.7GBRT回歸####
from sklearn import ensemble
model_GradientBoostingRegressor = ensemble.GradientBoostingRegressor(n_estimators=100)#這里使用100個(gè)決策樹
####3.8Bagging回歸####
from sklearn.ensemble import BaggingRegressor
model_BaggingRegressor = BaggingRegressor()
####3.9ExtraTree極端隨機(jī)樹回歸####
from sklearn.tree import ExtraTreeRegressor
model_ExtraTreeRegressor = ExtraTreeRegressor()
###########4.具體方法調(diào)用部分##########
try_different_method(model_DecisionTreeRegressor)
3.結(jié)果展示
決策樹回歸結(jié)果:

線性回歸結(jié)果:

SVM回歸結(jié)果:

KNN回歸結(jié)果:

隨機(jī)森林回歸結(jié)果:

Adaboost回歸結(jié)果:

GBRT回歸結(jié)果:

Bagging回歸結(jié)果:

極端隨機(jī)樹回歸結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python列表創(chuàng)建與銷毀及緩存池機(jī)制
這篇文章主要介紹了Python列表創(chuàng)建與銷毀及緩存池機(jī)制,文章基于python展開對(duì)列表創(chuàng)建與銷毀內(nèi)容的展開,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
Python3實(shí)現(xiàn)發(fā)送QQ郵件功能(html)
這篇文章主要為大家詳細(xì)介紹了Python3實(shí)現(xiàn)發(fā)送QQ郵件功能,html格式的qq郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Python?庫?PySimpleGUI?制作自動(dòng)化辦公小軟件的方法
Python?在運(yùn)維和辦公自動(dòng)化中扮演著重要的角色,PySimpleGUI?是一款很棒的自動(dòng)化輔助模塊,讓你更輕松的實(shí)現(xiàn)日常任務(wù)的自動(dòng)化,下面通過本文給大家介紹下Python?庫?PySimpleGUI?制作自動(dòng)化辦公小軟件的過程,一起看看吧2021-12-12
python 遞歸調(diào)用返回None的問題及解決方法
這篇文章主要介紹了python 遞歸調(diào)用返回None的問題,本文通過實(shí)例代碼給大家記錄了解決方案,代碼簡單易懂,非常不錯(cuò)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
python自動(dòng)分箱,計(jì)算woe,iv的實(shí)例代碼
今天小編就為大家分享一篇python自動(dòng)分箱,計(jì)算woe,iv的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Python使用wxPython和PyMuPDF實(shí)現(xiàn)合并PDF文檔
處理大量的PDF文檔可能會(huì)變得復(fù)雜和耗時(shí),但是,使用Python編程和一些強(qiáng)大的庫,可以使這個(gè)任務(wù)變得簡單而高效,下面我們就來看看Python如何使用wxPython和PyMuPDF合并PDF文檔并自動(dòng)復(fù)制到剪貼板吧2023-11-11
Python中ImportError錯(cuò)誤的詳細(xì)解決方法
最近辛辛苦苦安裝完了python,最后再運(yùn)行的時(shí)候會(huì)出現(xiàn)錯(cuò)誤,所以這篇文章主要給大家介紹了關(guān)于Python中ImportError錯(cuò)誤的詳細(xì)解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

