Mlxtend: Making a meta-classifier in a Loop
Today, using the Python package Mlxtend, I stacked the “best” classification tree, bagged classification tree, XGBoost, SVM, logistic regression, linear discriminant analysis, and k-nearest-neighbor classifiers from 10,000 runs (each) of Hyperopt parameter tuning. I created a meta-classifier using the best of each model type using logistic regression. Since each of my model types has its own tuned Pipeline, I found out that mlxtend works just fine with sklearn pipelines. The syntax looks like this:
from mlxtend.classifier import StackingClassifier from sklearn.linear_model import LogisticRegression classifiers_for_meta = [] ... # I used append so this process could be done in a loop classifiers_for_meta.append(best_pipeline_from_loop) ... stacked_classifier = StackingClassifier(classifiers=classifiers_for_meta, use_probas=True, average_probas=False, meta_classifier=lr)
Then, the stacked_classifier object has fit() and predict() methods just like a scikit-learn model object. Nice!