Sklearn--整合學習(1)
阿新 • • 發佈:2018-12-19
整合學習的目的是同個幾個若分類器以提升或者並行投票的方式得到一個強的整合分類器。
一、簡單整合
可以用 moons 資料集製造資料對這個資料分別用Logistic
、decisionTree
和 svm
進行分類, 再用VotingClassifier
整合這幾個分類器
用 make_moons
生成隨機資料後:
def Vot_clf(voting_type):
log_clf = LogisticRegression()
tree_clf = DecisionTreeClassifier(random_state = 42,max_depth = 3)
svm_clf = SVC(kernel = 'linear' ,probability = True)
return VotingClassifier([('lr', log_clf),
('tree', tree_clf),
('svc', svm_clf)],
voting = voting_type)
如果所有分類器都能預測類別的概率,那就可以讓sklearn
以最高的類概率來預測這個類, 平均在所有的分類器上,該方法為軟投票。
這種方法,經常會比硬投票表現的更好,因為他給與搞自信的投票權權重更大。
由於SVC
probability = True
。這樣會使得SVC
使用交叉驗證去預測類別概率,降低訓練速度,但會新增
predict_proba()方法
比較弱分類器和整合分類器的效果
def Acc_of_clf(x, y, x_t, y_t, voting_type):
log_clf = LogisticRegression()
tree_clf = DecisionTreeClassifier(random_state = 42,max_depth = 3)
svm_clf = SVC(kernel = 'linear' , probability = True) # 用以軟投票
vot_clf = Vot_clf(voting_type)
for clf in (log_clf, tree_clf, svm_clf, vot_clf):
clf.fit(x, y)
print(clf.__class__.__name__, "準確率%.4f" %(clf.score(x_t, y_t)))
def plot_moom(m_x, m_y):
x_1 = [ m_x[i][0] for i in range(len(m_x))]
x_2 = [ m_x[i][1] for i in range(len(m_x))]
plt.scatter(x_1, x_2, c = m_y)
if __name__ == '__main__' :
x, y, x_t, y_t = get_moons_data()
Acc_of_clf(x, y, x_t, y_t,'soft')
plot_moom(x, y)
plt.show()
"""
hard:
LogisticRegression 準確率0.8700
DecisionTreeClassifier 準確率0.8600
SVC 準確率0.8700
VotingClassifier 準確率0.8700
soft:
LogisticRegression 準確率0.8700
DecisionTreeClassifier 準確率0.8600
SVC 準確率0.8700
VotingClassifier 準確率0.8800
"""
由上述的結果中可以看出整合分類器的效果更佳
二、Bagging
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
if __name__ == '__main__' :
x, y, x_t, y_t = get_moons_data()
dec_clf = DecisionTreeClassifier(random_state = 42,max_depth = 3)
bag_clf = BaggingClassifier(dec_clf, #大小為[n_samples,n_features]
n_estimators=500, ## 500個基分類器
max_samples=100, ##
bootstrap=True, ## Fasle 即為Pasting
n_jobs=-1)
bag_clf.fit(x, y)
y_prd = bag_clf.predict(x_t)
plt.subplot(121)
plot_moom(x_t,y_t); plt.title("The real Class")
plt.subplot(122)
plot_moom(x_t, y_prd); plt.title("The predicted Class")
plt.show()
從圈起來的地方還是可以看出Bagging的分類具有一定優勢。
對於Bagging 來說,一些例項可能會被一些分類器重複取樣,但是其他的可能不被採用。演算法預設為(boostrap = True)
即有放回的取樣m個例項,其中m是訓練集的大小,這就說明僅僅63%的樣本進入了分類器,剩餘的37%未被採用,這些例項叫做Out of Bag
(每個分類器的37%均不同),因此可以拿分類器的oob
來評估整合本身。
if __name__ == '__main__' :
x, y, x_t, y_t = get_moons_data()
dec_clf = DecisionTreeClassifier(random_state = 42,max_depth = 3)
bag_clf = BaggingClassifier(dec_clf, #大小為[n_samples,n_features]
n_estimators=500, ## 500個基分類器
max_samples=100, ##
bootstrap=True, ## Fasle 即為Pasting
n_jobs=-1, oob_score=True)
bag_clf.fit(x, y)
print('Out-of-Bag 分值為:%.4f'% bag_clf.oob_score_)
##預測
y_prd = bag_clf.predict(x_t)
print('模型準確率為:%.4f' % accuracy_score(y_t, y_prd))
Out-of-Bag 分值為:0.8875
模型準確率為:0.8600
隨機切片與子空間
BaggingClassifier
支援取樣例項和特徵——隨機切片
max_sample
和bootstrap
是針對樣本max_features
和bootstrap_features
是針對特徵
當處理的樣本維度較多的時候 特徵的取樣就較為重要了。
當保留所有的訓練樣本(比如bootstrap = True
和 max_sample = 1.0
), 但切片特徵(booststap_features = True
並且/或者 max_features 小於 1.0
)的時候——隨機子空間。
採集特徵導致更多的預測多樣性, 用高偏差換低方差。