### Example 6 - Setup data for whitening demonstration Source: https://rasbt.github.io/mlxtend/user_guide/feature_extraction/PrincipalComponentAnalysis/ Imports and loads the wine dataset, splitting into training and test sets with stratification for the whitening example. ```python from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from mlxtend.data import wine_data X, y = wine_data() X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123, test_size=0.3, stratify=y) ``` -------------------------------- ### Example 5 - Setup data and train-test split Source: https://rasbt.github.io/mlxtend/user_guide/feature_extraction/PrincipalComponentAnalysis/ Imports required libraries and loads the wine dataset, splitting into training and test sets with stratification. ```python from sklearn.pipeline import make_pipeline from sklearn.neighbors import KNeighborsClassifier from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from mlxtend.data import wine_data X, y = wine_data() X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123, test_size=0.3, stratify=y) ``` -------------------------------- ### Example 1 - Scaling a Pandas DataFrame: Setup and initial arrays Source: https://rasbt.github.io/mlxtend/user_guide/preprocessing/shuffle_arrays_unison/ Initializes NumPy arrays X and y and prints them. This is the first part of the example, showing the data before shuffling. ```python import numpy as np from mlxtend.preprocessing import shuffle_arrays_unison X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) y = np.array([1, 2, 3]) print('X:\n%s' % X) print('y:\n%s' % y) ``` ```text X: [[1 2 3] [4 5 6] [7 8 9]] y: [1 2 3] ``` -------------------------------- ### Setup for zoom factor examples Source: https://rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/ Loads the Iris dataset and trains an SVM classifier, which is used in the subsequent zoom factor examples. The output shows the fitted SVC object. ```python from mlxtend.plotting import plot_decision_regions import matplotlib.pyplot as plt from sklearn import datasets from sklearn.svm import SVC # Loading some example data iris = datasets.load_iris() X = iris.data[:, [0, 2]] y = iris.target # Training a classifier svm = SVC(C=0.5, kernel='linear') svm.fit(X, y) ``` ```text SVC(C=0.5, kernel='linear') ``` -------------------------------- ### Install mlxtend from source distribution with uv Source: https://rasbt.github.io/mlxtend/installation/ Use this command if you encounter problems with the default binary distribution from PyPI. It installs mlxtend from the source distribution. ```bash uv add --no-binary-package mlxtend mlxtend ``` -------------------------------- ### Initialize dataset for GridSearch example Source: https://rasbt.github.io/mlxtend/user_guide/feature_selection/SequentialFeatureSelector/ Sets up the Iris dataset for Example 8, splitting into training and test sets with test_size=0.2 and random_state=123. ```python # Initialize the dataset from sklearn.neighbors import KNeighborsClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=123) ``` -------------------------------- ### Install latest development version of mlxtend from GitHub Source: https://rasbt.github.io/mlxtend/installation/ Installs the latest development version of mlxtend directly from the GitHub repository. ```bash uv add "mlxtend @ git+https://github.com/rasbt/mlxtend.git" ``` -------------------------------- ### Example git log output Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Example output of 'git log' showing commits from newest to oldest. ```bash commit 046e3af8a9127df8eac879454f029937c8a31c41 Author: rasbt Date: Tue Nov 24 03:46:37 2015 -0500 fixed setup.py commit c3c00f6ba0e8f48bbe1c9081b8ae3817e57ecc5c Author: rasbt Date: Tue Nov 24 03:04:39 2015 -0500 documented feature x commit d87934fe8726c46f0b166d6290a3bf38915d6e75 Author: rasbt Date: Tue Nov 24 02:44:45 2015 -0500 added support for feature x ``` -------------------------------- ### Install wheel and source distribution in isolated environments Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Installs the distribution files in isolated uv environments to verify they work. The filenames change with each version. ```bash $ uv run --isolated --with ./dist/mlxtend-*.tar.gz python -c 'import mlxtend; print(mlxtend.__version__)' $ uv run --isolated --with ./dist/mlxtend-*-py3-none-any.whl python -c 'import mlxtend; print(mlxtend.__version__)' ``` -------------------------------- ### Import libraries and set up DummyClassifier for GroupTimeSeriesSplit examples Source: https://rasbt.github.io/mlxtend/user_guide/evaluate/GroupTimeSeriesSplit/ Imports required libraries including numpy, pandas, sklearn's DummyClassifier and cross_val_score, and mlxtend's GroupTimeSeriesSplit, plot_splits, print_cv_info, and print_split_info. This setup is used in the following examples. ```python import numpy as np import pandas as pd from sklearn.dummy import DummyClassifier from sklearn.model_selection import cross_val_score from mlxtend.evaluate.time_series import ( GroupTimeSeriesSplit, plot_splits, print_cv_info, print_split_info, ) ``` -------------------------------- ### Install from PyPI test server and check import Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Installs mlxtend from the PyPI test server and checks that it imports, using the test index and the real PyPI as fallback. ```bash $ uv run --isolated --default-index https://test.pypi.org/simple/ --index https://pypi.org/simple --with mlxtend python -c 'import mlxtend; print(mlxtend.__version__)' ``` -------------------------------- ### Example output of git fetch upstream Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Example output showing the fetch progress and the new branch upstream/master. ```bash remote: Counting objects: xx, done. remote: Compressing objects: 100% (xx/xx), done. remote: Total xx (delta xx), reused xx (delta x) Unpacking objects: 100% (xx/xx), done. From https://github.com/rasbt/mlxtend * [new branch] master -> upstream/master ``` -------------------------------- ### Start interactive rebase to squash commits Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Start an interactive rebase for the last 3 commits to squash them into one. ```bash $ git rebase -i HEAD~3 ``` -------------------------------- ### Example output of git remote -v Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Example output showing the origin remote pointing to your fork. This is the expected output before adding the upstream remote. ```bash origin https://github.com//mlxtend.git (fetch) origin https://github.com//mlxtend.git (push) ``` -------------------------------- ### Serve documentation locally with mkdocs serve Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Run from the mlxtend/docs directory to preview the documentation locally. The command is executed from the docs directory. ```bash ~/github/mlxtend/docs$ mkdocs serve ``` -------------------------------- ### Setup for ROC curve with StackingCVClassifier Source: https://rasbt.github.io/mlxtend/user_guide/classifier/StackingClassifier/ Loads iris data, binarizes the target, splits into train/test, and initializes base classifiers and a StackingClassifier wrapped in OneVsRestClassifier. This setup is used for both predict_proba and decision_function ROC examples. ```python from sklearn import model_selection from sklearn.linear_model import LogisticRegression from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.ensemble import RandomForestClassifier from mlxtend.classifier import StackingCVClassifier from sklearn.metrics import roc_curve, auc import numpy as np from sklearn.model_selection import train_test_split from sklearn import datasets from sklearn.preprocessing import label_binarize from sklearn.multiclass import OneVsRestClassifier iris = datasets.load_iris() X, y = iris.data[:, [0, 1]], iris.target # Binarize the output y = label_binarize(y, classes=[0, 1, 2]) n_classes = y.shape[1] RANDOM_SEED = 42 X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=RANDOM_SEED) clf1 = LogisticRegression() clf2 = RandomForestClassifier(random_state=RANDOM_SEED) clf3 = SVC(random_state=RANDOM_SEED) lr = LogisticRegression() sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], meta_classifier=lr) # Learn to predict each class against the other classifier = OneVsRestClassifier(sclf) ``` -------------------------------- ### Run mlxtend from a local checkout Source: https://rasbt.github.io/mlxtend/installation/ Clone your fork of the mlxtend repository, set up the development environment, and run mlxtend from your local checkout. ```bash git clone https://github.com//mlxtend.git cd mlxtend uv sync --group dev uv run python -c "import mlxtend; print(mlxtend.__version__)" ``` -------------------------------- ### Example 5 - Build and evaluate PCA pipeline Source: https://rasbt.github.io/mlxtend/user_guide/feature_extraction/PrincipalComponentAnalysis/ Creates a pipeline with StandardScaler, PrincipalComponentAnalysis (n_components=3), and KNeighborsClassifier (n_neighbors=5), then fits and evaluates on training and test sets. ```python pipe_pca = make_pipeline(StandardScaler(), PrincipalComponentAnalysis(n_components=3), KNeighborsClassifier(n_neighbors=5)) pipe_pca.fit(X_train, y_train) print('Transf. training accyracy: %.2f%%' % (pipe_pca.score(X_train, y_train)*100)) print('Transf. test accyracy: %.2f%%' % (pipe_pca.score(X_test, y_test)*100)) ``` ```text Transf. training accyracy: 96.77% Transf. test accyracy: 96.30% ``` -------------------------------- ### Custom legend labels with plot_decision_regions Source: https://rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/ Shows how to set custom legend labels by getting the handles and labels from the axis object returned by plot_decision_regions and passing them to ax.legend. The first block shows the core pattern; the second block is a complete example using the Iris dataset and an SVM classifier. ```python ax = plot_decision_regions(X, y, clf=svm, legend=0) handles, labels = ax.get_legend_handles_labels() ax.legend(handles, ['class 0', 'class 1', 'class 2'], framealpha=0.3, scatterpoints=1) ``` ```python from mlxtend.plotting import plot_decision_regions import matplotlib.pyplot as plt from sklearn import datasets from sklearn.svm import SVC # Loading some example data iris = datasets.load_iris() X = iris.data[:, [0, 2]] y = iris.target # Training a classifier svm = SVC(C=0.5, kernel='linear') svm.fit(X, y) # Plotting decision regions ax = plot_decision_regions(X, y, clf=svm, legend=0) # Adding axes annotations plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.title('SVM on Iris') handles, labels = ax.get_legend_handles_labels() ax.legend(handles, ['class square', 'class triangle', 'class circle'], framealpha=0.3, scatterpoints=1) plt.show() ``` -------------------------------- ### API example for tokenizer_emoticons Source: https://rasbt.github.io/mlxtend/user_guide/text/tokenizer/ API documentation example showing the usage of tokenizer_emoticons. The output is a list of emoticons. ```python >>> tokenizer_emoticons('This :) is :( a test :-)!') [':)', ':(', ':-)'] ``` -------------------------------- ### Build source distribution and wheel with uv build Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Creates the source distribution and wheel in the ./dist directory. ```bash $ uv build ``` -------------------------------- ### Example output of git merge upstream/master Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Example output showing a fast-forward merge with changed files. ```bash Updating xxx...xxx Fast-forward SOME FILE1 | 12 +++++++ SOME FILE2 | 10 +++++++ 2 files changed, 22 insertions(+), ``` -------------------------------- ### API example for tokenizer_words_and_emoticons Source: https://rasbt.github.io/mlxtend/user_guide/text/tokenizer/ API documentation example showing the usage of tokenizer_words_and_emoticons. The output is a list of lowercase words and emoticons. ```python >>> tokenizer_words_and_emoticons('This :) is :( a test :-)!') ['this', 'is', 'a', 'test', ':)', ':(', ':-)'] ``` -------------------------------- ### Bootstrap example using mlxtend.evaluate.bootstrap Source: https://rasbt.github.io/mlxtend/api_subpackages/mlxtend.evaluate/ Shows how to compute the bootstrap statistic, standard error, and 95% confidence interval for the mean of a random sample. Requires numpy and mlxtend.evaluate.bootstrap. The output shows the mean, standard error, and confidence interval. ```python >>> from mlxtend.evaluate import bootstrap >>> rng = np.random.RandomState(123) >>> x = rng.normal(loc=5., size=100) >>> original, std_err, ci_bounds = bootstrap(x, ... num_rounds=1000, ... func=np.mean, ... ci=0.95, ... seed=123) >>> print('Mean: %.2f, SE: +/- %.2f, CI95: [%.2f, %.2f]' % (original, ... std_err, ... ci_bounds[0], ... ci_bounds[1])) Mean: 5.03, SE: +/- 0.11, CI95: [4.80, 5.26] >>> ``` -------------------------------- ### Build static HTML files with mkdocs build --clean Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Builds the static HTML files of the mlxtend documentation after checking locally. ```bash ~/github/mlxtend/docs$ mkdocs build --clean ``` -------------------------------- ### tokenizer_emoticons usage example Source: https://rasbt.github.io/mlxtend/api_subpackages/mlxtend.text/ Shows how to extract emoticons from a text string using tokenizer_emoticons. The example returns a list of emoticons found in the input text. ```python >>> tokenizer_emoticons('This :) is :( a test :-)!') [':)', ':(', ':-)'] ``` -------------------------------- ### Build API documentation with make_api.py Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Run from the mlxtend/docs directory to generate API documentation into the sources/api_modules and sources/api_subpackes directories. ```python ~/github/mlxtend/docs$ python make_api.py ``` -------------------------------- ### List current remote repositories Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Run this command to see the configured remote repositories for your fork. The output shows the origin remote pointing to your fork. ```bash $ git remote -v ``` -------------------------------- ### HTTPError 403 Forbidden example Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Example error message that may appear if twine is outdated. Ensure twine is up to date in the uv development environment. ```text HTTPError: 403 Forbidden from https://upload.pypi.org/legacy/ ``` -------------------------------- ### Set up StackingCVRegressor and GridSearchCV Source: https://rasbt.github.io/mlxtend/user_guide/regressor/StackingCVRegressor/ Imports necessary libraries, loads the Boston dataset, creates base regressors (Lasso, Ridge) and a meta regressor (RandomForestRegressor), and sets up a StackingCVRegressor with use_features_in_secondary=True. Defines a parameter grid for GridSearchCV and fits the grid search to the data. ```python from mlxtend.regressor import StackingCVRegressor from sklearn.datasets import load_boston from sklearn.linear_model import Lasso from sklearn.linear_model import Ridge from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import GridSearchCV X, y = load_boston(return_X_y=True) ridge = Ridge(random_state=RANDOM_SEED) lasso = Lasso(random_state=RANDOM_SEED) rf = RandomForestRegressor(random_state=RANDOM_SEED) stack = StackingCVRegressor(regressors=(lasso, ridge), meta_regressor=rf, random_state=RANDOM_SEED, use_features_in_secondary=True) params = {'lasso__alpha': [0.1, 1.0, 10.0], 'ridge__alpha': [0.1, 1.0, 10.0]} grid = GridSearchCV( estimator=stack, param_grid={ 'lasso__alpha': [x/5.0 for x in range(1, 10)], 'ridge__alpha': [x/20.0 for x in range(1, 10)], 'meta_regressor__n_estimators': [10, 100] }, cv=5, refit=True ) grid.fit(X, y) print("Best: %f using %s" % (grid.best_score_, grid.best_params_)) ``` -------------------------------- ### Fit Kmeans and print convergence info Source: https://rasbt.github.io/mlxtend/user_guide/cluster/Kmeans/ Initializes Kmeans with k=3, max_iter=50, random_seed=1, and print_progress=3, fits the model, and prints the number of iterations and final centroids. The output shows convergence after 2 iterations. ```python from mlxtend.cluster import Kmeans km = Kmeans(k=3, max_iter=50, random_seed=1, print_progress=3) km.fit(X) print('Iterations until convergence:', km.iterations_) print('Final centroids:\n', km.centroids_) ``` ```text Iteration: 2/50 | Elapsed: 00:00:00 | ETA: 00:00:00 Iterations until convergence: 2 Final centroids: [[-1.5947298 2.92236966] [ 2.06521743 0.96137409] [ 0.9329651 4.35420713]] ``` -------------------------------- ### Example 1: Standard Accuracy Source: https://rasbt.github.io/mlxtend/user_guide/evaluate/accuracy_score/ Computes the overall accuracy as the fraction of correct predictions over all samples. The example uses a 3-class dataset and prints the accuracy score. ```python import numpy as np from mlxtend.evaluate import accuracy_score y_targ = [0, 0, 0, 1, 1, 1, 2, 2, 2] y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 2] accuracy_score(y_targ, y_pred) ``` ```text 0.5555555555555556 ``` -------------------------------- ### Create uv environment with development tools Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ From the mlxtend repository root, creates a uv environment with Python 3.11 and the dev group. ```bash $ uv sync --group dev --python 3.11 ``` -------------------------------- ### Generate PDF of documentation with md2pdf.py Source: https://rasbt.github.io/mlxtend/CONTRIBUTING/ Run from the mlxtend/docs directory to generate a PDF version of the documentation. ```bash python md2pdf.py ``` -------------------------------- ### tokenizer_words_and_emoticons usage example Source: https://rasbt.github.io/mlxtend/api_subpackages/mlxtend.text/ Shows how to convert text to lowercase words and emoticons using tokenizer_words_and_emoticons. The example returns a list of words and emoticons from the input text. ```python >>> tokenizer_words_and_emoticons('This :) is :( a test :-)!') ['this', 'is', 'a', 'test', ':)', ':(', ':-)'] ```