Ensemble Methods ใน Python
Román de las Heras
Data Scientist, Appodeal
คุณสมบัติของการใช้งาน stacking จาก scikit-learn:
scikit-learn มี stacking estimators ให้ใช้งาน (ตั้งแต่เวอร์ชัน 0.22)scikit-learn ได้
ขั้นตอนทั่วไปในการใช้งาน:
from sklearn.ensemble import StackingClassifier
# Instantiate the 1st-layer classifiers
classifiers = [
('clf1', Classifier1(params1)),
('clf2', Classifier2(params2)),
...
('clfN', ClassifierN(paramsN))
]
# Instantiate the 2nd-layer classifier
clf_meta = ClassifierMeta(paramsMeta)
# Build the Stacking classifier
clf_stack = StackingClassifier(
estimators=classifiers,
final_estimator=clf_meta,
cv=5,
stack_method='predict_proba',
passthrough=False)
# Use the fit and predict methods
clf_stack.fit(X_train, y_train)
pred = clf_stack.predict(X_test)
from sklearn.ensemble import StackingRegressor
# Instantiate the 1st-layer regressors
regressors = [
('reg1', Regressor1(params1)),
('reg2', Regressor2(params2)),
...
('regN', RegressorN(paramsN))
]
# Instantiate the 2nd-layer regressor
reg_meta = RegressorMeta(paramsMeta)
# Build the Stacking regressor
reg_stack = StackingRegressor(
estimators=regressors,
final_estimator=reg_meta,
cv=5,
passthrough=False)
# Use the fit and predict methods
reg_stack.fit(X_train, y_train)
pred = reg_stack.predict(X_test)
Ensemble Methods ใน Python