動態分類器選擇是一種用於分類預測建模的集成學習算法。該技術涉及在訓練數據集上擬合多個機器學習模型,然後基於要預測的示例的特定細節,選擇在進行預測時預期表現最佳的模型。這可以通過以下方法實現:使用k最近鄰模型在訓練數據集中找到最接近要預測的新示例的示例,評估該鄰域中池中的所有模型,並使用在鄰域中表現最佳的模型來對新示例做出預測。這樣,動態分類器選擇通常可以比池中的任何單個模型更好地執行,並且提供了對來自多個模型的預測求平均的另一種方法,就像其他集成算法一樣。在本教程中,您將發現如何在Python中開發動態分類器選擇集合。
動態分類器選擇算法從眾多模型中選擇一種以對每個新示例進行預測。
如何使用scikit-learn API開發和評估用於分類任務的動態分類器選擇模型。
如何探索動態分類器選擇模型超參數對分類準確性的影響。
動態分類器選擇
使用Scikit-Learn進行動態分類器選擇
具有整體本地精度(OLA)的DCS
具有本地分類精度(LCA)的DCS
DCS的超參數調整
在k最近鄰居中探索k
探索分類器池的算法
多個分類器系統是指機器學習算法的一個領域,該算法使用多個模型來解決分類預測建模問題。這包括熟悉的技術,如「一對多休息」,「一對多所有」和輸出糾錯碼技術。它還包括更通用的技術,這些技術選擇模型以動態地用於需要預測的每個新示例。Dynamic Ensemble Selection Library或簡稱為DESlib是一個開放原始碼Python庫(https://github.com/scikit-learn-contrib/DESlib),它提供了許多不同的動態分類器選擇算法的實現。DESlib是一個易於使用的集成學習庫,致力於實現動態分類器和集成選擇的最新技術。pip install deslib
安裝完成後,我們可以通過加載庫並列印已安裝的版本來確認該庫已正確安裝並可以使用。驗證截圖如下所示表示可以正常使用了:DESlib通過每種分類器選擇技術分別通過OLA和LCA類提供了DCS-LA算法的實現。每個類都可以直接用作scikit學習模型,從而可以直接使用全套scikit學習數據準備,建模管道和模型評估技術。這兩個類都使用k最近鄰居算法來選擇默認值k = 7的鄰居。儘管可以通過將「 pool_classifiers」設置為模型列表來更改,但決策樹的自舉聚合(裝袋)合奏用作針對每個分類考慮的分類器模型池。我們可以使用make_classification()函數創建具有10,000個示例和20個輸入功能的綜合二進位分類問題。其中,OLA和LCA解釋如下:
局部精度,通常稱為LA或整體本地精度(OLA)。
類精度,通常稱為CA或本地類精度(LCA)。
局部精度(OLA)涉及評估k個訓練示例附近的每個模型的分類精度。 然後選擇在該鄰域中表現最佳的模型以對新示例進行預測。
類精度(LCA)涉及使用每種模型對新示例進行預測,並記錄所預測的類。 然後,評估每個模型在k個訓練示例的鄰居上的準確性,並選擇對新示例所預測的類具有最佳技能的模型,並返回其預測。
# synthetic binary classification dataset
from sklearn.datasets import make_classification
# define dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# summarize the dataset
print(X.shape, y.shape)
(10000, 20) (10000,)
我們可以使用綜合數據集上的整體局部精度來評估DCS-LA模型。在這種情況下,我們將使用默認的模型超參數,包括袋裝決策樹作為分類器模型的池,並且在進行預測時將k = 7用於選擇局部鄰域。我們將使用具有三個重複和10折的重複分層k折交叉驗證來評估模型。我們將報告所有重複和摺疊中模型準確性的均值和標準差。# evaluate dynamic classifier selection DCS-LA with overall local accuracy
from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from deslib.dcs.ola import OLA
# define dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# define the model
model = OLA()
# define the evaluation procedure
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluate the model
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# report performance
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
注意:由於算法或評估程序的隨機性,或者數值精度的差異,您的結果可能會有所不同。考慮運行該示例幾次並比較平均結果。在這種情況下,我們可以看到帶有OLA和默認超參數的DCS-LA達到了約88.3%的分類精度。Mean Accuracy: 0.883 (0.012)
我們還可以將DCS-LA模型與OLA一起用作最終模型,並進行分類預測。首先,模型適合所有可用數據,然後可以調用predict()函數對新數據進行預測。下面的示例在我們的二進位分類數據集中展示了這一點。# make a prediction with DCS-LA using overall local accuracy
from sklearn.datasets import make_classification
from deslib.dcs.ola import OLA
# define dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# define the model
model = OLA()
# fit the model on the whole dataset
model.fit(X, y)
# make a single prediction
row = [0.2929949,-4.21223056,-1.288332,-2.17849815,-0.64527665,2.58097719,0.28422388,-7.1827928,-1.91211104,2.73729512,0.81395695,3.96973717,-2.66939799,3.34692332,4.19791821,0.99990998,-0.30201875,-4.43170633,-2.82646737,0.44916808]
yhat = model.predict([row])
print('Predicted Class: %d' % yhat[0])
運行示例使DCS-LA和OLA模型適合整個數據集,然後用於對新的數據行進行預測,就像在應用程式中使用模型時一樣。我們可以在合成數據集上使用局部類的準確性評估DCS-LA模型。在這種情況下,我們將使用默認的模型超參數,包括袋裝決策樹作為分類器模型的池,並且在進行預測時將k = 7用於選擇局部鄰域。我們將使用具有三個重複和10折的重複分層k折交叉驗證來評估模型。我們將報告所有重複和摺疊中模型準確性的均值和標準差。# evaluate dynamic classifier selection DCS-LA using local class accuracy
from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from deslib.dcs.lca import LCA
# define dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# define the model
model = LCA()
# define the evaluation procedure
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluate the model
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# report performance
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
注意:由於算法或評估程序的隨機性,或者數值精度的差異,您的結果可能會有所不同。考慮運行該示例幾次並比較平均結果。在這種情況下,我們可以看到帶有LCA和默認超參數的DCS-LA達到了約92.2%的分類精度。看到帶有LCA和默認超參數的DCS-LA達到了約92.2%的分類精度。
我們還可以將帶有LCA的DCS-LA模型用作最終模型,並進行分類預測。首先,模型適合所有可用數據,然後可以調用predict()函數對新數據進行預測。下面的示例在我們的二進位分類數據集中展示了這一點。# make a prediction with DCS-LA using local class accuracy
from sklearn.datasets import make_classification
from deslib.dcs.lca import LCA
# define dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# define the model
model = LCA()
# fit the model on the whole dataset
model.fit(X, y)
# make a single prediction
row = [0.2929949,-4.21223056,-1.288332,-2.17849815,-0.64527665,2.58097719,0.28422388,-7.1827928,-1.91211104,2.73729512,0.81395695,3.96973717,-2.66939799,3.34692332,4.19791821,0.99990998,-0.30201875,-4.43170633,-2.82646737,0.44916808]
yhat = model.predict([row])
print('Predicted Class: %d' % yhat[0])
運行示例可以使DCS-LA和LCA模型適合整個數據集,然後用於對新數據行進行預測,就像我們在應用程式中使用模型時一樣。在本節中,我們將仔細研究一些您應該考慮調整DCS-LA模型的超參數及其對模型性能的影響。對於DCS-LA,我們可以查看許多超參數,儘管在這種情況下,我們將查看模型局部評估中使用的k最近鄰居模型中的k值,以及如何使用自定義的分類器。我們將使用帶有OLA的DCS-LA作為這些實驗的基礎,儘管具體方法的選擇是任意的。k近鄰算法的配置對DCS-LA模型至關重要,因為它定義了考慮每個分類器進行選擇的鄰域範圍。k值控制鄰域的大小,重要的是將其設置為適合您的數據集的值,特別是特徵空間中樣本的密度。值太小將意味著可能會將訓練集中的相關示例從鄰域中排除,而值太大可能意味著信號被太多示例所衝走。以下示例探討了kS值為2到21的帶OLA的DCS-LA的分類精度。# explore k in knn for DCS-LA with overall local accuracy
from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from deslib.dcs.ola import OLA
from matplotlib import pyplot
# get the dataset
def get_dataset():
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
return X, y
# get a list of models to evaluate
def get_models():
models = dict()
for n in range(2,22):
models[str(n)] = OLA(k=n)
return models
# evaluate a give model using cross-validation
def evaluate_model(model):
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
return scores
# define dataset
X, y = get_dataset()
# get the models to evaluate
models = get_models()
# evaluate the models and store results
results, names = list(), list()
for name, model in models.items():
scores = evaluate_model(model)
results.append(scores)
names.append(name)
print('>%s %.3f (%.3f)' % (name, mean(scores), std(scores)))
# plot model performance for comparison
pyplot.boxplot(results, labels=names, showmeans=True)
pyplot.show()
首先運行示例將報告每個配置的鄰域大小的平均準確性。注意:由於算法或評估程序的隨機性,或者數值精度的差異,您的結果可能會有所不同。考慮運行該示例幾次並比較平均結果。在這種情況下,我們可以看到,精度隨著鄰域大小的增加而增加,可能達到k = 13或k = 14,在此看來它趨於平穩>2 0.873 (0.009)
>3 0.874 (0.013)
>4 0.880 (0.009)
>5 0.881 (0.009)
>6 0.883 (0.010)
>7 0.883 (0.011)
>8 0.884 (0.012)
>9 0.883 (0.010)
>10 0.886 (0.012)
>11 0.886 (0.011)
>12 0.885 (0.010)
>13 0.888 (0.010)
>14 0.886 (0.009)
>15 0.889 (0.010)
>16 0.885 (0.012)
>17 0.888 (0.009)
>18 0.886 (0.010)
>19 0.889 (0.012)
>20 0.889 (0.011)
>21 0.886 (0.011)
創建箱須圖以分配每個配置的鄰域大小的準確性得分。我們可以看到在達到平穩之前,模型性能和k值增加的總體趨勢選擇DCS-LA池中使用的算法是另一個重要的超參數。默認情況下,使用袋裝決策樹,因為事實證明它是處理一系列分類任務的有效方法。不過,可以考慮使用自定義分類器池。這需要首先定義分類器模型的列表,以使用每個分類器模型並將其擬合到訓練數據集上。不幸的是,這意味著在這種情況下不能使用scikit-learn中的自動k折交叉驗證模型評估方法。取而代之的是,我們將使用訓練測試拆分,以便我們可以在訓練數據集上手動擬合分類器池。然後,可以通過「 pool_classifiers」參數將適合分類器的列表指定給OLA(或LCA)類。在這種情況下,我們將使用一個包含邏輯回歸,決策樹和樸素貝葉斯分類器的池。下面列出了使用OLA和綜合數據集中的一組自定義分類器評估DCS-LA的完整示例。# evaluate DCS-LA using OLA with a custom pool of algorithms
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from deslib.dcs.ola import OLA
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# split the dataset into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=1)
# define classifiers to use in the pool
classifiers = [
LogisticRegression(),
DecisionTreeClassifier(),
GaussianNB()]
# fit each classifier on the training set
for c in classifiers:
c.fit(X_train, y_train)
# define the DCS-LA model
model = OLA(pool_classifiers=classifiers)
# fit the model
model.fit(X_train, y_train)
# make predictions on the test set
yhat = model.predict(X_test)
# evaluate predictions
score = accuracy_score(y_test, yhat)
print('Accuracy: %.3f' % (score))
首先運行示例將使用自定義分類器報告模型的平均準確性。注意:由於算法或評估程序的隨機性,或者數值精度的差異,您的結果可能會有所不同。考慮運行該示例幾次並比較平均結果。在這種情況下,我們可以看到該模型的準確度約為91.2%。為了採用DCS模型,其性能必須優於任何貢獻模型。否則,我們將僅使用性能更好的貢獻模型。我們可以通過評估測試集中每個貢獻分類器的性能來檢查這一點。...
# evaluate contributing models
for c in classifiers:
yhat = c.predict(X_test)
score = accuracy_score(y_test, yhat)
print('>%s: %.3f' % (c.__class__.__name__, score))
下面列出了帶有自定義分類器池的DCS-LA的更新示例,該分類器也進行了獨立評估。# evaluate DCS-LA using OLA with a custom pool of algorithms
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from deslib.dcs.ola import OLA
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
# define dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# split the dataset into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=1)
# define classifiers to use in the pool
classifiers = [
LogisticRegression(),
DecisionTreeClassifier(),
GaussianNB()]
# fit each classifier on the training set
for c in classifiers:
c.fit(X_train, y_train)
# define the DCS-LA model
model = OLA(pool_classifiers=classifiers)
# fit the model
model.fit(X_train, y_train)
# make predictions on the test set
yhat = model.predict(X_test)
# evaluate predictions
score = accuracy_score(y_test, yhat)
print('Accuracy: %.3f' % (score))
# evaluate contributing models
for c in classifiers:
yhat = c.predict(X_test)
score = accuracy_score(y_test, yhat)
print('>%s: %.3f' % (c.__class__.__name__, score))
首先運行示例,報告使用定製分類器池的模型的平均準確性以及每個貢獻模型的準確性。注意:由於算法或評估程序的隨機性,或者數值精度的差異,您的結果可能會有所不同。考慮運行該示例幾次並比較平均結果。在這種情況下,我們可以再次看到,DCS-LA的精度達到了約91.3%,比任何貢獻模型都要好。Accuracy: 0.913
>LogisticRegression: 0.878
>DecisionTreeClassifier: 0.884
>GaussianNB: 0.873