From 7e24ddd1b657e6ea718ab77f977e5befa8f1265f Mon Sep 17 00:00:00 2001
From: Mariam Zakaria <123750992+mariam851@users.noreply.github.com>
Date: Sun, 18 Jan 2026 22:04:18 +0200
Subject: [PATCH] Fix #717: Corrected PCA sign flip and updated test
compatibility for Python 3.12
---
docs/ipynb2markdown.py | 3 -
docs/make_api.py | 6 +-
.../user_guide/classifier/Adaline.ipynb | 103 ++--
.../classifier/EnsembleVoteClassifier.ipynb | 209 ++++----
.../classifier/LogisticRegression.ipynb | 102 ++--
.../classifier/MultiLayerPerceptron.ipynb | 119 ++---
.../classifier/OneRClassifier.ipynb | 14 +-
.../user_guide/classifier/Perceptron.ipynb | 25 +-
.../classifier/SoftmaxRegression.ipynb | 89 ++--
.../classifier/StackingCVClassifier.ipynb | 208 ++++----
.../classifier/StackingClassifier.ipynb | 218 ++++----
docs/sources/user_guide/cluster/Kmeans.ipynb | 72 +--
.../user_guide/data/autompg_data.ipynb | 23 +-
.../user_guide/data/boston_housing_data.ipynb | 7 +-
docs/sources/user_guide/data/iris_data.ipynb | 15 +-
.../user_guide/data/loadlocal_mnist.ipynb | 28 +-
.../data/make_multiplexer_dataset.ipynb | 21 +-
docs/sources/user_guide/data/mnist_data.ipynb | 24 +-
.../user_guide/data/three_blobs_data.ipynb | 43 +-
docs/sources/user_guide/data/wine_data.ipynb | 35 +-
.../evaluate/BootstrapOutOfBag.ipynb | 31 +-
.../evaluate/GroupTimeSeriesSplit.ipynb | 2 +-
.../evaluate/PredefinedHoldoutSplit.ipynb | 14 +-
.../evaluate/RandomHoldoutSplit.ipynb | 14 +-
.../user_guide/evaluate/accuracy_score.ipynb | 21 +-
.../evaluate/bias_variance_decomp.ipynb | 116 ++---
.../user_guide/evaluate/bootstrap.ipynb | 38 +-
.../evaluate/bootstrap_point632_score.ipynb | 20 +-
.../user_guide/evaluate/cochrans_q.ipynb | 474 ++++++++++++++++--
.../evaluate/combined_ftest_5x2cv.ipynb | 36 +-
.../evaluate/confusion_matrix.ipynb | 24 +-
.../evaluate/create_counterfactual.ipynb | 132 ++---
.../feature_importance_permutation.ipynb | 188 ++++---
docs/sources/user_guide/evaluate/ftest.ipynb | 457 +++++++++++++++--
.../user_guide/evaluate/lift_score.ipynb | 19 +-
.../sources/user_guide/evaluate/mcnemar.ipynb | 22 +-
.../user_guide/evaluate/mcnemar_table.ipynb | 20 +-
.../user_guide/evaluate/mcnemar_tables.ipynb | 27 +-
.../evaluate/paired_ttest_5x2cv.ipynb | 34 +-
.../evaluate/paired_ttest_kfold_cv.ipynb | 34 +-
.../evaluate/paired_ttest_resampled.ipynb | 34 +-
.../evaluate/permutation_test.ipynb | 74 ++-
.../evaluate/proportion_difference.ipynb | 344 ++++++++++++-
.../sources/user_guide/evaluate/scoring.ipynb | 6 +-
.../LinearDiscriminantAnalysis.ipynb | 42 +-
.../PrincipalComponentAnalysis.ipynb | 142 +++---
.../feature_extraction/RBFKernelPCA.ipynb | 129 ++---
.../feature_selection/ColumnSelector.ipynb | 45 +-
.../ExhaustiveFeatureSelector.ipynb | 237 +++++----
.../SequentialFeatureSelector.ipynb | 325 ++++++------
.../user_guide/file_io/find_filegroups.ipynb | 14 +-
.../user_guide/file_io/find_files.ipynb | 4 +-
.../frequent_patterns/apriori.ipynb | 23 +-
.../frequent_patterns/association_rules.ipynb | 125 +++--
.../frequent_patterns/fpgrowth.ipynb | 18 +-
.../user_guide/frequent_patterns/fpmax.ipynb | 16 +-
.../user_guide/frequent_patterns/hmine.ipynb | 18 +-
.../user_guide/math/num_combinations.ipynb | 16 +-
.../user_guide/math/num_permutations.ipynb | 16 +-
.../math/vectorspace_dimensionality.ipynb | 16 +-
.../math/vectorspace_orthonormalization.ipynb | 17 +-
.../plotting/category_scatter.ipynb | 14 +-
.../plotting/checkerboard_plot.ipynb | 18 +-
docs/sources/user_guide/plotting/ecdf.ipynb | 16 +-
.../user_guide/plotting/enrichment_plot.ipynb | 7 +-
.../sources/user_guide/plotting/heatmap.ipynb | 40 +-
.../plotting/plot_confusion_matrix.ipynb | 103 ++--
.../plotting/plot_decision_regions.ipynb | 243 +++++----
.../plotting/plot_learning_curves.ipynb | 3 +-
.../plotting/plot_linear_regression.ipynb | 72 ++-
.../plot_sequential_feature_selection.ipynb | 17 +-
.../user_guide/plotting/scatter_hist.ipynb | 13 +-
.../plotting/scatterplotmatrix.ipynb | 16 +-
.../user_guide/plotting/stacked_barplot.ipynb | 11 +-
.../preprocessing/CopyTransformer.ipynb | 32 +-
.../preprocessing/DenseTransformer.ipynb | 32 +-
.../preprocessing/MeanCenterer.ipynb | 8 +-
.../preprocessing/TransactionEncoder.ipynb | 20 +-
.../preprocessing/minmax_scaling.ipynb | 11 +-
.../preprocessing/one-hot_encoding.ipynb | 4 +-
.../preprocessing/shuffle_arrays_unison.ipynb | 11 +-
.../preprocessing/standardize.ipynb | 18 +-
.../regressor/LinearRegression.ipynb | 146 +++---
.../regressor/StackingCVRegressor.ipynb | 90 ++--
.../regressor/StackingRegressor.ipynb | 80 ++-
.../user_guide/text/generalize_names.ipynb | 10 +-
.../text/generalize_names_duplcheck.ipynb | 10 +-
docs/sources/user_guide/text/tokenizer.ipynb | 14 +-
docs/sources/user_guide/utils/Counter.ipynb | 2 +-
mlxtend/_base/tests/test_classifier.py | 12 +-
mlxtend/_base/tests/test_cluster.py | 12 +-
mlxtend/classifier/ensemble_vote.py | 10 +-
mlxtend/classifier/oner.py | 2 +-
mlxtend/classifier/stacking_classification.py | 4 +-
.../tests/test_stacking_classifier.py | 13 +-
mlxtend/data/multiplexer.py | 4 +-
mlxtend/evaluate/accuracy.py | 2 +-
mlxtend/evaluate/bias_variance_decomp.py | 2 +-
mlxtend/evaluate/bootstrap_point632.py | 8 +-
mlxtend/evaluate/cochrans_q.py | 4 +-
mlxtend/evaluate/confusion_matrix.py | 2 +-
mlxtend/evaluate/f_test.py | 6 +-
mlxtend/evaluate/lift_score.py | 4 +-
mlxtend/evaluate/mcnemar.py | 8 +-
mlxtend/evaluate/permutation.py | 6 +-
mlxtend/evaluate/scoring.py | 6 +-
mlxtend/evaluate/tests/test_cochran_q.py | 4 +-
mlxtend/evaluate/tests/test_f_test.py | 4 +-
mlxtend/evaluate/tests/test_mcnemar_table.py | 4 +-
mlxtend/evaluate/tests/test_mcnemar_tables.py | 4 +-
.../tests/test_paired_ttest_resampled.py | 2 +-
mlxtend/evaluate/tests/test_permutation.py | 2 +-
mlxtend/evaluate/ttest.py | 4 +-
mlxtend/externals/adjust_text.py | 4 +-
mlxtend/externals/pyprind/prog_class.py | 11 +-
mlxtend/externals/signature_py27.py | 8 +-
.../exhaustive_feature_selector.py | 4 +-
.../sequential_feature_selector.py | 6 +-
.../tests/test_exhaustive_feature_selector.py | 8 +-
.../tests/test_sequential_feature_selector.py | 10 +-
...uential_feature_selector_feature_groups.py | 7 +-
.../frequent_patterns/association_rules.py | 45 +-
mlxtend/frequent_patterns/fpcommon.py | 1 -
.../frequent_patterns/tests/test_fpbase.py | 1 -
mlxtend/plotting/decision_regions.py | 9 +-
mlxtend/plotting/pca_correlation_graph.py | 17 +-
mlxtend/plotting/plot_confusion_matrix.py | 2 +-
mlxtend/plotting/scatter_hist.py | 2 -
.../plotting/tests/test_decision_regions.py | 14 +-
mlxtend/plotting/tests/test_pca_corr_graph.py | 2 +-
mlxtend/preprocessing/scaling.py | 4 +-
.../tests/test_copy_transformer.py | 2 +-
mlxtend/utils/checking.py | 2 +-
mlxtend/utils/tests/test_checking_inputs.py | 8 +-
134 files changed, 3647 insertions(+), 2458 deletions(-)
diff --git a/docs/ipynb2markdown.py b/docs/ipynb2markdown.py
index 51a89acd5..b06b1c02a 100644
--- a/docs/ipynb2markdown.py
+++ b/docs/ipynb2markdown.py
@@ -9,13 +9,10 @@
import glob
import os
-import shutil
import subprocess
-import markdown
from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor
-from nbconvert.exporters import MarkdownExporter
class ImgExtractor(Treeprocessor):
diff --git a/docs/make_api.py b/docs/make_api.py
index 84affeb43..b6674e350 100644
--- a/docs/make_api.py
+++ b/docs/make_api.py
@@ -416,13 +416,13 @@ def summarize_methdods_and_functions(
"-o1",
"--output_module_api",
default="../docs/sources/api_modules",
- help=("Target directory for the module-level" " API Markdown files"),
+ help=("Target directory for the module-level API Markdown files"),
)
parser.add_argument(
"-o2",
"--output_subpackage_api",
default="../docs/sources/api_subpackages",
- help=("Target directory for the" " subpackage-level API Markdown files"),
+ help=("Target directory for the subpackage-level API Markdown files"),
)
parser.add_argument(
"-c", "--clean", action="store_true", help="Remove previous API files"
@@ -459,5 +459,5 @@ def summarize_methdods_and_functions(
out_dir=args.output_subpackage_api,
printlog=not (args.silent),
clean=args.clean,
- str_above_header=("mlxtend" " version: %s \n" % (package.__version__)),
+ str_above_header=("mlxtend version: %s \n" % (package.__version__)),
)
diff --git a/docs/sources/user_guide/classifier/Adaline.ipynb b/docs/sources/user_guide/classifier/Adaline.ipynb
index 6ad2499fa..4ef87dbed 100644
--- a/docs/sources/user_guide/classifier/Adaline.ipynb
+++ b/docs/sources/user_guide/classifier/Adaline.ipynb
@@ -178,22 +178,19 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
"\n",
"\n",
- "ada = Adaline(epochs=30, \n",
- " eta=0.01, \n",
- " minibatches=None, \n",
- " random_seed=1)\n",
+ "ada = Adaline(epochs=30, eta=0.01, minibatches=None, random_seed=1)\n",
"ada.fit(X, y)\n",
"plot_decision_regions(X, y, clf=ada)\n",
- "plt.title('Adaline - Closed Form')\n",
+ "plt.title(\"Adaline - Closed Form\")\n",
"\n",
"plt.show()"
]
@@ -261,29 +258,31 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
"\n",
"\n",
- "ada = Adaline(epochs=30, \n",
- " eta=0.01, \n",
- " minibatches=1, # for Gradient Descent Learning\n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "ada = Adaline(\n",
+ " epochs=30,\n",
+ " eta=0.01,\n",
+ " minibatches=1, # for Gradient Descent Learning\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"\n",
"ada.fit(X, y)\n",
"plot_decision_regions(X, y, clf=ada)\n",
- "plt.title('Adaline - Gradient Descent')\n",
+ "plt.title(\"Adaline - Gradient Descent\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(ada.cost_)), ada.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')"
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")"
]
},
{
@@ -339,29 +338,31 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
"\n",
"\n",
- "ada = Adaline(epochs=15, \n",
- " eta=0.02, \n",
- " minibatches=len(y), # for SGD learning \n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "ada = Adaline(\n",
+ " epochs=15,\n",
+ " eta=0.02,\n",
+ " minibatches=len(y), # for SGD learning\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"\n",
"ada.fit(X, y)\n",
"plot_decision_regions(X, y, clf=ada)\n",
- "plt.title('Adaline - Stochastic Gradient Descent')\n",
+ "plt.title(\"Adaline - Stochastic Gradient Descent\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(ada.cost_)), ada.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")\n",
"plt.show()"
]
},
@@ -418,29 +419,31 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
"\n",
"\n",
- "ada = Adaline(epochs=15, \n",
- " eta=0.02, \n",
- " minibatches=5, # for SGD learning w. minibatch size 20\n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "ada = Adaline(\n",
+ " epochs=15,\n",
+ " eta=0.02,\n",
+ " minibatches=5, # for SGD learning w. minibatch size 20\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"\n",
"ada.fit(X, y)\n",
"plot_decision_regions(X, y, clf=ada)\n",
- "plt.title('Adaline - Stochastic Gradient Descent w. Minibatches')\n",
+ "plt.title(\"Adaline - Stochastic Gradient Descent w. Minibatches\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(ada.cost_)), ada.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")\n",
"plt.show()"
]
},
@@ -641,7 +644,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/Adaline.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/Adaline.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/classifier/EnsembleVoteClassifier.ipynb b/docs/sources/user_guide/classifier/EnsembleVoteClassifier.ipynb
index 5b0dd900b..b3b5af43d 100644
--- a/docs/sources/user_guide/classifier/EnsembleVoteClassifier.ipynb
+++ b/docs/sources/user_guide/classifier/EnsembleVoteClassifier.ipynb
@@ -229,7 +229,7 @@
"source": [
"from sklearn import model_selection\n",
"from sklearn.linear_model import LogisticRegression\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"import numpy as np\n",
"\n",
@@ -237,17 +237,13 @@
"clf2 = RandomForestClassifier(random_state=1)\n",
"clf3 = GaussianNB()\n",
"\n",
- "print('5-fold cross validation:\\n')\n",
+ "print(\"5-fold cross validation:\\n\")\n",
"\n",
- "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes']\n",
+ "labels = [\"Logistic Regression\", \"Random Forest\", \"Naive Bayes\"]\n",
"\n",
"for clf, label in zip([clf1, clf2, clf3], labels):\n",
- "\n",
- " scores = model_selection.cross_val_score(clf, X, y, \n",
- " cv=5, \n",
- " scoring='accuracy')\n",
- " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\"\n",
- " % (scores.mean(), scores.std(), label))"
+ " scores = model_selection.cross_val_score(clf, X, y, cv=5, scoring=\"accuracy\")\n",
+ " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
]
},
{
@@ -269,16 +265,12 @@
"source": [
"from mlxtend.classifier import EnsembleVoteClassifier\n",
"\n",
- "eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], weights=[1,1,1])\n",
+ "eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], weights=[1, 1, 1])\n",
"\n",
- "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'Ensemble']\n",
+ "labels = [\"Logistic Regression\", \"Random Forest\", \"Naive Bayes\", \"Ensemble\"]\n",
"for clf, label in zip([clf1, clf2, clf3, eclf], labels):\n",
- "\n",
- " scores = model_selection.cross_val_score(clf, X, y, \n",
- " cv=5, \n",
- " scoring='accuracy')\n",
- " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" \n",
- " % (scores.mean(), scores.std(), label))"
+ " scores = model_selection.cross_val_score(clf, X, y, cv=5, scoring=\"accuracy\")\n",
+ " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
]
},
{
@@ -314,13 +306,12 @@
"\n",
"gs = gridspec.GridSpec(2, 2)\n",
"\n",
- "fig = plt.figure(figsize=(10,8))\n",
- "\n",
- "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'Ensemble']\n",
- "for clf, lab, grd in zip([clf1, clf2, clf3, eclf],\n",
- " labels,\n",
- " itertools.product([0, 1], repeat=2)):\n",
+ "fig = plt.figure(figsize=(10, 8))\n",
"\n",
+ "labels = [\"Logistic Regression\", \"Random Forest\", \"Naive Bayes\", \"Ensemble\"]\n",
+ "for clf, lab, grd in zip(\n",
+ " [clf1, clf2, clf3, eclf], labels, itertools.product([0, 1], repeat=2)\n",
+ "):\n",
" clf.fit(X, y)\n",
" ax = plt.subplot(gs[grd[0], grd[1]])\n",
" fig = plot_decision_regions(X=X, y=y, clf=clf)\n",
@@ -356,28 +347,34 @@
"\n",
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.linear_model import LogisticRegression\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from mlxtend.classifier import EnsembleVoteClassifier\n",
"\n",
"clf1 = LogisticRegression(random_state=1)\n",
"clf2 = RandomForestClassifier(random_state=1)\n",
"clf3 = GaussianNB()\n",
- "eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], voting='soft')\n",
+ "eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], voting=\"soft\")\n",
"\n",
- "params = {'logisticregression__C': [1.0, 100.0],\n",
- " 'randomforestclassifier__n_estimators': [20, 200],}\n",
+ "params = {\n",
+ " \"logisticregression__C\": [1.0, 100.0],\n",
+ " \"randomforestclassifier__n_estimators\": [20, 200],\n",
+ "}\n",
"\n",
"grid = GridSearchCV(estimator=eclf, param_grid=params, cv=5)\n",
"grid.fit(iris.data, iris.target)\n",
- " \n",
- "cv_keys = ('mean_test_score', 'std_test_score', 'params')\n",
"\n",
- "for r, _ in enumerate(grid.cv_results_['mean_test_score']):\n",
- " print(\"%0.3f +/- %0.2f %r\"\n",
- " % (grid.cv_results_[cv_keys[0]][r],\n",
- " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
- " grid.cv_results_[cv_keys[2]][r]))"
+ "cv_keys = (\"mean_test_score\", \"std_test_score\", \"params\")\n",
+ "\n",
+ "for r, _ in enumerate(grid.cv_results_[\"mean_test_score\"]):\n",
+ " print(\n",
+ " \"%0.3f +/- %0.2f %r\"\n",
+ " % (\n",
+ " grid.cv_results_[cv_keys[0]][r],\n",
+ " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
+ " grid.cv_results_[cv_keys[2]][r],\n",
+ " )\n",
+ " )"
]
},
{
@@ -397,12 +394,13 @@
"\n",
"clf1 = LogisticRegression(random_state=1)\n",
"clf2 = RandomForestClassifier(random_state=1)\n",
- "eclf = EnsembleVoteClassifier(clfs=[clf1, clf1, clf2], \n",
- " voting='soft')\n",
+ "eclf = EnsembleVoteClassifier(clfs=[clf1, clf1, clf2], voting=\"soft\")\n",
"\n",
- "params = {'logisticregression-1__C': [1.0, 100.0],\n",
- " 'logisticregression-2__C': [1.0, 100.0],\n",
- " 'randomforestclassifier__n_estimators': [20, 200],}\n",
+ "params = {\n",
+ " \"logisticregression-1__C\": [1.0, 100.0],\n",
+ " \"logisticregression-2__C\": [1.0, 100.0],\n",
+ " \"randomforestclassifier__n_estimators\": [20, 200],\n",
+ "}\n",
"\n",
"grid = GridSearchCV(estimator=eclf, param_grid=params, cv=5)\n",
"grid = grid.fit(iris.data, iris.target)"
@@ -465,7 +463,7 @@
"\n",
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.linear_model import LogisticRegression\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from mlxtend.classifier import EnsembleVoteClassifier\n",
"from sklearn.pipeline import Pipeline\n",
@@ -477,37 +475,42 @@
"\n",
"# Creating a feature-selection-classifier pipeline\n",
"\n",
- "sfs1 = SequentialFeatureSelector(clf1, \n",
- " k_features=4,\n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " verbose=0,\n",
- " cv=0)\n",
+ "sfs1 = SequentialFeatureSelector(\n",
+ " clf1,\n",
+ " k_features=4,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " scoring=\"accuracy\",\n",
+ " verbose=0,\n",
+ " cv=0,\n",
+ ")\n",
"\n",
- "clf1_pipe = Pipeline([('sfs', sfs1),\n",
- " ('logreg', clf1)])\n",
+ "clf1_pipe = Pipeline([(\"sfs\", sfs1), (\"logreg\", clf1)])\n",
"\n",
- "eclf = EnsembleVoteClassifier(clfs=[clf1_pipe, clf2, clf3], \n",
- " voting='soft')\n",
+ "eclf = EnsembleVoteClassifier(clfs=[clf1_pipe, clf2, clf3], voting=\"soft\")\n",
"\n",
"\n",
- "params = {'pipeline__sfs__k_features': [1, 2, 3],\n",
- " 'pipeline__logreg__C': [1.0, 100.0],\n",
- " 'randomforestclassifier__n_estimators': [20, 200]}\n",
+ "params = {\n",
+ " \"pipeline__sfs__k_features\": [1, 2, 3],\n",
+ " \"pipeline__logreg__C\": [1.0, 100.0],\n",
+ " \"randomforestclassifier__n_estimators\": [20, 200],\n",
+ "}\n",
"\n",
"grid = GridSearchCV(estimator=eclf, param_grid=params, cv=5)\n",
"grid.fit(iris.data, iris.target)\n",
"\n",
"\n",
+ "cv_keys = (\"mean_test_score\", \"std_test_score\", \"params\")\n",
"\n",
- "cv_keys = ('mean_test_score', 'std_test_score', 'params')\n",
- "\n",
- "for r, _ in enumerate(grid.cv_results_['mean_test_score']):\n",
- " print(\"%0.3f +/- %0.2f %r\"\n",
- " % (grid.cv_results_[cv_keys[0]][r],\n",
- " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
- " grid.cv_results_[cv_keys[2]][r]))"
+ "for r, _ in enumerate(grid.cv_results_[\"mean_test_score\"]):\n",
+ " print(\n",
+ " \"%0.3f +/- %0.2f %r\"\n",
+ " % (\n",
+ " grid.cv_results_[cv_keys[0]][r],\n",
+ " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
+ " grid.cv_results_[cv_keys[2]][r],\n",
+ " )\n",
+ " )"
]
},
{
@@ -603,11 +606,9 @@
"\n",
"col_sel = ColumnSelector(cols=[0, 2])\n",
"\n",
- "clf1_pipe = Pipeline([('sel', col_sel),\n",
- " ('logreg', clf1)])\n",
+ "clf1_pipe = Pipeline([(\"sel\", col_sel), (\"logreg\", clf1)])\n",
"\n",
- "eclf = EnsembleVoteClassifier(clfs=[clf1_pipe, clf2, clf3],\n",
- " voting='soft')\n",
+ "eclf = EnsembleVoteClassifier(clfs=[clf1_pipe, clf2, clf3], voting=\"soft\")\n",
"eclf.fit(X, y).predict(X[[1, 51, 149]])"
]
},
@@ -643,22 +644,23 @@
}
],
"source": [
- "sfs1 = SequentialFeatureSelector(clf1, \n",
- " k_features=2,\n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " verbose=1,\n",
- " cv=0)\n",
+ "sfs1 = SequentialFeatureSelector(\n",
+ " clf1,\n",
+ " k_features=2,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " scoring=\"accuracy\",\n",
+ " verbose=1,\n",
+ " cv=0,\n",
+ ")\n",
"\n",
"sfs1.fit(X, y)\n",
"\n",
- "print('Best features', sfs1.k_feature_idx_)\n",
+ "print(\"Best features\", sfs1.k_feature_idx_)\n",
"\n",
"col_sel = ColumnSelector(cols=sfs1.k_feature_idx_)\n",
"\n",
- "clf1_pipe = Pipeline([('sel', col_sel),\n",
- " ('logreg', clf1)])"
+ "clf1_pipe = Pipeline([(\"sel\", col_sel), (\"logreg\", clf1)])"
]
},
{
@@ -678,8 +680,7 @@
}
],
"source": [
- "eclf = EnsembleVoteClassifier(clfs=[clf1_pipe, clf2, clf3], \n",
- " voting='soft')\n",
+ "eclf = EnsembleVoteClassifier(clfs=[clf1_pipe, clf2, clf3], voting=\"soft\")\n",
"eclf.fit(X, y).predict(X[[1, 51, 149]])"
]
},
@@ -717,9 +718,8 @@
"source": [
"from sklearn import model_selection\n",
"from sklearn.linear_model import LogisticRegression\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
- "import numpy as np\n",
"\n",
"clf1 = LogisticRegression(random_state=1)\n",
"clf2 = RandomForestClassifier(random_state=1)\n",
@@ -759,14 +759,16 @@
],
"source": [
"from mlxtend.classifier import EnsembleVoteClassifier\n",
- "import copy\n",
- "eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], weights=[1,1,1], fit_base_estimators=False)\n",
"\n",
- "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'Ensemble']\n",
+ "eclf = EnsembleVoteClassifier(\n",
+ " clfs=[clf1, clf2, clf3], weights=[1, 1, 1], fit_base_estimators=False\n",
+ ")\n",
+ "\n",
+ "labels = [\"Logistic Regression\", \"Random Forest\", \"Naive Bayes\", \"Ensemble\"]\n",
"\n",
"eclf.fit(X, y)\n",
"\n",
- "print('accuracy:', np.mean(y == eclf.predict(X)))"
+ "print(\"accuracy:\", np.mean(y == eclf.predict(X)))"
]
},
{
@@ -824,10 +826,8 @@
"X = iris.data\n",
"y = iris.target\n",
"\n",
- "pipe1 = make_pipeline(ColumnSelector(cols=(0, 2)),\n",
- " LogisticRegression())\n",
- "pipe2 = make_pipeline(ColumnSelector(cols=(1, 2, 3)),\n",
- " LogisticRegression())\n",
+ "pipe1 = make_pipeline(ColumnSelector(cols=(0, 2)), LogisticRegression())\n",
+ "pipe2 = make_pipeline(ColumnSelector(cols=(1, 2, 3)), LogisticRegression())\n",
"\n",
"eclf = EnsembleVoteClassifier(clfs=[pipe1, pipe2])\n",
"\n",
@@ -894,20 +894,24 @@
"\n",
"clf2 = SVC(probability=True, random_state=4)\n",
"clf2.fit(X, y)\n",
- "eclf = EnsembleVoteClassifier(clfs=[clf2], voting='soft', fit_base_estimators=False)\n",
+ "eclf = EnsembleVoteClassifier(clfs=[clf2], voting=\"soft\", fit_base_estimators=False)\n",
"eclf.fit(X, y)\n",
"\n",
- "for svm_class, e_class, svm_prob, e_prob, in zip(clf2.predict(X),\n",
- " eclf.predict(X),\n",
- " clf2.predict_proba(X),\n",
- " eclf.predict_proba(X)):\n",
+ "for (\n",
+ " svm_class,\n",
+ " e_class,\n",
+ " svm_prob,\n",
+ " e_prob,\n",
+ ") in zip(\n",
+ " clf2.predict(X), eclf.predict(X), clf2.predict_proba(X), eclf.predict_proba(X)\n",
+ "):\n",
" if svm_class != e_class:\n",
- " print('============')\n",
- " print('Probas from SVM :', svm_prob)\n",
- " print('Class from SVM :', svm_class)\n",
- " print('Probas from SVM in Ensemble:', e_prob)\n",
- " print('Class from SVM in Ensemble :', e_class)\n",
- " print('============')"
+ " print(\"============\")\n",
+ " print(\"Probas from SVM :\", svm_prob)\n",
+ " print(\"Class from SVM :\", svm_class)\n",
+ " print(\"Probas from SVM in Ensemble:\", e_prob)\n",
+ " print(\"Class from SVM in Ensemble :\", e_class)\n",
+ " print(\"============\")"
]
},
{
@@ -963,7 +967,7 @@
")\n",
"\n",
"clf1 = GaussianNB()\n",
- "clf2 = LogisticRegression(random_state=123, solver='newton-cg')\n",
+ "clf2 = LogisticRegression(random_state=123, solver=\"newton-cg\")\n",
"clf3 = DecisionTreeClassifier(random_state=123, max_depth=2)\n",
"\n",
"clf1.fit(X_train[500:1000], y_train[500:1000])\n",
@@ -1033,7 +1037,6 @@
"\n",
"\n",
"def function_to_minimize(weights, fitted_clfs):\n",
- "\n",
" w1, w2 = weights # these are the new weights!\n",
"\n",
" newclf = EnsembleVoteClassifier(\n",
@@ -1041,7 +1044,7 @@
" use_clones=False,\n",
" fit_base_estimators=False,\n",
" clfs=fitted_clfs,\n",
- " weights=(w1, w2, 1.), # use the new weights\n",
+ " weights=(w1, w2, 1.0), # use the new weights\n",
" )\n",
"\n",
" newclf.fit(X_train, y_train)\n",
@@ -1081,7 +1084,7 @@
"source": [
"%%capture --no-display\n",
"\n",
- "init_weights = [1., 1.]\n",
+ "init_weights = [1.0, 1.0]\n",
"\n",
"results = minimize(\n",
" function_to_minimize,\n",
@@ -1516,7 +1519,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/EnsembleVoteClassifier.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/EnsembleVoteClassifier.md\", \"r\") as f:\n",
" print(f.read())"
]
},
diff --git a/docs/sources/user_guide/classifier/LogisticRegression.ipynb b/docs/sources/user_guide/classifier/LogisticRegression.ipynb
index 513bfbd16..ef7364946 100644
--- a/docs/sources/user_guide/classifier/LogisticRegression.ipynb
+++ b/docs/sources/user_guide/classifier/LogisticRegression.ipynb
@@ -449,29 +449,31 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
- "\n",
- "lr = LogisticRegression(eta=0.1, \n",
- " l2_lambda=0.0, \n",
- " epochs=100,\n",
- " minibatches=1, # for Gradient Descent\n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
+ "\n",
+ "lr = LogisticRegression(\n",
+ " eta=0.1,\n",
+ " l2_lambda=0.0,\n",
+ " epochs=100,\n",
+ " minibatches=1, # for Gradient Descent\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"lr.fit(X, y)\n",
"\n",
"plot_decision_regions(X, y, clf=lr)\n",
- "plt.title('Logistic Regression - Gradient Descent')\n",
+ "plt.title(\"Logistic Regression - Gradient Descent\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(lr.cost_)), lr.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")\n",
"plt.show()"
]
},
@@ -497,7 +499,7 @@
],
"source": [
"y_pred = lr.predict(X)\n",
- "print('Last 3 Class Labels: %s' % y_pred[-3:])"
+ "print(\"Last 3 Class Labels: %s\" % y_pred[-3:])"
]
},
{
@@ -522,7 +524,7 @@
],
"source": [
"y_pred = lr.predict_proba(X)\n",
- "print('Last 3 Class Labels: %s' % y_pred[-3:])"
+ "print(\"Last 3 Class Labels: %s\" % y_pred[-3:])"
]
},
{
@@ -574,29 +576,31 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
- "\n",
- "lr = LogisticRegression(eta=0.5, \n",
- " epochs=30, \n",
- " l2_lambda=0.0, \n",
- " minibatches=len(y), # for SGD learning \n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
+ "\n",
+ "lr = LogisticRegression(\n",
+ " eta=0.5,\n",
+ " epochs=30,\n",
+ " l2_lambda=0.0,\n",
+ " minibatches=len(y), # for SGD learning\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"lr.fit(X, y)\n",
"\n",
"plot_decision_regions(X, y, clf=lr)\n",
- "plt.title('Logistic Regression - Stochastic Gradient Descent')\n",
+ "plt.title(\"Logistic Regression - Stochastic Gradient Descent\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(lr.cost_)), lr.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")\n",
"plt.show()"
]
},
@@ -656,29 +660,31 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
- "\n",
- "lr = LogisticRegression(eta=0.5, \n",
- " epochs=30, \n",
- " l2_lambda=0.0, \n",
- " minibatches=5, # 100/5 = 20 -> minibatch-s \n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
+ "\n",
+ "lr = LogisticRegression(\n",
+ " eta=0.5,\n",
+ " epochs=30,\n",
+ " l2_lambda=0.0,\n",
+ " minibatches=5, # 100/5 = 20 -> minibatch-s\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"lr.fit(X, y)\n",
"\n",
"plot_decision_regions(X, y, clf=lr)\n",
- "plt.title('Logistic Regression - Stochastic Gradient Descent')\n",
+ "plt.title(\"Logistic Regression - Stochastic Gradient Descent\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(lr.cost_)), lr.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")\n",
"plt.show()"
]
},
@@ -859,7 +865,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/LogisticRegression.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/LogisticRegression.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/classifier/MultiLayerPerceptron.ipynb b/docs/sources/user_guide/classifier/MultiLayerPerceptron.ipynb
index 14fc6d96a..9b80a15ab 100644
--- a/docs/sources/user_guide/classifier/MultiLayerPerceptron.ipynb
+++ b/docs/sources/user_guide/classifier/MultiLayerPerceptron.ipynb
@@ -161,8 +161,9 @@
"outputs": [],
"source": [
"from mlxtend.data import iris_data\n",
+ "\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] \n",
+ "X = X[:, [0, 3]]\n",
"\n",
"# standardize training data\n",
"X_std = (X - X.mean(axis=0)) / X.std(axis=0)"
@@ -205,16 +206,18 @@
"source": [
"from mlxtend.classifier import MultiLayerPerceptron as MLP\n",
"\n",
- "nn1 = MLP(hidden_layers=[50], \n",
- " l2=0.00, \n",
- " l1=0.0, \n",
- " epochs=150, \n",
- " eta=0.05, \n",
- " momentum=0.1,\n",
- " decrease_const=0.0,\n",
- " minibatches=1, \n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "nn1 = MLP(\n",
+ " hidden_layers=[50],\n",
+ " l2=0.00,\n",
+ " l1=0.0,\n",
+ " epochs=150,\n",
+ " eta=0.05,\n",
+ " momentum=0.1,\n",
+ " decrease_const=0.0,\n",
+ " minibatches=1,\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"\n",
"nn1 = nn1.fit(X_std, y)"
]
@@ -240,7 +243,7 @@
"import matplotlib.pyplot as plt\n",
"\n",
"fig = plot_decision_regions(X=X_std, y=y, clf=nn1, legend=2)\n",
- "plt.title('Multi-layer Perceptron w. 1 hidden layer (logistic sigmoid)')\n",
+ "plt.title(\"Multi-layer Perceptron w. 1 hidden layer (logistic sigmoid)\")\n",
"plt.show()"
]
},
@@ -262,9 +265,10 @@
],
"source": [
"import matplotlib.pyplot as plt\n",
+ "\n",
"plt.plot(range(len(nn1.cost_)), nn1.cost_)\n",
- "plt.ylabel('Cost')\n",
- "plt.xlabel('Epochs')\n",
+ "plt.ylabel(\"Cost\")\n",
+ "plt.xlabel(\"Epochs\")\n",
"plt.show()"
]
},
@@ -282,7 +286,7 @@
}
],
"source": [
- "print('Accuracy: %.2f%%' % (100 * nn1.score(X_std, y)))"
+ "print(\"Accuracy: %.2f%%\" % (100 * nn1.score(X_std, y)))"
]
},
{
@@ -323,22 +327,24 @@
}
],
"source": [
- "nn2 = MLP(hidden_layers=[50], \n",
- " l2=0.00, \n",
- " l1=0.0, \n",
- " epochs=5, \n",
- " eta=0.005, \n",
- " momentum=0.1,\n",
- " decrease_const=0.0,\n",
- " minibatches=len(y), \n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "nn2 = MLP(\n",
+ " hidden_layers=[50],\n",
+ " l2=0.00,\n",
+ " l1=0.0,\n",
+ " epochs=5,\n",
+ " eta=0.005,\n",
+ " momentum=0.1,\n",
+ " decrease_const=0.0,\n",
+ " minibatches=len(y),\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")\n",
"\n",
"nn2.fit(X_std, y)\n",
"\n",
"plt.plot(range(len(nn2.cost_)), nn2.cost_)\n",
- "plt.ylabel('Cost')\n",
- "plt.xlabel('Epochs')\n",
+ "plt.ylabel(\"Cost\")\n",
+ "plt.xlabel(\"Epochs\")\n",
"plt.show()"
]
},
@@ -385,8 +391,8 @@
],
"source": [
"plt.plot(range(len(nn2.cost_)), nn2.cost_)\n",
- "plt.ylabel('Cost')\n",
- "plt.xlabel('Epochs')\n",
+ "plt.ylabel(\"Cost\")\n",
+ "plt.xlabel(\"Epochs\")\n",
"plt.show()"
]
},
@@ -445,13 +451,15 @@
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
+ "\n",
"def plot_digit(X, y, idx):\n",
- " img = X[idx].reshape(28,28)\n",
- " plt.imshow(img, cmap='Greys', interpolation='nearest')\n",
- " plt.title('true label: %d' % y[idx])\n",
+ " img = X[idx].reshape(28, 28)\n",
+ " plt.imshow(img, cmap=\"Greys\", interpolation=\"nearest\")\n",
+ " plt.title(\"true label: %d\" % y[idx])\n",
" plt.show()\n",
- " \n",
- "plot_digit(X, y, 3500) "
+ "\n",
+ "\n",
+ "plot_digit(X, y, 3500)"
]
},
{
@@ -467,16 +475,13 @@
"metadata": {},
"outputs": [],
"source": [
- "import numpy as np\n",
"from mlxtend.preprocessing import standardize\n",
"\n",
- "X_train_std, params = standardize(X_train, \n",
- " columns=range(X_train.shape[1]), \n",
- " return_params=True)\n",
+ "X_train_std, params = standardize(\n",
+ " X_train, columns=range(X_train.shape[1]), return_params=True\n",
+ ")\n",
"\n",
- "X_test_std = standardize(X_test,\n",
- " columns=range(X_test.shape[1]),\n",
- " params=params)"
+ "X_test_std = standardize(X_test, columns=range(X_test.shape[1]), params=params)"
]
},
{
@@ -492,16 +497,18 @@
"metadata": {},
"outputs": [],
"source": [
- "nn1 = MLP(hidden_layers=[150], \n",
- " l2=0.00, \n",
- " l1=0.0, \n",
- " epochs=100, \n",
- " eta=0.005, \n",
- " momentum=0.0,\n",
- " decrease_const=0.0,\n",
- " minibatches=100, \n",
- " random_seed=1,\n",
- " print_progress=3)"
+ "nn1 = MLP(\n",
+ " hidden_layers=[150],\n",
+ " l2=0.00,\n",
+ " l1=0.0,\n",
+ " epochs=100,\n",
+ " eta=0.005,\n",
+ " momentum=0.0,\n",
+ " decrease_const=0.0,\n",
+ " minibatches=100,\n",
+ " random_seed=1,\n",
+ " print_progress=3,\n",
+ ")"
]
},
{
@@ -540,8 +547,8 @@
"nn1.fit(X_train_std, y_train)\n",
"\n",
"plt.plot(range(len(nn1.cost_)), nn1.cost_)\n",
- "plt.ylabel('Cost')\n",
- "plt.xlabel('Epochs')\n",
+ "plt.ylabel(\"Cost\")\n",
+ "plt.xlabel(\"Epochs\")\n",
"plt.show()"
]
},
@@ -560,8 +567,8 @@
}
],
"source": [
- "print('Train Accuracy: %.2f%%' % (100 * nn1.score(X_train_std, y_train)))\n",
- "print('Test Accuracy: %.2f%%' % (100 * nn1.score(X_test_std, y_test)))"
+ "print(\"Train Accuracy: %.2f%%\" % (100 * nn1.score(X_train_std, y_train)))\n",
+ "print(\"Test Accuracy: %.2f%%\" % (100 * nn1.score(X_test_std, y_test)))"
]
},
{
@@ -772,7 +779,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/MultiLayerPerceptron.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/MultiLayerPerceptron.md\", \"r\") as f:\n",
" print(f.read())"
]
},
diff --git a/docs/sources/user_guide/classifier/OneRClassifier.ipynb b/docs/sources/user_guide/classifier/OneRClassifier.ipynb
index 8e292cc79..19254996c 100644
--- a/docs/sources/user_guide/classifier/OneRClassifier.ipynb
+++ b/docs/sources/user_guide/classifier/OneRClassifier.ipynb
@@ -186,6 +186,7 @@
" X_discretized[X[:, col] <= threshold, col] = class_label\n",
" return X_discretized.astype(np.int)\n",
"\n",
+ "\n",
"Xd = get_feature_quartiles(X)\n",
"Xd[:15]"
]
@@ -223,6 +224,7 @@
"outputs": [],
"source": [
"from mlxtend.classifier import OneRClassifier\n",
+ "\n",
"oner = OneRClassifier()\n",
"\n",
"oner.fit(Xd_train, y_train);"
@@ -340,8 +342,8 @@
],
"source": [
"y_pred = oner.predict(Xd_train)\n",
- "train_acc = np.mean(y_pred == y_train) \n",
- "print(f'Training accuracy {train_acc*100:.2f}%')"
+ "train_acc = np.mean(y_pred == y_train)\n",
+ "print(f\"Training accuracy {train_acc * 100:.2f}%\")"
]
},
{
@@ -359,8 +361,8 @@
],
"source": [
"y_pred = oner.predict(Xd_test)\n",
- "test_acc = np.mean(y_pred == y_test) \n",
- "print(f'Test accuracy {test_acc*100:.2f}%')"
+ "test_acc = np.mean(y_pred == y_test)\n",
+ "print(f\"Test accuracy {test_acc * 100:.2f}%\")"
]
},
{
@@ -385,7 +387,7 @@
],
"source": [
"test_acc = oner.score(Xd_test, y_test)\n",
- "print(f'Test accuracy {test_acc*100:.2f}%')"
+ "print(f\"Test accuracy {test_acc * 100:.2f}%\")"
]
},
{
@@ -578,7 +580,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/OneRClassifier.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/OneRClassifier.md\", \"r\") as f:\n",
" print(f.read())"
]
},
diff --git a/docs/sources/user_guide/classifier/Perceptron.ipynb b/docs/sources/user_guide/classifier/Perceptron.ipynb
index e4dbdfd37..85ae324f8 100644
--- a/docs/sources/user_guide/classifier/Perceptron.ipynb
+++ b/docs/sources/user_guide/classifier/Perceptron.ipynb
@@ -275,32 +275,29 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
- "X = X[0:100] # class 0 and class 1\n",
- "y = y[0:100] # class 0 and class 1\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[0:100] # class 0 and class 1\n",
+ "y = y[0:100] # class 0 and class 1\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
"\n",
"\n",
"# Rosenblatt Perceptron\n",
"\n",
- "ppn = Perceptron(epochs=5, \n",
- " eta=0.05, \n",
- " random_seed=0,\n",
- " print_progress=3)\n",
+ "ppn = Perceptron(epochs=5, eta=0.05, random_seed=0, print_progress=3)\n",
"ppn.fit(X, y)\n",
"\n",
"plot_decision_regions(X, y, clf=ppn)\n",
- "plt.title('Perceptron - Rosenblatt Perceptron Rule')\n",
+ "plt.title(\"Perceptron - Rosenblatt Perceptron Rule\")\n",
"plt.show()\n",
"\n",
- "print('Bias & Weights: %s' % ppn.w_)\n",
+ "print(\"Bias & Weights: %s\" % ppn.w_)\n",
"\n",
"plt.plot(range(len(ppn.cost_)), ppn.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Missclassifications')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Missclassifications\")\n",
"plt.show()"
]
},
@@ -449,7 +446,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/Perceptron.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/Perceptron.md\", \"r\") as f:\n",
" print(f.read())"
]
},
diff --git a/docs/sources/user_guide/classifier/SoftmaxRegression.ipynb b/docs/sources/user_guide/classifier/SoftmaxRegression.ipynb
index 34703693c..c9c9cd57d 100644
--- a/docs/sources/user_guide/classifier/SoftmaxRegression.ipynb
+++ b/docs/sources/user_guide/classifier/SoftmaxRegression.ipynb
@@ -141,7 +141,7 @@
"source": [
"y_enc = (np.arange(np.max(y) + 1) == y[:, None]).astype(float)\n",
"\n",
- "print('one-hot encoding:\\n', y_enc)"
+ "print(\"one-hot encoding:\\n\", y_enc)"
]
},
{
@@ -184,19 +184,15 @@
}
],
"source": [
- "X = np.array([[0.1, 0.5],\n",
- " [1.1, 2.3],\n",
- " [-1.1, -2.3],\n",
- " [-1.5, -2.5]])\n",
+ "X = np.array([[0.1, 0.5], [1.1, 2.3], [-1.1, -2.3], [-1.5, -2.5]])\n",
"\n",
- "W = np.array([[0.1, 0.2, 0.3],\n",
- " [0.1, 0.2, 0.3]])\n",
+ "W = np.array([[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]])\n",
"\n",
"bias = np.array([0.01, 0.1, 0.1])\n",
"\n",
- "print('Inputs X:\\n', X)\n",
- "print('\\nWeights W:\\n', W)\n",
- "print('\\nbias:\\n', bias)"
+ "print(\"Inputs X:\\n\", X)\n",
+ "print(\"\\nWeights W:\\n\", W)\n",
+ "print(\"\\nbias:\\n\", bias)"
]
},
{
@@ -233,19 +229,15 @@
}
],
"source": [
- "X = np.array([[0.1, 0.5],\n",
- " [1.1, 2.3],\n",
- " [-1.1, -2.3],\n",
- " [-1.5, -2.5]])\n",
+ "X = np.array([[0.1, 0.5], [1.1, 2.3], [-1.1, -2.3], [-1.5, -2.5]])\n",
"\n",
- "W = np.array([[0.1, 0.2, 0.3],\n",
- " [0.1, 0.2, 0.3]])\n",
+ "W = np.array([[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]])\n",
"\n",
"bias = np.array([0.01, 0.1, 0.1])\n",
"\n",
- "print('Inputs X:\\n', X)\n",
- "print('\\nWeights W:\\n', W)\n",
- "print('\\nbias:\\n', bias)"
+ "print(\"Inputs X:\\n\", X)\n",
+ "print(\"\\nWeights W:\\n\", W)\n",
+ "print(\"\\nbias:\\n\", bias)"
]
},
{
@@ -267,10 +259,11 @@
],
"source": [
"def net_input(X, W, b):\n",
- " return (X.dot(W) + b)\n",
+ " return X.dot(W) + b\n",
+ "\n",
"\n",
"net_in = net_input(X, W, bias)\n",
- "print('net input:\\n', net_in)"
+ "print(\"net input:\\n\", net_in)"
]
},
{
@@ -303,8 +296,9 @@
"def softmax(z):\n",
" return (np.exp(z.T) / np.sum(np.exp(z), axis=1)).T\n",
"\n",
+ "\n",
"smax = softmax(net_in)\n",
- "print('softmax:\\n', smax)"
+ "print(\"softmax:\\n\", smax)"
]
},
{
@@ -345,7 +339,8 @@
"def to_classlabel(z):\n",
" return z.argmax(axis=1)\n",
"\n",
- "print('predicted class labels: ', to_classlabel(smax))"
+ "\n",
+ "print(\"predicted class labels: \", to_classlabel(smax))"
]
},
{
@@ -378,10 +373,11 @@
],
"source": [
"def cross_entropy(output, y_target):\n",
- " return - np.sum(np.log(output) * (y_target), axis=1)\n",
+ " return -np.sum(np.log(output) * (y_target), axis=1)\n",
+ "\n",
"\n",
"xent = cross_entropy(smax, y_enc)\n",
- "print('Cross Entropy:', xent)"
+ "print(\"Cross Entropy:\", xent)"
]
},
{
@@ -401,8 +397,9 @@
"def cost(output, y_target):\n",
" return np.mean(cross_entropy(output, y_target))\n",
"\n",
+ "\n",
"J_cost = cost(smax, y_enc)\n",
- "print('Cost: ', J_cost)"
+ "print(\"Cost: \", J_cost)"
]
},
{
@@ -502,26 +499,24 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
- "\n",
- "lr = SoftmaxRegression(eta=0.01, \n",
- " epochs=500, \n",
- " minibatches=1, \n",
- " random_seed=1,\n",
- " print_progress=3)\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
+ "\n",
+ "lr = SoftmaxRegression(\n",
+ " eta=0.01, epochs=500, minibatches=1, random_seed=1, print_progress=3\n",
+ ")\n",
"lr.fit(X, y)\n",
"\n",
"plot_decision_regions(X, y, clf=lr)\n",
- "plt.title('Softmax Regression - Gradient Descent')\n",
+ "plt.title(\"Softmax Regression - Gradient Descent\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(lr.cost_)), lr.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")\n",
"plt.show()"
]
},
@@ -547,7 +542,7 @@
],
"source": [
"y_pred = lr.predict(X)\n",
- "print('Last 3 Class Labels: %s' % y_pred[-3:])"
+ "print(\"Last 3 Class Labels: %s\" % y_pred[-3:])"
]
},
{
@@ -575,7 +570,7 @@
],
"source": [
"y_pred = lr.predict_proba(X)\n",
- "print('Last 3 Class Labels:\\n %s' % y_pred[-3:])"
+ "print(\"Last 3 Class Labels:\\n %s\" % y_pred[-3:])"
]
},
{
@@ -620,22 +615,22 @@
"# Loading Data\n",
"\n",
"X, y = iris_data()\n",
- "X = X[:, [0, 3]] # sepal length and petal width\n",
+ "X = X[:, [0, 3]] # sepal length and petal width\n",
"\n",
"# standardize\n",
- "X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()\n",
- "X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()\n",
+ "X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()\n",
+ "X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()\n",
"\n",
"lr = SoftmaxRegression(eta=0.01, epochs=300, minibatches=len(y), random_seed=1)\n",
"lr.fit(X, y)\n",
"\n",
"plot_decision_regions(X, y, clf=lr)\n",
- "plt.title('Softmax Regression - Stochastic Gradient Descent')\n",
+ "plt.title(\"Softmax Regression - Stochastic Gradient Descent\")\n",
"plt.show()\n",
"\n",
"plt.plot(range(len(lr.cost_)), lr.cost_)\n",
- "plt.xlabel('Iterations')\n",
- "plt.ylabel('Cost')\n",
+ "plt.xlabel(\"Iterations\")\n",
+ "plt.ylabel(\"Cost\")\n",
"plt.show()"
]
},
@@ -818,7 +813,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/SoftmaxRegression.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/SoftmaxRegression.md\", \"r\") as f:\n",
" print(f.read())"
]
},
diff --git a/docs/sources/user_guide/classifier/StackingCVClassifier.ipynb b/docs/sources/user_guide/classifier/StackingCVClassifier.ipynb
index d604c15d3..fe17bad11 100644
--- a/docs/sources/user_guide/classifier/StackingCVClassifier.ipynb
+++ b/docs/sources/user_guide/classifier/StackingCVClassifier.ipynb
@@ -119,13 +119,12 @@
"from sklearn import model_selection\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from mlxtend.classifier import StackingCVClassifier\n",
- "import numpy as np\n",
"import warnings\n",
"\n",
- "warnings.simplefilter('ignore')\n",
+ "warnings.simplefilter(\"ignore\")\n",
"\n",
"RANDOM_SEED = 42\n",
"\n",
@@ -136,22 +135,18 @@
"\n",
"# Starting from v0.16.0, StackingCVRegressor supports\n",
"# `random_state` to get deterministic result.\n",
- "sclf = StackingCVClassifier(classifiers=[clf1, clf2, clf3],\n",
- " meta_classifier=lr,\n",
- " random_state=RANDOM_SEED)\n",
+ "sclf = StackingCVClassifier(\n",
+ " classifiers=[clf1, clf2, clf3], meta_classifier=lr, random_state=RANDOM_SEED\n",
+ ")\n",
"\n",
- "print('3-fold cross validation:\\n')\n",
+ "print(\"3-fold cross validation:\\n\")\n",
"\n",
- "for clf, label in zip([clf1, clf2, clf3, sclf], \n",
- " ['KNN', \n",
- " 'Random Forest', \n",
- " 'Naive Bayes',\n",
- " 'StackingClassifier']):\n",
- "\n",
- " scores = model_selection.cross_val_score(clf, X, y, \n",
- " cv=3, scoring='accuracy')\n",
- " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" \n",
- " % (scores.mean(), scores.std(), label))"
+ "for clf, label in zip(\n",
+ " [clf1, clf2, clf3, sclf],\n",
+ " [\"KNN\", \"Random Forest\", \"Naive Bayes\", \"StackingClassifier\"],\n",
+ "):\n",
+ " scores = model_selection.cross_val_score(clf, X, y, cv=3, scoring=\"accuracy\")\n",
+ " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
]
},
{
@@ -180,15 +175,13 @@
"\n",
"gs = gridspec.GridSpec(2, 2)\n",
"\n",
- "fig = plt.figure(figsize=(10,8))\n",
- "\n",
- "for clf, lab, grd in zip([clf1, clf2, clf3, sclf], \n",
- " ['KNN', \n",
- " 'Random Forest', \n",
- " 'Naive Bayes',\n",
- " 'StackingCVClassifier'],\n",
- " itertools.product([0, 1], repeat=2)):\n",
+ "fig = plt.figure(figsize=(10, 8))\n",
"\n",
+ "for clf, lab, grd in zip(\n",
+ " [clf1, clf2, clf3, sclf],\n",
+ " [\"KNN\", \"Random Forest\", \"Naive Bayes\", \"StackingCVClassifier\"],\n",
+ " itertools.product([0, 1], repeat=2),\n",
+ "):\n",
" clf.fit(X, y)\n",
" ax = plt.subplot(gs[grd[0], grd[1]])\n",
" fig = plot_decision_regions(X=X, y=y, clf=clf)\n",
@@ -241,23 +234,18 @@
"clf3 = GaussianNB()\n",
"lr = LogisticRegression()\n",
"\n",
- "sclf = StackingCVClassifier(classifiers=[clf1, clf2, clf3],\n",
- " use_probas=True,\n",
- " meta_classifier=lr,\n",
- " random_state=42)\n",
+ "sclf = StackingCVClassifier(\n",
+ " classifiers=[clf1, clf2, clf3], use_probas=True, meta_classifier=lr, random_state=42\n",
+ ")\n",
"\n",
- "print('3-fold cross validation:\\n')\n",
+ "print(\"3-fold cross validation:\\n\")\n",
"\n",
- "for clf, label in zip([clf1, clf2, clf3, sclf], \n",
- " ['KNN', \n",
- " 'Random Forest', \n",
- " 'Naive Bayes',\n",
- " 'StackingClassifier']):\n",
- "\n",
- " scores = model_selection.cross_val_score(clf, X, y, \n",
- " cv=3, scoring='accuracy')\n",
- " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" \n",
- " % (scores.mean(), scores.std(), label))"
+ "for clf, label in zip(\n",
+ " [clf1, clf2, clf3, sclf],\n",
+ " [\"KNN\", \"Random Forest\", \"Naive Bayes\", \"StackingClassifier\"],\n",
+ "):\n",
+ " scores = model_selection.cross_val_score(clf, X, y, cv=3, scoring=\"accuracy\")\n",
+ " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
]
},
{
@@ -299,7 +287,7 @@
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.model_selection import GridSearchCV\n",
"from mlxtend.classifier import StackingCVClassifier\n",
@@ -311,30 +299,33 @@
"clf3 = GaussianNB()\n",
"lr = LogisticRegression()\n",
"\n",
- "sclf = StackingCVClassifier(classifiers=[clf1, clf2, clf3], \n",
- " meta_classifier=lr,\n",
- " random_state=42)\n",
+ "sclf = StackingCVClassifier(\n",
+ " classifiers=[clf1, clf2, clf3], meta_classifier=lr, random_state=42\n",
+ ")\n",
"\n",
- "params = {'kneighborsclassifier__n_neighbors': [1, 5],\n",
- " 'randomforestclassifier__n_estimators': [10, 50],\n",
- " 'meta_classifier__C': [0.1, 10.0]}\n",
+ "params = {\n",
+ " \"kneighborsclassifier__n_neighbors\": [1, 5],\n",
+ " \"randomforestclassifier__n_estimators\": [10, 50],\n",
+ " \"meta_classifier__C\": [0.1, 10.0],\n",
+ "}\n",
"\n",
- "grid = GridSearchCV(estimator=sclf, \n",
- " param_grid=params, \n",
- " cv=5,\n",
- " refit=True)\n",
+ "grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5, refit=True)\n",
"grid.fit(X, y)\n",
"\n",
- "cv_keys = ('mean_test_score', 'std_test_score', 'params')\n",
+ "cv_keys = (\"mean_test_score\", \"std_test_score\", \"params\")\n",
"\n",
- "for r, _ in enumerate(grid.cv_results_['mean_test_score']):\n",
- " print(\"%0.3f +/- %0.2f %r\"\n",
- " % (grid.cv_results_[cv_keys[0]][r],\n",
- " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
- " grid.cv_results_[cv_keys[2]][r]))\n",
+ "for r, _ in enumerate(grid.cv_results_[\"mean_test_score\"]):\n",
+ " print(\n",
+ " \"%0.3f +/- %0.2f %r\"\n",
+ " % (\n",
+ " grid.cv_results_[cv_keys[0]][r],\n",
+ " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
+ " grid.cv_results_[cv_keys[2]][r],\n",
+ " )\n",
+ " )\n",
"\n",
- "print('Best parameters: %s' % grid.best_params_)\n",
- "print('Accuracy: %.2f' % grid.best_score_)"
+ "print(\"Best parameters: %s\" % grid.best_params_)\n",
+ "print(\"Accuracy: %.2f\" % grid.best_score_)"
]
},
{
@@ -384,31 +375,34 @@
"clf3 = GaussianNB()\n",
"lr = LogisticRegression()\n",
"\n",
- "sclf = StackingCVClassifier(classifiers=[clf1, clf1, clf2, clf3], \n",
- " meta_classifier=lr,\n",
- " random_state=RANDOM_SEED)\n",
+ "sclf = StackingCVClassifier(\n",
+ " classifiers=[clf1, clf1, clf2, clf3], meta_classifier=lr, random_state=RANDOM_SEED\n",
+ ")\n",
"\n",
- "params = {'kneighborsclassifier-1__n_neighbors': [1, 5],\n",
- " 'kneighborsclassifier-2__n_neighbors': [1, 5],\n",
- " 'randomforestclassifier__n_estimators': [10, 50],\n",
- " 'meta_classifier__C': [0.1, 10.0]}\n",
+ "params = {\n",
+ " \"kneighborsclassifier-1__n_neighbors\": [1, 5],\n",
+ " \"kneighborsclassifier-2__n_neighbors\": [1, 5],\n",
+ " \"randomforestclassifier__n_estimators\": [10, 50],\n",
+ " \"meta_classifier__C\": [0.1, 10.0],\n",
+ "}\n",
"\n",
- "grid = GridSearchCV(estimator=sclf, \n",
- " param_grid=params, \n",
- " cv=5,\n",
- " refit=True)\n",
+ "grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5, refit=True)\n",
"grid.fit(X, y)\n",
"\n",
- "cv_keys = ('mean_test_score', 'std_test_score', 'params')\n",
+ "cv_keys = (\"mean_test_score\", \"std_test_score\", \"params\")\n",
"\n",
- "for r, _ in enumerate(grid.cv_results_['mean_test_score']):\n",
- " print(\"%0.3f +/- %0.2f %r\"\n",
- " % (grid.cv_results_[cv_keys[0]][r],\n",
- " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
- " grid.cv_results_[cv_keys[2]][r]))\n",
+ "for r, _ in enumerate(grid.cv_results_[\"mean_test_score\"]):\n",
+ " print(\n",
+ " \"%0.3f +/- %0.2f %r\"\n",
+ " % (\n",
+ " grid.cv_results_[cv_keys[0]][r],\n",
+ " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
+ " grid.cv_results_[cv_keys[2]][r],\n",
+ " )\n",
+ " )\n",
"\n",
- "print('Best parameters: %s' % grid.best_params_)\n",
- "print('Accuracy: %.2f' % grid.best_score_)"
+ "print(\"Best parameters: %s\" % grid.best_params_)\n",
+ "print(\"Accuracy: %.2f\" % grid.best_score_)"
]
},
{
@@ -499,14 +493,12 @@
"X = iris.data\n",
"y = iris.target\n",
"\n",
- "pipe1 = make_pipeline(ColumnSelector(cols=(0, 2)),\n",
- " LogisticRegression())\n",
- "pipe2 = make_pipeline(ColumnSelector(cols=(1, 2, 3)),\n",
- " LogisticRegression())\n",
+ "pipe1 = make_pipeline(ColumnSelector(cols=(0, 2)), LogisticRegression())\n",
+ "pipe2 = make_pipeline(ColumnSelector(cols=(1, 2, 3)), LogisticRegression())\n",
"\n",
- "sclf = StackingCVClassifier(classifiers=[pipe1, pipe2], \n",
- " meta_classifier=LogisticRegression(),\n",
- " random_state=42)\n",
+ "sclf = StackingCVClassifier(\n",
+ " classifiers=[pipe1, pipe2], meta_classifier=LogisticRegression(), random_state=42\n",
+ ")\n",
"\n",
"sclf.fit(X, y)"
]
@@ -538,7 +530,6 @@
"from sklearn.ensemble import RandomForestClassifier\n",
"from mlxtend.classifier import StackingCVClassifier\n",
"from sklearn.metrics import roc_curve, auc\n",
- "import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn import datasets\n",
"from sklearn.preprocessing import label_binarize\n",
@@ -554,21 +545,20 @@
"n_classes = y.shape[1]\n",
"\n",
"\n",
- "\n",
"RANDOM_SEED = 42\n",
"\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.33, random_state=RANDOM_SEED)\n",
+ " X, y, test_size=0.33, random_state=RANDOM_SEED\n",
+ ")\n",
"\n",
- "clf1 = LogisticRegression()\n",
+ "clf1 = LogisticRegression()\n",
"clf2 = RandomForestClassifier(random_state=RANDOM_SEED)\n",
"clf3 = SVC(random_state=RANDOM_SEED)\n",
"lr = LogisticRegression()\n",
"\n",
"\n",
- "sclf = StackingCVClassifier(classifiers=[clf1, clf2, clf3],\n",
- " meta_classifier=lr)\n",
+ "sclf = StackingCVClassifier(classifiers=[clf1, clf2, clf3], meta_classifier=lr)\n",
"\n",
"\n",
"# Learn to predict each class against the other\n",
@@ -617,14 +607,19 @@
"\n",
"plt.figure()\n",
"lw = 2\n",
- "plt.plot(fpr[2], tpr[2], color='darkorange',\n",
- " lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])\n",
- "plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')\n",
+ "plt.plot(\n",
+ " fpr[2],\n",
+ " tpr[2],\n",
+ " color=\"darkorange\",\n",
+ " lw=lw,\n",
+ " label=\"ROC curve (area = %0.2f)\" % roc_auc[2],\n",
+ ")\n",
+ "plt.plot([0, 1], [0, 1], color=\"navy\", lw=lw, linestyle=\"--\")\n",
"plt.xlim([0.0, 1.0])\n",
"plt.ylim([0.0, 1.05])\n",
- "plt.xlabel('False Positive Rate')\n",
- "plt.ylabel('True Positive Rate')\n",
- "plt.title('Receiver operating characteristic example')\n",
+ "plt.xlabel(\"False Positive Rate\")\n",
+ "plt.ylabel(\"True Positive Rate\")\n",
+ "plt.title(\"Receiver operating characteristic example\")\n",
"plt.legend(loc=\"lower right\")\n",
"plt.show()"
]
@@ -671,14 +666,19 @@
"\n",
"plt.figure()\n",
"lw = 2\n",
- "plt.plot(fpr[2], tpr[2], color='darkorange',\n",
- " lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])\n",
- "plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')\n",
+ "plt.plot(\n",
+ " fpr[2],\n",
+ " tpr[2],\n",
+ " color=\"darkorange\",\n",
+ " lw=lw,\n",
+ " label=\"ROC curve (area = %0.2f)\" % roc_auc[2],\n",
+ ")\n",
+ "plt.plot([0, 1], [0, 1], color=\"navy\", lw=lw, linestyle=\"--\")\n",
"plt.xlim([0.0, 1.0])\n",
"plt.ylim([0.0, 1.05])\n",
- "plt.xlabel('False Positive Rate')\n",
- "plt.ylabel('True Positive Rate')\n",
- "plt.title('Receiver operating characteristic example')\n",
+ "plt.xlabel(\"False Positive Rate\")\n",
+ "plt.ylabel(\"True Positive Rate\")\n",
+ "plt.title(\"Receiver operating characteristic example\")\n",
"plt.legend(loc=\"lower right\")\n",
"plt.show()"
]
@@ -1059,7 +1059,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/StackingCVClassifier.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/StackingCVClassifier.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/classifier/StackingClassifier.ipynb b/docs/sources/user_guide/classifier/StackingClassifier.ipynb
index fe11066c8..59608ed99 100644
--- a/docs/sources/user_guide/classifier/StackingClassifier.ipynb
+++ b/docs/sources/user_guide/classifier/StackingClassifier.ipynb
@@ -125,33 +125,28 @@
"from sklearn import model_selection\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from mlxtend.classifier import StackingClassifier\n",
"import numpy as np\n",
"import warnings\n",
"\n",
- "warnings.simplefilter('ignore')\n",
+ "warnings.simplefilter(\"ignore\")\n",
"\n",
"clf1 = KNeighborsClassifier(n_neighbors=1)\n",
"clf2 = RandomForestClassifier(random_state=1)\n",
"clf3 = GaussianNB()\n",
"lr = LogisticRegression()\n",
- "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], \n",
- " meta_classifier=lr)\n",
+ "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], meta_classifier=lr)\n",
"\n",
- "print('3-fold cross validation:\\n')\n",
+ "print(\"3-fold cross validation:\\n\")\n",
"\n",
- "for clf, label in zip([clf1, clf2, clf3, sclf], \n",
- " ['KNN', \n",
- " 'Random Forest', \n",
- " 'Naive Bayes',\n",
- " 'StackingClassifier']):\n",
- "\n",
- " scores = model_selection.cross_val_score(clf, X, y, \n",
- " cv=3, scoring='accuracy')\n",
- " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" \n",
- " % (scores.mean(), scores.std(), label))"
+ "for clf, label in zip(\n",
+ " [clf1, clf2, clf3, sclf],\n",
+ " [\"KNN\", \"Random Forest\", \"Naive Bayes\", \"StackingClassifier\"],\n",
+ "):\n",
+ " scores = model_selection.cross_val_score(clf, X, y, cv=3, scoring=\"accuracy\")\n",
+ " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
]
},
{
@@ -180,15 +175,13 @@
"\n",
"gs = gridspec.GridSpec(2, 2)\n",
"\n",
- "fig = plt.figure(figsize=(10,8))\n",
- "\n",
- "for clf, lab, grd in zip([clf1, clf2, clf3, sclf], \n",
- " ['KNN', \n",
- " 'Random Forest', \n",
- " 'Naive Bayes',\n",
- " 'StackingClassifier'],\n",
- " itertools.product([0, 1], repeat=2)):\n",
+ "fig = plt.figure(figsize=(10, 8))\n",
"\n",
+ "for clf, lab, grd in zip(\n",
+ " [clf1, clf2, clf3, sclf],\n",
+ " [\"KNN\", \"Random Forest\", \"Naive Bayes\", \"StackingClassifier\"],\n",
+ " itertools.product([0, 1], repeat=2),\n",
+ "):\n",
" clf.fit(X, y)\n",
" ax = plt.subplot(gs[grd[0], grd[1]])\n",
" fig = plot_decision_regions(X=X, y=y, clf=clf)\n",
@@ -243,23 +236,21 @@
"clf2 = RandomForestClassifier(random_state=1)\n",
"clf3 = GaussianNB()\n",
"lr = LogisticRegression()\n",
- "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3],\n",
- " use_probas=True,\n",
- " average_probas=False,\n",
- " meta_classifier=lr)\n",
+ "sclf = StackingClassifier(\n",
+ " classifiers=[clf1, clf2, clf3],\n",
+ " use_probas=True,\n",
+ " average_probas=False,\n",
+ " meta_classifier=lr,\n",
+ ")\n",
"\n",
- "print('3-fold cross validation:\\n')\n",
+ "print(\"3-fold cross validation:\\n\")\n",
"\n",
- "for clf, label in zip([clf1, clf2, clf3, sclf], \n",
- " ['KNN', \n",
- " 'Random Forest', \n",
- " 'Naive Bayes',\n",
- " 'StackingClassifier']):\n",
- "\n",
- " scores = model_selection.cross_val_score(clf, X, y, \n",
- " cv=3, scoring='accuracy')\n",
- " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" \n",
- " % (scores.mean(), scores.std(), label))"
+ "for clf, label in zip(\n",
+ " [clf1, clf2, clf3, sclf],\n",
+ " [\"KNN\", \"Random Forest\", \"Naive Bayes\", \"StackingClassifier\"],\n",
+ "):\n",
+ " scores = model_selection.cross_val_score(clf, X, y, cv=3, scoring=\"accuracy\")\n",
+ " print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
]
},
{
@@ -301,7 +292,7 @@
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.model_selection import GridSearchCV\n",
"from mlxtend.classifier import StackingClassifier\n",
@@ -312,29 +303,31 @@
"clf2 = RandomForestClassifier(random_state=1)\n",
"clf3 = GaussianNB()\n",
"lr = LogisticRegression()\n",
- "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], \n",
- " meta_classifier=lr)\n",
+ "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], meta_classifier=lr)\n",
"\n",
- "params = {'kneighborsclassifier__n_neighbors': [1, 5],\n",
- " 'randomforestclassifier__n_estimators': [10, 50],\n",
- " 'meta_classifier__C': [0.1, 10.0]}\n",
+ "params = {\n",
+ " \"kneighborsclassifier__n_neighbors\": [1, 5],\n",
+ " \"randomforestclassifier__n_estimators\": [10, 50],\n",
+ " \"meta_classifier__C\": [0.1, 10.0],\n",
+ "}\n",
"\n",
- "grid = GridSearchCV(estimator=sclf, \n",
- " param_grid=params, \n",
- " cv=5,\n",
- " refit=True)\n",
+ "grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5, refit=True)\n",
"grid.fit(X, y)\n",
"\n",
- "cv_keys = ('mean_test_score', 'std_test_score', 'params')\n",
+ "cv_keys = (\"mean_test_score\", \"std_test_score\", \"params\")\n",
"\n",
- "for r, _ in enumerate(grid.cv_results_['mean_test_score']):\n",
- " print(\"%0.3f +/- %0.2f %r\"\n",
- " % (grid.cv_results_[cv_keys[0]][r],\n",
- " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
- " grid.cv_results_[cv_keys[2]][r]))\n",
+ "for r, _ in enumerate(grid.cv_results_[\"mean_test_score\"]):\n",
+ " print(\n",
+ " \"%0.3f +/- %0.2f %r\"\n",
+ " % (\n",
+ " grid.cv_results_[cv_keys[0]][r],\n",
+ " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
+ " grid.cv_results_[cv_keys[2]][r],\n",
+ " )\n",
+ " )\n",
"\n",
- "print('Best parameters: %s' % grid.best_params_)\n",
- "print('Accuracy: %.2f' % grid.best_score_)"
+ "print(\"Best parameters: %s\" % grid.best_params_)\n",
+ "print(\"Accuracy: %.2f\" % grid.best_score_)"
]
},
{
@@ -383,30 +376,32 @@
"clf2 = RandomForestClassifier(random_state=1)\n",
"clf3 = GaussianNB()\n",
"lr = LogisticRegression()\n",
- "sclf = StackingClassifier(classifiers=[clf1, clf1, clf2, clf3], \n",
- " meta_classifier=lr)\n",
- "\n",
- "params = {'kneighborsclassifier-1__n_neighbors': [1, 5],\n",
- " 'kneighborsclassifier-2__n_neighbors': [1, 5],\n",
- " 'randomforestclassifier__n_estimators': [10, 50],\n",
- " 'meta_classifier__C': [0.1, 10.0]}\n",
- "\n",
- "grid = GridSearchCV(estimator=sclf, \n",
- " param_grid=params, \n",
- " cv=5,\n",
- " refit=True)\n",
+ "sclf = StackingClassifier(classifiers=[clf1, clf1, clf2, clf3], meta_classifier=lr)\n",
+ "\n",
+ "params = {\n",
+ " \"kneighborsclassifier-1__n_neighbors\": [1, 5],\n",
+ " \"kneighborsclassifier-2__n_neighbors\": [1, 5],\n",
+ " \"randomforestclassifier__n_estimators\": [10, 50],\n",
+ " \"meta_classifier__C\": [0.1, 10.0],\n",
+ "}\n",
+ "\n",
+ "grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5, refit=True)\n",
"grid.fit(X, y)\n",
"\n",
- "cv_keys = ('mean_test_score', 'std_test_score', 'params')\n",
+ "cv_keys = (\"mean_test_score\", \"std_test_score\", \"params\")\n",
"\n",
- "for r, _ in enumerate(grid.cv_results_['mean_test_score']):\n",
- " print(\"%0.3f +/- %0.2f %r\"\n",
- " % (grid.cv_results_[cv_keys[0]][r],\n",
- " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
- " grid.cv_results_[cv_keys[2]][r]))\n",
+ "for r, _ in enumerate(grid.cv_results_[\"mean_test_score\"]):\n",
+ " print(\n",
+ " \"%0.3f +/- %0.2f %r\"\n",
+ " % (\n",
+ " grid.cv_results_[cv_keys[0]][r],\n",
+ " grid.cv_results_[cv_keys[1]][r] / 2.0,\n",
+ " grid.cv_results_[cv_keys[2]][r],\n",
+ " )\n",
+ " )\n",
"\n",
- "print('Best parameters: %s' % grid.best_params_)\n",
- "print('Accuracy: %.2f' % grid.best_score_)"
+ "print(\"Best parameters: %s\" % grid.best_params_)\n",
+ "print(\"Accuracy: %.2f\" % grid.best_score_)"
]
},
{
@@ -496,13 +491,12 @@
"X = iris.data\n",
"y = iris.target\n",
"\n",
- "pipe1 = make_pipeline(ColumnSelector(cols=(0, 2)),\n",
- " LogisticRegression())\n",
- "pipe2 = make_pipeline(ColumnSelector(cols=(1, 2, 3)),\n",
- " LogisticRegression())\n",
+ "pipe1 = make_pipeline(ColumnSelector(cols=(0, 2)), LogisticRegression())\n",
+ "pipe2 = make_pipeline(ColumnSelector(cols=(1, 2, 3)), LogisticRegression())\n",
"\n",
- "sclf = StackingClassifier(classifiers=[pipe1, pipe2], \n",
- " meta_classifier=LogisticRegression())\n",
+ "sclf = StackingClassifier(\n",
+ " classifiers=[pipe1, pipe2], meta_classifier=LogisticRegression()\n",
+ ")\n",
"\n",
"sclf.fit(X, y)"
]
@@ -529,9 +523,8 @@
"source": [
"from sklearn import model_selection\n",
"from sklearn.linear_model import LogisticRegression\n",
- "from sklearn.naive_bayes import GaussianNB \n",
+ "from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.ensemble import RandomForestClassifier\n",
- "import numpy as np\n",
"\n",
"clf1 = KNeighborsClassifier(n_neighbors=1)\n",
"clf2 = RandomForestClassifier(random_state=1)\n",
@@ -565,15 +558,16 @@
],
"source": [
"from mlxtend.classifier import StackingClassifier\n",
- "import copy\n",
- "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], \n",
- " meta_classifier=lr, fit_base_estimators=False)\n",
"\n",
- "labels = ['KNN', 'Random Forest', 'Naive Bayes', 'StackingClassifier']\n",
+ "sclf = StackingClassifier(\n",
+ " classifiers=[clf1, clf2, clf3], meta_classifier=lr, fit_base_estimators=False\n",
+ ")\n",
+ "\n",
+ "labels = [\"KNN\", \"Random Forest\", \"Naive Bayes\", \"StackingClassifier\"]\n",
"\n",
"sclf.fit(X, y)\n",
"\n",
- "print('accuracy:', np.mean(y == sclf.predict(X)))"
+ "print(\"accuracy:\", np.mean(y == sclf.predict(X)))"
]
},
{
@@ -608,7 +602,6 @@
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.svm import SVC\n",
"from sklearn.ensemble import RandomForestClassifier\n",
- "from mlxtend.classifier import StackingCVClassifier\n",
"from sklearn.metrics import roc_curve, auc\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
@@ -626,21 +619,20 @@
"n_classes = y.shape[1]\n",
"\n",
"\n",
- "\n",
"RANDOM_SEED = 42\n",
"\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.33, random_state=RANDOM_SEED)\n",
+ " X, y, test_size=0.33, random_state=RANDOM_SEED\n",
+ ")\n",
"\n",
- "clf1 = LogisticRegression()\n",
+ "clf1 = LogisticRegression()\n",
"clf2 = RandomForestClassifier(random_state=RANDOM_SEED)\n",
"clf3 = SVC(random_state=RANDOM_SEED)\n",
"lr = LogisticRegression()\n",
"\n",
"\n",
- "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3],\n",
- " meta_classifier=lr)\n",
+ "sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], meta_classifier=lr)\n",
"\n",
"\n",
"# Learn to predict each class against the other\n",
@@ -689,14 +681,19 @@
"\n",
"plt.figure()\n",
"lw = 2\n",
- "plt.plot(fpr[2], tpr[2], color='darkorange',\n",
- " lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])\n",
- "plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')\n",
+ "plt.plot(\n",
+ " fpr[2],\n",
+ " tpr[2],\n",
+ " color=\"darkorange\",\n",
+ " lw=lw,\n",
+ " label=\"ROC curve (area = %0.2f)\" % roc_auc[2],\n",
+ ")\n",
+ "plt.plot([0, 1], [0, 1], color=\"navy\", lw=lw, linestyle=\"--\")\n",
"plt.xlim([0.0, 1.0])\n",
"plt.ylim([0.0, 1.05])\n",
- "plt.xlabel('False Positive Rate')\n",
- "plt.ylabel('True Positive Rate')\n",
- "plt.title('Receiver operating characteristic example')\n",
+ "plt.xlabel(\"False Positive Rate\")\n",
+ "plt.ylabel(\"True Positive Rate\")\n",
+ "plt.title(\"Receiver operating characteristic example\")\n",
"plt.legend(loc=\"lower right\")\n",
"plt.show()"
]
@@ -743,14 +740,19 @@
"\n",
"plt.figure()\n",
"lw = 2\n",
- "plt.plot(fpr[2], tpr[2], color='darkorange',\n",
- " lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])\n",
- "plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')\n",
+ "plt.plot(\n",
+ " fpr[2],\n",
+ " tpr[2],\n",
+ " color=\"darkorange\",\n",
+ " lw=lw,\n",
+ " label=\"ROC curve (area = %0.2f)\" % roc_auc[2],\n",
+ ")\n",
+ "plt.plot([0, 1], [0, 1], color=\"navy\", lw=lw, linestyle=\"--\")\n",
"plt.xlim([0.0, 1.0])\n",
"plt.ylim([0.0, 1.05])\n",
- "plt.xlabel('False Positive Rate')\n",
- "plt.ylabel('True Positive Rate')\n",
- "plt.title('Receiver operating characteristic example')\n",
+ "plt.xlabel(\"False Positive Rate\")\n",
+ "plt.ylabel(\"True Positive Rate\")\n",
+ "plt.title(\"Receiver operating characteristic example\")\n",
"plt.legend(loc=\"lower right\")\n",
"plt.show()"
]
@@ -780,7 +782,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.classifier/StackingClassifier.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.classifier/StackingClassifier.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/cluster/Kmeans.ipynb b/docs/sources/user_guide/cluster/Kmeans.ipynb
index 4a5684e68..c4a706815 100644
--- a/docs/sources/user_guide/cluster/Kmeans.ipynb
+++ b/docs/sources/user_guide/cluster/Kmeans.ipynb
@@ -105,7 +105,7 @@
"from mlxtend.data import three_blobs_data\n",
"\n",
"X, y = three_blobs_data()\n",
- "plt.scatter(X[:, 0], X[:, 1], c='white')\n",
+ "plt.scatter(X[:, 0], X[:, 1], c=\"white\")\n",
"plt.show()"
]
},
@@ -143,15 +143,12 @@
"source": [
"from mlxtend.cluster import Kmeans\n",
"\n",
- "km = Kmeans(k=3, \n",
- " max_iter=50, \n",
- " random_seed=1, \n",
- " print_progress=3)\n",
+ "km = Kmeans(k=3, max_iter=50, random_seed=1, print_progress=3)\n",
"\n",
"km.fit(X)\n",
"\n",
- "print('Iterations until convergence:', km.iterations_)\n",
- "print('Final centroids:\\n', km.centroids_)"
+ "print(\"Iterations until convergence:\", km.iterations_)\n",
+ "print(\"Final centroids:\\n\", km.centroids_)"
]
},
{
@@ -180,37 +177,44 @@
"source": [
"y_clust = km.predict(X)\n",
"\n",
- "plt.scatter(X[y_clust == 0, 0],\n",
- " X[y_clust == 0, 1],\n",
- " s=50,\n",
- " c='lightgreen',\n",
- " marker='s',\n",
- " label='cluster 1')\n",
+ "plt.scatter(\n",
+ " X[y_clust == 0, 0],\n",
+ " X[y_clust == 0, 1],\n",
+ " s=50,\n",
+ " c=\"lightgreen\",\n",
+ " marker=\"s\",\n",
+ " label=\"cluster 1\",\n",
+ ")\n",
"\n",
- "plt.scatter(X[y_clust == 1,0],\n",
- " X[y_clust == 1,1],\n",
- " s=50,\n",
- " c='orange',\n",
- " marker='o',\n",
- " label='cluster 2')\n",
+ "plt.scatter(\n",
+ " X[y_clust == 1, 0],\n",
+ " X[y_clust == 1, 1],\n",
+ " s=50,\n",
+ " c=\"orange\",\n",
+ " marker=\"o\",\n",
+ " label=\"cluster 2\",\n",
+ ")\n",
"\n",
- "plt.scatter(X[y_clust == 2,0],\n",
- " X[y_clust == 2,1],\n",
- " s=50,\n",
- " c='lightblue',\n",
- " marker='v',\n",
- " label='cluster 3')\n",
+ "plt.scatter(\n",
+ " X[y_clust == 2, 0],\n",
+ " X[y_clust == 2, 1],\n",
+ " s=50,\n",
+ " c=\"lightblue\",\n",
+ " marker=\"v\",\n",
+ " label=\"cluster 3\",\n",
+ ")\n",
"\n",
"\n",
- "plt.scatter(km.centroids_[:,0],\n",
- " km.centroids_[:,1],\n",
- " s=250,\n",
- " marker='*',\n",
- " c='red',\n",
- " label='centroids')\n",
+ "plt.scatter(\n",
+ " km.centroids_[:, 0],\n",
+ " km.centroids_[:, 1],\n",
+ " s=250,\n",
+ " marker=\"*\",\n",
+ " c=\"red\",\n",
+ " label=\"centroids\",\n",
+ ")\n",
"\n",
- "plt.legend(loc='lower left',\n",
- " scatterpoints=1)\n",
+ "plt.legend(loc=\"lower left\", scatterpoints=1)\n",
"plt.grid()\n",
"plt.show()"
]
@@ -341,7 +345,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.cluster/Kmeans.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.cluster/Kmeans.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/data/autompg_data.ipynb b/docs/sources/user_guide/data/autompg_data.ipynb
index efbc4c4bc..6a1db42cb 100644
--- a/docs/sources/user_guide/data/autompg_data.ipynb
+++ b/docs/sources/user_guide/data/autompg_data.ipynb
@@ -86,13 +86,24 @@
],
"source": [
"from mlxtend.data import autompg_data\n",
+ "\n",
"X, y = autompg_data()\n",
"\n",
- "print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))\n",
- "print('\\nHeader: %s' % ['cylinders', 'displacement', \n",
- " 'horsepower', 'weight', 'acceleration',\n",
- " 'model year', 'origin', 'car name'])\n",
- "print('1st row', X[0])"
+ "print(\"Dimensions: %s x %s\" % (X.shape[0], X.shape[1]))\n",
+ "print(\n",
+ " \"\\nHeader: %s\"\n",
+ " % [\n",
+ " \"cylinders\",\n",
+ " \"displacement\",\n",
+ " \"horsepower\",\n",
+ " \"weight\",\n",
+ " \"acceleration\",\n",
+ " \"model year\",\n",
+ " \"origin\",\n",
+ " \"car name\",\n",
+ " ]\n",
+ ")\n",
+ "print(\"1st row\", X[0])"
]
},
{
@@ -190,7 +201,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/autompg_data.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.data/autompg_data.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/data/boston_housing_data.ipynb b/docs/sources/user_guide/data/boston_housing_data.ipynb
index 709d45ac5..82719ab1e 100644
--- a/docs/sources/user_guide/data/boston_housing_data.ipynb
+++ b/docs/sources/user_guide/data/boston_housing_data.ipynb
@@ -94,10 +94,11 @@
],
"source": [
"from mlxtend.data import boston_housing_data\n",
+ "\n",
"X, y = boston_housing_data()\n",
"\n",
- "print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))\n",
- "print('1st row', X[0])"
+ "print(\"Dimensions: %s x %s\" % (X.shape[0], X.shape[1]))\n",
+ "print(\"1st row\", X[0])"
]
},
{
@@ -170,7 +171,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/boston_housing_data.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.data/boston_housing_data.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/data/iris_data.ipynb b/docs/sources/user_guide/data/iris_data.ipynb
index fd1bd0be7..378206ea7 100644
--- a/docs/sources/user_guide/data/iris_data.ipynb
+++ b/docs/sources/user_guide/data/iris_data.ipynb
@@ -83,12 +83,12 @@
],
"source": [
"from mlxtend.data import iris_data\n",
+ "\n",
"X, y = iris_data()\n",
"\n",
- "print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))\n",
- "print('\\nHeader: %s' % ['sepal length', 'sepal width',\n",
- " 'petal length', 'petal width'])\n",
- "print('1st row', X[0])"
+ "print(\"Dimensions: %s x %s\" % (X.shape[0], X.shape[1]))\n",
+ "print(\"\\nHeader: %s\" % [\"sepal length\", \"sepal width\", \"petal length\", \"petal width\"])\n",
+ "print(\"1st row\", X[0])"
]
},
{
@@ -108,9 +108,10 @@
],
"source": [
"import numpy as np\n",
- "print('Classes: Setosa, Versicolor, Virginica')\n",
+ "\n",
+ "print(\"Classes: Setosa, Versicolor, Virginica\")\n",
"print(np.unique(y))\n",
- "print('Class distribution: %s' % np.bincount(y))"
+ "print(\"Class distribution: %s\" % np.bincount(y))"
]
},
{
@@ -198,7 +199,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/iris_data.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.data/iris_data.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/data/loadlocal_mnist.ipynb b/docs/sources/user_guide/data/loadlocal_mnist.ipynb
index 52eccc3eb..369a01024 100644
--- a/docs/sources/user_guide/data/loadlocal_mnist.ipynb
+++ b/docs/sources/user_guide/data/loadlocal_mnist.ipynb
@@ -115,15 +115,15 @@
"metadata": {},
"outputs": [],
"source": [
- "if not platform.system() == 'Windows':\n",
+ "if not platform.system() == \"Windows\":\n",
" X, y = loadlocal_mnist(\n",
- " images_path='train-images-idx3-ubyte', \n",
- " labels_path='train-labels-idx1-ubyte')\n",
+ " images_path=\"train-images-idx3-ubyte\", labels_path=\"train-labels-idx1-ubyte\"\n",
+ " )\n",
"\n",
"else:\n",
" X, y = loadlocal_mnist(\n",
- " images_path='train-images.idx3-ubyte', \n",
- " labels_path='train-labels.idx1-ubyte')"
+ " images_path=\"train-images.idx3-ubyte\", labels_path=\"train-labels.idx1-ubyte\"\n",
+ " )"
]
},
{
@@ -185,8 +185,8 @@
}
],
"source": [
- "print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))\n",
- "print('\\n1st row', X[0])"
+ "print(\"Dimensions: %s x %s\" % (X.shape[0], X.shape[1]))\n",
+ "print(\"\\n1st row\", X[0])"
]
},
{
@@ -207,9 +207,9 @@
"source": [
"import numpy as np\n",
"\n",
- "print('Digits: 0 1 2 3 4 5 6 7 8 9')\n",
- "print('labels: %s' % np.unique(y))\n",
- "print('Class distribution: %s' % np.bincount(y))"
+ "print(\"Digits: 0 1 2 3 4 5 6 7 8 9\")\n",
+ "print(\"labels: %s\" % np.unique(y))\n",
+ "print(\"Class distribution: %s\" % np.bincount(y))"
]
},
{
@@ -225,10 +225,8 @@
"metadata": {},
"outputs": [],
"source": [
- "np.savetxt(fname='images.csv', \n",
- " X=X, delimiter=',', fmt='%d')\n",
- "np.savetxt(fname='labels.csv', \n",
- " X=y, delimiter=',', fmt='%d')"
+ "np.savetxt(fname=\"images.csv\", X=X, delimiter=\",\", fmt=\"%d\")\n",
+ "np.savetxt(fname=\"labels.csv\", X=y, delimiter=\",\", fmt=\"%d\")"
]
},
{
@@ -283,7 +281,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/loadlocal_mnist.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.data/loadlocal_mnist.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/data/make_multiplexer_dataset.ipynb b/docs/sources/user_guide/data/make_multiplexer_dataset.ipynb
index af37b6725..87c6de5cf 100644
--- a/docs/sources/user_guide/data/make_multiplexer_dataset.ipynb
+++ b/docs/sources/user_guide/data/make_multiplexer_dataset.ipynb
@@ -108,18 +108,19 @@
}
],
"source": [
- "import numpy as np\n",
"from mlxtend.data import make_multiplexer_dataset\n",
"\n",
"\n",
- "X, y = make_multiplexer_dataset(address_bits=2, \n",
- " sample_size=10,\n",
- " positive_class_ratio=0.5, \n",
- " shuffle=False,\n",
- " random_seed=123)\n",
+ "X, y = make_multiplexer_dataset(\n",
+ " address_bits=2,\n",
+ " sample_size=10,\n",
+ " positive_class_ratio=0.5,\n",
+ " shuffle=False,\n",
+ " random_seed=123,\n",
+ ")\n",
"\n",
- "print('Features:\\n', X)\n",
- "print('\\nClass labels:\\n', y)"
+ "print(\"Features:\\n\", X)\n",
+ "print(\"\\nClass labels:\\n\", y)"
]
},
{
@@ -209,8 +210,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/make_multiplexer_dataset.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.data/make_multiplexer_dataset.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/data/mnist_data.ipynb b/docs/sources/user_guide/data/mnist_data.ipynb
index 0e58bdbac..9c54b3b77 100644
--- a/docs/sources/user_guide/data/mnist_data.ipynb
+++ b/docs/sources/user_guide/data/mnist_data.ipynb
@@ -145,10 +145,11 @@
],
"source": [
"from mlxtend.data import mnist_data\n",
+ "\n",
"X, y = mnist_data()\n",
"\n",
- "print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))\n",
- "print('1st row', X[0])"
+ "print(\"Dimensions: %s x %s\" % (X.shape[0], X.shape[1]))\n",
+ "print(\"1st row\", X[0])"
]
},
{
@@ -168,9 +169,10 @@
],
"source": [
"import numpy as np\n",
- "print('Classes: Setosa, Versicolor, Virginica')\n",
+ "\n",
+ "print(\"Classes: Setosa, Versicolor, Virginica\")\n",
"print(np.unique(y))\n",
- "print('Class distribution: %s' % np.bincount(y))"
+ "print(\"Class distribution: %s\" % np.bincount(y))"
]
},
{
@@ -199,12 +201,16 @@
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
+ "\n",
+ "\n",
"def plot_digit(X, y, idx):\n",
- " img = X[idx].reshape(28,28)\n",
- " plt.imshow(img, cmap='Greys', interpolation='nearest')\n",
- " plt.title('true label: %d' % y[idx])\n",
+ " img = X[idx].reshape(28, 28)\n",
+ " plt.imshow(img, cmap=\"Greys\", interpolation=\"nearest\")\n",
+ " plt.title(\"true label: %d\" % y[idx])\n",
" plt.show()\n",
- "plot_digit(X, y, 4) "
+ "\n",
+ "\n",
+ "plot_digit(X, y, 4)"
]
},
{
@@ -252,7 +258,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/mnist_data.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.data/mnist_data.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/data/three_blobs_data.ipynb b/docs/sources/user_guide/data/three_blobs_data.ipynb
index 7ea153752..01855f42e 100644
--- a/docs/sources/user_guide/data/three_blobs_data.ipynb
+++ b/docs/sources/user_guide/data/three_blobs_data.ipynb
@@ -84,11 +84,12 @@
],
"source": [
"from mlxtend.data import three_blobs_data\n",
+ "\n",
"X, y = three_blobs_data()\n",
"\n",
- "print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))\n",
+ "print(\"Dimensions: %s x %s\" % (X.shape[0], X.shape[1]))\n",
"\n",
- "print('1st row', X[0])"
+ "print(\"1st row\", X[0])"
]
},
{
@@ -109,9 +110,9 @@
"source": [
"import numpy as np\n",
"\n",
- "print('Suggested cluster labels')\n",
+ "print(\"Suggested cluster labels\")\n",
"print(np.unique(y))\n",
- "print('Label distribution: %s' % np.bincount(y))"
+ "print(\"Label distribution: %s\" % np.bincount(y))"
]
},
{
@@ -133,10 +134,7 @@
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
- "plt.scatter(X[:,0], X[:,1],\n",
- " c='white',\n",
- " marker='o',\n",
- " s=50)\n",
+ "plt.scatter(X[:, 0], X[:, 1], c=\"white\", marker=\"o\", s=50)\n",
"\n",
"plt.grid()\n",
"plt.show()"
@@ -159,28 +157,17 @@
}
],
"source": [
- "plt.scatter(X[y == 0, 0],\n",
- " X[y == 0, 1],\n",
- " s=50,\n",
- " c='lightgreen',\n",
- " marker='s',\n",
- " label='cluster 1')\n",
+ "plt.scatter(\n",
+ " X[y == 0, 0], X[y == 0, 1], s=50, c=\"lightgreen\", marker=\"s\", label=\"cluster 1\"\n",
+ ")\n",
"\n",
- "plt.scatter(X[y == 1,0],\n",
- " X[y == 1,1],\n",
- " s=50,\n",
- " c='orange',\n",
- " marker='o',\n",
- " label='cluster 2')\n",
+ "plt.scatter(X[y == 1, 0], X[y == 1, 1], s=50, c=\"orange\", marker=\"o\", label=\"cluster 2\")\n",
"\n",
- "plt.scatter(X[y == 2,0],\n",
- " X[y == 2,1],\n",
- " s=50,\n",
- " c='lightblue',\n",
- " marker='v',\n",
- " label='cluster 3')\n",
+ "plt.scatter(\n",
+ " X[y == 2, 0], X[y == 2, 1], s=50, c=\"lightblue\", marker=\"v\", label=\"cluster 3\"\n",
+ ")\n",
"\n",
- "plt.legend(loc='lower left')\n",
+ "plt.legend(loc=\"lower left\")\n",
"plt.grid()\n",
"plt.show()"
]
@@ -232,7 +219,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/three_blobs_data.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.data/three_blobs_data.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/data/wine_data.ipynb b/docs/sources/user_guide/data/wine_data.ipynb
index 7f80b29ab..c8a5d1796 100644
--- a/docs/sources/user_guide/data/wine_data.ipynb
+++ b/docs/sources/user_guide/data/wine_data.ipynb
@@ -111,15 +111,29 @@
],
"source": [
"from mlxtend.data import wine_data\n",
+ "\n",
"X, y = wine_data()\n",
"\n",
- "print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))\n",
- "print('\\nHeader: %s' % ['alcohol', 'malic acid', 'ash', 'ash alcalinity',\n",
- " 'magnesium', 'total phenols', 'flavanoids',\n",
- " 'nonflavanoid phenols', 'proanthocyanins',\n",
- " 'color intensity', 'hue', 'OD280/OD315 of diluted wines',\n",
- " 'proline'])\n",
- "print('1st row', X[0])"
+ "print(\"Dimensions: %s x %s\" % (X.shape[0], X.shape[1]))\n",
+ "print(\n",
+ " \"\\nHeader: %s\"\n",
+ " % [\n",
+ " \"alcohol\",\n",
+ " \"malic acid\",\n",
+ " \"ash\",\n",
+ " \"ash alcalinity\",\n",
+ " \"magnesium\",\n",
+ " \"total phenols\",\n",
+ " \"flavanoids\",\n",
+ " \"nonflavanoid phenols\",\n",
+ " \"proanthocyanins\",\n",
+ " \"color intensity\",\n",
+ " \"hue\",\n",
+ " \"OD280/OD315 of diluted wines\",\n",
+ " \"proline\",\n",
+ " ]\n",
+ ")\n",
+ "print(\"1st row\", X[0])"
]
},
{
@@ -138,8 +152,9 @@
],
"source": [
"import numpy as np\n",
- "print('Classes: %s' % np.unique(y))\n",
- "print('Class distribution: %s' % np.bincount(y))"
+ "\n",
+ "print(\"Classes: %s\" % np.unique(y))\n",
+ "print(\"Class distribution: %s\" % np.bincount(y))"
]
},
{
@@ -208,7 +223,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.data/wine_data.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.data/wine_data.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/evaluate/BootstrapOutOfBag.ipynb b/docs/sources/user_guide/evaluate/BootstrapOutOfBag.ipynb
index fb9edd46f..4ab8c896f 100644
--- a/docs/sources/user_guide/evaluate/BootstrapOutOfBag.ipynb
+++ b/docs/sources/user_guide/evaluate/BootstrapOutOfBag.ipynb
@@ -160,8 +160,13 @@
}
],
"source": [
- "print('Mean accuracy: %.1f%%' % np.mean(100*cross_val_score(\n",
- " lr, X, y, cv=BootstrapOutOfBag(n_splits=200, random_seed=456))))"
+ "print(\n",
+ " \"Mean accuracy: %.1f%%\"\n",
+ " % np.mean(\n",
+ " 100\n",
+ " * cross_val_score(lr, X, y, cv=BootstrapOutOfBag(n_splits=200, random_seed=456))\n",
+ " )\n",
+ ")"
]
},
{
@@ -188,6 +193,7 @@
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
+ "\n",
"%matplotlib inline"
]
},
@@ -208,22 +214,21 @@
}
],
"source": [
- "accuracies = cross_val_score(lr, X, y, cv=BootstrapOutOfBag(n_splits=1000, random_seed=456))\n",
+ "accuracies = cross_val_score(\n",
+ " lr, X, y, cv=BootstrapOutOfBag(n_splits=1000, random_seed=456)\n",
+ ")\n",
"mean = np.mean(accuracies)\n",
"\n",
"lower = np.percentile(accuracies, 2.5)\n",
"upper = np.percentile(accuracies, 97.5)\n",
"\n",
"fig, ax = plt.subplots(figsize=(8, 4))\n",
- "ax.vlines(mean, [0], 40, lw=2.5, linestyle='-', label='mean')\n",
- "ax.vlines(lower, [0], 15, lw=2.5, linestyle='-.', label='CI95 percentile')\n",
- "ax.vlines(upper, [0], 15, lw=2.5, linestyle='-.')\n",
- "\n",
- "ax.hist(accuracies, bins=11,\n",
- " color='#0080ff', edgecolor=\"none\", \n",
- " alpha=0.3)\n",
- "plt.legend(loc='upper left')\n",
+ "ax.vlines(mean, [0], 40, lw=2.5, linestyle=\"-\", label=\"mean\")\n",
+ "ax.vlines(lower, [0], 15, lw=2.5, linestyle=\"-.\", label=\"CI95 percentile\")\n",
+ "ax.vlines(upper, [0], 15, lw=2.5, linestyle=\"-.\")\n",
"\n",
+ "ax.hist(accuracies, bins=11, color=\"#0080ff\", edgecolor=\"none\", alpha=0.3)\n",
+ "plt.legend(loc=\"upper left\")\n",
"\n",
"\n",
"plt.show()"
@@ -330,8 +335,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/BootstrapOutOfBag.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/BootstrapOutOfBag.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/GroupTimeSeriesSplit.ipynb b/docs/sources/user_guide/evaluate/GroupTimeSeriesSplit.ipynb
index f96460596..d61c743bf 100644
--- a/docs/sources/user_guide/evaluate/GroupTimeSeriesSplit.ipynb
+++ b/docs/sources/user_guide/evaluate/GroupTimeSeriesSplit.ipynb
@@ -214,7 +214,7 @@
}
],
"source": [
- "months_map = {i: f\"2021-0{i+1}\" for i in range(6)}\n",
+ "months_map = {i: f\"2021-0{i + 1}\" for i in range(6)}\n",
"months = np.array([months_map[group] for group in groups])\n",
"months"
]
diff --git a/docs/sources/user_guide/evaluate/PredefinedHoldoutSplit.ipynb b/docs/sources/user_guide/evaluate/PredefinedHoldoutSplit.ipynb
index 77ef20e20..f9d4f1fdf 100644
--- a/docs/sources/user_guide/evaluate/PredefinedHoldoutSplit.ipynb
+++ b/docs/sources/user_guide/evaluate/PredefinedHoldoutSplit.ipynb
@@ -123,11 +123,13 @@
"X, y = iris_data()\n",
"\n",
"\n",
- "params = {'n_neighbors': [1, 2, 3, 4, 5]}\n",
+ "params = {\"n_neighbors\": [1, 2, 3, 4, 5]}\n",
"\n",
- "grid = GridSearchCV(KNeighborsClassifier(),\n",
- " param_grid=params,\n",
- " cv=PredefinedHoldoutSplit(valid_indices=[0, 1, 99]))\n",
+ "grid = GridSearchCV(\n",
+ " KNeighborsClassifier(),\n",
+ " param_grid=params,\n",
+ " cv=PredefinedHoldoutSplit(valid_indices=[0, 1, 99]),\n",
+ ")\n",
"\n",
"grid.fit(X, y)"
]
@@ -243,8 +245,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/PredefinedHoldoutSplit.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/PredefinedHoldoutSplit.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/RandomHoldoutSplit.ipynb b/docs/sources/user_guide/evaluate/RandomHoldoutSplit.ipynb
index 109ca509d..3d5d4f41d 100644
--- a/docs/sources/user_guide/evaluate/RandomHoldoutSplit.ipynb
+++ b/docs/sources/user_guide/evaluate/RandomHoldoutSplit.ipynb
@@ -122,11 +122,13 @@
"\n",
"X, y = iris_data()\n",
"\n",
- "params = {'n_neighbors': [1, 2, 3, 4, 5]}\n",
+ "params = {\"n_neighbors\": [1, 2, 3, 4, 5]}\n",
"\n",
- "grid = GridSearchCV(KNeighborsClassifier(),\n",
- " param_grid=params,\n",
- " cv=RandomHoldoutSplit(valid_size=0.3, random_seed=123))\n",
+ "grid = GridSearchCV(\n",
+ " KNeighborsClassifier(),\n",
+ " param_grid=params,\n",
+ " cv=RandomHoldoutSplit(valid_size=0.3, random_seed=123),\n",
+ ")\n",
"\n",
"grid.fit(X, y)"
]
@@ -250,8 +252,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/RandomHoldoutSplit.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/RandomHoldoutSplit.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/accuracy_score.ipynb b/docs/sources/user_guide/evaluate/accuracy_score.ipynb
index 0126045b3..87f790542 100644
--- a/docs/sources/user_guide/evaluate/accuracy_score.ipynb
+++ b/docs/sources/user_guide/evaluate/accuracy_score.ipynb
@@ -65,7 +65,6 @@
}
],
"source": [
- "import numpy as np\n",
"from mlxtend.evaluate import accuracy_score\n",
"\n",
"\n",
@@ -104,7 +103,6 @@
}
],
"source": [
- "import numpy as np\n",
"from mlxtend.evaluate import accuracy_score\n",
"\n",
"\n",
@@ -112,10 +110,10 @@
"y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 2]\n",
"\n",
"std_acc = accuracy_score(y_targ, y_pred)\n",
- "bin_acc = accuracy_score(y_targ, y_pred, method='binary', pos_label=1)\n",
+ "bin_acc = accuracy_score(y_targ, y_pred, method=\"binary\", pos_label=1)\n",
"\n",
- "print(f'Standard accuracy: {std_acc*100:.2f}%')\n",
- "print(f'Class 1 accuracy: {bin_acc*100:.2f}%')"
+ "print(f\"Standard accuracy: {std_acc * 100:.2f}%\")\n",
+ "print(f\"Class 1 accuracy: {bin_acc * 100:.2f}%\")"
]
},
{
@@ -202,7 +200,6 @@
}
],
"source": [
- "import numpy as np\n",
"from mlxtend.evaluate import accuracy_score\n",
"\n",
"\n",
@@ -210,12 +207,12 @@
"y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 1]\n",
"\n",
"std_acc = accuracy_score(y_targ, y_pred)\n",
- "bin_acc = accuracy_score(y_targ, y_pred, method='binary', pos_label=1)\n",
- "avg_acc = accuracy_score(y_targ, y_pred, method='average')\n",
+ "bin_acc = accuracy_score(y_targ, y_pred, method=\"binary\", pos_label=1)\n",
+ "avg_acc = accuracy_score(y_targ, y_pred, method=\"average\")\n",
"\n",
- "print(f'Standard accuracy: {std_acc*100:.2f}%')\n",
- "print(f'Class 1 accuracy: {bin_acc*100:.2f}%')\n",
- "print(f'Average per-class accuracy: {avg_acc*100:.2f}%')"
+ "print(f\"Standard accuracy: {std_acc * 100:.2f}%\")\n",
+ "print(f\"Class 1 accuracy: {bin_acc * 100:.2f}%\")\n",
+ "print(f\"Average per-class accuracy: {avg_acc * 100:.2f}%\")"
]
},
{
@@ -296,7 +293,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/accuracy_score.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.evaluate/accuracy_score.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/evaluate/bias_variance_decomp.ipynb b/docs/sources/user_guide/evaluate/bias_variance_decomp.ipynb
index 6da9e65fb..fc5d0ea8c 100644
--- a/docs/sources/user_guide/evaluate/bias_variance_decomp.ipynb
+++ b/docs/sources/user_guide/evaluate/bias_variance_decomp.ipynb
@@ -244,24 +244,20 @@
"\n",
"\n",
"X, y = iris_data()\n",
- "X_train, X_test, y_train, y_test = train_test_split(X, y,\n",
- " test_size=0.3,\n",
- " random_state=123,\n",
- " shuffle=True,\n",
- " stratify=y)\n",
- "\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, test_size=0.3, random_state=123, shuffle=True, stratify=y\n",
+ ")\n",
"\n",
"\n",
"tree = DecisionTreeClassifier(random_state=123)\n",
"\n",
"avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(\n",
- " tree, X_train, y_train, X_test, y_test, \n",
- " loss='0-1_loss',\n",
- " random_seed=123)\n",
+ " tree, X_train, y_train, X_test, y_test, loss=\"0-1_loss\", random_seed=123\n",
+ ")\n",
"\n",
- "print('Average expected loss: %.3f' % avg_expected_loss)\n",
- "print('Average bias: %.3f' % avg_bias)\n",
- "print('Average variance: %.3f' % avg_var)"
+ "print(\"Average expected loss: %.3f\" % avg_expected_loss)\n",
+ "print(\"Average bias: %.3f\" % avg_bias)\n",
+ "print(\"Average variance: %.3f\" % avg_var)"
]
},
{
@@ -290,18 +286,15 @@
"from sklearn.ensemble import BaggingClassifier\n",
"\n",
"tree = DecisionTreeClassifier(random_state=123)\n",
- "bag = BaggingClassifier(base_estimator=tree,\n",
- " n_estimators=100,\n",
- " random_state=123)\n",
+ "bag = BaggingClassifier(base_estimator=tree, n_estimators=100, random_state=123)\n",
"\n",
"avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(\n",
- " bag, X_train, y_train, X_test, y_test, \n",
- " loss='0-1_loss',\n",
- " random_seed=123)\n",
+ " bag, X_train, y_train, X_test, y_test, loss=\"0-1_loss\", random_seed=123\n",
+ ")\n",
"\n",
- "print('Average expected loss: %.3f' % avg_expected_loss)\n",
- "print('Average bias: %.3f' % avg_bias)\n",
- "print('Average variance: %.3f' % avg_var)"
+ "print(\"Average expected loss: %.3f\" % avg_expected_loss)\n",
+ "print(\"Average bias: %.3f\" % avg_bias)\n",
+ "print(\"Average variance: %.3f\" % avg_var)"
]
},
{
@@ -334,23 +327,20 @@
"\n",
"\n",
"X, y = boston_housing_data()\n",
- "X_train, X_test, y_train, y_test = train_test_split(X, y,\n",
- " test_size=0.3,\n",
- " random_state=123,\n",
- " shuffle=True)\n",
- "\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, test_size=0.3, random_state=123, shuffle=True\n",
+ ")\n",
"\n",
"\n",
"tree = DecisionTreeRegressor(random_state=123)\n",
"\n",
"avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(\n",
- " tree, X_train, y_train, X_test, y_test, \n",
- " loss='mse',\n",
- " random_seed=123)\n",
+ " tree, X_train, y_train, X_test, y_test, loss=\"mse\", random_seed=123\n",
+ ")\n",
"\n",
- "print('Average expected loss: %.3f' % avg_expected_loss)\n",
- "print('Average bias: %.3f' % avg_bias)\n",
- "print('Average variance: %.3f' % avg_var)"
+ "print(\"Average expected loss: %.3f\" % avg_expected_loss)\n",
+ "print(\"Average bias: %.3f\" % avg_bias)\n",
+ "print(\"Average variance: %.3f\" % avg_var)"
]
},
{
@@ -379,18 +369,15 @@
"from sklearn.ensemble import BaggingRegressor\n",
"\n",
"tree = DecisionTreeRegressor(random_state=123)\n",
- "bag = BaggingRegressor(base_estimator=tree,\n",
- " n_estimators=100,\n",
- " random_state=123)\n",
+ "bag = BaggingRegressor(base_estimator=tree, n_estimators=100, random_state=123)\n",
"\n",
"avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(\n",
- " bag, X_train, y_train, X_test, y_test, \n",
- " loss='mse',\n",
- " random_seed=123)\n",
+ " bag, X_train, y_train, X_test, y_test, loss=\"mse\", random_seed=123\n",
+ ")\n",
"\n",
- "print('Average expected loss: %.3f' % avg_expected_loss)\n",
- "print('Average bias: %.3f' % avg_bias)\n",
- "print('Average variance: %.3f' % avg_var)"
+ "print(\"Average expected loss: %.3f\" % avg_expected_loss)\n",
+ "print(\"Average bias: %.3f\" % avg_bias)\n",
+ "print(\"Average variance: %.3f\" % avg_var)"
]
},
{
@@ -437,19 +424,17 @@
"\n",
"\n",
"X, y = boston_housing_data()\n",
- "X_train, X_test, y_train, y_test = train_test_split(X, y,\n",
- " test_size=0.3,\n",
- " random_state=123,\n",
- " shuffle=True)\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, test_size=0.3, random_state=123, shuffle=True\n",
+ ")\n",
"\n",
"\n",
- "model = tf.keras.Sequential([\n",
- " tf.keras.layers.Dense(32, activation=tf.nn.relu),\n",
- " tf.keras.layers.Dense(1)\n",
- " ])\n",
+ "model = tf.keras.Sequential(\n",
+ " [tf.keras.layers.Dense(32, activation=tf.nn.relu), tf.keras.layers.Dense(1)]\n",
+ ")\n",
"\n",
"optimizer = tf.keras.optimizers.Adam()\n",
- "model.compile(loss='mean_squared_error', optimizer=optimizer)\n",
+ "model.compile(loss=\"mean_squared_error\", optimizer=optimizer)\n",
"\n",
"model.fit(X_train, y_train, epochs=100, verbose=0)\n",
"\n",
@@ -484,17 +469,22 @@
"\n",
"\n",
"avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(\n",
- " model, X_train, y_train, X_test, y_test, \n",
- " loss='mse',\n",
- " num_rounds=100,\n",
- " random_seed=123,\n",
- " epochs=200, # fit_param\n",
- " verbose=0) # fit_param\n",
- "\n",
- "\n",
- "print('Average expected loss: %.3f' % avg_expected_loss)\n",
- "print('Average bias: %.3f' % avg_bias)\n",
- "print('Average variance: %.3f' % avg_var)"
+ " model,\n",
+ " X_train,\n",
+ " y_train,\n",
+ " X_test,\n",
+ " y_test,\n",
+ " loss=\"mse\",\n",
+ " num_rounds=100,\n",
+ " random_seed=123,\n",
+ " epochs=200, # fit_param\n",
+ " verbose=0,\n",
+ ") # fit_param\n",
+ "\n",
+ "\n",
+ "print(\"Average expected loss: %.3f\" % avg_expected_loss)\n",
+ "print(\"Average bias: %.3f\" % avg_bias)\n",
+ "print(\"Average variance: %.3f\" % avg_var)"
]
},
{
@@ -587,8 +577,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/bias_variance_decomp.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/bias_variance_decomp.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/bootstrap.ipynb b/docs/sources/user_guide/evaluate/bootstrap.ipynb
index ebb2d5dc8..54f3aed82 100644
--- a/docs/sources/user_guide/evaluate/bootstrap.ipynb
+++ b/docs/sources/user_guide/evaluate/bootstrap.ipynb
@@ -89,12 +89,14 @@
"\n",
"\n",
"rng = np.random.RandomState(123)\n",
- "x = rng.normal(loc=5., size=100)\n",
- "original, std_err, ci_bounds = bootstrap(x, num_rounds=1000, func=np.mean, ci=0.95, seed=123)\n",
- "print('Mean: %.2f, SE: +/- %.2f, CI95: [%.2f, %.2f]' % (original, \n",
- " std_err, \n",
- " ci_bounds[0],\n",
- " ci_bounds[1]))"
+ "x = rng.normal(loc=5.0, size=100)\n",
+ "original, std_err, ci_bounds = bootstrap(\n",
+ " x, num_rounds=1000, func=np.mean, ci=0.95, seed=123\n",
+ ")\n",
+ "print(\n",
+ " \"Mean: %.2f, SE: +/- %.2f, CI95: [%.2f, %.2f]\"\n",
+ " % (original, std_err, ci_bounds[0], ci_bounds[1])\n",
+ ")"
]
},
{
@@ -135,20 +137,20 @@
"\n",
"lr = LinearRegression()\n",
"\n",
+ "\n",
"def r2_fit(X, model=lr):\n",
" x, y = X[:, 0].reshape(-1, 1), X[:, 1]\n",
" pred = lr.fit(x, y).predict(x)\n",
" return r2_score(y, pred)\n",
- " \n",
- " \n",
- "original, std_err, ci_bounds = bootstrap(X, num_rounds=1000,\n",
- " func=r2_fit,\n",
- " ci=0.95,\n",
- " seed=123)\n",
- "print('Mean: %.2f, SE: +/- %.2f, CI95: [%.2f, %.2f]' % (original, \n",
- " std_err, \n",
- " ci_bounds[0],\n",
- " ci_bounds[1]))"
+ "\n",
+ "\n",
+ "original, std_err, ci_bounds = bootstrap(\n",
+ " X, num_rounds=1000, func=r2_fit, ci=0.95, seed=123\n",
+ ")\n",
+ "print(\n",
+ " \"Mean: %.2f, SE: +/- %.2f, CI95: [%.2f, %.2f]\"\n",
+ " % (original, std_err, ci_bounds[0], ci_bounds[1])\n",
+ ")"
]
},
{
@@ -251,8 +253,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/bootstrap.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/bootstrap.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/bootstrap_point632_score.ipynb b/docs/sources/user_guide/evaluate/bootstrap_point632_score.ipynb
index df6d3b5f7..112737b73 100644
--- a/docs/sources/user_guide/evaluate/bootstrap_point632_score.ipynb
+++ b/docs/sources/user_guide/evaluate/bootstrap_point632_score.ipynb
@@ -138,15 +138,15 @@
"tree = DecisionTreeClassifier(random_state=123)\n",
"\n",
"# Model accuracy\n",
- "scores = bootstrap_point632_score(tree, X, y, method='oob')\n",
+ "scores = bootstrap_point632_score(tree, X, y, method=\"oob\")\n",
"acc = np.mean(scores)\n",
- "print('Accuracy: %.2f%%' % (100*acc))\n",
+ "print(\"Accuracy: %.2f%%\" % (100 * acc))\n",
"\n",
"\n",
"# Confidence interval\n",
"lower = np.percentile(scores, 2.5)\n",
"upper = np.percentile(scores, 97.5)\n",
- "print('95%% Confidence interval: [%.2f, %.2f]' % (100*lower, 100*upper))"
+ "print(\"95%% Confidence interval: [%.2f, %.2f]\" % (100 * lower, 100 * upper))"
]
},
{
@@ -184,13 +184,13 @@
"# Model accuracy\n",
"scores = bootstrap_point632_score(tree, X, y)\n",
"acc = np.mean(scores)\n",
- "print('Accuracy: %.2f%%' % (100*acc))\n",
+ "print(\"Accuracy: %.2f%%\" % (100 * acc))\n",
"\n",
"\n",
"# Confidence interval\n",
"lower = np.percentile(scores, 2.5)\n",
"upper = np.percentile(scores, 97.5)\n",
- "print('95%% Confidence interval: [%.2f, %.2f]' % (100*lower, 100*upper))"
+ "print(\"95%% Confidence interval: [%.2f, %.2f]\" % (100 * lower, 100 * upper))"
]
},
{
@@ -226,15 +226,15 @@
"tree = DecisionTreeClassifier(random_state=123)\n",
"\n",
"# Model accuracy\n",
- "scores = bootstrap_point632_score(tree, X, y, method='.632+')\n",
+ "scores = bootstrap_point632_score(tree, X, y, method=\".632+\")\n",
"acc = np.mean(scores)\n",
- "print('Accuracy: %.2f%%' % (100*acc))\n",
+ "print(\"Accuracy: %.2f%%\" % (100 * acc))\n",
"\n",
"\n",
"# Confidence interval\n",
"lower = np.percentile(scores, 2.5)\n",
"upper = np.percentile(scores, 97.5)\n",
- "print('95%% Confidence interval: [%.2f, %.2f]' % (100*lower, 100*upper))"
+ "print(\"95%% Confidence interval: [%.2f, %.2f]\" % (100 * lower, 100 * upper))"
]
},
{
@@ -374,8 +374,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/bootstrap_point632_score.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/bootstrap_point632_score.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/cochrans_q.ipynb b/docs/sources/user_guide/evaluate/cochrans_q.ipynb
index a1684b347..381ca4cfb 100644
--- a/docs/sources/user_guide/evaluate/cochrans_q.ipynb
+++ b/docs/sources/user_guide/evaluate/cochrans_q.ipynb
@@ -145,36 +145,428 @@
"\n",
"# ground truth labels of the test dataset:\n",
"\n",
- "y_true = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0])\n",
+ "y_true = np.array(\n",
+ " [\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
"\n",
"\n",
"# predictions by 3 classifiers (`y_model_1`, `y_model_2`, and `y_model_3`):\n",
"\n",
- "y_model_1 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0])\n",
- "\n",
- "y_model_2 = np.array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0])\n",
- "\n",
- "y_model_3 = np.array([1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 1, 1])"
+ "y_model_1 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
+ "\n",
+ "y_model_2 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
+ "\n",
+ "y_model_3 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 1,\n",
+ " ]\n",
+ ")"
]
},
{
@@ -199,13 +591,10 @@
}
],
"source": [
- "q, p_value = cochrans_q(y_true, \n",
- " y_model_1, \n",
- " y_model_2, \n",
- " y_model_3)\n",
+ "q, p_value = cochrans_q(y_true, y_model_1, y_model_2, y_model_3)\n",
"\n",
- "print('Q: %.3f' % q)\n",
- "print('p-value: %.3f' % p_value)"
+ "print(\"Q: %.3f\" % q)\n",
+ "print(\"p-value: %.3f\" % p_value)"
]
},
{
@@ -237,12 +626,10 @@
}
],
"source": [
- "chi2, p_value = cochrans_q(y_true, \n",
- " y_model_1, \n",
- " y_model_2)\n",
+ "chi2, p_value = cochrans_q(y_true, y_model_1, y_model_2)\n",
"\n",
- "print('Cochran\\'s Q Chi^2: %.3f' % chi2)\n",
- "print('Cochran\\'s Q p-value: %.3f' % p_value)"
+ "print(\"Cochran's Q Chi^2: %.3f\" % chi2)\n",
+ "print(\"Cochran's Q p-value: %.3f\" % p_value)"
]
},
{
@@ -260,13 +647,10 @@
}
],
"source": [
- "chi2, p_value = mcnemar(mcnemar_table(y_true, \n",
- " y_model_1, \n",
- " y_model_2),\n",
- " corrected=False)\n",
+ "chi2, p_value = mcnemar(mcnemar_table(y_true, y_model_1, y_model_2), corrected=False)\n",
"\n",
- "print('McNemar\\'s Chi^2: %.3f' % chi2)\n",
- "print('McNemar\\'s p-value: %.3f' % p_value)"
+ "print(\"McNemar's Chi^2: %.3f\" % chi2)\n",
+ "print(\"McNemar's p-value: %.3f\" % p_value)"
]
},
{
@@ -321,8 +705,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/cochrans_q.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/cochrans_q.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/combined_ftest_5x2cv.ipynb b/docs/sources/user_guide/evaluate/combined_ftest_5x2cv.ipynb
index 741c1d96e..af968a12f 100644
--- a/docs/sources/user_guide/evaluate/combined_ftest_5x2cv.ipynb
+++ b/docs/sources/user_guide/evaluate/combined_ftest_5x2cv.ipynb
@@ -120,18 +120,18 @@
"\n",
"\n",
"X, y = iris_data()\n",
- "clf1 = LogisticRegression(random_state=1, solver='liblinear', multi_class='ovr')\n",
+ "clf1 = LogisticRegression(random_state=1, solver=\"liblinear\", multi_class=\"ovr\")\n",
"clf2 = DecisionTreeClassifier(random_state=1)\n",
"\n",
- "X_train, X_test, y_train, y_test = \\\n",
- " train_test_split(X, y, test_size=0.25,\n",
- " random_state=123)\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, test_size=0.25, random_state=123\n",
+ ")\n",
"\n",
"score1 = clf1.fit(X_train, y_train).score(X_test, y_test)\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
"\n",
- "print('Logistic regression accuracy: %.2f%%' % (score1*100))\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))"
+ "print(\"Logistic regression accuracy: %.2f%%\" % (score1 * 100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))"
]
},
{
@@ -161,13 +161,10 @@
"from mlxtend.evaluate import combined_ftest_5x2cv\n",
"\n",
"\n",
- "f, p = combined_ftest_5x2cv(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "f, p = combined_ftest_5x2cv(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('F statistic: %.3f' % f)\n",
- "print('p value: %.3f' % p)"
+ "print(\"F statistic: %.3f\" % f)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -198,16 +195,13 @@
"clf2 = DecisionTreeClassifier(random_state=1, max_depth=1)\n",
"\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))\n",
"\n",
"\n",
- "f, p = combined_ftest_5x2cv(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "f, p = combined_ftest_5x2cv(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('F statistic: %.3f' % f)\n",
- "print('p value: %.3f' % p)"
+ "print(\"F statistic: %.3f\" % f)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -305,8 +299,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/combined_ftest_5x2cv.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/combined_ftest_5x2cv.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/confusion_matrix.ipynb b/docs/sources/user_guide/evaluate/confusion_matrix.ipynb
index 93060e7b7..5400c0648 100644
--- a/docs/sources/user_guide/evaluate/confusion_matrix.ipynb
+++ b/docs/sources/user_guide/evaluate/confusion_matrix.ipynb
@@ -103,11 +103,10 @@
"source": [
"from mlxtend.evaluate import confusion_matrix\n",
"\n",
- "y_target = [0, 0, 1, 0, 0, 1, 1, 1]\n",
+ "y_target = [0, 0, 1, 0, 0, 1, 1, 1]\n",
"y_predicted = [1, 0, 1, 0, 0, 0, 0, 1]\n",
"\n",
- "cm = confusion_matrix(y_target=y_target, \n",
- " y_predicted=y_predicted)\n",
+ "cm = confusion_matrix(y_target=y_target, y_predicted=y_predicted)\n",
"cm"
]
},
@@ -171,12 +170,10 @@
"source": [
"from mlxtend.evaluate import confusion_matrix\n",
"\n",
- "y_target = [1, 1, 1, 0, 0, 2, 0, 3]\n",
+ "y_target = [1, 1, 1, 0, 0, 2, 0, 3]\n",
"y_predicted = [1, 0, 1, 0, 0, 2, 1, 3]\n",
"\n",
- "cm = confusion_matrix(y_target=y_target, \n",
- " y_predicted=y_predicted, \n",
- " binary=False)\n",
+ "cm = confusion_matrix(y_target=y_target, y_predicted=y_predicted, binary=False)\n",
"cm"
]
},
@@ -246,13 +243,12 @@
"import matplotlib.pyplot as plt\n",
"from mlxtend.evaluate import confusion_matrix\n",
"\n",
- "y_target = [1, 1, 1, 0, 0, 2, 0, 3]\n",
+ "y_target = [1, 1, 1, 0, 0, 2, 0, 3]\n",
"y_predicted = [1, 0, 1, 0, 0, 2, 1, 3]\n",
"\n",
- "cm = confusion_matrix(y_target=y_target, \n",
- " y_predicted=y_predicted, \n",
- " binary=True, \n",
- " positive_label=1)\n",
+ "cm = confusion_matrix(\n",
+ " y_target=y_target, y_predicted=y_predicted, binary=True, positive_label=1\n",
+ ")\n",
"cm"
]
},
@@ -344,8 +340,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/confusion_matrix.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/confusion_matrix.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/create_counterfactual.ipynb b/docs/sources/user_guide/evaluate/create_counterfactual.ipynb
index 602289ea4..2b8d620b4 100644
--- a/docs/sources/user_guide/evaluate/create_counterfactual.ipynb
+++ b/docs/sources/user_guide/evaluate/create_counterfactual.ipynb
@@ -127,10 +127,12 @@
"\n",
"x_ref = X[15]\n",
"\n",
- "print('True label:', y[15])\n",
- "print('Predicted label:', clf.predict(x_ref.reshape(1, -1))[0])\n",
- "print('Predicted probas:', clf.predict_proba(x_ref.reshape(1, -1)))\n",
- "print('Predicted probability for label 0:', clf.predict_proba(x_ref.reshape(1, -1))[0][0])"
+ "print(\"True label:\", y[15])\n",
+ "print(\"Predicted label:\", clf.predict(x_ref.reshape(1, -1))[0])\n",
+ "print(\"Predicted probas:\", clf.predict_proba(x_ref.reshape(1, -1)))\n",
+ "print(\n",
+ " \"Predicted probability for label 0:\", clf.predict_proba(x_ref.reshape(1, -1))[0][0]\n",
+ ")"
]
},
{
@@ -162,20 +164,22 @@
"from mlxtend.evaluate import create_counterfactual\n",
"\n",
"\n",
- "res = create_counterfactual(x_reference=x_ref, \n",
- " y_desired=2, \n",
- " model=clf, \n",
- " X_dataset=X,\n",
- " y_desired_proba=1.,\n",
- " lammbda=1, # hyperparameter\n",
- " random_seed=123)\n",
+ "res = create_counterfactual(\n",
+ " x_reference=x_ref,\n",
+ " y_desired=2,\n",
+ " model=clf,\n",
+ " X_dataset=X,\n",
+ " y_desired_proba=1.0,\n",
+ " lammbda=1, # hyperparameter\n",
+ " random_seed=123,\n",
+ ")\n",
"\n",
- "print('Features of the 16th training example:', x_ref)\n",
- "print('Features of the countefactual:', res)\n",
+ "print(\"Features of the 16th training example:\", x_ref)\n",
+ "print(\"Features of the countefactual:\", res)\n",
"\n",
- "print('Predictions for counterfactual:\\n')\n",
- "print('Predicted label:', clf.predict(res.reshape(1, -1))[0])\n",
- "print('Predicted probas:', clf.predict_proba(res.reshape(1, -1)))"
+ "print(\"Predictions for counterfactual:\\n\")\n",
+ "print(\"Predicted label:\", clf.predict(res.reshape(1, -1))[0])\n",
+ "print(\"Predicted probas:\", clf.predict_proba(res.reshape(1, -1)))"
]
},
{
@@ -251,15 +255,16 @@
"ax = plot_decision_regions(X, y, clf=clf, legend=2)\n",
"\n",
"\n",
- "scatter_highlight_defaults = {'c': 'red',\n",
- " 'edgecolor': 'yellow',\n",
- " 'alpha': 1.0,\n",
- " 'linewidths': 2,\n",
- " 'marker': 'o',\n",
- " 's': 80}\n",
+ "scatter_highlight_defaults = {\n",
+ " \"c\": \"red\",\n",
+ " \"edgecolor\": \"yellow\",\n",
+ " \"alpha\": 1.0,\n",
+ " \"linewidths\": 2,\n",
+ " \"marker\": \"o\",\n",
+ " \"s\": 80,\n",
+ "}\n",
"\n",
- "ax.scatter(*X[15],\n",
- " **scatter_highlight_defaults)\n",
+ "ax.scatter(*X[15], **scatter_highlight_defaults)\n",
"plt.show()"
]
},
@@ -291,18 +296,19 @@
}
],
"source": [
- "counterfact = create_counterfactual(x_reference=X[15], \n",
- " y_desired=2, \n",
- " model=clf, \n",
- " X_dataset=X,\n",
- " y_desired_proba=1.0,\n",
- " lammbda=1, \n",
- " random_seed=123)\n",
+ "counterfact = create_counterfactual(\n",
+ " x_reference=X[15],\n",
+ " y_desired=2,\n",
+ " model=clf,\n",
+ " X_dataset=X,\n",
+ " y_desired_proba=1.0,\n",
+ " lammbda=1,\n",
+ " random_seed=123,\n",
+ ")\n",
"\n",
"\n",
"ax = plot_decision_regions(X, y, clf=clf, legend=2)\n",
- "ax.scatter(*counterfact,\n",
- " **scatter_highlight_defaults)\n",
+ "ax.scatter(*counterfact, **scatter_highlight_defaults)\n",
"plt.show()"
]
},
@@ -388,19 +394,18 @@
],
"source": [
"for i in [0.4, 0.5, 1.0, 5.0, 100]:\n",
- "\n",
- " counterfact = create_counterfactual(x_reference=X[15], \n",
- " y_desired=2, \n",
- " model=clf, \n",
- " X_dataset=X,\n",
- " y_desired_proba=1.0,\n",
- " lammbda=i, \n",
- " random_seed=123)\n",
- "\n",
+ " counterfact = create_counterfactual(\n",
+ " x_reference=X[15],\n",
+ " y_desired=2,\n",
+ " model=clf,\n",
+ " X_dataset=X,\n",
+ " y_desired_proba=1.0,\n",
+ " lammbda=i,\n",
+ " random_seed=123,\n",
+ " )\n",
"\n",
" ax = plot_decision_regions(X, y, clf=clf, legend=2)\n",
- " ax.scatter(*counterfact,\n",
- " **scatter_highlight_defaults)\n",
+ " ax.scatter(*counterfact, **scatter_highlight_defaults)\n",
"\n",
" plt.show()"
]
@@ -464,32 +469,31 @@
"desired_class_2_proba = 1.0\n",
"\n",
"for i in np.arange(0, 10000, 0.1):\n",
- "\n",
- " counterfact = create_counterfactual(x_reference=X[15], \n",
- " y_desired=2, \n",
- " model=clf, \n",
- " X_dataset=X,\n",
- " y_desired_proba=desired_class_2_proba,\n",
- " lammbda=i, \n",
- " random_seed=123)\n",
+ " counterfact = create_counterfactual(\n",
+ " x_reference=X[15],\n",
+ " y_desired=2,\n",
+ " model=clf,\n",
+ " X_dataset=X,\n",
+ " y_desired_proba=desired_class_2_proba,\n",
+ " lammbda=i,\n",
+ " random_seed=123,\n",
+ " )\n",
"\n",
" predicted_class_2_proba = clf.predict_proba(counterfact.reshape(1, -1))[0][2]\n",
"\n",
" if not i:\n",
- " print('Initial lambda:', i)\n",
- " print('Initial diff:', np.abs(predicted_class_2_proba - desired_class_2_proba))\n",
- " \n",
- " \n",
+ " print(\"Initial lambda:\", i)\n",
+ " print(\"Initial diff:\", np.abs(predicted_class_2_proba - desired_class_2_proba))\n",
+ "\n",
" if not np.abs(predicted_class_2_proba - desired_class_2_proba) > 0.3:\n",
" break\n",
- " \n",
- " \n",
+ "\n",
+ "\n",
"ax = plot_decision_regions(X, y, clf=clf, legend=2)\n",
- "ax.scatter(*counterfact,\n",
- " **scatter_highlight_defaults)\n",
+ "ax.scatter(*counterfact, **scatter_highlight_defaults)\n",
"\n",
- "print('Final lambda:', i)\n",
- "print('Final diff:', np.abs(predicted_class_2_proba - desired_class_2_proba))\n",
+ "print(\"Final lambda:\", i)\n",
+ "print(\"Final diff:\", np.abs(predicted_class_2_proba - desired_class_2_proba))\n",
"\n",
"\n",
"plt.show()"
@@ -580,8 +584,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/create_counterfactual.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/create_counterfactual.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
},
diff --git a/docs/sources/user_guide/evaluate/feature_importance_permutation.ipynb b/docs/sources/user_guide/evaluate/feature_importance_permutation.ipynb
index 392acb665..d3be20179 100644
--- a/docs/sources/user_guide/evaluate/feature_importance_permutation.ipynb
+++ b/docs/sources/user_guide/evaluate/feature_importance_permutation.ipynb
@@ -107,17 +107,20 @@
"from sklearn.ensemble import RandomForestClassifier\n",
"\n",
"# Build a classification task using 3 informative features\n",
- "X, y = make_classification(n_samples=10000,\n",
- " n_features=10,\n",
- " n_informative=3,\n",
- " n_redundant=0,\n",
- " n_repeated=0,\n",
- " n_classes=2,\n",
- " random_state=0,\n",
- " shuffle=False)\n",
+ "X, y = make_classification(\n",
+ " n_samples=10000,\n",
+ " n_features=10,\n",
+ " n_informative=3,\n",
+ " n_redundant=0,\n",
+ " n_repeated=0,\n",
+ " n_classes=2,\n",
+ " random_state=0,\n",
+ " shuffle=False,\n",
+ ")\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.3, random_state=1, stratify=y)"
+ " X, y, test_size=0.3, random_state=1, stratify=y\n",
+ ")"
]
},
{
@@ -151,13 +154,12 @@
}
],
"source": [
- "forest = RandomForestClassifier(n_estimators=250,\n",
- " random_state=0)\n",
+ "forest = RandomForestClassifier(n_estimators=250, random_state=0)\n",
"\n",
"forest.fit(X_train, y_train)\n",
"\n",
- "print('Training accuracy:', np.mean(forest.predict(X_train) == y_train)*100)\n",
- "print('Test accuracy:', np.mean(forest.predict(X_test) == y_test)*100)\n",
+ "print(\"Training accuracy:\", np.mean(forest.predict(X_train) == y_train) * 100)\n",
+ "print(\"Test accuracy:\", np.mean(forest.predict(X_test) == y_test) * 100)\n",
"\n",
"importance_vals = forest.feature_importances_\n",
"print(importance_vals)"
@@ -196,15 +198,13 @@
}
],
"source": [
- "std = np.std([tree.feature_importances_ for tree in forest.estimators_],\n",
- " axis=0)\n",
+ "std = np.std([tree.feature_importances_ for tree in forest.estimators_], axis=0)\n",
"indices = np.argsort(importance_vals)[::-1]\n",
"\n",
"# Plot the feature importances of the forest\n",
"plt.figure()\n",
"plt.title(\"Random Forest feature importance\")\n",
- "plt.bar(range(X.shape[1]), importance_vals[indices],\n",
- " yerr=std[indices], align=\"center\")\n",
+ "plt.bar(range(X.shape[1]), importance_vals[indices], yerr=std[indices], align=\"center\")\n",
"plt.xticks(range(X.shape[1]), indices)\n",
"plt.xlim([-1, X.shape[1]])\n",
"plt.ylim([0, 0.5])\n",
@@ -244,12 +244,13 @@
],
"source": [
"imp_vals, _ = feature_importance_permutation(\n",
- " predict_method=forest.predict, \n",
+ " predict_method=forest.predict,\n",
" X=X_test,\n",
" y=y_test,\n",
- " metric='accuracy',\n",
+ " metric=\"accuracy\",\n",
" num_rounds=1,\n",
- " seed=1)\n",
+ " seed=1,\n",
+ ")\n",
"\n",
"imp_vals"
]
@@ -338,12 +339,13 @@
],
"source": [
"imp_vals, imp_all = feature_importance_permutation(\n",
- " predict_method=forest.predict, \n",
+ " predict_method=forest.predict,\n",
" X=X_test,\n",
" y=y_test,\n",
- " metric='accuracy',\n",
+ " metric=\"accuracy\",\n",
" num_rounds=10,\n",
- " seed=1)\n",
+ " seed=1,\n",
+ ")\n",
"\n",
"\n",
"std = np.std(imp_all, axis=1)\n",
@@ -351,8 +353,7 @@
"\n",
"plt.figure()\n",
"plt.title(\"Random Forest feature importance via permutation importance w. std. dev.\")\n",
- "plt.bar(range(X.shape[1]), imp_vals[indices],\n",
- " yerr=std[indices])\n",
+ "plt.bar(range(X.shape[1]), imp_vals[indices], yerr=std[indices])\n",
"plt.xticks(range(X.shape[1]), indices)\n",
"plt.xlim([-1, X.shape[1]])\n",
"plt.show()"
@@ -394,14 +395,11 @@
}
],
"source": [
- "from sklearn.svm import SVC\n",
- "\n",
- "\n",
- "svm = SVC(C=1.0, kernel='rbf')\n",
+ "svm = SVC(C=1.0, kernel=\"rbf\")\n",
"svm.fit(X_train, y_train)\n",
"\n",
- "print('Training accuracy', np.mean(svm.predict(X_train) == y_train)*100)\n",
- "print('Test accuracy', np.mean(svm.predict(X_test) == y_test)*100)"
+ "print(\"Training accuracy\", np.mean(svm.predict(X_train) == y_train) * 100)\n",
+ "print(\"Test accuracy\", np.mean(svm.predict(X_test) == y_test) * 100)"
]
},
{
@@ -424,12 +422,13 @@
],
"source": [
"imp_vals, imp_all = feature_importance_permutation(\n",
- " predict_method=svm.predict, \n",
+ " predict_method=svm.predict,\n",
" X=X_test,\n",
" y=y_test,\n",
- " metric='accuracy',\n",
+ " metric=\"accuracy\",\n",
" num_rounds=10,\n",
- " seed=1)\n",
+ " seed=1,\n",
+ ")\n",
"\n",
"\n",
"std = np.std(imp_all, axis=1)\n",
@@ -437,8 +436,7 @@
"\n",
"plt.figure()\n",
"plt.title(\"SVM feature importance via permutation importance\")\n",
- "plt.bar(range(X.shape[1]), imp_vals[indices],\n",
- " yerr=std[indices])\n",
+ "plt.bar(range(X.shape[1]), imp_vals[indices], yerr=std[indices])\n",
"plt.xticks(range(X.shape[1]), indices)\n",
"plt.xlim([-1, X.shape[1]])\n",
"plt.show()"
@@ -476,26 +474,25 @@
"from sklearn.svm import SVR\n",
"\n",
"\n",
- "X, y = make_regression(n_samples=1000,\n",
- " n_features=5,\n",
- " n_informative=2,\n",
- " n_targets=1,\n",
- " random_state=123,\n",
- " shuffle=False)\n",
+ "X, y = make_regression(\n",
+ " n_samples=1000,\n",
+ " n_features=5,\n",
+ " n_informative=2,\n",
+ " n_targets=1,\n",
+ " random_state=123,\n",
+ " shuffle=False,\n",
+ ")\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.3, random_state=123) \n",
+ " X, y, test_size=0.3, random_state=123\n",
+ ")\n",
"\n",
- "svm = SVR(kernel='rbf')\n",
+ "svm = SVR(kernel=\"rbf\")\n",
"svm.fit(X_train, y_train)\n",
"\n",
"imp_vals, _ = feature_importance_permutation(\n",
- " predict_method=svm.predict, \n",
- " X=X_test,\n",
- " y=y_test,\n",
- " metric='r2',\n",
- " num_rounds=1,\n",
- " seed=1)\n",
+ " predict_method=svm.predict, X=X_test, y=y_test, metric=\"r2\", num_rounds=1, seed=1\n",
+ ")\n",
"\n",
"imp_vals"
]
@@ -653,7 +650,9 @@
"import pandas as pd\n",
"\n",
"\n",
- "df_data = pd.read_csv('https://gist.githubusercontent.com/rasbt/b99bf69079bc0d601eeae8a49248d358/raw/a114be9801647ec5460089f3a9576713dabf5f1f/onehot-numeric-mixed-data.csv')\n",
+ "df_data = pd.read_csv(\n",
+ " \"https://gist.githubusercontent.com/rasbt/b99bf69079bc0d601eeae8a49248d358/raw/a114be9801647ec5460089f3a9576713dabf5f1f/onehot-numeric-mixed-data.csv\"\n",
+ ")\n",
"df_data.head()"
]
},
@@ -666,12 +665,13 @@
"from sklearn.model_selection import train_test_split\n",
"\n",
"\n",
- "df_X = df_data[['measurement1', 'measurement2', 'measurement3', 'categorical']]\n",
- "df_y = df_data['label']\n",
+ "df_X = df_data[[\"measurement1\", \"measurement2\", \"measurement3\", \"categorical\"]]\n",
+ "df_y = df_data[\"label\"]\n",
"\n",
"\n",
"df_X_train, df_X_test, df_y_train, df_y_test = train_test_split(\n",
- " df_X, df_y, test_size=0.33, random_state=42, stratify=df_y)"
+ " df_X, df_y, test_size=0.33, random_state=42, stratify=df_y\n",
+ ")"
]
},
{
@@ -691,14 +691,14 @@
"import numpy as np\n",
"\n",
"\n",
- "ohe = OneHotEncoder(drop='first')\n",
- "ohe.fit(df_X_train[['categorical']])\n",
+ "ohe = OneHotEncoder(drop=\"first\")\n",
+ "ohe.fit(df_X_train[[\"categorical\"]])\n",
"\n",
- "df_X_train_ohe = df_X_train.drop(columns=['categorical'])\n",
- "df_X_test_ohe = df_X_test.drop(columns=['categorical'])\n",
+ "df_X_train_ohe = df_X_train.drop(columns=[\"categorical\"])\n",
+ "df_X_test_ohe = df_X_test.drop(columns=[\"categorical\"])\n",
"\n",
- "ohe_train = np.asarray(ohe.transform(df_X_train[['categorical']]).todense())\n",
- "ohe_test = np.asarray(ohe.transform(df_X_test[['categorical']]).todense())\n",
+ "ohe_train = np.asarray(ohe.transform(df_X_train[[\"categorical\"]]).todense())\n",
+ "ohe_test = np.asarray(ohe.transform(df_X_test[[\"categorical\"]]).todense())\n",
"\n",
"X_train_ohe = np.hstack((df_X_train_ohe.values, ohe_train))\n",
"X_test_ohe = np.hstack((df_X_test_ohe.values, ohe_test))"
@@ -752,26 +752,19 @@
"from sklearn.model_selection import GridSearchCV\n",
"\n",
"\n",
- "pipe = make_pipeline(StandardScaler(),\n",
- " MLPClassifier(max_iter=10000, random_state=123))\n",
+ "pipe = make_pipeline(StandardScaler(), MLPClassifier(max_iter=10000, random_state=123))\n",
"\n",
"params = {\n",
- " 'mlpclassifier__hidden_layer_sizes': [(30, 20, 10), \n",
- " (20, 10), \n",
- " (20,),\n",
- " (10,)],\n",
- " 'mlpclassifier__activation': ['tanh', 'relu'],\n",
- " 'mlpclassifier__solver': ['sgd'],\n",
- " 'mlpclassifier__alpha': [0.0001],\n",
- " 'mlpclassifier__learning_rate': ['adaptive'],\n",
+ " \"mlpclassifier__hidden_layer_sizes\": [(30, 20, 10), (20, 10), (20,), (10,)],\n",
+ " \"mlpclassifier__activation\": [\"tanh\", \"relu\"],\n",
+ " \"mlpclassifier__solver\": [\"sgd\"],\n",
+ " \"mlpclassifier__alpha\": [0.0001],\n",
+ " \"mlpclassifier__learning_rate\": [\"adaptive\"],\n",
"}\n",
"\n",
- "gs = GridSearchCV(estimator=pipe, \n",
- " param_grid=params, \n",
- " scoring='accuracy', \n",
- " refit=True,\n",
- " n_jobs=-1,\n",
- " cv=10)\n",
+ "gs = GridSearchCV(\n",
+ " estimator=pipe, param_grid=params, scoring=\"accuracy\", refit=True, n_jobs=-1, cv=10\n",
+ ")\n",
"\n",
"gs = gs.fit(X_train_ohe, df_y_train.values)\n",
"model = gs.best_estimator_"
@@ -807,12 +800,13 @@
"outputs": [],
"source": [
"imp_vals, imp_all = feature_importance_permutation(\n",
- " predict_method=model.predict, \n",
+ " predict_method=model.predict,\n",
" X=X_test_ohe,\n",
" y=df_y_test.values,\n",
- " metric='accuracy',\n",
+ " metric=\"accuracy\",\n",
" num_rounds=50,\n",
- " seed=1)"
+ " seed=1,\n",
+ ")"
]
},
{
@@ -821,8 +815,9 @@
"metadata": {},
"outputs": [],
"source": [
- "feat_names_with_ohe = ['measurement1', 'measurement2', 'measurement3'] \\\n",
- " + [f'categorical_ohe_{i}' for i in range(2, 20)]"
+ "feat_names_with_ohe = [\"measurement1\", \"measurement2\", \"measurement3\"] + [\n",
+ " f\"categorical_ohe_{i}\" for i in range(2, 20)\n",
+ "]"
]
},
{
@@ -852,11 +847,11 @@
"indices = np.argsort(imp_vals)[::-1]\n",
"\n",
"plt.figure()\n",
- "#plt.title(\"Feature importance via permutation importance w. std. dev.\")\n",
- "plt.bar(range(len(feat_names_with_ohe)), imp_vals[indices],\n",
- " yerr=std[indices])\n",
- "plt.xticks(range(len(feat_names_with_ohe)),\n",
- " np.array(feat_names_with_ohe)[indices], rotation=90)\n",
+ "# plt.title(\"Feature importance via permutation importance w. std. dev.\")\n",
+ "plt.bar(range(len(feat_names_with_ohe)), imp_vals[indices], yerr=std[indices])\n",
+ "plt.xticks(\n",
+ " range(len(feat_names_with_ohe)), np.array(feat_names_with_ohe)[indices], rotation=90\n",
+ ")\n",
"plt.xlim([-1, len(feat_names_with_ohe)])\n",
"plt.show()"
]
@@ -891,13 +886,14 @@
"feature_groups = [0, 1, 2, range(3, 21)]\n",
"\n",
"imp_vals, imp_all = feature_importance_permutation(\n",
- " predict_method=model.predict, \n",
+ " predict_method=model.predict,\n",
" X=X_test_ohe,\n",
" y=df_y_test.values,\n",
- " metric='accuracy',\n",
+ " metric=\"accuracy\",\n",
" num_rounds=50,\n",
" feature_groups=feature_groups,\n",
- " seed=1)"
+ " seed=1,\n",
+ ")"
]
},
{
@@ -919,16 +915,14 @@
}
],
"source": [
- "feature_names = ['measurement1', 'measurement2', 'measurement3', 'categorical']\n",
+ "feature_names = [\"measurement1\", \"measurement2\", \"measurement3\", \"categorical\"]\n",
"\n",
"std = np.std(imp_all, axis=1)\n",
"indices = np.argsort(imp_vals)[::-1]\n",
"\n",
"plt.figure()\n",
- "plt.bar(range(len(feature_names)), imp_vals[indices],\n",
- " yerr=std[indices])\n",
- "plt.xticks(range(len(feature_names)),\n",
- " np.array(feature_names)[indices], rotation=90)\n",
+ "plt.bar(range(len(feature_names)), imp_vals[indices], yerr=std[indices])\n",
+ "plt.xticks(range(len(feature_names)), np.array(feature_names)[indices], rotation=90)\n",
"plt.xlim([-1, len(feature_names)])\n",
"plt.show()"
]
@@ -1025,8 +1019,10 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/feature_importance_permutation.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\n",
+ " \"../../api_modules/mlxtend.evaluate/feature_importance_permutation.md\", \"r\"\n",
+ ") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
},
diff --git a/docs/sources/user_guide/evaluate/ftest.ipynb b/docs/sources/user_guide/evaluate/ftest.ipynb
index 0d911cf00..de47a122b 100644
--- a/docs/sources/user_guide/evaluate/ftest.ipynb
+++ b/docs/sources/user_guide/evaluate/ftest.ipynb
@@ -124,36 +124,428 @@
"\n",
"# ground truth labels of the test dataset:\n",
"\n",
- "y_true = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0])\n",
+ "y_true = np.array(\n",
+ " [\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
"\n",
"\n",
"# predictions by 3 classifiers (`y_model_1`, `y_model_2`, and `y_model_3`):\n",
"\n",
- "y_model_1 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0])\n",
- "\n",
- "y_model_2 = np.array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0])\n",
- "\n",
- "y_model_3 = np.array([1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 1, 1])"
+ "y_model_1 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
+ "\n",
+ "y_model_2 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
+ "\n",
+ "y_model_3 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 1,\n",
+ " ]\n",
+ ")"
]
},
{
@@ -178,13 +570,10 @@
}
],
"source": [
- "f, p_value = ftest(y_true, \n",
- " y_model_1, \n",
- " y_model_2, \n",
- " y_model_3)\n",
+ "f, p_value = ftest(y_true, y_model_1, y_model_2, y_model_3)\n",
"\n",
- "print('F: %.3f' % f)\n",
- "print('p-value: %.3f' % p_value)"
+ "print(\"F: %.3f\" % f)\n",
+ "print(\"p-value: %.3f\" % p_value)"
]
},
{
@@ -246,8 +635,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/ftest.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/ftest.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/lift_score.ipynb b/docs/sources/user_guide/evaluate/lift_score.ipynb
index 5e68d5d84..fd62c98c1 100644
--- a/docs/sources/user_guide/evaluate/lift_score.ipynb
+++ b/docs/sources/user_guide/evaluate/lift_score.ipynb
@@ -133,7 +133,7 @@
"import numpy as np\n",
"from mlxtend.evaluate import lift_score\n",
"\n",
- "y_target = np.array([0, 0, 1, 0, 0, 1, 1, 1, 1, 1])\n",
+ "y_target = np.array([0, 0, 1, 0, 0, 1, 1, 1, 1, 1])\n",
"y_predicted = np.array([1, 0, 1, 0, 0, 0, 0, 1, 0, 0])\n",
"\n",
"lift_score(y_target, y_predicted)"
@@ -182,14 +182,15 @@
"X, y = iris.data, iris.target\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.2, stratify=y, random_state=123)\n",
+ " X, y, test_size=0.2, stratify=y, random_state=123\n",
+ ")\n",
"\n",
- "hyperparameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],\n",
- " 'C': [1, 10, 100, 1000]},\n",
- " {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]\n",
+ "hyperparameters = [\n",
+ " {\"kernel\": [\"rbf\"], \"gamma\": [1e-3, 1e-4], \"C\": [1, 10, 100, 1000]},\n",
+ " {\"kernel\": [\"linear\"], \"C\": [1, 10, 100, 1000]},\n",
+ "]\n",
"\n",
- "clf = GridSearchCV(SVC(), hyperparameters, cv=10,\n",
- " scoring=lift_scorer)\n",
+ "clf = GridSearchCV(SVC(), hyperparameters, cv=10, scoring=lift_scorer)\n",
"clf.fit(X_train, y_train)\n",
"\n",
"print(clf.best_score_)\n",
@@ -262,8 +263,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/lift_score.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/lift_score.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/mcnemar.ipynb b/docs/sources/user_guide/evaluate/mcnemar.ipynb
index 52588cc6c..2e20d2c6a 100644
--- a/docs/sources/user_guide/evaluate/mcnemar.ipynb
+++ b/docs/sources/user_guide/evaluate/mcnemar.ipynb
@@ -192,9 +192,7 @@
"# Class labels predicted by model 2\n",
"y_model2 = np.array([0, 0, 1, 1, 0, 1, 1, 0, 0, 0])\n",
"\n",
- "tb = mcnemar_table(y_target=y_target, \n",
- " y_model1=y_model1, \n",
- " y_model2=y_model2)\n",
+ "tb = mcnemar_table(y_target=y_target, y_model1=y_model1, y_model2=y_model2)\n",
"\n",
"print(tb)"
]
@@ -228,8 +226,7 @@
"source": [
"import numpy as np\n",
"\n",
- "tb_b = np.array([[9945, 25],\n",
- " [15, 15]])"
+ "tb_b = np.array([[9945, 25], [15, 15]])"
]
},
{
@@ -257,8 +254,8 @@
"from mlxtend.evaluate import mcnemar\n",
"\n",
"chi2, p = mcnemar(ary=tb_b, corrected=True)\n",
- "print('chi-squared:', chi2)\n",
- "print('p-value:', p)"
+ "print(\"chi-squared:\", chi2)\n",
+ "print(\"p-value:\", p)"
]
},
{
@@ -314,13 +311,12 @@
"from mlxtend.evaluate import mcnemar\n",
"import numpy as np\n",
"\n",
- "tb_a = np.array([[9959, 11],\n",
- " [1, 29]])\n",
+ "tb_a = np.array([[9959, 11], [1, 29]])\n",
"\n",
"chi2, p = mcnemar(ary=tb_a, exact=True)\n",
"\n",
- "print('chi-squared:', chi2)\n",
- "print('p-value:', p)"
+ "print(\"chi-squared:\", chi2)\n",
+ "print(\"p-value:\", p)"
]
},
{
@@ -392,8 +388,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/mcnemar.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/mcnemar.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
},
diff --git a/docs/sources/user_guide/evaluate/mcnemar_table.ipynb b/docs/sources/user_guide/evaluate/mcnemar_table.ipynb
index 98f979c31..4e5241565 100644
--- a/docs/sources/user_guide/evaluate/mcnemar_table.ipynb
+++ b/docs/sources/user_guide/evaluate/mcnemar_table.ipynb
@@ -124,9 +124,7 @@
"y_mod1 = np.array([0, 1, 0, 0, 0, 1, 1, 0, 0, 0])\n",
"y_mod2 = np.array([0, 0, 1, 1, 0, 1, 1, 0, 0, 0])\n",
"\n",
- "tb = mcnemar_table(y_target=y_true, \n",
- " y_model1=y_mod1, \n",
- " y_model2=y_mod2)\n",
+ "tb = mcnemar_table(y_target=y_true, y_model1=y_mod1, y_model2=y_mod2)\n",
"\n",
"tb"
]
@@ -158,11 +156,13 @@
"from mlxtend.plotting import checkerboard_plot\n",
"import matplotlib.pyplot as plt\n",
"\n",
- "brd = checkerboard_plot(tb,\n",
- " figsize=(3, 3),\n",
- " fmt='%d',\n",
- " col_labels=['model 2 wrong', 'model 2 right'],\n",
- " row_labels=['model 1 wrong', 'model 1 right'])\n",
+ "brd = checkerboard_plot(\n",
+ " tb,\n",
+ " figsize=(3, 3),\n",
+ " fmt=\"%d\",\n",
+ " col_labels=[\"model 2 wrong\", \"model 2 right\"],\n",
+ " row_labels=[\"model 1 wrong\", \"model 1 right\"],\n",
+ ")\n",
"plt.show()"
]
},
@@ -222,8 +222,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/mcnemar_table.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/mcnemar_table.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/mcnemar_tables.ipynb b/docs/sources/user_guide/evaluate/mcnemar_tables.ipynb
index ecb867441..ae793d31f 100644
--- a/docs/sources/user_guide/evaluate/mcnemar_tables.ipynb
+++ b/docs/sources/user_guide/evaluate/mcnemar_tables.ipynb
@@ -119,9 +119,7 @@
"y_mod0 = np.array([0, 1, 0, 0, 0, 1, 1, 0, 0, 0])\n",
"y_mod1 = np.array([0, 0, 1, 1, 0, 1, 1, 0, 0, 0])\n",
"\n",
- "tb = mcnemar_tables(y_true, \n",
- " y_mod0, \n",
- " y_mod1)\n",
+ "tb = mcnemar_tables(y_true, y_mod0, y_mod1)\n",
"\n",
"tb"
]
@@ -153,11 +151,13 @@
"from mlxtend.plotting import checkerboard_plot\n",
"import matplotlib.pyplot as plt\n",
"\n",
- "brd = checkerboard_plot(tb['model_0 vs model_1'],\n",
- " figsize=(3, 3),\n",
- " fmt='%d',\n",
- " col_labels=['model 2 wrong', 'model 2 right'],\n",
- " row_labels=['model 1 wrong', 'model 1 right'])\n",
+ "brd = checkerboard_plot(\n",
+ " tb[\"model_0 vs model_1\"],\n",
+ " figsize=(3, 3),\n",
+ " fmt=\"%d\",\n",
+ " col_labels=[\"model 2 wrong\", \"model 2 right\"],\n",
+ " row_labels=[\"model 1 wrong\", \"model 1 right\"],\n",
+ ")\n",
"plt.show()"
]
},
@@ -209,13 +209,10 @@
"y_mod1 = np.array([0, 0, 1, 1, 0, 1, 1, 0, 0, 0])\n",
"y_mod2 = np.array([0, 0, 1, 1, 0, 1, 1, 0, 1, 0])\n",
"\n",
- "tb = mcnemar_tables(y_true, \n",
- " y_mod0, \n",
- " y_mod1,\n",
- " y_mod2)\n",
+ "tb = mcnemar_tables(y_true, y_mod0, y_mod1, y_mod2)\n",
"\n",
"for key, value in tb.items():\n",
- " print(key, '\\n', value, '\\n')"
+ " print(key, \"\\n\", value, \"\\n\")"
]
},
{
@@ -298,8 +295,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/mcnemar_tables.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/mcnemar_tables.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
},
diff --git a/docs/sources/user_guide/evaluate/paired_ttest_5x2cv.ipynb b/docs/sources/user_guide/evaluate/paired_ttest_5x2cv.ipynb
index 63414aeef..f69e97f69 100644
--- a/docs/sources/user_guide/evaluate/paired_ttest_5x2cv.ipynb
+++ b/docs/sources/user_guide/evaluate/paired_ttest_5x2cv.ipynb
@@ -118,15 +118,15 @@
"clf1 = LogisticRegression(random_state=1)\n",
"clf2 = DecisionTreeClassifier(random_state=1)\n",
"\n",
- "X_train, X_test, y_train, y_test = \\\n",
- " train_test_split(X, y, test_size=0.25,\n",
- " random_state=123)\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, test_size=0.25, random_state=123\n",
+ ")\n",
"\n",
"score1 = clf1.fit(X_train, y_train).score(X_test, y_test)\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
"\n",
- "print('Logistic regression accuracy: %.2f%%' % (score1*100))\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))"
+ "print(\"Logistic regression accuracy: %.2f%%\" % (score1 * 100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))"
]
},
{
@@ -156,13 +156,10 @@
"from mlxtend.evaluate import paired_ttest_5x2cv\n",
"\n",
"\n",
- "t, p = paired_ttest_5x2cv(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "t, p = paired_ttest_5x2cv(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('t statistic: %.3f' % t)\n",
- "print('p value: %.3f' % p)"
+ "print(\"t statistic: %.3f\" % t)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -193,16 +190,13 @@
"clf2 = DecisionTreeClassifier(random_state=1, max_depth=1)\n",
"\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))\n",
"\n",
"\n",
- "t, p = paired_ttest_5x2cv(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "t, p = paired_ttest_5x2cv(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('t statistic: %.3f' % t)\n",
- "print('p value: %.3f' % p)"
+ "print(\"t statistic: %.3f\" % t)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -300,8 +294,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/paired_ttest_5x2cv.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/paired_ttest_5x2cv.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/paired_ttest_kfold_cv.ipynb b/docs/sources/user_guide/evaluate/paired_ttest_kfold_cv.ipynb
index dc6d41c84..b87528ff3 100644
--- a/docs/sources/user_guide/evaluate/paired_ttest_kfold_cv.ipynb
+++ b/docs/sources/user_guide/evaluate/paired_ttest_kfold_cv.ipynb
@@ -108,15 +108,15 @@
"clf1 = LogisticRegression(random_state=1)\n",
"clf2 = DecisionTreeClassifier(random_state=1)\n",
"\n",
- "X_train, X_test, y_train, y_test = \\\n",
- " train_test_split(X, y, test_size=0.25,\n",
- " random_state=123)\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, test_size=0.25, random_state=123\n",
+ ")\n",
"\n",
"score1 = clf1.fit(X_train, y_train).score(X_test, y_test)\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
"\n",
- "print('Logistic regression accuracy: %.2f%%' % (score1*100))\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))"
+ "print(\"Logistic regression accuracy: %.2f%%\" % (score1 * 100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))"
]
},
{
@@ -146,13 +146,10 @@
"from mlxtend.evaluate import paired_ttest_kfold_cv\n",
"\n",
"\n",
- "t, p = paired_ttest_kfold_cv(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "t, p = paired_ttest_kfold_cv(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('t statistic: %.3f' % t)\n",
- "print('p value: %.3f' % p)"
+ "print(\"t statistic: %.3f\" % t)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -183,16 +180,13 @@
"clf2 = DecisionTreeClassifier(random_state=1, max_depth=1)\n",
"\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))\n",
"\n",
"\n",
- "t, p = paired_ttest_kfold_cv(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "t, p = paired_ttest_kfold_cv(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('t statistic: %.3f' % t)\n",
- "print('p value: %.3f' % p)"
+ "print(\"t statistic: %.3f\" % t)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -303,8 +297,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/paired_ttest_kfold_cv.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/paired_ttest_kfold_cv.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/paired_ttest_resampled.ipynb b/docs/sources/user_guide/evaluate/paired_ttest_resampled.ipynb
index 9bf773e3d..523ffa30f 100644
--- a/docs/sources/user_guide/evaluate/paired_ttest_resampled.ipynb
+++ b/docs/sources/user_guide/evaluate/paired_ttest_resampled.ipynb
@@ -120,15 +120,15 @@
"clf1 = LogisticRegression(random_state=1)\n",
"clf2 = DecisionTreeClassifier(random_state=1)\n",
"\n",
- "X_train, X_test, y_train, y_test = \\\n",
- " train_test_split(X, y, test_size=0.25,\n",
- " random_state=123)\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, test_size=0.25, random_state=123\n",
+ ")\n",
"\n",
"score1 = clf1.fit(X_train, y_train).score(X_test, y_test)\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
"\n",
- "print('Logistic regression accuracy: %.2f%%' % (score1*100))\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))"
+ "print(\"Logistic regression accuracy: %.2f%%\" % (score1 * 100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))"
]
},
{
@@ -158,13 +158,10 @@
"from mlxtend.evaluate import paired_ttest_resampled\n",
"\n",
"\n",
- "t, p = paired_ttest_resampled(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "t, p = paired_ttest_resampled(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('t statistic: %.3f' % t)\n",
- "print('p value: %.3f' % p)"
+ "print(\"t statistic: %.3f\" % t)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -195,16 +192,13 @@
"clf2 = DecisionTreeClassifier(random_state=1, max_depth=1)\n",
"\n",
"score2 = clf2.fit(X_train, y_train).score(X_test, y_test)\n",
- "print('Decision tree accuracy: %.2f%%' % (score2*100))\n",
+ "print(\"Decision tree accuracy: %.2f%%\" % (score2 * 100))\n",
"\n",
"\n",
- "t, p = paired_ttest_resampled(estimator1=clf1,\n",
- " estimator2=clf2,\n",
- " X=X, y=y,\n",
- " random_seed=1)\n",
+ "t, p = paired_ttest_resampled(estimator1=clf1, estimator2=clf2, X=X, y=y, random_seed=1)\n",
"\n",
- "print('t statistic: %.3f' % t)\n",
- "print('p value: %.3f' % p)"
+ "print(\"t statistic: %.3f\" % t)\n",
+ "print(\"p value: %.3f\" % p)"
]
},
{
@@ -316,8 +310,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/paired_ttest_resampled.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/paired_ttest_resampled.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/permutation_test.ipynb b/docs/sources/user_guide/evaluate/permutation_test.ipynb
index 83089335c..4936c5d92 100644
--- a/docs/sources/user_guide/evaluate/permutation_test.ipynb
+++ b/docs/sources/user_guide/evaluate/permutation_test.ipynb
@@ -107,12 +107,50 @@
"metadata": {},
"outputs": [],
"source": [
- "treatment = [ 28.44, 29.32, 31.22, 29.58, 30.34, 28.76, 29.21, 30.4 ,\n",
- " 31.12, 31.78, 27.58, 31.57, 30.73, 30.43, 30.31, 30.32,\n",
- " 29.18, 29.52, 29.22, 30.56]\n",
- "control = [ 33.51, 30.63, 32.38, 32.52, 29.41, 30.93, 49.78, 28.96,\n",
- " 35.77, 31.42, 30.76, 30.6 , 23.64, 30.54, 47.78, 31.98,\n",
- " 34.52, 32.42, 31.32, 40.72]"
+ "treatment = [\n",
+ " 28.44,\n",
+ " 29.32,\n",
+ " 31.22,\n",
+ " 29.58,\n",
+ " 30.34,\n",
+ " 28.76,\n",
+ " 29.21,\n",
+ " 30.4,\n",
+ " 31.12,\n",
+ " 31.78,\n",
+ " 27.58,\n",
+ " 31.57,\n",
+ " 30.73,\n",
+ " 30.43,\n",
+ " 30.31,\n",
+ " 30.32,\n",
+ " 29.18,\n",
+ " 29.52,\n",
+ " 29.22,\n",
+ " 30.56,\n",
+ "]\n",
+ "control = [\n",
+ " 33.51,\n",
+ " 30.63,\n",
+ " 32.38,\n",
+ " 32.52,\n",
+ " 29.41,\n",
+ " 30.93,\n",
+ " 49.78,\n",
+ " 28.96,\n",
+ " 35.77,\n",
+ " 31.42,\n",
+ " 30.76,\n",
+ " 30.6,\n",
+ " 23.64,\n",
+ " 30.54,\n",
+ " 47.78,\n",
+ " 31.98,\n",
+ " 34.52,\n",
+ " 32.42,\n",
+ " 31.32,\n",
+ " 40.72,\n",
+ "]"
]
},
{
@@ -138,10 +176,9 @@
"source": [
"from mlxtend.evaluate import permutation_test\n",
"\n",
- "p_value = permutation_test(treatment, control,\n",
- " method='approximate',\n",
- " num_rounds=10000,\n",
- " seed=0)\n",
+ "p_value = permutation_test(\n",
+ " treatment, control, method=\"approximate\", num_rounds=10000, seed=0\n",
+ ")\n",
"print(p_value)"
]
},
@@ -187,14 +224,13 @@
"x = np.array([1, 2, 3, 4, 5, 6])\n",
"y = np.array([2, 4, 1, 5, 6, 7])\n",
"\n",
- "print('Observed pearson R: %.2f' % np.corrcoef(x, y)[1][0])\n",
+ "print(\"Observed pearson R: %.2f\" % np.corrcoef(x, y)[1][0])\n",
"\n",
"\n",
- "p_value = permutation_test(x, y,\n",
- " method='exact',\n",
- " func=lambda x, y: np.corrcoef(x, y)[1][0],\n",
- " seed=0)\n",
- "print('P value: %.2f' % p_value)"
+ "p_value = permutation_test(\n",
+ " x, y, method=\"exact\", func=lambda x, y: np.corrcoef(x, y)[1][0], seed=0\n",
+ ")\n",
+ "print(\"P value: %.2f\" % p_value)"
]
},
{
@@ -245,7 +281,7 @@
" lakes_1980, lakes_1990, paired=True, method=\"approximate\", seed=0, num_rounds=100000\n",
")\n",
"\n",
- "print('P value: %.3f' % p_value)"
+ "print(\"P value: %.3f\" % p_value)"
]
},
{
@@ -344,8 +380,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/permutation_test.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/permutation_test.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/proportion_difference.ipynb b/docs/sources/user_guide/evaluate/proportion_difference.ipynb
index d39131b80..427ddce81 100644
--- a/docs/sources/user_guide/evaluate/proportion_difference.ipynb
+++ b/docs/sources/user_guide/evaluate/proportion_difference.ipynb
@@ -140,29 +140,323 @@
"\n",
"# ground truth labels of the test dataset:\n",
"\n",
- "y_true = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0])\n",
+ "y_true = np.array(\n",
+ " [\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
"\n",
"\n",
"# predictions by 3 classifiers (`y_model_1`, `y_model_2`, and `y_model_3`):\n",
"\n",
- "y_model_1 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0])\n",
- "\n",
- "y_model_2 = np.array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
- " 0, 0])"
+ "y_model_1 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")\n",
+ "\n",
+ "y_model_2 = np.array(\n",
+ " [\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 1,\n",
+ " 1,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " 0,\n",
+ " ]\n",
+ ")"
]
},
{
@@ -190,8 +484,8 @@
"acc_1 = np.sum(y_true == y_model_1) / y_true.shape[0]\n",
"acc_2 = np.sum(y_true == y_model_2) / y_true.shape[0]\n",
"\n",
- "print('Accuracy Model 1:', acc_1)\n",
- "print('Accuracy Model 2:', acc_2)\n"
+ "print(\"Accuracy Model 1:\", acc_1)\n",
+ "print(\"Accuracy Model 2:\", acc_2)"
]
},
{
@@ -220,8 +514,8 @@
"\n",
"z, p_value = proportion_difference(acc_1, acc_2, n_1=y_true.shape[0])\n",
"\n",
- "print('z: %.3f' % z)\n",
- "print('p-value: %.3f' % p_value)"
+ "print(\"z: %.3f\" % z)\n",
+ "print(\"p-value: %.3f\" % p_value)"
]
},
{
@@ -291,8 +585,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/proportion_difference.md', 'r') as f:\n",
- " s = f.read() \n",
+ "with open(\"../../api_modules/mlxtend.evaluate/proportion_difference.md\", \"r\") as f:\n",
+ " s = f.read()\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/evaluate/scoring.ipynb b/docs/sources/user_guide/evaluate/scoring.ipynb
index d051f6999..b9e21ed41 100644
--- a/docs/sources/user_guide/evaluate/scoring.ipynb
+++ b/docs/sources/user_guide/evaluate/scoring.ipynb
@@ -248,9 +248,9 @@
"\n",
"y_targ = [1, 1, 1, 0, 0, 2, 0, 3]\n",
"y_pred = [1, 0, 1, 0, 0, 2, 1, 3]\n",
- "res = scoring(y_target=y_targ, y_predicted=y_pred, metric='error')\n",
+ "res = scoring(y_target=y_targ, y_predicted=y_pred, metric=\"error\")\n",
"\n",
- "print('Error: %s%%' % (res * 100))"
+ "print(\"Error: %s%%\" % (res * 100))"
]
},
{
@@ -346,7 +346,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.evaluate/scoring.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.evaluate/scoring.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/feature_extraction/LinearDiscriminantAnalysis.ipynb b/docs/sources/user_guide/feature_extraction/LinearDiscriminantAnalysis.ipynb
index d431b1fd8..562beb321 100644
--- a/docs/sources/user_guide/feature_extraction/LinearDiscriminantAnalysis.ipynb
+++ b/docs/sources/user_guide/feature_extraction/LinearDiscriminantAnalysis.ipynb
@@ -132,17 +132,13 @@
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
- "with plt.style.context('seaborn-whitegrid'):\n",
+ "with plt.style.context(\"seaborn-whitegrid\"):\n",
" plt.figure(figsize=(6, 4))\n",
- " for lab, col in zip((0, 1, 2),\n",
- " ('blue', 'red', 'green')):\n",
- " plt.scatter(X_lda[y == lab, 0],\n",
- " X_lda[y == lab, 1],\n",
- " label=lab,\n",
- " c=col)\n",
- " plt.xlabel('Linear Discriminant 1')\n",
- " plt.ylabel('Linear Discriminant 2')\n",
- " plt.legend(loc='lower right')\n",
+ " for lab, col in zip((0, 1, 2), (\"blue\", \"red\", \"green\")):\n",
+ " plt.scatter(X_lda[y == lab, 0], X_lda[y == lab, 1], label=lab, c=col)\n",
+ " plt.xlabel(\"Linear Discriminant 1\")\n",
+ " plt.ylabel(\"Linear Discriminant 2\")\n",
+ " plt.legend(loc=\"lower right\")\n",
" plt.tight_layout()\n",
" plt.show()"
]
@@ -181,7 +177,7 @@
"import numpy as np\n",
"\n",
"tot = sum(lda.e_vals_)\n",
- "var_exp = [(i / tot)*100 for i in sorted(lda.e_vals_, reverse=True)]\n",
+ "var_exp = [(i / tot) * 100 for i in sorted(lda.e_vals_, reverse=True)]\n",
"cum_var_exp = np.cumsum(var_exp)"
]
},
@@ -202,17 +198,21 @@
}
],
"source": [
- "with plt.style.context('seaborn-whitegrid'):\n",
+ "with plt.style.context(\"seaborn-whitegrid\"):\n",
" fig, ax = plt.subplots(figsize=(6, 4))\n",
- " plt.bar(range(4), var_exp, alpha=0.5, align='center',\n",
- " label='individual explained variance')\n",
- " plt.step(range(4), cum_var_exp, where='mid',\n",
- " label='cumulative explained variance')\n",
- " plt.ylabel('Explained variance ratio')\n",
- " plt.xlabel('Principal components')\n",
+ " plt.bar(\n",
+ " range(4),\n",
+ " var_exp,\n",
+ " alpha=0.5,\n",
+ " align=\"center\",\n",
+ " label=\"individual explained variance\",\n",
+ " )\n",
+ " plt.step(range(4), cum_var_exp, where=\"mid\", label=\"cumulative explained variance\")\n",
+ " plt.ylabel(\"Explained variance ratio\")\n",
+ " plt.xlabel(\"Principal components\")\n",
" plt.xticks(range(4))\n",
" ax.set_xticklabels(np.arange(1, X.shape[1] + 1))\n",
- " plt.legend(loc='best')\n",
+ " plt.legend(loc=\"best\")\n",
" plt.tight_layout()"
]
},
@@ -318,7 +318,9 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.feature_extraction/LinearDiscriminantAnalysis.md', 'r') as f:\n",
+ "with open(\n",
+ " \"../../api_modules/mlxtend.feature_extraction/LinearDiscriminantAnalysis.md\", \"r\"\n",
+ ") as f:\n",
" s = f.read()\n",
"print(s)"
]
diff --git a/docs/sources/user_guide/feature_extraction/PrincipalComponentAnalysis.ipynb b/docs/sources/user_guide/feature_extraction/PrincipalComponentAnalysis.ipynb
index be70d1e2a..34a89d38d 100644
--- a/docs/sources/user_guide/feature_extraction/PrincipalComponentAnalysis.ipynb
+++ b/docs/sources/user_guide/feature_extraction/PrincipalComponentAnalysis.ipynb
@@ -121,17 +121,13 @@
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
- "with plt.style.context('seaborn-whitegrid'):\n",
+ "with plt.style.context(\"seaborn-whitegrid\"):\n",
" plt.figure(figsize=(6, 4))\n",
- " for lab, col in zip((0, 1, 2),\n",
- " ('blue', 'red', 'green')):\n",
- " plt.scatter(X_pca[y==lab, 0],\n",
- " X_pca[y==lab, 1],\n",
- " label=lab,\n",
- " c=col)\n",
- " plt.xlabel('Principal Component 1')\n",
- " plt.ylabel('Principal Component 2')\n",
- " plt.legend(loc='lower center')\n",
+ " for lab, col in zip((0, 1, 2), (\"blue\", \"red\", \"green\")):\n",
+ " plt.scatter(X_pca[y == lab, 0], X_pca[y == lab, 1], label=lab, c=col)\n",
+ " plt.xlabel(\"Principal Component 1\")\n",
+ " plt.ylabel(\"Principal Component 2\")\n",
+ " plt.legend(loc=\"lower center\")\n",
" plt.tight_layout()\n",
" plt.show()"
]
@@ -209,8 +205,8 @@
"import numpy as np\n",
"\n",
"tot = sum(pca.e_vals_)\n",
- "var_exp = [(i / tot)*100 for i in sorted(pca.e_vals_, reverse=True)]\n",
- "cum_var_exp = np.cumsum(pca.e_vals_normalized_*100)"
+ "var_exp = [(i / tot) * 100 for i in sorted(pca.e_vals_, reverse=True)]\n",
+ "cum_var_exp = np.cumsum(pca.e_vals_normalized_ * 100)"
]
},
{
@@ -230,17 +226,21 @@
}
],
"source": [
- "with plt.style.context('seaborn-whitegrid'):\n",
+ "with plt.style.context(\"seaborn-whitegrid\"):\n",
" fig, ax = plt.subplots(figsize=(6, 4))\n",
- " plt.bar(range(4), var_exp, alpha=0.5, align='center',\n",
- " label='individual explained variance')\n",
- " plt.step(range(4), cum_var_exp, where='mid',\n",
- " label='cumulative explained variance')\n",
- " plt.ylabel('Explained variance ratio')\n",
- " plt.xlabel('Principal components')\n",
+ " plt.bar(\n",
+ " range(4),\n",
+ " var_exp,\n",
+ " alpha=0.5,\n",
+ " align=\"center\",\n",
+ " label=\"individual explained variance\",\n",
+ " )\n",
+ " plt.step(range(4), cum_var_exp, where=\"mid\", label=\"cumulative explained variance\")\n",
+ " plt.ylabel(\"Explained variance ratio\")\n",
+ " plt.xlabel(\"Principal components\")\n",
" plt.xticks(range(4))\n",
" ax.set_xticklabels(np.arange(1, X.shape[1] + 1))\n",
- " plt.legend(loc='best')\n",
+ " plt.legend(loc=\"best\")\n",
" plt.tight_layout()"
]
},
@@ -271,8 +271,7 @@
"X, y = iris_data()\n",
"X = standardize(X)\n",
"\n",
- "pca = PrincipalComponentAnalysis(n_components=2,\n",
- " solver='svd')\n",
+ "pca = PrincipalComponentAnalysis(n_components=2, solver=\"svd\")\n",
"pca.fit(X)\n",
"X_pca = pca.transform(X)"
]
@@ -296,17 +295,13 @@
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
- "with plt.style.context('seaborn-whitegrid'):\n",
+ "with plt.style.context(\"seaborn-whitegrid\"):\n",
" plt.figure(figsize=(6, 4))\n",
- " for lab, col in zip((0, 1, 2),\n",
- " ('blue', 'red', 'green')):\n",
- " plt.scatter(X_pca[y==lab, 0],\n",
- " X_pca[y==lab, 1],\n",
- " label=lab,\n",
- " c=col)\n",
- " plt.xlabel('Principal Component 1')\n",
- " plt.ylabel('Principal Component 2')\n",
- " plt.legend(loc='lower center')\n",
+ " for lab, col in zip((0, 1, 2), (\"blue\", \"red\", \"green\")):\n",
+ " plt.scatter(X_pca[y == lab, 0], X_pca[y == lab, 1], label=lab, c=col)\n",
+ " plt.xlabel(\"Principal Component 1\")\n",
+ " plt.ylabel(\"Principal Component 2\")\n",
+ " plt.legend(loc=\"lower center\")\n",
" plt.tight_layout()\n",
" plt.show()"
]
@@ -358,8 +353,7 @@
"X, y = iris_data()\n",
"X = standardize(X)\n",
"\n",
- "pca = PrincipalComponentAnalysis(n_components=2,\n",
- " solver='eigen')\n",
+ "pca = PrincipalComponentAnalysis(n_components=2, solver=\"eigen\")\n",
"pca.fit(X);"
]
},
@@ -382,15 +376,15 @@
}
],
"source": [
- "xlabels = ['sepal length', 'sepal width', 'petal length', 'petal width']\n",
+ "xlabels = [\"sepal length\", \"sepal width\", \"petal length\", \"petal width\"]\n",
"\n",
"fig, ax = plt.subplots(1, 2, figsize=(8, 3))\n",
"\n",
- "ax[0].bar(range(4), pca.loadings_[:, 0], align='center')\n",
- "ax[1].bar(range(4), pca.loadings_[:, 1], align='center')\n",
+ "ax[0].bar(range(4), pca.loadings_[:, 0], align=\"center\")\n",
+ "ax[1].bar(range(4), pca.loadings_[:, 1], align=\"center\")\n",
"\n",
- "ax[0].set_ylabel('Factor loading onto PC1')\n",
- "ax[1].set_ylabel('Factor loading onto PC2')\n",
+ "ax[0].set_ylabel(\"Factor loading onto PC1\")\n",
+ "ax[1].set_ylabel(\"Factor loading onto PC2\")\n",
"\n",
"ax[0].set_xticks(range(4))\n",
"ax[1].set_xticks(range(4))\n",
@@ -427,7 +421,9 @@
"from mlxtend.data import wine_data\n",
"\n",
"X, y = wine_data()\n",
- "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123, test_size=0.3, stratify=y)"
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, random_state=123, test_size=0.3, stratify=y\n",
+ ")"
]
},
{
@@ -445,15 +441,17 @@
}
],
"source": [
- "pipe_pca = make_pipeline(StandardScaler(),\n",
- " PrincipalComponentAnalysis(n_components=3),\n",
- " KNeighborsClassifier(n_neighbors=5))\n",
+ "pipe_pca = make_pipeline(\n",
+ " StandardScaler(),\n",
+ " PrincipalComponentAnalysis(n_components=3),\n",
+ " KNeighborsClassifier(n_neighbors=5),\n",
+ ")\n",
"\n",
"pipe_pca.fit(X_train, y_train)\n",
"\n",
"\n",
- "print('Transf. training accyracy: %.2f%%' % (pipe_pca.score(X_train, y_train)*100))\n",
- "print('Transf. test accyracy: %.2f%%' % (pipe_pca.score(X_test, y_test)*100))"
+ "print(\"Transf. training accyracy: %.2f%%\" % (pipe_pca.score(X_train, y_train) * 100))\n",
+ "print(\"Transf. test accyracy: %.2f%%\" % (pipe_pca.score(X_test, y_test) * 100))"
]
},
{
@@ -487,7 +485,9 @@
"from mlxtend.data import wine_data\n",
"\n",
"X, y = wine_data()\n",
- "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123, test_size=0.3, stratify=y)"
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, random_state=123, test_size=0.3, stratify=y\n",
+ ")"
]
},
{
@@ -522,17 +522,18 @@
"X_train_transf = pca1.fit(X_train_scaled).transform(X_train_scaled)\n",
"\n",
"\n",
- "with plt.style.context('seaborn-whitegrid'):\n",
+ "with plt.style.context(\"seaborn-whitegrid\"):\n",
" plt.figure(figsize=(6, 4))\n",
- " for lab, col in zip((0, 1, 2),\n",
- " ('blue', 'red', 'green')):\n",
- " plt.scatter(X_train_transf[y_train==lab, 0],\n",
- " X_train_transf[y_train==lab, 1],\n",
- " label=lab,\n",
- " c=col)\n",
- " plt.xlabel('Principal Component 1')\n",
- " plt.ylabel('Principal Component 2')\n",
- " plt.legend(loc='lower center')\n",
+ " for lab, col in zip((0, 1, 2), (\"blue\", \"red\", \"green\")):\n",
+ " plt.scatter(\n",
+ " X_train_transf[y_train == lab, 0],\n",
+ " X_train_transf[y_train == lab, 1],\n",
+ " label=lab,\n",
+ " c=col,\n",
+ " )\n",
+ " plt.xlabel(\"Principal Component 1\")\n",
+ " plt.ylabel(\"Principal Component 2\")\n",
+ " plt.legend(loc=\"lower center\")\n",
" plt.tight_layout()\n",
" plt.show()"
]
@@ -565,7 +566,7 @@
"source": [
"np.set_printoptions(precision=1, suppress=True)\n",
"\n",
- "print('Covariance matrix:\\n')\n",
+ "print(\"Covariance matrix:\\n\")\n",
"np.cov(X_train_transf.T)"
]
},
@@ -608,17 +609,18 @@
"X_train_transf = pca1.fit(X_train_scaled).transform(X_train_scaled)\n",
"\n",
"\n",
- "with plt.style.context('seaborn-whitegrid'):\n",
+ "with plt.style.context(\"seaborn-whitegrid\"):\n",
" plt.figure(figsize=(6, 4))\n",
- " for lab, col in zip((0, 1, 2),\n",
- " ('blue', 'red', 'green')):\n",
- " plt.scatter(X_train_transf[y_train==lab, 0],\n",
- " X_train_transf[y_train==lab, 1],\n",
- " label=lab,\n",
- " c=col)\n",
- " plt.xlabel('Principal Component 1')\n",
- " plt.ylabel('Principal Component 2')\n",
- " plt.legend(loc='lower center')\n",
+ " for lab, col in zip((0, 1, 2), (\"blue\", \"red\", \"green\")):\n",
+ " plt.scatter(\n",
+ " X_train_transf[y_train == lab, 0],\n",
+ " X_train_transf[y_train == lab, 1],\n",
+ " label=lab,\n",
+ " c=col,\n",
+ " )\n",
+ " plt.xlabel(\"Principal Component 1\")\n",
+ " plt.ylabel(\"Principal Component 2\")\n",
+ " plt.legend(loc=\"lower center\")\n",
" plt.tight_layout()\n",
" plt.show()"
]
@@ -651,7 +653,7 @@
"source": [
"np.set_printoptions(precision=1, suppress=True)\n",
"\n",
- "print('Covariance matrix:\\n')\n",
+ "print(\"Covariance matrix:\\n\")\n",
"np.cov(X_train_transf.T)"
]
},
@@ -824,7 +826,9 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.feature_extraction/PrincipalComponentAnalysis.md', 'r') as f:\n",
+ "with open(\n",
+ " \"../../api_modules/mlxtend.feature_extraction/PrincipalComponentAnalysis.md\", \"r\"\n",
+ ") as f:\n",
" s = f.read()\n",
"print(s)"
]
diff --git a/docs/sources/user_guide/feature_extraction/RBFKernelPCA.ipynb b/docs/sources/user_guide/feature_extraction/RBFKernelPCA.ipynb
index 12397b0eb..4d9fad9ab 100644
--- a/docs/sources/user_guide/feature_extraction/RBFKernelPCA.ipynb
+++ b/docs/sources/user_guide/feature_extraction/RBFKernelPCA.ipynb
@@ -234,12 +234,10 @@
"\n",
"X, y = make_moons(n_samples=50, random_state=1)\n",
"\n",
- "plt.scatter(X[y==0, 0], X[y==0, 1], \n",
- " color='red', marker='o', alpha=0.5)\n",
- "plt.scatter(X[y==1, 0], X[y==1, 1], \n",
- " color='blue', marker='^', alpha=0.5)\n",
- "plt.ylabel('y coordinate')\n",
- "plt.xlabel('x coordinate')\n",
+ "plt.scatter(X[y == 0, 0], X[y == 0, 1], color=\"red\", marker=\"o\", alpha=0.5)\n",
+ "plt.scatter(X[y == 1, 0], X[y == 1, 1], color=\"blue\", marker=\"^\", alpha=0.5)\n",
+ "plt.ylabel(\"y coordinate\")\n",
+ "plt.xlabel(\"x coordinate\")\n",
"\n",
"plt.show()"
]
@@ -273,13 +271,11 @@
"pca = PCA(n_components=2)\n",
"X_pca = pca.fit(X).transform(X)\n",
"\n",
- "plt.scatter(X_pca[y==0, 0], X_pca[y==0, 1], \n",
- " color='red', marker='o', alpha=0.5)\n",
- "plt.scatter(X_pca[y==1, 0], X_pca[y==1, 1], \n",
- " color='blue', marker='^', alpha=0.5)\n",
+ "plt.scatter(X_pca[y == 0, 0], X_pca[y == 0, 1], color=\"red\", marker=\"o\", alpha=0.5)\n",
+ "plt.scatter(X_pca[y == 1, 0], X_pca[y == 1, 1], color=\"blue\", marker=\"^\", alpha=0.5)\n",
"\n",
- "plt.xlabel('PC1')\n",
- "plt.ylabel('PC2')\n",
+ "plt.xlabel(\"PC1\")\n",
+ "plt.ylabel(\"PC2\")\n",
"plt.show()"
]
},
@@ -304,8 +300,6 @@
"metadata": {},
"outputs": [],
"source": [
- "from mlxtend.data import iris_data\n",
- "from mlxtend.preprocessing import standardize\n",
"from mlxtend.feature_extraction import RBFKernelPCA as KPCA\n",
"\n",
"kpca = KPCA(gamma=15.0, n_components=2)\n",
@@ -337,14 +331,12 @@
}
],
"source": [
- "plt.scatter(X_kpca[y==0, 0], X_kpca[y==0, 1], \n",
- " color='red', marker='o', alpha=0.5)\n",
- "plt.scatter(X_kpca[y==1, 0], X_kpca[y==1, 1], \n",
- " color='blue', marker='^', alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 0, 0], X_kpca[y == 0, 1], color=\"red\", marker=\"o\", alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 1, 0], X_kpca[y == 1, 1], color=\"blue\", marker=\"^\", alpha=0.5)\n",
"\n",
- "plt.title('First 2 principal components after RBF Kernel PCA')\n",
- "plt.xlabel('PC1')\n",
- "plt.ylabel('PC2')\n",
+ "plt.title(\"First 2 principal components after RBF Kernel PCA\")\n",
+ "plt.xlabel(\"PC1\")\n",
+ "plt.ylabel(\"PC2\")\n",
"plt.show()"
]
},
@@ -374,13 +366,11 @@
"source": [
"import numpy as np\n",
"\n",
- "plt.scatter(X_kpca[y==0, 0], np.zeros((25, 1)), \n",
- " color='red', marker='o', alpha=0.5)\n",
- "plt.scatter(X_kpca[y==1, 0], np.zeros((25, 1)), \n",
- " color='blue', marker='^', alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 0, 0], np.zeros((25, 1)), color=\"red\", marker=\"o\", alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 1, 0], np.zeros((25, 1)), color=\"blue\", marker=\"^\", alpha=0.5)\n",
"\n",
- "plt.title('First principal component after RBF Kernel PCA')\n",
- "plt.xlabel('PC1')\n",
+ "plt.title(\"First principal component after RBF Kernel PCA\")\n",
+ "plt.xlabel(\"PC1\")\n",
"plt.yticks([])\n",
"plt.show()"
]
@@ -429,17 +419,39 @@
"X2, y2 = make_moons(n_samples=200, random_state=5)\n",
"X2_kpca = kpca.transform(X2)\n",
"\n",
- "plt.scatter(X_kpca[y==0, 0], X_kpca[y==0, 1], \n",
- " color='red', marker='o', alpha=0.5, label='fit data')\n",
- "plt.scatter(X_kpca[y==1, 0], X_kpca[y==1, 1], \n",
- " color='blue', marker='^', alpha=0.5, label='fit data')\n",
- "\n",
- "plt.scatter(X2_kpca[y2==0, 0], X2_kpca[y2==0, 1], \n",
- " color='orange', marker='v', \n",
- " alpha=0.2, label='new data')\n",
- "plt.scatter(X2_kpca[y2==1, 0], X2_kpca[y2==1, 1], \n",
- " color='cyan', marker='s', \n",
- " alpha=0.2, label='new data')\n",
+ "plt.scatter(\n",
+ " X_kpca[y == 0, 0],\n",
+ " X_kpca[y == 0, 1],\n",
+ " color=\"red\",\n",
+ " marker=\"o\",\n",
+ " alpha=0.5,\n",
+ " label=\"fit data\",\n",
+ ")\n",
+ "plt.scatter(\n",
+ " X_kpca[y == 1, 0],\n",
+ " X_kpca[y == 1, 1],\n",
+ " color=\"blue\",\n",
+ " marker=\"^\",\n",
+ " alpha=0.5,\n",
+ " label=\"fit data\",\n",
+ ")\n",
+ "\n",
+ "plt.scatter(\n",
+ " X2_kpca[y2 == 0, 0],\n",
+ " X2_kpca[y2 == 0, 1],\n",
+ " color=\"orange\",\n",
+ " marker=\"v\",\n",
+ " alpha=0.2,\n",
+ " label=\"new data\",\n",
+ ")\n",
+ "plt.scatter(\n",
+ " X2_kpca[y2 == 1, 0],\n",
+ " X2_kpca[y2 == 1, 1],\n",
+ " color=\"cyan\",\n",
+ " marker=\"s\",\n",
+ " alpha=0.2,\n",
+ " label=\"new data\",\n",
+ ")\n",
"\n",
"plt.legend()\n",
"plt.show()"
@@ -478,16 +490,15 @@
"source": [
"from sklearn.datasets import make_circles\n",
"\n",
- "X, y = make_circles(n_samples=1000, random_state=123, \n",
- " noise=0.1, factor=0.2)\n",
+ "X, y = make_circles(n_samples=1000, random_state=123, noise=0.1, factor=0.2)\n",
"\n",
- "plt.figure(figsize=(8,6))\n",
+ "plt.figure(figsize=(8, 6))\n",
"\n",
- "plt.scatter(X[y==0, 0], X[y==0, 1], color='red', alpha=0.5)\n",
- "plt.scatter(X[y==1, 0], X[y==1, 1], color='blue', alpha=0.5)\n",
- "plt.title('Concentric circles')\n",
- "plt.ylabel('y coordinate')\n",
- "plt.xlabel('x coordinate')\n",
+ "plt.scatter(X[y == 0, 0], X[y == 0, 1], color=\"red\", alpha=0.5)\n",
+ "plt.scatter(X[y == 1, 0], X[y == 1, 1], color=\"blue\", alpha=0.5)\n",
+ "plt.title(\"Concentric circles\")\n",
+ "plt.ylabel(\"y coordinate\")\n",
+ "plt.xlabel(\"x coordinate\")\n",
"plt.show()"
]
},
@@ -502,8 +513,6 @@
},
"outputs": [],
"source": [
- "from mlxtend.data import iris_data\n",
- "from mlxtend.preprocessing import standardize\n",
"from mlxtend.feature_extraction import RBFKernelPCA as KPCA\n",
"\n",
"kpca = KPCA(gamma=15.0, n_components=2)\n",
@@ -528,14 +537,12 @@
}
],
"source": [
- "plt.scatter(X_kpca[y==0, 0], X_kpca[y==0, 1], \n",
- " color='red', marker='o', alpha=0.5)\n",
- "plt.scatter(X_kpca[y==1, 0], X_kpca[y==1, 1], \n",
- " color='blue', marker='^', alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 0, 0], X_kpca[y == 0, 1], color=\"red\", marker=\"o\", alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 1, 0], X_kpca[y == 1, 1], color=\"blue\", marker=\"^\", alpha=0.5)\n",
"\n",
- "plt.title('First 2 principal components after RBF Kernel PCA')\n",
- "plt.xlabel('PC1')\n",
- "plt.ylabel('PC2')\n",
+ "plt.title(\"First 2 principal components after RBF Kernel PCA\")\n",
+ "plt.xlabel(\"PC1\")\n",
+ "plt.ylabel(\"PC2\")\n",
"plt.show()"
]
},
@@ -556,13 +563,11 @@
}
],
"source": [
- "plt.scatter(X_kpca[y==0, 0], np.zeros((500, 1)), \n",
- " color='red', marker='o', alpha=0.5)\n",
- "plt.scatter(X_kpca[y==1, 0], np.zeros((500, 1)), \n",
- " color='blue', marker='^', alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 0, 0], np.zeros((500, 1)), color=\"red\", marker=\"o\", alpha=0.5)\n",
+ "plt.scatter(X_kpca[y == 1, 0], np.zeros((500, 1)), color=\"blue\", marker=\"^\", alpha=0.5)\n",
"\n",
- "plt.title('First principal component after RBF Kernel PCA')\n",
- "plt.xlabel('PC1')\n",
+ "plt.title(\"First principal component after RBF Kernel PCA\")\n",
+ "plt.xlabel(\"PC1\")\n",
"plt.yticks([])\n",
"plt.show()"
]
@@ -668,7 +673,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.feature_extraction/RBFKernelPCA.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.feature_extraction/RBFKernelPCA.md\", \"r\") as f:\n",
" s = f.read()\n",
"print(s)"
]
diff --git a/docs/sources/user_guide/feature_selection/ColumnSelector.ipynb b/docs/sources/user_guide/feature_selection/ColumnSelector.ipynb
index 6c6b9fb8f..9b765dbcb 100644
--- a/docs/sources/user_guide/feature_selection/ColumnSelector.ipynb
+++ b/docs/sources/user_guide/feature_selection/ColumnSelector.ipynb
@@ -261,9 +261,9 @@
"from sklearn.pipeline import make_pipeline\n",
"\n",
"\n",
- "pipe = make_pipeline(StandardScaler(),\n",
- " ColumnSelector(cols=(0, 1)),\n",
- " KNeighborsClassifier())\n",
+ "pipe = make_pipeline(\n",
+ " StandardScaler(), ColumnSelector(cols=(0, 1)), KNeighborsClassifier()\n",
+ ")\n",
"\n",
"pipe.fit(X, y)\n",
"pipe.score(X, y)"
@@ -367,17 +367,17 @@
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.pipeline import make_pipeline\n",
"\n",
- "pipe = make_pipeline(StandardScaler(),\n",
- " ColumnSelector(),\n",
- " KNeighborsClassifier())\n",
+ "pipe = make_pipeline(StandardScaler(), ColumnSelector(), KNeighborsClassifier())\n",
"\n",
- "param_grid = {'columnselector__cols': all_comb,\n",
- " 'kneighborsclassifier__n_neighbors': list(range(1, 11))}\n",
+ "param_grid = {\n",
+ " \"columnselector__cols\": all_comb,\n",
+ " \"kneighborsclassifier__n_neighbors\": list(range(1, 11)),\n",
+ "}\n",
"\n",
"grid = GridSearchCV(pipe, param_grid, cv=5, n_jobs=-1)\n",
"grid.fit(X, y)\n",
- "print('Best parameters:', grid.best_params_)\n",
- "print('Best performance:', grid.best_score_)"
+ "print(\"Best parameters:\", grid.best_params_)\n",
+ "print(\"Best performance:\", grid.best_score_)"
]
},
{
@@ -427,16 +427,19 @@
"\n",
"X, y = iris_data()\n",
"\n",
- "scale_pipe = make_pipeline(ColumnSelector(cols=(0, 1)),\n",
- " MinMaxScaler())\n",
+ "scale_pipe = make_pipeline(ColumnSelector(cols=(0, 1)), MinMaxScaler())\n",
"\n",
- "pipeline = Pipeline([\n",
- " ('feats', FeatureUnion([\n",
- " ('col_1-2', scale_pipe),\n",
- " ('col_3-4', ColumnSelector(cols=(2, 3)))\n",
- " ])),\n",
- " ('clf', KNeighborsClassifier())\n",
- "])\n",
+ "pipeline = Pipeline(\n",
+ " [\n",
+ " (\n",
+ " \"feats\",\n",
+ " FeatureUnion(\n",
+ " [(\"col_1-2\", scale_pipe), (\"col_3-4\", ColumnSelector(cols=(2, 3)))]\n",
+ " ),\n",
+ " ),\n",
+ " (\"clf\", KNeighborsClassifier()),\n",
+ " ]\n",
+ ")\n",
"\n",
"\n",
"pipeline.fit(X, y)"
@@ -592,8 +595,8 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.feature_selection/ColumnSelector.md', 'r') as f:\n",
- " s = f.read() + '
'\n",
+ "with open(\"../../api_modules/mlxtend.feature_selection/ColumnSelector.md\", \"r\") as f:\n",
+ " s = f.read() + \"
\"\n",
"print(s)"
]
}
diff --git a/docs/sources/user_guide/feature_selection/ExhaustiveFeatureSelector.ipynb b/docs/sources/user_guide/feature_selection/ExhaustiveFeatureSelector.ipynb
index 13c0870c7..ae12608a1 100644
--- a/docs/sources/user_guide/feature_selection/ExhaustiveFeatureSelector.ipynb
+++ b/docs/sources/user_guide/feature_selection/ExhaustiveFeatureSelector.ipynb
@@ -151,18 +151,15 @@
"\n",
"knn = KNeighborsClassifier(n_neighbors=3)\n",
"\n",
- "efs1 = EFS(knn, \n",
- " min_features=1,\n",
- " max_features=4,\n",
- " scoring='accuracy',\n",
- " print_progress=True,\n",
- " cv=5)\n",
+ "efs1 = EFS(\n",
+ " knn, min_features=1, max_features=4, scoring=\"accuracy\", print_progress=True, cv=5\n",
+ ")\n",
"\n",
"efs1 = efs1.fit(X, y)\n",
"\n",
- "print('Best accuracy score: %.2f' % efs1.best_score_)\n",
- "print('Best subset (indices):', efs1.best_idx_)\n",
- "print('Best subset (corresponding names):', efs1.best_feature_names_)"
+ "print(\"Best accuracy score: %.2f\" % efs1.best_score_)\n",
+ "print(\"Best subset (indices):\", efs1.best_idx_)\n",
+ "print(\"Best subset (corresponding names):\", efs1.best_feature_names_)"
]
},
{
@@ -275,7 +272,9 @@
"source": [
"import pandas as pd\n",
"\n",
- "df_X = pd.DataFrame(X, columns=[\"Sepal length\", \"Sepal width\", \"Petal length\", \"Petal width\"])\n",
+ "df_X = pd.DataFrame(\n",
+ " X, columns=[\"Sepal length\", \"Sepal width\", \"Petal length\", \"Petal width\"]\n",
+ ")\n",
"df_X.head()"
]
},
@@ -304,9 +303,9 @@
"source": [
"efs1 = efs1.fit(df_X, y)\n",
"\n",
- "print('Best accuracy score: %.2f' % efs1.best_score_)\n",
- "print('Best subset (indices):', efs1.best_idx_)\n",
- "print('Best subset (corresponding names):', efs1.best_feature_names_)"
+ "print(\"Best accuracy score: %.2f\" % efs1.best_score_)\n",
+ "print(\"Best subset (indices):\", efs1.best_idx_)\n",
+ "print(\"Best subset (corresponding names):\", efs1.best_feature_names_)"
]
},
{
@@ -690,22 +689,19 @@
"\n",
"knn = KNeighborsClassifier(n_neighbors=3)\n",
"\n",
- "efs1 = EFS(knn, \n",
- " min_features=1,\n",
- " max_features=4,\n",
- " scoring='accuracy',\n",
- " print_progress=True,\n",
- " cv=5)\n",
+ "efs1 = EFS(\n",
+ " knn, min_features=1, max_features=4, scoring=\"accuracy\", print_progress=True, cv=5\n",
+ ")\n",
"\n",
- "feature_names = ('sepal length', 'sepal width',\n",
- " 'petal length', 'petal width')\n",
+ "feature_names = (\"sepal length\", \"sepal width\", \"petal length\", \"petal width\")\n",
"\n",
"df_X = pd.DataFrame(\n",
- " X, columns=[\"Sepal length\", \"Sepal width\", \"Petal length\", \"Petal width\"])\n",
+ " X, columns=[\"Sepal length\", \"Sepal width\", \"Petal length\", \"Petal width\"]\n",
+ ")\n",
"efs1 = efs1.fit(df_X, y)\n",
"\n",
"df = pd.DataFrame.from_dict(efs1.get_metric_dict()).T\n",
- "df.sort_values('avg_score', inplace=True, ascending=False)\n",
+ "df.sort_values(\"avg_score\", inplace=True, ascending=False)\n",
"df"
]
},
@@ -732,30 +728,21 @@
"\n",
"fig = plt.figure()\n",
"k_feat = sorted(metric_dict.keys())\n",
- "avg = [metric_dict[k]['avg_score'] for k in k_feat]\n",
+ "avg = [metric_dict[k][\"avg_score\"] for k in k_feat]\n",
"\n",
"upper, lower = [], []\n",
"for k in k_feat:\n",
- " upper.append(metric_dict[k]['avg_score'] +\n",
- " metric_dict[k]['std_dev'])\n",
- " lower.append(metric_dict[k]['avg_score'] -\n",
- " metric_dict[k]['std_dev'])\n",
- " \n",
- "plt.fill_between(k_feat,\n",
- " upper,\n",
- " lower,\n",
- " alpha=0.2,\n",
- " color='blue',\n",
- " lw=1)\n",
- "\n",
- "plt.plot(k_feat, avg, color='blue', marker='o')\n",
- "plt.ylabel('Accuracy +/- Standard Deviation')\n",
- "plt.xlabel('Number of Features')\n",
- "feature_min = len(metric_dict[k_feat[0]]['feature_idx'])\n",
- "feature_max = len(metric_dict[k_feat[-1]]['feature_idx'])\n",
- "plt.xticks(k_feat, \n",
- " [str(metric_dict[k]['feature_names']) for k in k_feat], \n",
- " rotation=90)\n",
+ " upper.append(metric_dict[k][\"avg_score\"] + metric_dict[k][\"std_dev\"])\n",
+ " lower.append(metric_dict[k][\"avg_score\"] - metric_dict[k][\"std_dev\"])\n",
+ "\n",
+ "plt.fill_between(k_feat, upper, lower, alpha=0.2, color=\"blue\", lw=1)\n",
+ "\n",
+ "plt.plot(k_feat, avg, color=\"blue\", marker=\"o\")\n",
+ "plt.ylabel(\"Accuracy +/- Standard Deviation\")\n",
+ "plt.xlabel(\"Number of Features\")\n",
+ "feature_min = len(metric_dict[k_feat[0]][\"feature_idx\"])\n",
+ "feature_max = len(metric_dict[k_feat[-1]][\"feature_idx\"])\n",
+ "plt.xticks(k_feat, [str(metric_dict[k][\"feature_names\"]) for k in k_feat], rotation=90)\n",
"plt.show()"
]
},
@@ -803,16 +790,12 @@
"\n",
"lr = LinearRegression()\n",
"\n",
- "efs = EFS(lr, \n",
- " min_features=10,\n",
- " max_features=12,\n",
- " scoring='neg_mean_squared_error',\n",
- " cv=10)\n",
+ "efs = EFS(lr, min_features=10, max_features=12, scoring=\"neg_mean_squared_error\", cv=10)\n",
"\n",
"efs.fit(X, y)\n",
"\n",
- "print('Best MSE score: %.2f' % efs.best_score_ * (-1))\n",
- "print('Best subset:', efs.best_idx_)"
+ "print(\"Best MSE score: %.2f\" % efs.best_score_ * (-1))\n",
+ "print(\"Best subset:\", efs.best_idx_)"
]
},
{
@@ -901,7 +884,7 @@
"outputs": [],
"source": [
"def adjust_r2(r2, num_examples, num_features):\n",
- " coef = (num_examples - 1) / (num_examples - num_features - 1) \n",
+ " coef = (num_examples - 1) / (num_examples - num_features - 1)\n",
" return 1 - (1 - r2) * coef"
]
},
@@ -935,11 +918,12 @@
"score = -99e10\n",
"\n",
"for i in efs.subsets_:\n",
- " score = efs.subsets_[i]['adjusted_avg_score']\n",
- " if ( efs.subsets_[i]['adjusted_avg_score'] == score and\n",
- " len(efs.subsets_[i]['feature_idx']) < len(efs.best_idx_) )\\\n",
- " or efs.subsets_[i]['adjusted_avg_score'] > score:\n",
- " efs.best_idx_ = efs.subsets_[i]['feature_idx']"
+ " score = efs.subsets_[i][\"adjusted_avg_score\"]\n",
+ " if (\n",
+ " efs.subsets_[i][\"adjusted_avg_score\"] == score\n",
+ " and len(efs.subsets_[i][\"feature_idx\"]) < len(efs.best_idx_)\n",
+ " ) or efs.subsets_[i][\"adjusted_avg_score\"] > score:\n",
+ " efs.best_idx_ = efs.subsets_[i][\"feature_idx\"]"
]
},
{
@@ -983,7 +967,8 @@
"iris = load_iris()\n",
"X, y = iris.data, iris.target\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.33, random_state=1)\n",
+ " X, y, test_size=0.33, random_state=1\n",
+ ")\n",
"\n",
"knn = KNeighborsClassifier(n_neighbors=3)"
]
@@ -1007,11 +992,7 @@
"\n",
"from mlxtend.feature_selection import ExhaustiveFeatureSelector as EFS\n",
"\n",
- "efs1 = EFS(knn, \n",
- " min_features=1,\n",
- " max_features=4,\n",
- " scoring='accuracy',\n",
- " cv=5)\n",
+ "efs1 = EFS(knn, min_features=1, max_features=4, scoring=\"accuracy\", cv=5)\n",
"efs1 = efs1.fit(X_train, y_train)"
]
},
@@ -1029,7 +1010,7 @@
}
],
"source": [
- "print('Selected features:', efs1.best_idx_)"
+ "print(\"Selected features:\", efs1.best_idx_)"
]
},
{
@@ -1060,7 +1041,7 @@
"\n",
"# Compute the accuracy of the prediction\n",
"acc = float((y_test == y_pred).sum()) / y_pred.shape[0]\n",
- "print('Test set accuracy: %.2f %%' % (acc*100))"
+ "print(\"Test set accuracy: %.2f %%\" % (acc * 100))"
]
},
{
@@ -1084,7 +1065,8 @@
"iris = load_iris()\n",
"X, y = iris.data, iris.target\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.33, random_state=1)"
+ " X, y, test_size=0.33, random_state=1\n",
+ ")"
]
},
{
@@ -1113,28 +1095,32 @@
"from sklearn.linear_model import LogisticRegression\n",
"from mlxtend.feature_selection import ExhaustiveFeatureSelector as EFS\n",
"\n",
- "lr = LogisticRegression( solver='newton-cg',random_state=123)\n",
- "\n",
- "efs1 = EFS(estimator=lr, \n",
- " min_features=2,\n",
- " max_features=3,\n",
- " scoring='accuracy',\n",
- " print_progress=False,\n",
- " clone_estimator=False,\n",
- " cv=5,\n",
- " n_jobs=1)\n",
+ "lr = LogisticRegression(solver=\"newton-cg\", random_state=123)\n",
+ "\n",
+ "efs1 = EFS(\n",
+ " estimator=lr,\n",
+ " min_features=2,\n",
+ " max_features=3,\n",
+ " scoring=\"accuracy\",\n",
+ " print_progress=False,\n",
+ " clone_estimator=False,\n",
+ " cv=5,\n",
+ " n_jobs=1,\n",
+ ")\n",
"\n",
"pipe = make_pipeline(efs1, lr)\n",
"\n",
- "param_grid = {'exhaustivefeatureselector__estimator__C': [0.1, 1.0, 10.0]}\n",
- " \n",
- "gs = GridSearchCV(estimator=pipe, \n",
- " param_grid=param_grid, \n",
- " scoring='accuracy', \n",
- " n_jobs=1, \n",
- " cv=2, \n",
- " verbose=1, \n",
- " refit=False)\n",
+ "param_grid = {\"exhaustivefeatureselector__estimator__C\": [0.1, 1.0, 10.0]}\n",
+ "\n",
+ "gs = GridSearchCV(\n",
+ " estimator=pipe,\n",
+ " param_grid=param_grid,\n",
+ " scoring=\"accuracy\",\n",
+ " n_jobs=1,\n",
+ " cv=2,\n",
+ " verbose=1,\n",
+ " refit=False,\n",
+ ")\n",
"\n",
"# run gridearch\n",
"gs = gs.fit(X_train, y_train)"
@@ -1184,13 +1170,15 @@
"metadata": {},
"outputs": [],
"source": [
- "gs = GridSearchCV(estimator=pipe, \n",
- " param_grid=param_grid, \n",
- " scoring='accuracy', \n",
- " n_jobs=1, \n",
- " cv=2, \n",
- " verbose=1, \n",
- " refit=True)"
+ "gs = GridSearchCV(\n",
+ " estimator=pipe,\n",
+ " param_grid=param_grid,\n",
+ " scoring=\"accuracy\",\n",
+ " n_jobs=1,\n",
+ " cv=2,\n",
+ " verbose=1,\n",
+ " refit=True,\n",
+ ")"
]
},
{
@@ -1256,7 +1244,7 @@
}
],
"source": [
- "print('Best features:', gs.best_estimator_.steps[0][1].best_idx_)"
+ "print(\"Best features:\", gs.best_estimator_.steps[0][1].best_idx_)"
]
},
{
@@ -1280,7 +1268,7 @@
}
],
"source": [
- "print('Best score:', gs.best_score_)"
+ "print(\"Best score:\", gs.best_score_)"
]
},
{
@@ -1325,7 +1313,7 @@
],
"source": [
"pipe.set_params(**gs.best_params_).fit(X_train, y_train)\n",
- "print('Best features:', pipe.steps[0][1].best_idx_)"
+ "print(\"Best features:\", pipe.steps[0][1].best_idx_)"
]
},
{
@@ -1379,18 +1367,20 @@
"\n",
"knn = KNeighborsClassifier(n_neighbors=3)\n",
"\n",
- "efs1 = EFS(knn, \n",
- " min_features=1,\n",
- " max_features=4,\n",
- " scoring='accuracy',\n",
- " print_progress=True,\n",
- " cv=LeaveOneOut()) ### Use cross-validation generator here\n",
+ "efs1 = EFS(\n",
+ " knn,\n",
+ " min_features=1,\n",
+ " max_features=4,\n",
+ " scoring=\"accuracy\",\n",
+ " print_progress=True,\n",
+ " cv=LeaveOneOut(),\n",
+ ") ### Use cross-validation generator here\n",
"\n",
"efs1 = efs1.fit(X, y)\n",
"\n",
- "print('Best accuracy score: %.2f' % efs1.best_score_)\n",
- "print('Best subset (indices):', efs1.best_idx_)\n",
- "print('Best subset (corresponding names):', efs1.best_feature_names_)"
+ "print(\"Best accuracy score: %.2f\" % efs1.best_score_)\n",
+ "print(\"Best subset (indices):\", efs1.best_idx_)\n",
+ "print(\"Best subset (corresponding names):\", efs1.best_feature_names_)"
]
},
{
@@ -1469,11 +1459,9 @@
"\n",
"model = LogisticRegression(max_iter=10000)\n",
"\n",
- "efs1 = EFS(model, \n",
- " min_features=1, \n",
- " max_features=4,\n",
- " print_progress=True,\n",
- " scoring='accuracy')\n",
+ "efs1 = EFS(\n",
+ " model, min_features=1, max_features=4, print_progress=True, scoring=\"accuracy\"\n",
+ ")\n",
"\n",
"efs1 = efs1.fit(X_train, y_train)"
]
@@ -1516,8 +1504,8 @@
}
],
"source": [
- "print('Best accuracy score: %.2f' % efs1.best_score_)\n",
- "print('Best subset (indices):', efs1.best_idx_)"
+ "print(\"Best accuracy score: %.2f\" % efs1.best_score_)\n",
+ "print(\"Best subset (indices):\", efs1.best_idx_)"
]
},
{
@@ -1637,8 +1625,7 @@
"X = iris.data\n",
"y = iris.target\n",
"\n",
- "X_df = pd.DataFrame(X, columns=['sepal len', 'petal len',\n",
- " 'sepal wid', 'petal wid'])\n",
+ "X_df = pd.DataFrame(X, columns=[\"sepal len\", \"petal len\", \"sepal wid\", \"petal wid\"])\n",
"X_df.head()"
]
},
@@ -1670,18 +1657,20 @@
"\n",
"knn = KNeighborsClassifier(n_neighbors=3)\n",
"\n",
- "efs1 = EFS(knn, \n",
- " min_features=2,\n",
- " max_features=2,\n",
- " scoring='accuracy',\n",
- " feature_groups=[['sepal len', 'sepal wid'], ['petal len'], ['petal wid']],\n",
- " cv=3)\n",
+ "efs1 = EFS(\n",
+ " knn,\n",
+ " min_features=2,\n",
+ " max_features=2,\n",
+ " scoring=\"accuracy\",\n",
+ " feature_groups=[[\"sepal len\", \"sepal wid\"], [\"petal len\"], [\"petal wid\"]],\n",
+ " cv=3,\n",
+ ")\n",
"\n",
"efs1 = efs1.fit(X_df, y)\n",
"\n",
- "print('Best accuracy score: %.2f' % efs1.best_score_)\n",
- "print('Best subset (indices):', efs1.best_idx_)\n",
- "print('Best subset (corresponding names):', efs1.best_feature_names_)"
+ "print(\"Best accuracy score: %.2f\" % efs1.best_score_)\n",
+ "print(\"Best subset (indices):\", efs1.best_idx_)\n",
+ "print(\"Best subset (corresponding names):\", efs1.best_feature_names_)"
]
},
{
@@ -2100,7 +2089,9 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.feature_selection/ExhaustiveFeatureSelector.md', 'r') as f:\n",
+ "with open(\n",
+ " \"../../api_modules/mlxtend.feature_selection/ExhaustiveFeatureSelector.md\", \"r\"\n",
+ ") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/feature_selection/SequentialFeatureSelector.ipynb b/docs/sources/user_guide/feature_selection/SequentialFeatureSelector.ipynb
index 93e6d464c..d1c2a1d41 100644
--- a/docs/sources/user_guide/feature_selection/SequentialFeatureSelector.ipynb
+++ b/docs/sources/user_guide/feature_selection/SequentialFeatureSelector.ipynb
@@ -388,13 +388,9 @@
"source": [
"from mlxtend.feature_selection import SequentialFeatureSelector as SFS\n",
"\n",
- "sfs1 = SFS(knn, \n",
- " k_features=3, \n",
- " forward=True, \n",
- " floating=False, \n",
- " verbose=2,\n",
- " scoring='accuracy',\n",
- " cv=0)\n",
+ "sfs1 = SFS(\n",
+ " knn, k_features=3, forward=True, floating=False, verbose=2, scoring=\"accuracy\", cv=0\n",
+ ")\n",
"\n",
"sfs1 = sfs1.fit(X, y)"
]
@@ -645,7 +641,9 @@
"source": [
"import pandas as pd\n",
"\n",
- "df_X = pd.DataFrame(X, columns=[\"Sepal length\", \"Sepal width\", \"Petal length\", \"Petal width\"])\n",
+ "df_X = pd.DataFrame(\n",
+ " X, columns=[\"Sepal length\", \"Sepal width\", \"Petal length\", \"Petal width\"]\n",
+ ")\n",
"df_X.head()"
]
},
@@ -692,9 +690,9 @@
"source": [
"sfs1 = sfs1.fit(df_X, y)\n",
"\n",
- "print('Best accuracy score: %.2f' % sfs1.k_score_)\n",
- "print('Best subset (indices):', sfs1.k_feature_idx_)\n",
- "print('Best subset (corresponding names):', sfs1.k_feature_names_)"
+ "print(\"Best accuracy score: %.2f\" % sfs1.k_score_)\n",
+ "print(\"Best subset (indices):\", sfs1.k_feature_idx_)\n",
+ "print(\"Best subset (corresponding names):\", sfs1.k_feature_names_)"
]
},
{
@@ -745,69 +743,59 @@
],
"source": [
"# Sequential Forward Selection\n",
- "sfs = SFS(knn, \n",
- " k_features=3, \n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " cv=4,\n",
- " n_jobs=-1)\n",
+ "sfs = SFS(\n",
+ " knn, k_features=3, forward=True, floating=False, scoring=\"accuracy\", cv=4, n_jobs=-1\n",
+ ")\n",
"sfs = sfs.fit(X, y)\n",
"\n",
- "print('\\nSequential Forward Selection (k=3):')\n",
+ "print(\"\\nSequential Forward Selection (k=3):\")\n",
"print(sfs.k_feature_idx_)\n",
- "print('CV Score:')\n",
+ "print(\"CV Score:\")\n",
"print(sfs.k_score_)\n",
"\n",
"###################################################\n",
"\n",
"# Sequential Backward Selection\n",
- "sbs = SFS(knn, \n",
- " k_features=3, \n",
- " forward=False, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " cv=4,\n",
- " n_jobs=-1)\n",
+ "sbs = SFS(\n",
+ " knn,\n",
+ " k_features=3,\n",
+ " forward=False,\n",
+ " floating=False,\n",
+ " scoring=\"accuracy\",\n",
+ " cv=4,\n",
+ " n_jobs=-1,\n",
+ ")\n",
"sbs = sbs.fit(X, y)\n",
"\n",
- "print('\\nSequential Backward Selection (k=3):')\n",
+ "print(\"\\nSequential Backward Selection (k=3):\")\n",
"print(sbs.k_feature_idx_)\n",
- "print('CV Score:')\n",
+ "print(\"CV Score:\")\n",
"print(sbs.k_score_)\n",
"\n",
"###################################################\n",
"\n",
"# Sequential Forward Floating Selection\n",
- "sffs = SFS(knn, \n",
- " k_features=3, \n",
- " forward=True, \n",
- " floating=True, \n",
- " scoring='accuracy',\n",
- " cv=4,\n",
- " n_jobs=-1)\n",
+ "sffs = SFS(\n",
+ " knn, k_features=3, forward=True, floating=True, scoring=\"accuracy\", cv=4, n_jobs=-1\n",
+ ")\n",
"sffs = sffs.fit(X, y)\n",
"\n",
- "print('\\nSequential Forward Floating Selection (k=3):')\n",
+ "print(\"\\nSequential Forward Floating Selection (k=3):\")\n",
"print(sffs.k_feature_idx_)\n",
- "print('CV Score:')\n",
+ "print(\"CV Score:\")\n",
"print(sffs.k_score_)\n",
"\n",
"###################################################\n",
"\n",
"# Sequential Backward Floating Selection\n",
- "sbfs = SFS(knn, \n",
- " k_features=3, \n",
- " forward=False, \n",
- " floating=True, \n",
- " scoring='accuracy',\n",
- " cv=4,\n",
- " n_jobs=-1)\n",
+ "sbfs = SFS(\n",
+ " knn, k_features=3, forward=False, floating=True, scoring=\"accuracy\", cv=4, n_jobs=-1\n",
+ ")\n",
"sbfs = sbfs.fit(X, y)\n",
"\n",
- "print('\\nSequential Backward Floating Selection (k=3):')\n",
+ "print(\"\\nSequential Backward Floating Selection (k=3):\")\n",
"print(sbfs.k_feature_idx_)\n",
- "print('CV Score:')\n",
+ "print(\"CV Score:\")\n",
"print(sbfs.k_score_)"
]
},
@@ -928,6 +916,7 @@
],
"source": [
"import pandas as pd\n",
+ "\n",
"pd.DataFrame.from_dict(sfs.get_metric_dict()).T"
]
},
@@ -1169,20 +1158,16 @@
"from mlxtend.plotting import plot_sequential_feature_selection as plot_sfs\n",
"import matplotlib.pyplot as plt\n",
"\n",
- "sfs = SFS(knn, \n",
- " k_features=4, \n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " verbose=2,\n",
- " cv=5)\n",
+ "sfs = SFS(\n",
+ " knn, k_features=4, forward=True, floating=False, scoring=\"accuracy\", verbose=2, cv=5\n",
+ ")\n",
"\n",
"sfs = sfs.fit(X, y)\n",
"\n",
- "fig1 = plot_sfs(sfs.get_metric_dict(), kind='std_dev')\n",
+ "fig1 = plot_sfs(sfs.get_metric_dict(), kind=\"std_dev\")\n",
"\n",
"plt.ylim([0.8, 1])\n",
- "plt.title('Sequential Forward Selection (w. StdDev)')\n",
+ "plt.title(\"Sequential Forward Selection (w. StdDev)\")\n",
"plt.grid()\n",
"plt.show()"
]
@@ -1227,17 +1212,19 @@
"\n",
"lr = LinearRegression()\n",
"\n",
- "sfs = SFS(lr, \n",
- " k_features=8, \n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='neg_mean_squared_error',\n",
- " cv=10)\n",
+ "sfs = SFS(\n",
+ " lr,\n",
+ " k_features=8,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " scoring=\"neg_mean_squared_error\",\n",
+ " cv=10,\n",
+ ")\n",
"\n",
"sfs = sfs.fit(X, y)\n",
- "fig = plot_sfs(sfs.get_metric_dict(), kind='std_err')\n",
+ "fig = plot_sfs(sfs.get_metric_dict(), kind=\"std_err\")\n",
"\n",
- "plt.title('Sequential Forward Selection (w. StdErr)')\n",
+ "plt.title(\"Sequential Forward Selection (w. StdErr)\")\n",
"plt.grid()\n",
"plt.show()"
]
@@ -1315,17 +1302,18 @@
"from mlxtend.feature_selection import SequentialFeatureSelector as SFS\n",
"\n",
"\n",
- "\n",
"knn = KNeighborsClassifier(n_neighbors=4)\n",
"piter = PredefinedHoldoutSplit(my_validation_indices)\n",
"\n",
- "sfs1 = SFS(knn, \n",
- " k_features=3, \n",
- " forward=True, \n",
- " floating=False, \n",
- " verbose=2,\n",
- " scoring='accuracy',\n",
- " cv=piter)\n",
+ "sfs1 = SFS(\n",
+ " knn,\n",
+ " k_features=3,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " verbose=2,\n",
+ " scoring=\"accuracy\",\n",
+ " cv=piter,\n",
+ ")\n",
"\n",
"sfs1 = sfs1.fit(X, y)"
]
@@ -1352,7 +1340,8 @@
"iris = load_iris()\n",
"X, y = iris.data, iris.target\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.33, random_state=1)\n",
+ " X, y, test_size=0.33, random_state=1\n",
+ ")\n",
"\n",
"knn = KNeighborsClassifier(n_neighbors=4)"
]
@@ -1368,12 +1357,7 @@
"\n",
"from mlxtend.feature_selection import SequentialFeatureSelector as SFS\n",
"\n",
- "sfs1 = SFS(knn, \n",
- " k_features=3, \n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " cv=5)\n",
+ "sfs1 = SFS(knn, k_features=3, forward=True, floating=False, scoring=\"accuracy\", cv=5)\n",
"sfs1 = sfs1.fit(X_train, y_train)"
]
},
@@ -1391,7 +1375,7 @@
}
],
"source": [
- "print('Selected features:', sfs1.k_feature_idx_)"
+ "print(\"Selected features:\", sfs1.k_feature_idx_)"
]
},
{
@@ -1422,7 +1406,7 @@
"\n",
"# Compute the accuracy of the prediction\n",
"acc = float((y_test == y_pred).sum()) / y_pred.shape[0]\n",
- "print('Test set accuracy: %.2f %%' % (acc * 100))"
+ "print(\"Test set accuracy: %.2f %%\" % (acc * 100))"
]
},
{
@@ -1454,7 +1438,8 @@
"iris = load_iris()\n",
"X, y = iris.data, iris.target\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
- " X, y, test_size=0.2, random_state=123)"
+ " X, y, test_size=0.2, random_state=123\n",
+ ")"
]
},
{
@@ -1466,33 +1451,30 @@
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.pipeline import Pipeline\n",
"from mlxtend.feature_selection import SequentialFeatureSelector as SFS\n",
- "import mlxtend\n",
"\n",
"knn1 = KNeighborsClassifier()\n",
"knn2 = KNeighborsClassifier()\n",
"\n",
- "sfs1 = SFS(estimator=knn1, \n",
- " k_features=3,\n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " cv=5)\n",
+ "sfs1 = SFS(\n",
+ " estimator=knn1, k_features=3, forward=True, floating=False, scoring=\"accuracy\", cv=5\n",
+ ")\n",
"\n",
- "pipe = Pipeline([('sfs', sfs1), \n",
- " ('knn2', knn2)])\n",
+ "pipe = Pipeline([(\"sfs\", sfs1), (\"knn2\", knn2)])\n",
"\n",
"param_grid = {\n",
- " 'sfs__k_features': [1, 2, 3],\n",
- " 'sfs__estimator__n_neighbors': [3, 4, 7], # inner knn\n",
- " 'knn2__n_neighbors': [3, 4, 7] # outer knn\n",
- " }\n",
- " \n",
- "gs = GridSearchCV(estimator=pipe, \n",
- " param_grid=param_grid, \n",
- " scoring='accuracy', \n",
- " n_jobs=1, \n",
- " cv=5,\n",
- " refit=False)\n",
+ " \"sfs__k_features\": [1, 2, 3],\n",
+ " \"sfs__estimator__n_neighbors\": [3, 4, 7], # inner knn\n",
+ " \"knn2__n_neighbors\": [3, 4, 7], # outer knn\n",
+ "}\n",
+ "\n",
+ "gs = GridSearchCV(\n",
+ " estimator=pipe,\n",
+ " param_grid=param_grid,\n",
+ " scoring=\"accuracy\",\n",
+ " n_jobs=1,\n",
+ " cv=5,\n",
+ " refit=False,\n",
+ ")\n",
"\n",
"# run gridearch\n",
"gs = gs.fit(X_train, y_train)"
@@ -1643,27 +1625,28 @@
"from sklearn.pipeline import make_pipeline\n",
"\n",
"X, y = wine_data()\n",
- "X_train, X_test, y_train, y_test= train_test_split(X, y, \n",
- " stratify=y,\n",
- " test_size=0.3,\n",
- " random_state=1)\n",
+ "X_train, X_test, y_train, y_test = train_test_split(\n",
+ " X, y, stratify=y, test_size=0.3, random_state=1\n",
+ ")\n",
"\n",
"knn = KNeighborsClassifier(n_neighbors=2)\n",
"\n",
- "sfs1 = SFS(estimator=knn, \n",
- " k_features=(3, 10),\n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " cv=5)\n",
+ "sfs1 = SFS(\n",
+ " estimator=knn,\n",
+ " k_features=(3, 10),\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " scoring=\"accuracy\",\n",
+ " cv=5,\n",
+ ")\n",
"\n",
"pipe = make_pipeline(StandardScaler(), sfs1)\n",
"\n",
"pipe.fit(X_train, y_train)\n",
"\n",
- "print('best combination (ACC: %.3f): %s\\n' % (sfs1.k_score_, sfs1.k_feature_idx_))\n",
- "print('all subsets:\\n', sfs1.subsets_)\n",
- "plot_sfs(sfs1.get_metric_dict(), kind='std_err');"
+ "print(\"best combination (ACC: %.3f): %s\\n\" % (sfs1.k_score_, sfs1.k_feature_idx_))\n",
+ "print(\"all subsets:\\n\", sfs1.subsets_)\n",
+ "plot_sfs(sfs1.get_metric_dict(), kind=\"std_err\");"
]
},
{
@@ -1715,7 +1698,7 @@
"\n",
"X, y = iris_data()\n",
"groups = np.arange(len(y)) // 10\n",
- "print('groups: {}'.format(groups))"
+ "print(\"groups: {}\".format(groups))"
]
},
{
@@ -1778,14 +1761,11 @@
],
"source": [
"knn = KNeighborsClassifier(n_neighbors=2)\n",
- "sfs = SFS(estimator=knn, \n",
- " k_features=2,\n",
- " scoring='accuracy',\n",
- " cv=cv)\n",
+ "sfs = SFS(estimator=knn, k_features=2, scoring=\"accuracy\", cv=cv)\n",
"\n",
"sfs.fit(X, y)\n",
"\n",
- "print('best combination (ACC: %.3f): %s\\n' % (sfs.k_score_, sfs.k_feature_idx_))"
+ "print(\"best combination (ACC: %.3f): %s\\n\" % (sfs.k_score_, sfs.k_feature_idx_))"
]
},
{
@@ -1904,13 +1884,15 @@
"\n",
"model = LogisticRegression()\n",
"\n",
- "sfs1 = SFS(model, \n",
- " k_features=10, \n",
- " forward=True, \n",
- " floating=False, \n",
- " verbose=2,\n",
- " scoring='accuracy',\n",
- " cv=5)\n",
+ "sfs1 = SFS(\n",
+ " model,\n",
+ " k_features=10,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " verbose=2,\n",
+ " scoring=\"accuracy\",\n",
+ " cv=5,\n",
+ ")\n",
"\n",
"sfs1 = sfs1.fit(X_train, y_train)"
]
@@ -1988,12 +1970,7 @@
"y = iris.target\n",
"knn = KNeighborsClassifier(n_neighbors=4)\n",
"\n",
- "sfs1 = SFS(knn, \n",
- " k_features=3, \n",
- " forward=True, \n",
- " floating=False, \n",
- " scoring='accuracy',\n",
- " cv=0)"
+ "sfs1 = SFS(knn, k_features=3, forward=True, floating=False, scoring=\"accuracy\", cv=0)"
]
},
{
@@ -2083,8 +2060,7 @@
}
],
"source": [
- "X_df = pd.DataFrame(X, columns=['sepal len', 'petal len',\n",
- " 'sepal width', 'petal width'])\n",
+ "X_df = pd.DataFrame(X, columns=[\"sepal len\", \"petal len\", \"sepal width\", \"petal width\"])\n",
"X_df.head()"
]
},
@@ -2225,14 +2201,16 @@
"\n",
"from mlxtend.feature_selection import SequentialFeatureSelector as SFS\n",
"\n",
- "sfs1 = SFS(knn, \n",
- " k_features=4, \n",
- " forward=True, \n",
- " floating=False, \n",
- " verbose=2,\n",
- " scoring='accuracy',\n",
- " fixed_features=(0, 2),\n",
- " cv=3)\n",
+ "sfs1 = SFS(\n",
+ " knn,\n",
+ " k_features=4,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " verbose=2,\n",
+ " scoring=\"accuracy\",\n",
+ " fixed_features=(0, 2),\n",
+ " cv=3,\n",
+ ")\n",
"\n",
"sfs1 = sfs1.fit(X, y)"
]
@@ -2371,8 +2349,7 @@
}
],
"source": [
- "X_df = pd.DataFrame(X, columns=['sepal len', 'petal len',\n",
- " 'sepal width', 'petal width'])\n",
+ "X_df = pd.DataFrame(X, columns=[\"sepal len\", \"petal len\", \"sepal width\", \"petal width\"])\n",
"X_df.head()"
]
},
@@ -2398,14 +2375,16 @@
}
],
"source": [
- "sfs2 = SFS(knn, \n",
- " k_features=4, \n",
- " forward=True, \n",
- " floating=False, \n",
- " verbose=2,\n",
- " scoring='accuracy',\n",
- " fixed_features=('sepal len', 'petal len'),\n",
- " cv=3)\n",
+ "sfs2 = SFS(\n",
+ " knn,\n",
+ " k_features=4,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " verbose=2,\n",
+ " scoring=\"accuracy\",\n",
+ " fixed_features=(\"sepal len\", \"petal len\"),\n",
+ " cv=3,\n",
+ ")\n",
"\n",
"sfs2 = sfs2.fit(X_df, y_series)"
]
@@ -2558,9 +2537,8 @@
"X = iris.data\n",
"y = iris.target\n",
"\n",
- "X_df = pd.DataFrame(X, columns=['sepal len', 'petal len',\n",
- " 'sepal wid', 'petal wid'])\n",
- "X_df.head()\n"
+ "X_df = pd.DataFrame(X, columns=[\"sepal len\", \"petal len\", \"sepal wid\", \"petal wid\"])\n",
+ "X_df.head()"
]
},
{
@@ -2574,11 +2552,13 @@
"\n",
"knn = KNeighborsClassifier(n_neighbors=3)\n",
"\n",
- "sfs1 = SFS(knn, \n",
- " k_features=2, \n",
- " scoring='accuracy',\n",
- " feature_groups=(['sepal len', 'sepal wid'], ['petal len'], ['petal wid']),\n",
- " cv=3)\n",
+ "sfs1 = SFS(\n",
+ " knn,\n",
+ " k_features=2,\n",
+ " scoring=\"accuracy\",\n",
+ " feature_groups=([\"sepal len\", \"sepal wid\"], [\"petal len\"], [\"petal wid\"]),\n",
+ " cv=3,\n",
+ ")\n",
"\n",
"sfs1 = sfs1.fit(X_df, y)"
]
@@ -2619,6 +2599,7 @@
"outputs": [],
"source": [
"from sklearn.datasets import make_blobs\n",
+ "\n",
"X, y = make_blobs(n_samples=10, centers=4, n_features=5, random_state=0)"
]
},
@@ -2650,13 +2631,15 @@
"source": [
"from mlxtend.feature_selection import SequentialFeatureSelector as SFS\n",
"\n",
- "sfs1 = SFS(knn, \n",
- " k_features=3, \n",
- " forward=True, \n",
- " floating=False, \n",
- " verbose=2,\n",
- " scoring='roc_auc_ovr',\n",
- " cv=0)\n",
+ "sfs1 = SFS(\n",
+ " knn,\n",
+ " k_features=3,\n",
+ " forward=True,\n",
+ " floating=False,\n",
+ " verbose=2,\n",
+ " scoring=\"roc_auc_ovr\",\n",
+ " cv=0,\n",
+ ")\n",
"\n",
"sfs1 = sfs1.fit(X, y)"
]
@@ -3041,7 +3024,9 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.feature_selection/SequentialFeatureSelector.md', 'r') as f:\n",
+ "with open(\n",
+ " \"../../api_modules/mlxtend.feature_selection/SequentialFeatureSelector.md\", \"r\"\n",
+ ") as f:\n",
" s = f.read()\n",
"print(s)"
]
diff --git a/docs/sources/user_guide/file_io/find_filegroups.ipynb b/docs/sources/user_guide/file_io/find_filegroups.ipynb
index b97ce7225..a2a7b10a5 100644
--- a/docs/sources/user_guide/file_io/find_filegroups.ipynb
+++ b/docs/sources/user_guide/file_io/find_filegroups.ipynb
@@ -113,10 +113,14 @@
"source": [
"from mlxtend.file_io import find_filegroups\n",
"\n",
- "find_filegroups(paths=['./data_find_filegroups/dir_1', \n",
- " './data_find_filegroups/dir_2', \n",
- " './data_find_filegroups/dir_3'], \n",
- " substring='file_')"
+ "find_filegroups(\n",
+ " paths=[\n",
+ " \"./data_find_filegroups/dir_1\",\n",
+ " \"./data_find_filegroups/dir_2\",\n",
+ " \"./data_find_filegroups/dir_3\",\n",
+ " ],\n",
+ " substring=\"file_\",\n",
+ ")"
]
},
{
@@ -198,7 +202,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.file_io/find_filegroups.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.file_io/find_filegroups.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/file_io/find_files.ipynb b/docs/sources/user_guide/file_io/find_files.ipynb
index 96d49529d..dcdae1374 100644
--- a/docs/sources/user_guide/file_io/find_files.ipynb
+++ b/docs/sources/user_guide/file_io/find_files.ipynb
@@ -94,7 +94,7 @@
"source": [
"from mlxtend.file_io import find_files\n",
"\n",
- "find_files(substring='_2', path='./data_find_filegroups/', recursive=True)"
+ "find_files(substring=\"_2\", path=\"./data_find_filegroups/\", recursive=True)"
]
},
{
@@ -163,7 +163,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.file_io/find_files.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.file_io/find_files.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/frequent_patterns/apriori.ipynb b/docs/sources/user_guide/frequent_patterns/apriori.ipynb
index fa1189919..2304ce5a1 100644
--- a/docs/sources/user_guide/frequent_patterns/apriori.ipynb
+++ b/docs/sources/user_guide/frequent_patterns/apriori.ipynb
@@ -70,11 +70,13 @@
"metadata": {},
"outputs": [],
"source": [
- "dataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\n",
- " ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\n",
- " ['Milk', 'Apple', 'Kidney Beans', 'Eggs'],\n",
- " ['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],\n",
- " ['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]"
+ "dataset = [\n",
+ " [\"Milk\", \"Onion\", \"Nutmeg\", \"Kidney Beans\", \"Eggs\", \"Yogurt\"],\n",
+ " [\"Dill\", \"Onion\", \"Nutmeg\", \"Kidney Beans\", \"Eggs\", \"Yogurt\"],\n",
+ " [\"Milk\", \"Apple\", \"Kidney Beans\", \"Eggs\"],\n",
+ " [\"Milk\", \"Unicorn\", \"Corn\", \"Kidney Beans\", \"Yogurt\"],\n",
+ " [\"Corn\", \"Onion\", \"Onion\", \"Kidney Beans\", \"Ice cream\", \"Eggs\"],\n",
+ "]"
]
},
{
@@ -611,7 +613,7 @@
],
"source": [
"frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)\n",
- "frequent_itemsets['length'] = frequent_itemsets['itemsets'].apply(lambda x: len(x))\n",
+ "frequent_itemsets[\"length\"] = frequent_itemsets[\"itemsets\"].apply(lambda x: len(x))\n",
"frequent_itemsets"
]
},
@@ -675,8 +677,9 @@
}
],
"source": [
- "frequent_itemsets[ (frequent_itemsets['length'] == 2) &\n",
- " (frequent_itemsets['support'] >= 0.8) ]"
+ "frequent_itemsets[\n",
+ " (frequent_itemsets[\"length\"] == 2) & (frequent_itemsets[\"support\"] >= 0.8)\n",
+ "]"
]
},
{
@@ -739,7 +742,7 @@
}
],
"source": [
- "frequent_itemsets[ frequent_itemsets['itemsets'] == {'Onion', 'Eggs'} ]"
+ "frequent_itemsets[frequent_itemsets[\"itemsets\"] == {\"Onion\", \"Eggs\"}]"
]
},
{
@@ -1143,7 +1146,7 @@
}
],
"source": [
- "with open('../../api_modules/mlxtend.frequent_patterns/apriori.md', 'r') as f:\n",
+ "with open(\"../../api_modules/mlxtend.frequent_patterns/apriori.md\", \"r\") as f:\n",
" print(f.read())"
]
}
diff --git a/docs/sources/user_guide/frequent_patterns/association_rules.ipynb b/docs/sources/user_guide/frequent_patterns/association_rules.ipynb
index 99519183c..fe4502abb 100644
--- a/docs/sources/user_guide/frequent_patterns/association_rules.ipynb
+++ b/docs/sources/user_guide/frequent_patterns/association_rules.ipynb
@@ -320,14 +320,16 @@
"source": [
"import pandas as pd\n",
"from mlxtend.preprocessing import TransactionEncoder\n",
- "from mlxtend.frequent_patterns import apriori, fpmax, fpgrowth\n",
+ "from mlxtend.frequent_patterns import fpgrowth\n",
"\n",
"\n",
- "dataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\n",
- " ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\n",
- " ['Milk', 'Apple', 'Kidney Beans', 'Eggs'],\n",
- " ['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],\n",
- " ['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]\n",
+ "dataset = [\n",
+ " [\"Milk\", \"Onion\", \"Nutmeg\", \"Kidney Beans\", \"Eggs\", \"Yogurt\"],\n",
+ " [\"Dill\", \"Onion\", \"Nutmeg\", \"Kidney Beans\", \"Eggs\", \"Yogurt\"],\n",
+ " [\"Milk\", \"Apple\", \"Kidney Beans\", \"Eggs\"],\n",
+ " [\"Milk\", \"Unicorn\", \"Corn\", \"Kidney Beans\", \"Yogurt\"],\n",
+ " [\"Corn\", \"Onion\", \"Onion\", \"Kidney Beans\", \"Ice cream\", \"Eggs\"],\n",
+ "]\n",
"\n",
"te = TransactionEncoder()\n",
"te_ary = te.fit(dataset).transform(dataset)\n",
@@ -335,8 +337,8 @@
"\n",
"frequent_itemsets = fpgrowth(df, min_support=0.6, use_colnames=True)\n",
"### alternatively:\n",
- "#frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)\n",
- "#frequent_itemsets = fpmax(df, min_support=0.6, use_colnames=True)\n",
+ "# frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)\n",
+ "# frequent_itemsets = fpmax(df, min_support=0.6, use_colnames=True)\n",
"\n",
"frequent_itemsets"
]
@@ -659,7 +661,12 @@
"source": [
"from mlxtend.frequent_patterns import association_rules\n",
"\n",
- "association_rules(frequent_itemsets, metric=\"confidence\", min_threshold=0.7, num_itemsets=len(df.index))"
+ "association_rules(\n",
+ " frequent_itemsets,\n",
+ " metric=\"confidence\",\n",
+ " min_threshold=0.7,\n",
+ " num_itemsets=len(df.index),\n",
+ ")"
]
},
{
@@ -857,7 +864,9 @@
}
],
"source": [
- "rules = association_rules(frequent_itemsets, metric=\"lift\", min_threshold=1.2, num_itemsets=len(df.index))\n",
+ "rules = association_rules(\n",
+ " frequent_itemsets, metric=\"lift\", min_threshold=1.2, num_itemsets=len(df.index)\n",
+ ")\n",
"rules"
]
},
@@ -1156,9 +1165,11 @@
}
],
"source": [
- "rules[ (rules['antecedent_len'] >= 2) &\n",
- " (rules['confidence'] > 0.75) &\n",
- " (rules['lift'] > 1.2) ]"
+ "rules[\n",
+ " (rules[\"antecedent_len\"] >= 2)\n",
+ " & (rules[\"confidence\"] > 0.75)\n",
+ " & (rules[\"lift\"] > 1.2)\n",
+ "]"
]
},
{
@@ -1251,7 +1262,7 @@
}
],
"source": [
- "rules[rules['antecedents'] == {'Eggs', 'Kidney Beans'}]"
+ "rules[rules[\"antecedents\"] == {\"Eggs\", \"Kidney Beans\"}]"
]
},
{
@@ -1376,12 +1387,18 @@
"source": [
"import pandas as pd\n",
"\n",
- "dict = {'itemsets': [['177', '176'], ['177', '179'],\n",
- " ['176', '178'], ['176', '179'],\n",
- " ['93', '100'], ['177', '178'],\n",
- " ['177', '176', '178']],\n",
- " 'support':[0.253623, 0.253623, 0.217391,\n",
- " 0.217391, 0.181159, 0.108696, 0.108696]}\n",
+ "dict = {\n",
+ " \"itemsets\": [\n",
+ " [\"177\", \"176\"],\n",
+ " [\"177\", \"179\"],\n",
+ " [\"176\", \"178\"],\n",
+ " [\"176\", \"179\"],\n",
+ " [\"93\", \"100\"],\n",
+ " [\"177\", \"178\"],\n",
+ " [\"177\", \"176\", \"178\"],\n",
+ " ],\n",
+ " \"support\": [0.253623, 0.253623, 0.217391, 0.217391, 0.181159, 0.108696, 0.108696],\n",
+ "}\n",
"\n",
"freq_itemsets = pd.DataFrame(dict)\n",
"freq_itemsets"
@@ -1830,7 +1847,9 @@
"source": [
"from mlxtend.frequent_patterns import association_rules\n",
"\n",
- "res = association_rules(freq_itemsets, support_only=True, min_threshold=0.1, num_itemsets=0)\n",
+ "res = association_rules(\n",
+ " freq_itemsets, support_only=True, min_threshold=0.1, num_itemsets=0\n",
+ ")\n",
"res"
]
},
@@ -2013,7 +2032,7 @@
}
],
"source": [
- "res = res[['antecedents', 'consequents', 'support']]\n",
+ "res = res[[\"antecedents\", \"consequents\", \"support\"]]\n",
"res"
]
},
@@ -2215,22 +2234,26 @@
"source": [
"import pandas as pd\n",
"from mlxtend.preprocessing import TransactionEncoder\n",
- "from mlxtend.frequent_patterns import apriori, fpmax, fpgrowth\n",
+ "from mlxtend.frequent_patterns import fpgrowth\n",
"from mlxtend.frequent_patterns import association_rules\n",
"\n",
"\n",
- "dataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\n",
- " ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\n",
- " ['Milk', 'Apple', 'Kidney Beans', 'Eggs'],\n",
- " ['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],\n",
- " ['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]\n",
+ "dataset = [\n",
+ " [\"Milk\", \"Onion\", \"Nutmeg\", \"Kidney Beans\", \"Eggs\", \"Yogurt\"],\n",
+ " [\"Dill\", \"Onion\", \"Nutmeg\", \"Kidney Beans\", \"Eggs\", \"Yogurt\"],\n",
+ " [\"Milk\", \"Apple\", \"Kidney Beans\", \"Eggs\"],\n",
+ " [\"Milk\", \"Unicorn\", \"Corn\", \"Kidney Beans\", \"Yogurt\"],\n",
+ " [\"Corn\", \"Onion\", \"Onion\", \"Kidney Beans\", \"Ice cream\", \"Eggs\"],\n",
+ "]\n",
"\n",
"te = TransactionEncoder()\n",
"te_ary = te.fit(dataset).transform(dataset)\n",
"df = pd.DataFrame(te_ary, columns=te.columns_)\n",
"\n",
"frequent_itemsets = fpgrowth(df, min_support=0.6, use_colnames=True)\n",
- "rules = association_rules(frequent_itemsets, metric=\"lift\", min_threshold=1.2, num_itemsets=len(df.index))\n",
+ "rules = association_rules(\n",
+ " frequent_itemsets, metric=\"lift\", min_threshold=1.2, num_itemsets=len(df.index)\n",
+ ")\n",
"rules"
]
},
@@ -2402,11 +2425,13 @@
}
],
"source": [
- "antecedent_sele = rules['antecedents'] == frozenset({'Onion', 'Kidney Beans'}) # or frozenset({'Kidney Beans', 'Onion'})\n",
- "consequent_sele = rules['consequents'] == frozenset({'Eggs'})\n",
- "final_sele = (antecedent_sele & consequent_sele)\n",
+ "antecedent_sele = rules[\"antecedents\"] == frozenset(\n",
+ " {\"Onion\", \"Kidney Beans\"}\n",
+ ") # or frozenset({'Kidney Beans', 'Onion'})\n",
+ "consequent_sele = rules[\"consequents\"] == frozenset({\"Eggs\"})\n",
+ "final_sele = antecedent_sele & consequent_sele\n",
"\n",
- "rules.loc[ ~final_sele ]"
+ "rules.loc[~final_sele]"
]
},
{
@@ -2418,16 +2443,13 @@
},
{
"cell_type": "code",
-
"execution_count": 20,
-
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
-
"/tmp/ipykernel_34953/2823279667.py:23: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'nan' has dtype incompatible with bool, please explicitly cast to a compatible dtype first.\n",
" df.iloc[idx[i], col[i]] = np.nan\n",
"/tmp/ipykernel_34953/2823279667.py:23: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'nan' has dtype incompatible with bool, please explicitly cast to a compatible dtype first.\n",
@@ -2441,7 +2463,6 @@
"/tmp/ipykernel_34953/2823279667.py:23: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'nan' has dtype incompatible with bool, please explicitly cast to a compatible dtype first.\n",
" df.iloc[idx[i], col[i]] = np.nan\n",
"/tmp/ipykernel_34953/2823279667.py:23: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'nan' has dtype incompatible with bool, please explicitly cast to a compatible dtype first.\n",
-
" df.iloc[idx[i], col[i]] = np.nan\n"
]
},
@@ -2493,7 +2514,6 @@
"