Machine Learning Library Extensions (mlxtend)

0.24.0 · active · verified Sun Apr 12

mlxtend is a Python library of useful tools for machine learning tasks, offering a diverse range of functionality including feature selection, frequent pattern mining, model stacking, and plotting utilities. It builds upon popular libraries like scikit-learn, NumPy, and pandas. The current version is 0.24.0, and it maintains an active release cadence, frequently pushing updates for bug fixes and compatibility with its core dependencies.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `StackingClassifier` to combine multiple base models (Decision Tree, Logistic Regression) with a meta-classifier (Logistic Regression) to improve prediction accuracy. It uses a synthetic dataset from scikit-learn for illustration.

import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from mlxtend.classifier import StackingClassifier

# Generate a synthetic dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=10, n_redundant=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize base classifiers
clf1 = DecisionTreeClassifier(random_state=42)
clf2 = LogisticRegression(random_state=42, solver='liblinear')

# Initialize meta-classifier
lr = LogisticRegression(random_state=42, solver='liblinear')

# Initialize StackingClassifier
sclf = StackingClassifier(classifiers=[clf1, clf2], meta_classifier=lr, use_probas=True, verbose=0)

# Train and evaluate
sclf.fit(X_train, y_train)
score = sclf.score(X_test, y_test)
print(f"Stacking Classifier Test Accuracy: {score:.4f}")

view raw JSON →