Python使用sklearn實現(xiàn)的各種回歸算法示例
本文實例講述了Python使用sklearn實現(xiàn)的各種回歸算法。分享給大家供大家參考,具體如下:
使用sklearn做各種回歸
基本回歸:線性、決策樹、SVM、KNN
集成方法:隨機森林、Adaboost、GradientBoosting、Bagging、ExtraTrees
1. 數(shù)據(jù)準備
為了實驗用,我自己寫了一個二元函數(shù),y=0.5*np.sin(x1)+ 0.5*np.cos(x2)+0.1*x1+3。其中x1的取值范圍是0~50,x2的取值范圍是-10~10,x1和x2的訓練集一共有500個,測試集有100個。其中,在訓練集的上加了一個-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
其中訓練集(y上加有-0.5~0.5的隨機噪聲)和測試集(沒有噪聲)的圖像如下:

2. scikit-learn的簡單使用
scikit-learn非常簡單,只需實例化一個算法對象,然后調用fit()函數(shù)就可以了,fit之后,就可以使用predict()函數(shù)來預測了,然后可以使用score()函數(shù)來評估預測值和真實值的差異,函數(shù)返回一個得分。
完整程式化代碼為:
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有隨機噪聲
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隨機森林回歸####
from sklearn import ensemble
model_RandomForestRegressor = ensemble.RandomForestRegressor(n_estimators=20)#這里使用20個決策樹
####3.6Adaboost回歸####
from sklearn import ensemble
model_AdaBoostRegressor = ensemble.AdaBoostRegressor(n_estimators=50)#這里使用50個決策樹
####3.7GBRT回歸####
from sklearn import ensemble
model_GradientBoostingRegressor = ensemble.GradientBoostingRegressor(n_estimators=100)#這里使用100個決策樹
####3.8Bagging回歸####
from sklearn.ensemble import BaggingRegressor
model_BaggingRegressor = BaggingRegressor()
####3.9ExtraTree極端隨機樹回歸####
from sklearn.tree import ExtraTreeRegressor
model_ExtraTreeRegressor = ExtraTreeRegressor()
###########4.具體方法調用部分##########
try_different_method(model_DecisionTreeRegressor)
3.結果展示
決策樹回歸結果:

線性回歸結果:

SVM回歸結果:

KNN回歸結果:

隨機森林回歸結果:

Adaboost回歸結果:

GBRT回歸結果:

Bagging回歸結果:

極端隨機樹回歸結果:

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結構與算法教程》、《Python加密解密算法與技巧總結》、《Python編碼操作技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python lxml解析HTML并用xpath獲取元素的方法
今天小編就為大家分享一篇Python lxml解析HTML并用xpath獲取元素的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python實現(xiàn)Tab自動補全和歷史命令管理的方法
這篇文章主要介紹了Python實現(xiàn)Tab自動補全和歷史命令管理的方法,實例分析了tab自動補全的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
安裝Python的web.py框架并從hello world開始編程
這篇文章主要介紹了安裝Python的web.py框架并從hello world開始編程,web.py的作者年輕的Aaron Swartz已經(jīng)離世,緬懷大神,需要的朋友可以參考下2015-04-04
python爬蟲 使用真實瀏覽器打開網(wǎng)頁的兩種方法總結
下面小編就為大家分享一篇python爬蟲 使用真實瀏覽器打開網(wǎng)頁的兩種方法總結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

