主頁 > 知識庫 > Python集成學(xué)習(xí)之Blending算法詳解

Python集成學(xué)習(xí)之Blending算法詳解

熱門標(biāo)簽:百度AI接口 電話運(yùn)營中心 硅谷的囚徒呼叫中心 呼叫中心市場需求 語音系統(tǒng) 企業(yè)做大做強(qiáng) 客戶服務(wù) Win7旗艦版

一、前言

普通機(jī)器學(xué)習(xí):從訓(xùn)練數(shù)據(jù)中學(xué)習(xí)一個(gè)假設(shè)。

集成方法:試圖構(gòu)建一組假設(shè)并將它們組合起來,集成學(xué)習(xí)是一種機(jī)器學(xué)習(xí)范式,多個(gè)學(xué)習(xí)器被訓(xùn)練來解決同一個(gè)問題。

集成方法分類為:

Bagging(并行訓(xùn)練):隨機(jī)森林

Boosting(串行訓(xùn)練):Adaboost; GBDT; XgBoost

Stacking:

Blending:

或者分類為串行集成方法和并行集成方法

1.串行模型:通過基礎(chǔ)模型之間的依賴,給錯(cuò)誤分類樣本一個(gè)較大的權(quán)重來提升模型的性能。

2.并行模型的原理:利用基礎(chǔ)模型的獨(dú)立性,然后通過平均能夠較大地降低誤差

二、Blending介紹

訓(xùn)練數(shù)據(jù)劃分為訓(xùn)練和驗(yàn)證集+新的訓(xùn)練數(shù)據(jù)集和新的測試集

將訓(xùn)練數(shù)據(jù)進(jìn)行劃分,劃分之后的訓(xùn)練數(shù)據(jù)一部分訓(xùn)練基模型,一部分經(jīng)模型預(yù)測后作為新的特征訓(xùn)練元模型。
測試數(shù)據(jù)同樣經(jīng)過基模型預(yù)測,形成新的測試數(shù)據(jù)。最后,元模型對新的測試數(shù)據(jù)進(jìn)行預(yù)測。Blending框架圖如下所示:
注意:其是在stacking的基礎(chǔ)上加了劃分?jǐn)?shù)據(jù)

三、Blending流程圖

  • 第一步:將原始訓(xùn)練數(shù)據(jù)劃分為訓(xùn)練集和驗(yàn)證集。
  • 第二步:使用訓(xùn)練集對訓(xùn)練T個(gè)不同的模型。
  • 第三步:使用T個(gè)基模型,對驗(yàn)證集進(jìn)行預(yù)測,結(jié)果作為新的訓(xùn)練數(shù)據(jù)。
  • 第四步:使用新的訓(xùn)練數(shù)據(jù),訓(xùn)練一個(gè)元模型。
  • 第五步:使用T個(gè)基模型,對測試數(shù)據(jù)進(jìn)行預(yù)測,結(jié)果作為新的測試數(shù)據(jù)。
  • 第六步:使用元模型對新的測試數(shù)據(jù)進(jìn)行預(yù)測,得到最終結(jié)果。

四、案例

相關(guān)工具包加載

import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
plt.style.use("ggplot")
%matplotlib inline
import seaborn as sns



創(chuàng)建數(shù)據(jù)

from sklearn import datasets 
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
data, target = make_blobs(n_samples=10000, centers=2, random_state=1, cluster_std=1.0 )
## 創(chuàng)建訓(xùn)練集和測試集
X_train1,X_test,y_train1,y_test = train_test_split(data, target, test_size=0.2, random_state=1)
## 創(chuàng)建訓(xùn)練集和驗(yàn)證集
X_train,X_val,y_train,y_val = train_test_split(X_train1, y_train1, test_size=0.3, random_state=1)
print("The shape of training X:",X_train.shape)
print("The shape of training y:",y_train.shape)
print("The shape of test X:",X_test.shape)
print("The shape of test y:",y_test.shape)
print("The shape of validation X:",X_val.shape)
print("The shape of validation y:",y_val.shape)
 

設(shè)置第一層分類器

from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier

clfs = [SVC(probability=True),RandomForestClassifier(n_estimators=5,n_jobs=-1,criterion='gini'),KNeighborsClassifier()]



設(shè)置第二層分類器

from sklearn.linear_model import LinearRegression
lr = LinearRegression()



第一層

val_features = np.zeros((X_val.shape[0],len(clfs)))
test_features = np.zeros((X_test.shape[0],len(clfs)))

for i,clf in enumerate(clfs):
    clf.fit(X_train,y_train)
    val_feature = clf.predict_proba(X_val)[:,1]
    test_feature = clf.predict_proba(X_test)[:,1]
    val_features[:,i] = val_feature
    test_features[:,i] = test_feature



第二層

lr.fit(val_features,y_val)



輸出預(yù)測的結(jié)果

lr.fit(val_features,y_val)
from sklearn.model_selection import cross_val_score
cross_val_score(lr,test_features,y_test,cv=5)
 

到此這篇關(guān)于Python集成學(xué)習(xí)之Blending算法詳解的文章就介紹到這了,更多相關(guān)Python Blending算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python 算法題——快樂數(shù)的多種解法
  • python使用ProjectQ生成量子算法指令集
  • Python機(jī)器學(xué)習(xí)算法之決策樹算法的實(shí)現(xiàn)與優(yōu)缺點(diǎn)
  • python3實(shí)現(xiàn)Dijkstra算法最短路徑的實(shí)現(xiàn)
  • Python實(shí)現(xiàn)K-means聚類算法并可視化生成動圖步驟詳解
  • Python自然語言處理之切分算法詳解
  • python入門之算法學(xué)習(xí)
  • Python實(shí)現(xiàn)機(jī)器學(xué)習(xí)算法的分類

標(biāo)簽:山西 崇左 山西 海南 長沙 濟(jì)南 安康 喀什

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python集成學(xué)習(xí)之Blending算法詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266