溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Python中使用Blending算法

發(fā)布時間:2021-05-13 15:56:22 來源:億速云 閱讀:193 作者:Leah 欄目:開發(fā)技術(shù)

如何在Python中使用Blending算法?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

一、前言

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

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

集成方法分類為:

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

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

Stacking:

Blending:

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

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

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

二、Blending介紹

訓(xùn)練數(shù)據(jù)劃分為訓(xù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流程圖

如何在Python中使用Blending算法

  • 第一步:將原始訓(xùn)練數(shù)據(jù)劃分為訓(xùn)練集和驗證集。

  • 第二步:使用訓(xùn)練集對訓(xùn)練T個不同的模型。

  • 第三步:使用T個基模型,對驗證集進(jìn)行預(yù)測,結(jié)果作為新的訓(xùn)練數(shù)據(jù)。

  • 第四步:使用新的訓(xùn)練數(shù)據(jù),訓(xùn)練一個元模型。

  • 第五步:使用T個基模型,對測試數(shù)據(jù)進(jìn)行預(yù)測,結(jié)果作為新的測試數(shù)據(jù)。

  • 第六步:使用元模型對新的測試數(shù)據(jù)進(jìn)行預(yù)測,得到最終結(jié)果。

如何在Python中使用Blending算法

四、案例

相關(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)練集和驗證集
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)

python主要應(yīng)用領(lǐng)域有哪些

1、云計算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運維工程項目,自動化運維的標(biāo)配就是python+Django/flask。5、金融理財分析,量化交易,金融分析。6、大數(shù)據(jù)分析。

看完上述內(nèi)容,你們掌握如何在Python中使用Blending算法的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI