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 @@ " True\n", " False\n", " NaN\n", - " \n", " \n", " 1\n", @@ -2580,15 +2600,17 @@ "import pandas as pd\n", "import numpy as np\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", @@ -2715,16 +2737,21 @@ ] }, "execution_count": 21, - "metadata": {}, "output_type": "execute_result" } ], "source": [ - "frequent_itemsets = fpgrowth(df, min_support=0.6, null_values = True, use_colnames=True)\n", + "frequent_itemsets = fpgrowth(df, min_support=0.6, null_values=True, use_colnames=True)\n", "# frequent_itemsets = fpmax(df, min_support=0.6, null_values = True, use_colnames=True)\n", - "rules = association_rules(frequent_itemsets, len(df), df, null_values = True, metric=\"confidence\", min_threshold=0.8)\n", - + "rules = association_rules(\n", + " frequent_itemsets,\n", + " len(df),\n", + " df,\n", + " null_values=True,\n", + " metric=\"confidence\",\n", + " min_threshold=0.8,\n", + ")\n", "rules" ] }, @@ -2741,7 +2768,7 @@ "metadata": {}, "outputs": [], "source": [ - "with open('../../api_modules/mlxtend.frequent_patterns/association_rules.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.frequent_patterns/association_rules.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/frequent_patterns/fpgrowth.ipynb b/docs/sources/user_guide/frequent_patterns/fpgrowth.ipynb index 279c0e106..ca228c884 100644 --- a/docs/sources/user_guide/frequent_patterns/fpgrowth.ipynb +++ b/docs/sources/user_guide/frequent_patterns/fpgrowth.ipynb @@ -80,11 +80,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", + "]" ] }, { @@ -735,7 +737,7 @@ } ], "source": [ - "fpgrowth(df, min_support=0.6, null_values = True, use_colnames=True)" + "fpgrowth(df, min_support=0.6, null_values=True, use_colnames=True)" ] }, { @@ -780,8 +782,6 @@ } ], "source": [ - "from mlxtend.frequent_patterns import apriori\n", - "\n", "%timeit -n 100 -r 10 apriori(df, min_support=0.6)" ] }, @@ -928,7 +928,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.frequent_patterns/fpgrowth.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.frequent_patterns/fpgrowth.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/frequent_patterns/fpmax.ipynb b/docs/sources/user_guide/frequent_patterns/fpmax.ipynb index f4ef6865b..b514499a4 100644 --- a/docs/sources/user_guide/frequent_patterns/fpmax.ipynb +++ b/docs/sources/user_guide/frequent_patterns/fpmax.ipynb @@ -78,11 +78,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", + "]" ] }, { @@ -619,7 +621,7 @@ } ], "source": [ - "fpmax(df, min_support=0.6, null_values = True, use_colnames=True)" + "fpmax(df, min_support=0.6, null_values=True, use_colnames=True)" ] }, { @@ -731,7 +733,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.frequent_patterns/fpmax.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.frequent_patterns/fpmax.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/frequent_patterns/hmine.ipynb b/docs/sources/user_guide/frequent_patterns/hmine.ipynb index aa4b907d6..6eccd1d98 100644 --- a/docs/sources/user_guide/frequent_patterns/hmine.ipynb +++ b/docs/sources/user_guide/frequent_patterns/hmine.ipynb @@ -92,11 +92,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", + "]" ] }, { @@ -537,8 +539,6 @@ } ], "source": [ - "from mlxtend.frequent_patterns import apriori\n", - "\n", "%timeit -n 100 -r 10 apriori(df, min_support=0.6, use_colnames=True)" ] }, @@ -573,8 +573,6 @@ } ], "source": [ - "from mlxtend.frequent_patterns import fpgrowth\n", - "\n", "%timeit -n 100 -r 10 fpgrowth(df, min_support=0.6, use_colnames=True)" ] }, @@ -991,7 +989,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.frequent_patterns/hmine.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.frequent_patterns/hmine.md\", \"r\") as f:\n", " print(f.read())" ] }, diff --git a/docs/sources/user_guide/math/num_combinations.ipynb b/docs/sources/user_guide/math/num_combinations.ipynb index a26e53888..1adc60005 100644 --- a/docs/sources/user_guide/math/num_combinations.ipynb +++ b/docs/sources/user_guide/math/num_combinations.ipynb @@ -118,8 +118,7 @@ "from mlxtend.math import num_combinations\n", "\n", "c = num_combinations(n=20, k=8, with_replacement=False)\n", - "print('Number of ways to combine 20 elements'\n", - " ' into 8 subelements: %d' % c)" + "print(\"Number of ways to combine 20 elements into 8 subelements: %d\" % c)" ] }, { @@ -139,8 +138,10 @@ "from mlxtend.math import num_combinations\n", "\n", "c = num_combinations(n=20, k=8, with_replacement=True)\n", - "print('Number of ways to combine 20 elements'\n", - " ' into 8 subelements (with replacement): %d' % c)" + "print(\n", + " \"Number of ways to combine 20 elements\"\n", + " \" into 8 subelements (with replacement): %d\" % c\n", + ")" ] }, { @@ -177,13 +178,12 @@ "from mlxtend.math import num_combinations\n", "\n", "items = {1, 2, 3, 4, 5, 6, 7, 8}\n", - "max_iter = num_combinations(n=len(items), k=3, \n", - " with_replacement=False)\n", + "max_iter = num_combinations(n=len(items), k=3, with_replacement=False)\n", "\n", "for idx, i in enumerate(itertools.combinations(items, r=3)):\n", " # do some computation with itemset i\n", " time.sleep(0.1)\n", - " sys.stdout.write('\\rProgress: %d/%d' % (idx + 1, max_iter))\n", + " sys.stdout.write(\"\\rProgress: %d/%d\" % (idx + 1, max_iter))\n", " sys.stdout.flush()" ] }, @@ -239,7 +239,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.math/num_combinations.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.math/num_combinations.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/math/num_permutations.ipynb b/docs/sources/user_guide/math/num_permutations.ipynb index 482a9be4e..9a91b95b5 100644 --- a/docs/sources/user_guide/math/num_permutations.ipynb +++ b/docs/sources/user_guide/math/num_permutations.ipynb @@ -104,8 +104,7 @@ "from mlxtend.math import num_permutations\n", "\n", "c = num_permutations(n=20, k=8, with_replacement=False)\n", - "print('Number of ways to permute 20 elements'\n", - " ' into 8 subelements: %d' % c)" + "print(\"Number of ways to permute 20 elements into 8 subelements: %d\" % c)" ] }, { @@ -125,8 +124,10 @@ "from mlxtend.math import num_permutations\n", "\n", "c = num_permutations(n=20, k=8, with_replacement=True)\n", - "print('Number of ways to combine 20 elements'\n", - " ' into 8 subelements (with replacement): %d' % c)" + "print(\n", + " \"Number of ways to combine 20 elements\"\n", + " \" into 8 subelements (with replacement): %d\" % c\n", + ")" ] }, { @@ -163,13 +164,12 @@ "from mlxtend.math import num_permutations\n", "\n", "items = {1, 2, 3, 4, 5, 6, 7, 8}\n", - "max_iter = num_permutations(n=len(items), k=3, \n", - " with_replacement=False)\n", + "max_iter = num_permutations(n=len(items), k=3, with_replacement=False)\n", "\n", "for idx, i in enumerate(itertools.permutations(items, r=3)):\n", " # do some computation with itemset i\n", " time.sleep(0.01)\n", - " sys.stdout.write('\\rProgress: %d/%d' % (idx + 1, max_iter))\n", + " sys.stdout.write(\"\\rProgress: %d/%d\" % (idx + 1, max_iter))\n", " sys.stdout.flush()" ] }, @@ -225,7 +225,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.math/num_permutations.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.math/num_permutations.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/math/vectorspace_dimensionality.ipynb b/docs/sources/user_guide/math/vectorspace_dimensionality.ipynb index 13a6f0a2d..75833dcbb 100644 --- a/docs/sources/user_guide/math/vectorspace_dimensionality.ipynb +++ b/docs/sources/user_guide/math/vectorspace_dimensionality.ipynb @@ -79,8 +79,7 @@ "from mlxtend.math import vectorspace_dimensionality\n", "\n", "\n", - "a = np.array([[1, 0],\n", - " [0, 1]])\n", + "a = np.array([[1, 0], [0, 1]])\n", "\n", "vectorspace_dimensionality(a)" ] @@ -109,8 +108,7 @@ } ], "source": [ - "b = np.array([[1, 2],\n", - " [0, 0]])\n", + "b = np.array([[1, 2], [0, 0]])\n", "\n", "vectorspace_dimensionality(a)" ] @@ -139,9 +137,7 @@ } ], "source": [ - "d = np.array([[1, 9, 1],\n", - " [3, 2, 2],\n", - " [5, 4, 3]])\n", + "d = np.array([[1, 9, 1], [3, 2, 2], [5, 4, 3]])\n", "\n", "vectorspace_dimensionality(d)" ] @@ -170,9 +166,7 @@ } ], "source": [ - "c = np.array([[1, 2, 1],\n", - " [3, 6, 2],\n", - " [5, 10, 3]])\n", + "c = np.array([[1, 2, 1], [3, 6, 2], [5, 10, 3]])\n", "\n", "vectorspace_dimensionality(c)" ] @@ -222,7 +216,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.math/vectorspace_dimensionality.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.math/vectorspace_dimensionality.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/math/vectorspace_orthonormalization.ipynb b/docs/sources/user_guide/math/vectorspace_orthonormalization.ipynb index dbbe0b74a..55df8d98c 100644 --- a/docs/sources/user_guide/math/vectorspace_orthonormalization.ipynb +++ b/docs/sources/user_guide/math/vectorspace_orthonormalization.ipynb @@ -87,17 +87,14 @@ "source": [ "import numpy as np\n", "from mlxtend.math import vectorspace_orthonormalization\n", - " \n", - "a = np.array([[2, 0, 4, 12],\n", - " [0, 2, 16, 4],\n", - " [4, 16, 6, 2],\n", - " [2, -12, 4, 6]])\n", "\n", + "a = np.array([[2, 0, 4, 12], [0, 2, 16, 4], [4, 16, 6, 2], [2, -12, 4, 6]])\n", "\n", - "s = ''\n", + "\n", + "s = \"\"\n", "if np.linalg.det(a) == 0.0:\n", - " s = ' not'\n", - "print('Input vectors are%s linearly independent' % s)\n", + " s = \" not\"\n", + "print(\"Input vectors are%s linearly independent\" % s)\n", "\n", "\n", "vectorspace_orthonormalization(a)" @@ -130,7 +127,7 @@ } ], "source": [ - "vectorspace_orthonormalization(a/2)" + "vectorspace_orthonormalization(a / 2)" ] }, { @@ -218,7 +215,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.math/vectorspace_orthonormalization.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.math/vectorspace_orthonormalization.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/category_scatter.ipynb b/docs/sources/user_guide/plotting/category_scatter.ipynb index def5df4fb..90b2f433f 100644 --- a/docs/sources/user_guide/plotting/category_scatter.ipynb +++ b/docs/sources/user_guide/plotting/category_scatter.ipynb @@ -167,11 +167,11 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", "from mlxtend.plotting import category_scatter\n", "\n", - "fig = category_scatter(x='x', y='y', label_col='label', \n", - " data=df, legend_loc='upper left')" + "fig = category_scatter(\n", + " x=\"x\", y=\"y\", label_col=\"label\", data=df, legend_loc=\"upper left\"\n", + ")" ] }, { @@ -213,7 +213,7 @@ "3,3.5,3.5\n", "3,3.8,5.1\"\"\"\n", "\n", - "ary = np.genfromtxt(BytesIO(csvfile.encode()), delimiter=',')\n", + "ary = np.genfromtxt(BytesIO(csvfile.encode()), delimiter=\",\")\n", "ary" ] }, @@ -241,11 +241,9 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", "from mlxtend.plotting import category_scatter\n", "\n", - "fix = category_scatter(x=1, y=2, label_col=0, \n", - " data=ary, legend_loc='upper left')" + "fix = category_scatter(x=1, y=2, label_col=0, data=ary, legend_loc=\"upper left\")" ] }, { @@ -322,7 +320,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/category_scatter.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/category_scatter.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/checkerboard_plot.ipynb b/docs/sources/user_guide/plotting/checkerboard_plot.ipynb index cb7d4ec36..e8cd8773f 100644 --- a/docs/sources/user_guide/plotting/checkerboard_plot.ipynb +++ b/docs/sources/user_guide/plotting/checkerboard_plot.ipynb @@ -120,12 +120,14 @@ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", - "checkerboard_plot(ary, \n", - " col_labels=['abc', 'def', 'ghi', 'jkl'],\n", - " row_labels=['sample %d' % i for i in range(1, 6)],\n", - " cell_colors=['skyblue', 'whitesmoke'],\n", - " font_colors=['black', 'black'],\n", - " figsize=(4.5, 5))\n", + "checkerboard_plot(\n", + " ary,\n", + " col_labels=[\"abc\", \"def\", \"ghi\", \"jkl\"],\n", + " row_labels=[\"sample %d\" % i for i in range(1, 6)],\n", + " cell_colors=[\"skyblue\", \"whitesmoke\"],\n", + " font_colors=[\"black\", \"black\"],\n", + " figsize=(4.5, 5),\n", + ")\n", "plt.show()" ] }, @@ -206,8 +208,8 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/checkerboard_plot.md', 'r') as f:\n", - " s = f.read() \n", + "with open(\"../../api_modules/mlxtend.plotting/checkerboard_plot.md\", \"r\") as f:\n", + " s = f.read()\n", "print(s)" ] } diff --git a/docs/sources/user_guide/plotting/ecdf.ipynb b/docs/sources/user_guide/plotting/ecdf.ipynb index b59a1d1c4..f2e6b6827 100644 --- a/docs/sources/user_guide/plotting/ecdf.ipynb +++ b/docs/sources/user_guide/plotting/ecdf.ipynb @@ -88,7 +88,7 @@ "\n", "X, y = iris_data()\n", "\n", - "ax, _, _ = ecdf(x=X[:, 0], x_label='sepal length (cm)')\n", + "ax, _, _ = ecdf(x=X[:, 0], x_label=\"sepal length (cm)\")\n", "plt.show()" ] }, @@ -124,13 +124,13 @@ "\n", "# first ecdf\n", "x1 = X[:, 0]\n", - "ax, _, _ = ecdf(x1, x_label='cm')\n", + "ax, _, _ = ecdf(x1, x_label=\"cm\")\n", "\n", "# second ecdf\n", "x2 = X[:, 1]\n", "ax, _, _ = ecdf(x2, ax=ax)\n", "\n", - "plt.legend(['sepal length', 'sepal width'])\n", + "plt.legend([\"sepal length\", \"sepal width\"])\n", "plt.show()" ] }, @@ -172,14 +172,12 @@ "\n", "X, y = iris_data()\n", "\n", - "ax, threshold, count = ecdf(x=X[:, 0], \n", - " x_label='sepal length (cm)',\n", - " percentile=0.8)\n", + "ax, threshold, count = ecdf(x=X[:, 0], x_label=\"sepal length (cm)\", percentile=0.8)\n", "\n", "plt.show()\n", "\n", - "print('Feature threshold at the 80th percentile:', threshold)\n", - "print('Number of samples below the threshold:', count)" + "print(\"Feature threshold at the 80th percentile:\", threshold)\n", + "print(\"Number of samples below the threshold:\", count)" ] }, { @@ -270,7 +268,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/ecdf.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/ecdf.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/enrichment_plot.ipynb b/docs/sources/user_guide/plotting/enrichment_plot.ipynb index d1ab0e653..8bc9137ab 100644 --- a/docs/sources/user_guide/plotting/enrichment_plot.ipynb +++ b/docs/sources/user_guide/plotting/enrichment_plot.ipynb @@ -124,7 +124,7 @@ "s3 = [3.1, 2.1]\n", "s4 = [3.9, 2.5]\n", "data = [s1, s2, s3, s4]\n", - "df = pd.DataFrame(data, columns=['X1', 'X2'])\n", + "df = pd.DataFrame(data, columns=[\"X1\", \"X2\"])\n", "df" ] }, @@ -152,10 +152,9 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", "from mlxtend.plotting import enrichment_plot\n", "\n", - "ax = enrichment_plot(df, legend_loc='upper left')" + "ax = enrichment_plot(df, legend_loc=\"upper left\")" ] }, { @@ -256,7 +255,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/enrichment_plot.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/enrichment_plot.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/heatmap.ipynb b/docs/sources/user_guide/plotting/heatmap.ipynb index 2b39c222e..44e77b982 100644 --- a/docs/sources/user_guide/plotting/heatmap.ipynb +++ b/docs/sources/user_guide/plotting/heatmap.ipynb @@ -280,17 +280,31 @@ "from mlxtend.plotting import heatmap\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import pandas as pd\n", "\n", - "df = pd.read_csv('https://raw.githubusercontent.com/rasbt/'\n", - " 'python-machine-learning-book-2nd-edition'\n", - " '/master/code/ch10/housing.data.txt',\n", - " header=None,\n", - " sep='\\s+')\n", + "df = pd.read_csv(\n", + " \"https://raw.githubusercontent.com/rasbt/\"\n", + " \"python-machine-learning-book-2nd-edition\"\n", + " \"/master/code/ch10/housing.data.txt\",\n", + " header=None,\n", + " sep=\"\\s+\",\n", + ")\n", "\n", - "df.columns = ['CRIM', 'ZN', 'INDUS', 'CHAS', \n", - " 'NOX', 'RM', 'AGE', 'DIS', 'RAD', \n", - " 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']\n", + "df.columns = [\n", + " \"CRIM\",\n", + " \"ZN\",\n", + " \"INDUS\",\n", + " \"CHAS\",\n", + " \"NOX\",\n", + " \"RM\",\n", + " \"AGE\",\n", + " \"DIS\",\n", + " \"RAD\",\n", + " \"TAX\",\n", + " \"PTRATIO\",\n", + " \"B\",\n", + " \"LSTAT\",\n", + " \"MEDV\",\n", + "]\n", "df.head()" ] }, @@ -313,7 +327,7 @@ "source": [ "from matplotlib import cm\n", "\n", - "cols = ['LSTAT', 'INDUS', 'NOX', 'RM', 'MEDV']\n", + "cols = [\"LSTAT\", \"INDUS\", \"NOX\", \"RM\", \"MEDV\"]\n", "\n", "corrmat = np.corrcoef(df[cols].values.T)\n", "fig, ax = heatmap(corrmat, column_names=cols, row_names=cols, cmap=cm.PiYG)\n", @@ -321,7 +335,7 @@ "# set colorbar cutoff at -1, 1\n", "for im in ax.get_images():\n", " im.set_clim(-1, 1)\n", - " \n", + "\n", "plt.show()" ] }, @@ -431,8 +445,8 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/heatmap.md', 'r') as f:\n", - " s = f.read() \n", + "with open(\"../../api_modules/mlxtend.plotting/heatmap.md\", \"r\") as f:\n", + " s = f.read()\n", "print(s)" ] } diff --git a/docs/sources/user_guide/plotting/plot_confusion_matrix.ipynb b/docs/sources/user_guide/plotting/plot_confusion_matrix.ipynb index b586ff7ca..8526f00db 100644 --- a/docs/sources/user_guide/plotting/plot_confusion_matrix.ipynb +++ b/docs/sources/user_guide/plotting/plot_confusion_matrix.ipynb @@ -90,8 +90,7 @@ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", - "binary1 = np.array([[4, 1],\n", - " [1, 2]])\n", + "binary1 = np.array([[4, 1], [1, 2]])\n", "\n", "fig, ax = plot_confusion_matrix(conf_mat=binary1)\n", "plt.show()" @@ -116,8 +115,7 @@ } ], "source": [ - "binary2 = np.array([[21, 1],\n", - " [3, 1]])\n", + "binary2 = np.array([[21, 1], [3, 1]])\n", "\n", "fig, ax = plot_confusion_matrix(conf_mat=binary2, figsize=(2, 2))\n", "plt.show()" @@ -149,13 +147,11 @@ } ], "source": [ - "binary = np.array([[4, 1],\n", - " [1, 2]])\n", + "binary = np.array([[4, 1], [1, 2]])\n", "\n", - "fig, ax = plot_confusion_matrix(conf_mat=binary,\n", - " show_absolute=True,\n", - " show_normed=True,\n", - " colorbar=True)\n", + "fig, ax = plot_confusion_matrix(\n", + " conf_mat=binary, show_absolute=True, show_normed=True, colorbar=True\n", + ")\n", "plt.show()" ] }, @@ -185,15 +181,11 @@ } ], "source": [ - "multiclass = np.array([[2, 1, 0, 0],\n", - " [1, 2, 0, 0],\n", - " [0, 0, 1, 0],\n", - " [0, 0, 0, 1]])\n", + "multiclass = np.array([[2, 1, 0, 0], [1, 2, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])\n", "\n", - "fig, ax = plot_confusion_matrix(conf_mat=multiclass,\n", - " colorbar=True,\n", - " show_absolute=False,\n", - " show_normed=True)\n", + "fig, ax = plot_confusion_matrix(\n", + " conf_mat=multiclass, colorbar=True, show_absolute=False, show_normed=True\n", + ")\n", "plt.show()" ] }, @@ -223,18 +215,17 @@ } ], "source": [ - "multiclass = np.array([[2, 1, 0, 0],\n", - " [1, 2, 0, 0],\n", - " [0, 0, 1, 0],\n", - " [0, 0, 0, 1]])\n", + "multiclass = np.array([[2, 1, 0, 0], [1, 2, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])\n", "\n", - "class_names = ['class a', 'class b', 'class c', 'class d']\n", + "class_names = [\"class a\", \"class b\", \"class c\", \"class d\"]\n", "\n", - "fig, ax = plot_confusion_matrix(conf_mat=multiclass,\n", - " colorbar=True,\n", - " show_absolute=False,\n", - " show_normed=True,\n", - " class_names=class_names)\n", + "fig, ax = plot_confusion_matrix(\n", + " conf_mat=multiclass,\n", + " colorbar=True,\n", + " show_absolute=False,\n", + " show_normed=True,\n", + " class_names=class_names,\n", + ")\n", "plt.show()" ] }, @@ -271,14 +262,9 @@ } ], "source": [ - "multiclass = np.array([[2, 1, 0, 0],\n", - " [1, 2, 0, 0],\n", - " [0, 0, 1, 0],\n", - " [0, 0, 0, 1]])\n", + "multiclass = np.array([[2, 1, 0, 0], [1, 2, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])\n", "\n", - "fig, ax = plot_confusion_matrix(conf_mat=multiclass,\n", - " colorbar=True,\n", - " cmap='summer')\n", + "fig, ax = plot_confusion_matrix(conf_mat=multiclass, colorbar=True, cmap=\"summer\")\n", "\n", "plt.show()" ] @@ -311,10 +297,9 @@ } ], "source": [ - "fig, ax = plot_confusion_matrix(conf_mat=multiclass,\n", - " colorbar=True,\n", - " fontcolor_threshold=1,\n", - " cmap='summer')\n", + "fig, ax = plot_confusion_matrix(\n", + " conf_mat=multiclass, colorbar=True, fontcolor_threshold=1, cmap=\"summer\"\n", + ")\n", "\n", "plt.show()" ] @@ -352,21 +337,27 @@ } ], "source": [ - "class_dict = {0: 'airplane',\n", - " 1: 'automobile',\n", - " 2: 'bird',\n", - " 3: 'cat',\n", - " 4: 'deer',\n", - " 5: 'dog',\n", - " 6: 'frog'}\n", + "class_dict = {\n", + " 0: \"airplane\",\n", + " 1: \"automobile\",\n", + " 2: \"bird\",\n", + " 3: \"cat\",\n", + " 4: \"deer\",\n", + " 5: \"dog\",\n", + " 6: \"frog\",\n", + "}\n", "\n", - "cmat = np.array([[972, 0, 1, 1, 1, 1, 3],\n", - " [0, 1123, 3, 1, 0, 1, 2],\n", - " [2, 0, 1025, 0, 0, 0, 1],\n", - " [0, 0, 0, 1005, 0, 2, 0],\n", - " [0, 1, 1, 0, 967, 0, 4],\n", - " [0, 0, 0, 6, 0, 881, 3],\n", - " [2, 3, 0, 1, 3, 4, 941]])\n", + "cmat = np.array(\n", + " [\n", + " [972, 0, 1, 1, 1, 1, 3],\n", + " [0, 1123, 3, 1, 0, 1, 2],\n", + " [2, 0, 1025, 0, 0, 0, 1],\n", + " [0, 0, 0, 1005, 0, 2, 0],\n", + " [0, 1, 1, 0, 967, 0, 4],\n", + " [0, 0, 0, 6, 0, 881, 3],\n", + " [2, 3, 0, 1, 3, 4, 941],\n", + " ]\n", + ")\n", "\n", "fig, ax = plot_confusion_matrix(\n", " conf_mat=cmat,\n", @@ -405,7 +396,7 @@ "fig, ax = plot_confusion_matrix(\n", " conf_mat=cmat,\n", " class_names=class_dict.values(),\n", - " norm_colormap=matplotlib.colors.LogNorm() \n", + " norm_colormap=matplotlib.colors.LogNorm(),\n", ")" ] }, @@ -520,8 +511,8 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/plot_confusion_matrix.md', 'r') as f:\n", - " s = f.read() \n", + "with open(\"../../api_modules/mlxtend.plotting/plot_confusion_matrix.md\", \"r\") as f:\n", + " s = f.read()\n", "print(s)" ] } diff --git a/docs/sources/user_guide/plotting/plot_decision_regions.ipynb b/docs/sources/user_guide/plotting/plot_decision_regions.ipynb index 5df2ad203..d16aaac74 100644 --- a/docs/sources/user_guide/plotting/plot_decision_regions.ipynb +++ b/docs/sources/user_guide/plotting/plot_decision_regions.ipynb @@ -76,7 +76,7 @@ "y = iris.target\n", "\n", "# Training a classifier\n", - "svm = SVC(C=0.5, kernel='linear')\n", + "svm = SVC(C=0.5, kernel=\"linear\")\n", "svm.fit(X, y)\n", "\n", "\n", @@ -84,9 +84,9 @@ "plot_decision_regions(X, y, clf=svm, legend=2)\n", "\n", "# Adding axes annotations\n", - "plt.xlabel('sepal length [cm]')\n", - "plt.ylabel('petal length [cm]')\n", - "plt.title('SVM on Iris')\n", + "plt.xlabel(\"sepal length [cm]\")\n", + "plt.ylabel(\"petal length [cm]\")\n", + "plt.title(\"SVM on Iris\")\n", "plt.show()" ] }, @@ -126,15 +126,15 @@ "y = iris.target\n", "\n", "# Training a classifier\n", - "svm = SVC(C=0.5, kernel='linear')\n", + "svm = SVC(C=0.5, kernel=\"linear\")\n", "svm.fit(X, y)\n", "\n", "# Plotting decision regions\n", "plot_decision_regions(X, y, clf=svm, legend=2)\n", "\n", "# Adding axes annotations\n", - "plt.xlabel('sepal length [cm]')\n", - "plt.title('SVM on Iris')\n", + "plt.xlabel(\"sepal length [cm]\")\n", + "plt.title(\"SVM on Iris\")\n", "\n", "plt.show()" ] @@ -153,21 +153,21 @@ "outputs": [], "source": [ "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 sklearn.svm import SVC\n", "from sklearn import datasets\n", "import numpy as np\n", "\n", "# Initializing Classifiers\n", - "clf1 = LogisticRegression(random_state=1, solver='newton-cg')\n", + "clf1 = LogisticRegression(random_state=1, solver=\"newton-cg\")\n", "clf2 = RandomForestClassifier(random_state=1, n_estimators=100)\n", "clf3 = GaussianNB()\n", - "clf4 = SVC(gamma='auto')\n", + "clf4 = SVC(gamma=\"auto\")\n", "\n", "# Loading some example data\n", "iris = datasets.load_iris()\n", - "X = iris.data[:, [0,2]]\n", + "X = iris.data[:, [0, 2]]\n", "y = iris.target" ] }, @@ -192,20 +192,20 @@ "from mlxtend.plotting import plot_decision_regions\n", "import matplotlib.gridspec as gridspec\n", "import itertools\n", - "gs = gridspec.GridSpec(2, 2)\n", "\n", - "fig = plt.figure(figsize=(10,8))\n", + "gs = gridspec.GridSpec(2, 2)\n", "\n", - "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM']\n", - "for clf, lab, grd in zip([clf1, clf2, clf3, clf4],\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\", \"SVM\"]\n", + "for clf, lab, grd in zip(\n", + " [clf1, clf2, clf3, clf4], 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, legend=2)\n", " plt.title(lab)\n", - " \n", + "\n", "plt.show()" ] }, @@ -242,24 +242,23 @@ "\n", "# Loading some example data\n", "iris = datasets.load_iris()\n", - "X, y = iris.data[:, [0,2]], iris.target\n", + "X, y = iris.data[:, [0, 2]], iris.target\n", "X, y = shuffle_arrays_unison(arrays=[X, y], random_seed=3)\n", "\n", "X_train, y_train = X[:100], y[:100]\n", "X_test, y_test = X[100:], y[100:]\n", "\n", "# Training a classifier\n", - "svm = SVC(C=0.5, kernel='linear')\n", + "svm = SVC(C=0.5, kernel=\"linear\")\n", "svm.fit(X_train, y_train)\n", "\n", "# Plotting decision regions\n", - "plot_decision_regions(X, y, clf=svm, legend=2, \n", - " X_highlight=X_test)\n", + "plot_decision_regions(X, y, clf=svm, legend=2, X_highlight=X_test)\n", "\n", "# Adding axes annotations\n", - "plt.xlabel('sepal length [cm]')\n", - "plt.ylabel('petal length [cm]')\n", - "plt.title('SVM on Iris')\n", + "plt.xlabel(\"sepal length [cm]\")\n", + "plt.ylabel(\"petal length [cm]\")\n", + "plt.title(\"SVM on Iris\")\n", "plt.show()" ] }, @@ -277,16 +276,15 @@ "outputs": [], "source": [ "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 sklearn.svm import SVC\n", "\n", "# Initializing Classifiers\n", - "clf1 = LogisticRegression(random_state=1, solver='lbfgs')\n", - "clf2 = RandomForestClassifier(n_estimators=100, \n", - " random_state=1)\n", + "clf1 = LogisticRegression(random_state=1, solver=\"lbfgs\")\n", + "clf2 = RandomForestClassifier(n_estimators=100, random_state=1)\n", "clf3 = GaussianNB()\n", - "clf4 = SVC(gamma='auto')" + "clf4 = SVC(gamma=\"auto\")" ] }, { @@ -299,8 +297,7 @@ "import matplotlib.pyplot as plt\n", "import matplotlib.gridspec as gridspec\n", "import itertools\n", - "from mlxtend.plotting import plot_decision_regions\n", - "import numpy as np" + "from mlxtend.plotting import plot_decision_regions" ] }, { @@ -316,12 +313,10 @@ "metadata": {}, "outputs": [], "source": [ - "xx, yy = np.meshgrid(np.linspace(-3, 3, 50),\n", - " np.linspace(-3, 3, 50))\n", + "xx, yy = np.meshgrid(np.linspace(-3, 3, 50), np.linspace(-3, 3, 50))\n", "rng = np.random.RandomState(0)\n", "X = rng.randn(300, 2)\n", - "y = np.array(np.logical_xor(X[:, 0] > 0, X[:, 1] > 0), \n", - " dtype=int)" + "y = np.array(np.logical_xor(X[:, 0] > 0, X[:, 1] > 0), dtype=int)" ] }, { @@ -343,13 +338,12 @@ "source": [ "gs = gridspec.GridSpec(2, 2)\n", "\n", - "fig = plt.figure(figsize=(10,8))\n", - "\n", - "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM']\n", - "for clf, lab, grd in zip([clf1, clf2, clf3, clf4],\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\", \"SVM\"]\n", + "for clf, lab, grd in zip(\n", + " [clf1, clf2, clf3, clf4], 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, legend=2)\n", @@ -383,17 +377,17 @@ ], "source": [ "from sklearn.datasets import make_moons\n", + "\n", "X, y = make_moons(n_samples=100, random_state=123)\n", "\n", "gs = gridspec.GridSpec(2, 2)\n", "\n", - "fig = plt.figure(figsize=(10,8))\n", - "\n", - "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM']\n", - "for clf, lab, grd in zip([clf1, clf2, clf3, clf4],\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\", \"SVM\"]\n", + "for clf, lab, grd in zip(\n", + " [clf1, clf2, clf3, clf4], 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, legend=2)\n", @@ -427,17 +421,17 @@ ], "source": [ "from sklearn.datasets import make_circles\n", + "\n", "X, y = make_circles(n_samples=1000, random_state=123, noise=0.1, factor=0.2)\n", "\n", "gs = gridspec.GridSpec(2, 2)\n", "\n", - "fig = plt.figure(figsize=(10,8))\n", - "\n", - "labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM']\n", - "for clf, lab, grd in zip([clf1, clf2, clf3, clf4],\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\", \"SVM\"]\n", + "for clf, lab, grd in zip(\n", + " [clf1, clf2, clf3, clf4], 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, legend=2)\n", @@ -474,7 +468,7 @@ "from mlxtend.plotting import plot_decision_regions\n", "\n", "from sklearn.linear_model import LogisticRegression\n", - "from sklearn.naive_bayes import GaussianNB \n", + "from sklearn.naive_bayes import GaussianNB\n", "from sklearn import datasets\n", "import numpy as np\n", "\n", @@ -485,8 +479,7 @@ "y = iris.target\n", "\n", "# Initializing and fitting classifiers\n", - "clf1 = LogisticRegression(random_state=1,\n", - " solver='lbfgs')\n", + "clf1 = LogisticRegression(random_state=1, solver=\"lbfgs\")\n", "clf2 = GaussianNB()\n", "clf1.fit(X, y)\n", "clf2.fit(X, y)\n", @@ -495,7 +488,7 @@ "\n", "fig = plot_decision_regions(X=X, y=y, clf=clf1, ax=axes[0], legend=2)\n", "fig = plot_decision_regions(X=X, y=y, clf=clf2, ax=axes[1], legend=1)\n", - " \n", + "\n", "plt.show()" ] }, @@ -529,12 +522,16 @@ "from sklearn.svm import SVC\n", "\n", "# Loading some example data\n", - "X, y = datasets.make_blobs(n_samples=600, n_features=3,\n", - " centers=[[2, 2, -2],[-2, -2, 2]],\n", - " cluster_std=[2, 2], random_state=2)\n", + "X, y = datasets.make_blobs(\n", + " n_samples=600,\n", + " n_features=3,\n", + " centers=[[2, 2, -2], [-2, -2, 2]],\n", + " cluster_std=[2, 2],\n", + " random_state=2,\n", + ")\n", "\n", "# Training a classifier\n", - "svm = SVC(gamma='auto')\n", + "svm = SVC(gamma=\"auto\")\n", "svm.fit(X, y)\n", "\n", "# Plotting decision regions\n", @@ -543,16 +540,21 @@ "value = 1.5\n", "# Plot training sample with feature 3 = 1.5 +/- 0.75\n", "width = 0.75\n", - "plot_decision_regions(X, y, clf=svm,\n", - " filler_feature_values={2: value},\n", - " filler_feature_ranges={2: width},\n", - " legend=2, ax=ax)\n", - "ax.set_xlabel('Feature 1')\n", - "ax.set_ylabel('Feature 2')\n", - "ax.set_title('Feature 3 = {}'.format(value))\n", + "plot_decision_regions(\n", + " X,\n", + " y,\n", + " clf=svm,\n", + " filler_feature_values={2: value},\n", + " filler_feature_ranges={2: width},\n", + " legend=2,\n", + " ax=ax,\n", + ")\n", + "ax.set_xlabel(\"Feature 1\")\n", + "ax.set_ylabel(\"Feature 2\")\n", + "ax.set_title(\"Feature 3 = {}\".format(value))\n", "\n", "# Adding axes annotations\n", - "fig.suptitle('SVM on make_blobs')\n", + "fig.suptitle(\"SVM on make_blobs\")\n", "plt.show()" ] }, @@ -586,28 +588,38 @@ "from sklearn.svm import SVC\n", "\n", "# Loading some example data\n", - "X, y = datasets.make_blobs(n_samples=500, n_features=3, centers=[[2, 2, -2],[-2, -2, 2]],\n", - " cluster_std=[2, 2], random_state=2)\n", + "X, y = datasets.make_blobs(\n", + " n_samples=500,\n", + " n_features=3,\n", + " centers=[[2, 2, -2], [-2, -2, 2]],\n", + " cluster_std=[2, 2],\n", + " random_state=2,\n", + ")\n", "\n", "# Training a classifier\n", - "svm = SVC(gamma='auto')\n", + "svm = SVC(gamma=\"auto\")\n", "svm.fit(X, y)\n", "\n", "# Plotting decision regions\n", - "fig, axarr = plt.subplots(2, 2, figsize=(10,8), sharex=True, sharey=True)\n", + "fig, axarr = plt.subplots(2, 2, figsize=(10, 8), sharex=True, sharey=True)\n", "values = [-4.0, -1.0, 1.0, 4.0]\n", "width = 0.75\n", "for value, ax in zip(values, axarr.flat):\n", - " plot_decision_regions(X, y, clf=svm,\n", - " filler_feature_values={2: value},\n", - " filler_feature_ranges={2: width},\n", - " legend=2, ax=ax)\n", - " ax.set_xlabel('Feature 1')\n", - " ax.set_ylabel('Feature 2')\n", - " ax.set_title('Feature 3 = {}'.format(value))\n", + " plot_decision_regions(\n", + " X,\n", + " y,\n", + " clf=svm,\n", + " filler_feature_values={2: value},\n", + " filler_feature_ranges={2: width},\n", + " legend=2,\n", + " ax=ax,\n", + " )\n", + " ax.set_xlabel(\"Feature 1\")\n", + " ax.set_ylabel(\"Feature 2\")\n", + " ax.set_title(\"Feature 3 = {}\".format(value))\n", "\n", "# Adding axes annotations\n", - "fig.suptitle('SVM on make_blobs')\n", + "fig.suptitle(\"SVM on make_blobs\")\n", "plt.show()" ] }, @@ -652,24 +664,29 @@ "X_test, y_test = X[100:], y[100:]\n", "\n", "# Training a classifier\n", - "svm = SVC(C=0.5, kernel='linear')\n", + "svm = SVC(C=0.5, kernel=\"linear\")\n", "svm.fit(X_train, y_train)\n", "\n", "# Specify keyword arguments to be passed to underlying plotting functions\n", - "scatter_kwargs = {'s': 120, 'edgecolor': None, 'alpha': 0.7}\n", - "contourf_kwargs = {'alpha': 0.2}\n", - "scatter_highlight_kwargs = {'s': 120, 'label': 'Test data', 'alpha': 0.7}\n", + "scatter_kwargs = {\"s\": 120, \"edgecolor\": None, \"alpha\": 0.7}\n", + "contourf_kwargs = {\"alpha\": 0.2}\n", + "scatter_highlight_kwargs = {\"s\": 120, \"label\": \"Test data\", \"alpha\": 0.7}\n", "# Plotting decision regions\n", - "plot_decision_regions(X, y, clf=svm, legend=2,\n", - " X_highlight=X_test,\n", - " scatter_kwargs=scatter_kwargs,\n", - " contourf_kwargs=contourf_kwargs,\n", - " scatter_highlight_kwargs=scatter_highlight_kwargs)\n", + "plot_decision_regions(\n", + " X,\n", + " y,\n", + " clf=svm,\n", + " legend=2,\n", + " X_highlight=X_test,\n", + " scatter_kwargs=scatter_kwargs,\n", + " contourf_kwargs=contourf_kwargs,\n", + " scatter_highlight_kwargs=scatter_highlight_kwargs,\n", + ")\n", "\n", "# Adding axes annotations\n", - "plt.xlabel('sepal length [cm]')\n", - "plt.ylabel('petal length [cm]')\n", - "plt.title('SVM on Iris')\n", + "plt.xlabel(\"sepal length [cm]\")\n", + "plt.ylabel(\"petal length [cm]\")\n", + "plt.title(\"SVM on Iris\")\n", "plt.show()" ] }, @@ -728,7 +745,7 @@ "y = iris.target\n", "\n", "# Training a classifier\n", - "svm = SVC(C=0.5, kernel='linear')\n", + "svm = SVC(C=0.5, kernel=\"linear\")\n", "svm.fit(X, y)\n", "\n", "\n", @@ -736,14 +753,17 @@ "ax = plot_decision_regions(X, y, clf=svm, legend=0)\n", "\n", "# Adding axes annotations\n", - "plt.xlabel('sepal length [cm]')\n", - "plt.ylabel('petal length [cm]')\n", - "plt.title('SVM on Iris')\n", + "plt.xlabel(\"sepal length [cm]\")\n", + "plt.ylabel(\"petal length [cm]\")\n", + "plt.title(\"SVM on Iris\")\n", "\n", "handles, labels = ax.get_legend_handles_labels()\n", - "ax.legend(handles, \n", - " ['class square', 'class triangle', 'class circle'], \n", - " framealpha=0.3, scatterpoints=1)\n", + "ax.legend(\n", + " handles,\n", + " [\"class square\", \"class triangle\", \"class circle\"],\n", + " framealpha=0.3,\n", + " scatterpoints=1,\n", + ")\n", "\n", "plt.show()" ] @@ -1701,7 +1721,7 @@ "y = iris.target\n", "\n", "# Training a classifier\n", - "svm = SVC(C=0.5, kernel='linear')\n", + "svm = SVC(C=0.5, kernel=\"linear\")\n", "svm.fit(X, y)" ] }, @@ -1729,7 +1749,7 @@ } ], "source": [ - "plot_decision_regions(X, y, clf=svm, zoom_factor=1.)\n", + "plot_decision_regions(X, y, clf=svm, zoom_factor=1.0)\n", "plt.show()" ] }, @@ -1854,7 +1874,6 @@ "outputs": [], "source": [ "class Onehot2Int(object):\n", - "\n", " def __init__(self, model):\n", " self.model = model\n", "\n", @@ -1924,17 +1943,21 @@ "# Create the model\n", "np.random.seed(123)\n", "model = Sequential()\n", - "model.add(Dense(8, input_shape=(2,), activation='relu', kernel_initializer='he_uniform'))\n", - "model.add(Dense(4, activation='relu', kernel_initializer='he_uniform'))\n", - "model.add(Dense(3, activation='softmax'))\n", + "model.add(\n", + " Dense(8, input_shape=(2,), activation=\"relu\", kernel_initializer=\"he_uniform\")\n", + ")\n", + "model.add(Dense(4, activation=\"relu\", kernel_initializer=\"he_uniform\"))\n", + "model.add(Dense(3, activation=\"softmax\"))\n", "\n", "# Configure the model and start training\n", "model.compile(\n", " loss=\"categorical_crossentropy\",\n", " optimizer=keras.optimizers.Adam(learning_rate=0.005),\n", - " metrics=['accuracy']\n", + " metrics=[\"accuracy\"],\n", ")\n", - "history = model.fit(X, y_onehot, epochs=10, batch_size=5, verbose=1, validation_split=0.1)" + "history = model.fit(\n", + " X, y_onehot, epochs=10, batch_size=5, verbose=1, validation_split=0.1\n", + ")" ] }, { @@ -2115,7 +2138,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/plot_decision_regions.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/plot_decision_regions.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/plot_learning_curves.ipynb b/docs/sources/user_guide/plotting/plot_learning_curves.ipynb index a3fa66bed..cd060b194 100644 --- a/docs/sources/user_guide/plotting/plot_learning_curves.ipynb +++ b/docs/sources/user_guide/plotting/plot_learning_curves.ipynb @@ -89,7 +89,6 @@ "from mlxtend.data import mnist_data\n", "from mlxtend.preprocessing import shuffle_arrays_unison\n", "from sklearn.neighbors import KNeighborsClassifier\n", - "import numpy as np\n", "\n", "\n", "# Loading some example data\n", @@ -210,7 +209,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/plot_learning_curves.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/plot_learning_curves.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/plot_linear_regression.ipynb b/docs/sources/user_guide/plotting/plot_linear_regression.ipynb index 3e4caf662..0fc72def0 100644 --- a/docs/sources/user_guide/plotting/plot_linear_regression.ipynb +++ b/docs/sources/user_guide/plotting/plot_linear_regression.ipynb @@ -86,11 +86,73 @@ "from mlxtend.plotting import plot_linear_regression\n", "import numpy as np\n", "\n", - "X = np.array([4, 8, 13, 26, 31, 10, 8, 30, 18, 12, 20, 5, 28, 18, 6, 31, 12,\n", - " 12, 27, 11, 6, 14, 25, 7, 13,4, 15, 21, 15])\n", + "X = np.array(\n", + " [\n", + " 4,\n", + " 8,\n", + " 13,\n", + " 26,\n", + " 31,\n", + " 10,\n", + " 8,\n", + " 30,\n", + " 18,\n", + " 12,\n", + " 20,\n", + " 5,\n", + " 28,\n", + " 18,\n", + " 6,\n", + " 31,\n", + " 12,\n", + " 12,\n", + " 27,\n", + " 11,\n", + " 6,\n", + " 14,\n", + " 25,\n", + " 7,\n", + " 13,\n", + " 4,\n", + " 15,\n", + " 21,\n", + " 15,\n", + " ]\n", + ")\n", "\n", - "y = np.array([14, 24, 22, 59, 66, 25, 18, 60, 39, 32, 53, 18, 55, 41, 28, 61, 35,\n", - " 36, 52, 23, 19, 25, 73, 16, 32, 14, 31, 43, 34])\n", + "y = np.array(\n", + " [\n", + " 14,\n", + " 24,\n", + " 22,\n", + " 59,\n", + " 66,\n", + " 25,\n", + " 18,\n", + " 60,\n", + " 39,\n", + " 32,\n", + " 53,\n", + " 18,\n", + " 55,\n", + " 41,\n", + " 28,\n", + " 61,\n", + " 35,\n", + " 36,\n", + " 52,\n", + " 23,\n", + " 19,\n", + " 25,\n", + " 73,\n", + " 16,\n", + " 32,\n", + " 14,\n", + " 31,\n", + " 43,\n", + " 34,\n", + " ]\n", + ")\n", "\n", "intercept, slope, corr_coeff = plot_linear_regression(X, y)\n", "plt.show()" @@ -162,7 +224,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/plot_linear_regression.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/plot_linear_regression.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/plot_sequential_feature_selection.ipynb b/docs/sources/user_guide/plotting/plot_sequential_feature_selection.ipynb index e7ed8e162..62a0d0527 100644 --- a/docs/sources/user_guide/plotting/plot_sequential_feature_selection.ipynb +++ b/docs/sources/user_guide/plotting/plot_sequential_feature_selection.ipynb @@ -86,21 +86,14 @@ "y = iris.target\n", "knn = KNeighborsClassifier(n_neighbors=4)\n", "\n", - "sfs = SFS(knn, \n", - " k_features=4, \n", - " forward=True, \n", - " floating=False,\n", - " scoring='accuracy',\n", - " cv=5)\n", + "sfs = SFS(knn, k_features=4, forward=True, floating=False, scoring=\"accuracy\", cv=5)\n", "\n", "sfs = sfs.fit(X, y)\n", "\n", - "fig1 = plot_sfs(sfs.get_metric_dict(),\n", - " kind='std_dev',\n", - " figsize=(6, 4))\n", + "fig1 = plot_sfs(sfs.get_metric_dict(), kind=\"std_dev\", figsize=(6, 4))\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()" ] @@ -182,7 +175,9 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/plot_sequential_feature_selection.md', 'r') as f:\n", + "with open(\n", + " \"../../api_modules/mlxtend.plotting/plot_sequential_feature_selection.md\", \"r\"\n", + ") as f:\n", " s = f.read()\n", "print(s)" ] diff --git a/docs/sources/user_guide/plotting/scatter_hist.ipynb b/docs/sources/user_guide/plotting/scatter_hist.ipynb index 01f182e36..8bbd05451 100644 --- a/docs/sources/user_guide/plotting/scatter_hist.ipynb +++ b/docs/sources/user_guide/plotting/scatter_hist.ipynb @@ -147,7 +147,12 @@ "\n", "X, y = iris_data()\n", "df = pd.DataFrame(X)\n", - "df.columns = ['sepal length [cm]', 'sepal width [cm]', 'petal length [cm]', 'petal width [cm]']\n", + "df.columns = [\n", + " \"sepal length [cm]\",\n", + " \"sepal width [cm]\",\n", + " \"petal length [cm]\",\n", + " \"petal width [cm]\",\n", + "]\n", "df.head(5)" ] }, @@ -172,10 +177,6 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", - "from mlxtend.plotting import scatter_hist\n", - "\n", - "\n", "fig = scatter_hist(df[\"sepal length [cm]\"], df[\"sepal width [cm]\"])" ] }, @@ -298,7 +299,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/scatter_hist.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/scatter_hist.md\", \"r\") as f:\n", " s = f.read()\n", "print(s)" ] diff --git a/docs/sources/user_guide/plotting/scatterplotmatrix.ipynb b/docs/sources/user_guide/plotting/scatterplotmatrix.ipynb index 78e1a970b..2770358cf 100644 --- a/docs/sources/user_guide/plotting/scatterplotmatrix.ipynb +++ b/docs/sources/user_guide/plotting/scatterplotmatrix.ipynb @@ -112,12 +112,16 @@ } ], "source": [ - "names = ['sepal length [cm]', 'sepal width [cm]',\n", - " 'petal length [cm]', 'petal width [cm]']\n", + "names = [\n", + " \"sepal length [cm]\",\n", + " \"sepal width [cm]\",\n", + " \"petal length [cm]\",\n", + " \"petal width [cm]\",\n", + "]\n", "\n", - "fig, axes = scatterplotmatrix(X[y==0], figsize=(10, 8), alpha=0.5)\n", - "fig, axes = scatterplotmatrix(X[y==1], fig_axes=(fig, axes), alpha=0.5)\n", - "fig, axes = scatterplotmatrix(X[y==2], fig_axes=(fig, axes), alpha=0.5, names=names)\n", + "fig, axes = scatterplotmatrix(X[y == 0], figsize=(10, 8), alpha=0.5)\n", + "fig, axes = scatterplotmatrix(X[y == 1], fig_axes=(fig, axes), alpha=0.5)\n", + "fig, axes = scatterplotmatrix(X[y == 2], fig_axes=(fig, axes), alpha=0.5, names=names)\n", "\n", "plt.tight_layout()\n", "plt.show()" @@ -197,7 +201,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/scatterplotmatrix.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/scatterplotmatrix.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/plotting/stacked_barplot.ipynb b/docs/sources/user_guide/plotting/stacked_barplot.ipynb index 963412a35..bd8dfe60b 100644 --- a/docs/sources/user_guide/plotting/stacked_barplot.ipynb +++ b/docs/sources/user_guide/plotting/stacked_barplot.ipynb @@ -139,9 +139,9 @@ "s4 = [1.4, 2.5, 3.5, 4.2]\n", "data = [s1, s2, s3, s4]\n", "\n", - "df = pd.DataFrame(data, columns=['X1', 'X2', 'X3', 'X4'])\n", - "df.columns = ['X1', 'X2', 'X3', 'X4']\n", - "df.index = ['Sample1', 'Sample2', 'Sample3', 'Sample4']\n", + "df = pd.DataFrame(data, columns=[\"X1\", \"X2\", \"X3\", \"X4\"])\n", + "df.columns = [\"X1\", \"X2\", \"X3\", \"X4\"]\n", + "df.index = [\"Sample1\", \"Sample2\", \"Sample3\", \"Sample4\"]\n", "df" ] }, @@ -169,10 +169,9 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", "from mlxtend.plotting import stacked_barplot\n", "\n", - "fig = stacked_barplot(df, rotation=45, legend_loc='best')" + "fig = stacked_barplot(df, rotation=45, legend_loc=\"best\")" ] }, { @@ -237,7 +236,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.plotting/stacked_barplot.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.plotting/stacked_barplot.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/preprocessing/CopyTransformer.ipynb b/docs/sources/user_guide/preprocessing/CopyTransformer.ipynb index b408115b0..e0267fae8 100644 --- a/docs/sources/user_guide/preprocessing/CopyTransformer.ipynb +++ b/docs/sources/user_guide/preprocessing/CopyTransformer.ipynb @@ -61,29 +61,29 @@ "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "from mlxtend.preprocessing import CopyTransformer\n", - "import re\n", "import numpy as np\n", "\n", - "X_train = np.array(['abc def ghi', 'this is a test',\n", - " 'this is a test', 'this is a test'])\n", + "X_train = np.array(\n", + " [\"abc def ghi\", \"this is a test\", \"this is a test\", \"this is a test\"]\n", + ")\n", "y_train = np.array([0, 0, 1, 1])\n", "\n", - "pipe_1 = Pipeline([\n", - " ('vect', CountVectorizer()),\n", - " ('to_dense', CopyTransformer()),\n", - " ('clf', RandomForestClassifier())\n", - "])\n", + "pipe_1 = Pipeline(\n", + " [\n", + " (\"vect\", CountVectorizer()),\n", + " (\"to_dense\", CopyTransformer()),\n", + " (\"clf\", RandomForestClassifier()),\n", + " ]\n", + ")\n", "\n", "parameters_1 = dict(\n", " clf__n_estimators=[50, 100, 200],\n", - " clf__max_features=['sqrt', 'log2', None],)\n", + " clf__max_features=[\"sqrt\", \"log2\", None],\n", + ")\n", "\n", - "grid_search_1 = GridSearchCV(pipe_1, \n", - " parameters_1, \n", - " n_jobs=1, \n", - " verbose=1,\n", - " scoring='accuracy',\n", - " cv=2)\n", + "grid_search_1 = GridSearchCV(\n", + " pipe_1, parameters_1, n_jobs=1, verbose=1, scoring=\"accuracy\", cv=2\n", + ")\n", "\n", "\n", "print(\"Performing grid search...\")\n", @@ -225,7 +225,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/CopyTransformer.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/CopyTransformer.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/preprocessing/DenseTransformer.ipynb b/docs/sources/user_guide/preprocessing/DenseTransformer.ipynb index 758c51707..d9a804d27 100644 --- a/docs/sources/user_guide/preprocessing/DenseTransformer.ipynb +++ b/docs/sources/user_guide/preprocessing/DenseTransformer.ipynb @@ -61,29 +61,29 @@ "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "from mlxtend.preprocessing import DenseTransformer\n", - "import re\n", "import numpy as np\n", "\n", - "X_train = np.array(['abc def ghi', 'this is a test',\n", - " 'this is a test', 'this is a test'])\n", + "X_train = np.array(\n", + " [\"abc def ghi\", \"this is a test\", \"this is a test\", \"this is a test\"]\n", + ")\n", "y_train = np.array([0, 0, 1, 1])\n", "\n", - "pipe_1 = Pipeline([\n", - " ('vect', CountVectorizer()),\n", - " ('to_dense', DenseTransformer()),\n", - " ('clf', RandomForestClassifier())\n", - "])\n", + "pipe_1 = Pipeline(\n", + " [\n", + " (\"vect\", CountVectorizer()),\n", + " (\"to_dense\", DenseTransformer()),\n", + " (\"clf\", RandomForestClassifier()),\n", + " ]\n", + ")\n", "\n", "parameters_1 = dict(\n", " clf__n_estimators=[50, 100, 200],\n", - " clf__max_features=['sqrt', 'log2', None],)\n", + " clf__max_features=[\"sqrt\", \"log2\", None],\n", + ")\n", "\n", - "grid_search_1 = GridSearchCV(pipe_1, \n", - " parameters_1, \n", - " n_jobs=1, \n", - " verbose=1,\n", - " scoring='accuracy',\n", - " cv=2)\n", + "grid_search_1 = GridSearchCV(\n", + " pipe_1, parameters_1, n_jobs=1, verbose=1, scoring=\"accuracy\", cv=2\n", + ")\n", "\n", "\n", "print(\"Performing grid search...\")\n", @@ -225,7 +225,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/DenseTransformer.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/DenseTransformer.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/preprocessing/MeanCenterer.ipynb b/docs/sources/user_guide/preprocessing/MeanCenterer.ipynb index 96d3231b4..5e31642cc 100644 --- a/docs/sources/user_guide/preprocessing/MeanCenterer.ipynb +++ b/docs/sources/user_guide/preprocessing/MeanCenterer.ipynb @@ -56,10 +56,8 @@ "source": [ "import numpy as np\n", "from mlxtend.preprocessing import MeanCenterer\n", - "X_train = np.array(\n", - " [[1, 2, 3],\n", - " [4, 5, 6],\n", - " [7, 8, 9]])\n", + "\n", + "X_train = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", "mc = MeanCenterer().fit(X_train)\n", "mc.transform(X_train)" ] @@ -160,7 +158,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/MeanCenterer.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/MeanCenterer.md\", \"r\") as f:\n", " print(f.read())" ] }, diff --git a/docs/sources/user_guide/preprocessing/TransactionEncoder.ipynb b/docs/sources/user_guide/preprocessing/TransactionEncoder.ipynb index 01e2db775..d7b6fa5d6 100644 --- a/docs/sources/user_guide/preprocessing/TransactionEncoder.ipynb +++ b/docs/sources/user_guide/preprocessing/TransactionEncoder.ipynb @@ -57,14 +57,16 @@ "source": [ "from mlxtend.preprocessing import TransactionEncoder\n", "\n", - "dataset = [['Apple', 'Beer', 'Rice', 'Chicken'],\n", - " ['Apple', 'Beer', 'Rice'],\n", - " ['Apple', 'Beer'],\n", - " ['Apple', 'Bananas'],\n", - " ['Milk', 'Beer', 'Rice', 'Chicken'],\n", - " ['Milk', 'Beer', 'Rice'],\n", - " ['Milk', 'Beer'],\n", - " ['Apple', 'Bananas']]" + "dataset = [\n", + " [\"Apple\", \"Beer\", \"Rice\", \"Chicken\"],\n", + " [\"Apple\", \"Beer\", \"Rice\"],\n", + " [\"Apple\", \"Beer\"],\n", + " [\"Apple\", \"Bananas\"],\n", + " [\"Milk\", \"Beer\", \"Rice\", \"Chicken\"],\n", + " [\"Milk\", \"Beer\", \"Rice\"],\n", + " [\"Milk\", \"Beer\"],\n", + " [\"Apple\", \"Bananas\"],\n", + "]" ] }, { @@ -565,7 +567,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/TransactionEncoder.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/TransactionEncoder.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/preprocessing/minmax_scaling.ipynb b/docs/sources/user_guide/preprocessing/minmax_scaling.ipynb index 6433e33ae..b35578969 100644 --- a/docs/sources/user_guide/preprocessing/minmax_scaling.ipynb +++ b/docs/sources/user_guide/preprocessing/minmax_scaling.ipynb @@ -152,8 +152,8 @@ "\n", "s1 = pd.Series([1, 2, 3, 4, 5, 6], index=(range(6)))\n", "s2 = pd.Series([10, 9, 8, 7, 6, 5], index=(range(6)))\n", - "df = pd.DataFrame(s1, columns=['s1'])\n", - "df['s2'] = s2\n", + "df = pd.DataFrame(s1, columns=[\"s1\"])\n", + "df[\"s2\"] = s2\n", "df" ] }, @@ -240,7 +240,7 @@ "source": [ "from mlxtend.preprocessing import minmax_scaling\n", "\n", - "minmax_scaling(df, columns=['s1', 's2'])" + "minmax_scaling(df, columns=[\"s1\", \"s2\"])" ] }, { @@ -274,8 +274,7 @@ "source": [ "import numpy as np\n", "\n", - "X = np.array([[1, 10], [2, 9], [3, 8], \n", - " [4, 7], [5, 6], [6, 5]])\n", + "X = np.array([[1, 10], [2, 9], [3, 8], [4, 7], [5, 6], [6, 5]])\n", "X" ] }, @@ -362,7 +361,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/minmax_scaling.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/minmax_scaling.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/preprocessing/one-hot_encoding.ipynb b/docs/sources/user_guide/preprocessing/one-hot_encoding.ipynb index 70bf78057..85ee22eed 100644 --- a/docs/sources/user_guide/preprocessing/one-hot_encoding.ipynb +++ b/docs/sources/user_guide/preprocessing/one-hot_encoding.ipynb @@ -154,7 +154,7 @@ "from mlxtend.preprocessing import one_hot\n", "\n", "y = [0, 1, 2, 1, 2]\n", - "one_hot(y, dtype='int')" + "one_hot(y, dtype=\"int\")" ] }, { @@ -245,7 +245,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/one_hot.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/one_hot.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/preprocessing/shuffle_arrays_unison.ipynb b/docs/sources/user_guide/preprocessing/shuffle_arrays_unison.ipynb index 6c5feedfa..0be390afd 100644 --- a/docs/sources/user_guide/preprocessing/shuffle_arrays_unison.ipynb +++ b/docs/sources/user_guide/preprocessing/shuffle_arrays_unison.ipynb @@ -49,10 +49,11 @@ "source": [ "import numpy as np\n", "from mlxtend.preprocessing import shuffle_arrays_unison\n", + "\n", "X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", "y = np.array([1, 2, 3])\n", - "print('X:\\n%s' % X)\n", - "print('y:\\n%s' % y)" + "print(\"X:\\n%s\" % X)\n", + "print(\"y:\\n%s\" % y)" ] }, { @@ -75,8 +76,8 @@ ], "source": [ "X2, y2 = shuffle_arrays_unison(arrays=[X, y], random_seed=3)\n", - "print('X2:\\n%s' % X2)\n", - "print('y2:\\n%s' % y2)" + "print(\"X2:\\n%s\" % X2)\n", + "print(\"y2:\\n%s\" % y2)" ] }, { @@ -135,7 +136,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/shuffle_arrays_unison.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/shuffle_arrays_unison.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/preprocessing/standardize.ipynb b/docs/sources/user_guide/preprocessing/standardize.ipynb index 4ed1f8a67..caa4e5b71 100644 --- a/docs/sources/user_guide/preprocessing/standardize.ipynb +++ b/docs/sources/user_guide/preprocessing/standardize.ipynb @@ -148,8 +148,8 @@ "\n", "s1 = pd.Series([1, 2, 3, 4, 5, 6], index=(range(6)))\n", "s2 = pd.Series([10, 9, 8, 7, 6, 5], index=(range(6)))\n", - "df = pd.DataFrame(s1, columns=['s1'])\n", - "df['s2'] = s2\n", + "df = pd.DataFrame(s1, columns=[\"s1\"])\n", + "df[\"s2\"] = s2\n", "df" ] }, @@ -222,7 +222,8 @@ ], "source": [ "from mlxtend.preprocessing import standardize\n", - "standardize(df, columns=['s1', 's2'])" + "\n", + "standardize(df, columns=[\"s1\", \"s2\"])" ] }, { @@ -283,6 +284,7 @@ ], "source": [ "from mlxtend.preprocessing import standardize\n", + "\n", "standardize(X, columns=[0, 1])" ] }, @@ -325,9 +327,7 @@ "X_train = np.array([[1, 10], [4, 7], [3, 8]])\n", "X_test = np.array([[1, 2], [3, 4], [5, 6]])\n", "\n", - "X_train_std, params = standardize(X_train, \n", - " columns=[0, 1], \n", - " return_params=True)\n", + "X_train_std, params = standardize(X_train, columns=[0, 1], return_params=True)\n", "X_train_std" ] }, @@ -371,9 +371,7 @@ } ], "source": [ - "X_test_std = standardize(X_test, \n", - " columns=[0, 1], \n", - " params=params)\n", + "X_test_std = standardize(X_test, columns=[0, 1], params=params)\n", "X_test_std" ] }, @@ -452,7 +450,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.preprocessing/standardize.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.preprocessing/standardize.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/regressor/LinearRegression.ipynb b/docs/sources/user_guide/regressor/LinearRegression.ipynb index b1cf2dae6..af3bfa98a 100644 --- a/docs/sources/user_guide/regressor/LinearRegression.ipynb +++ b/docs/sources/user_guide/regressor/LinearRegression.ipynb @@ -214,20 +214,22 @@ "import matplotlib.pyplot as plt\n", "from mlxtend.regressor import LinearRegression\n", "\n", - "X = np.array([ 1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", - "y = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])\n", + "X = np.array([1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", + "y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])\n", "\n", "ne_lr = LinearRegression()\n", "ne_lr.fit(X, y)\n", "\n", - "print('Intercept: %.2f' % ne_lr.b_)\n", - "print('Slope: %.2f' % ne_lr.w_[0])\n", + "print(\"Intercept: %.2f\" % ne_lr.b_)\n", + "print(\"Slope: %.2f\" % ne_lr.w_[0])\n", + "\n", "\n", "def lin_regplot(X, y, model):\n", - " plt.scatter(X, y, c='blue')\n", - " plt.plot(X, model.predict(X), color='red') \n", + " plt.scatter(X, y, c=\"blue\")\n", + " plt.plot(X, model.predict(X), color=\"red\")\n", " return\n", "\n", + "\n", "lin_regplot(X, y, ne_lr)\n", "plt.show()" ] @@ -270,20 +272,22 @@ "import matplotlib.pyplot as plt\n", "from mlxtend.regressor import LinearRegression\n", "\n", - "X = np.array([ 1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", - "y = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])\n", + "X = np.array([1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", + "y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])\n", "\n", - "qr_lr = LinearRegression(method='qr')\n", + "qr_lr = LinearRegression(method=\"qr\")\n", "qr_lr.fit(X, y)\n", "\n", - "print('Intercept: %.2f' % qr_lr.b_)\n", - "print('Slope: %.2f' % qr_lr.w_[0])\n", + "print(\"Intercept: %.2f\" % qr_lr.b_)\n", + "print(\"Slope: %.2f\" % qr_lr.w_[0])\n", + "\n", "\n", "def lin_regplot(X, y, model):\n", - " plt.scatter(X, y, c='blue')\n", - " plt.plot(X, model.predict(X), color='red') \n", + " plt.scatter(X, y, c=\"blue\")\n", + " plt.plot(X, model.predict(X), color=\"red\")\n", " return\n", "\n", + "\n", "lin_regplot(X, y, qr_lr)\n", "plt.show()" ] @@ -326,20 +330,22 @@ "import matplotlib.pyplot as plt\n", "from mlxtend.regressor import LinearRegression\n", "\n", - "X = np.array([ 1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", - "y = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])\n", + "X = np.array([1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", + "y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])\n", "\n", - "svd_lr = LinearRegression(method='svd')\n", + "svd_lr = LinearRegression(method=\"svd\")\n", "svd_lr.fit(X, y)\n", "\n", - "print('Intercept: %.2f' %svd_lr.b_)\n", - "print('Slope: %.2f' % svd_lr.w_[0])\n", + "print(\"Intercept: %.2f\" % svd_lr.b_)\n", + "print(\"Slope: %.2f\" % svd_lr.w_[0])\n", + "\n", "\n", "def lin_regplot(X, y, model):\n", - " plt.scatter(X, y, c='blue')\n", - " plt.plot(X, model.predict(X), color='red') \n", + " plt.scatter(X, y, c=\"blue\")\n", + " plt.plot(X, model.predict(X), color=\"red\")\n", " return\n", "\n", + "\n", "lin_regplot(X, y, svd_lr)\n", "plt.show()" ] @@ -389,25 +395,29 @@ "import matplotlib.pyplot as plt\n", "from mlxtend.regressor import LinearRegression\n", "\n", - "X = np.array([ 1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", - "y = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])\n", - "\n", - "gd_lr = LinearRegression(method='sgd',\n", - " eta=0.005, \n", - " epochs=100,\n", - " minibatches=1,\n", - " random_seed=123,\n", - " print_progress=3)\n", + "X = np.array([1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", + "y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])\n", + "\n", + "gd_lr = LinearRegression(\n", + " method=\"sgd\",\n", + " eta=0.005,\n", + " epochs=100,\n", + " minibatches=1,\n", + " random_seed=123,\n", + " print_progress=3,\n", + ")\n", "gd_lr.fit(X, y)\n", "\n", - "print('Intercept: %.2f' % gd_lr.b_)\n", - "print('Slope: %.2f' % gd_lr.w_)\n", + "print(\"Intercept: %.2f\" % gd_lr.b_)\n", + "print(\"Slope: %.2f\" % gd_lr.w_)\n", + "\n", "\n", "def lin_regplot(X, y, model):\n", - " plt.scatter(X, y, c='blue')\n", - " plt.plot(X, model.predict(X), color='red') \n", + " plt.scatter(X, y, c=\"blue\")\n", + " plt.plot(X, model.predict(X), color=\"red\")\n", " return\n", "\n", + "\n", "lin_regplot(X, y, gd_lr)\n", "plt.show()" ] @@ -433,12 +443,12 @@ "source": [ "# Visualizing the cost to check for convergence and plotting the linear model:\n", "\n", - "plt.plot(range(1, gd_lr.epochs+1), gd_lr.cost_)\n", - "plt.xlabel('Epochs')\n", - "plt.ylabel('Cost')\n", + "plt.plot(range(1, gd_lr.epochs + 1), gd_lr.cost_)\n", + "plt.xlabel(\"Epochs\")\n", + "plt.ylabel(\"Cost\")\n", "plt.ylim([0, 0.2])\n", "plt.tight_layout()\n", - "plt.show() " + "plt.show()" ] }, { @@ -479,24 +489,24 @@ "import matplotlib.pyplot as plt\n", "from mlxtend.regressor import LinearRegression\n", "\n", - "X = np.array([ 1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", - "y = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])\n", + "X = np.array([1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", + "y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])\n", "\n", - "sgd_lr = LinearRegression(method='sgd',\n", - " eta=0.01, \n", - " epochs=100, \n", - " random_seed=0, \n", - " minibatches=len(y))\n", + "sgd_lr = LinearRegression(\n", + " method=\"sgd\", eta=0.01, epochs=100, random_seed=0, minibatches=len(y)\n", + ")\n", "sgd_lr.fit(X, y)\n", "\n", - "print('Intercept: %.2f' % sgd_lr.w_)\n", - "print('Slope: %.2f' % sgd_lr.b_)\n", + "print(\"Intercept: %.2f\" % sgd_lr.w_)\n", + "print(\"Slope: %.2f\" % sgd_lr.b_)\n", + "\n", "\n", "def lin_regplot(X, y, model):\n", - " plt.scatter(X, y, c='blue')\n", - " plt.plot(X, model.predict(X), color='red') \n", + " plt.scatter(X, y, c=\"blue\")\n", + " plt.plot(X, model.predict(X), color=\"red\")\n", " return\n", "\n", + "\n", "lin_regplot(X, y, sgd_lr)\n", "plt.show()" ] @@ -520,12 +530,12 @@ } ], "source": [ - "plt.plot(range(1, sgd_lr.epochs+1), sgd_lr.cost_)\n", - "plt.xlabel('Epochs')\n", - "plt.ylabel('Cost')\n", + "plt.plot(range(1, sgd_lr.epochs + 1), sgd_lr.cost_)\n", + "plt.xlabel(\"Epochs\")\n", + "plt.ylabel(\"Cost\")\n", "plt.ylim([0, 0.2])\n", "plt.tight_layout()\n", - "plt.show() " + "plt.show()" ] }, { @@ -566,24 +576,24 @@ "import matplotlib.pyplot as plt\n", "from mlxtend.regressor import LinearRegression\n", "\n", - "X = np.array([ 1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", - "y = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])\n", + "X = np.array([1.0, 2.1, 3.6, 4.2, 6])[:, np.newaxis]\n", + "y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])\n", "\n", - "sgd_lr = LinearRegression(method='sgd',\n", - " eta=0.01, \n", - " epochs=100, \n", - " random_seed=0, \n", - " minibatches=3)\n", + "sgd_lr = LinearRegression(\n", + " method=\"sgd\", eta=0.01, epochs=100, random_seed=0, minibatches=3\n", + ")\n", "sgd_lr.fit(X, y)\n", "\n", - "print('Intercept: %.2f' % sgd_lr.b_)\n", - "print('Slope: %.2f' % sgd_lr.w_)\n", + "print(\"Intercept: %.2f\" % sgd_lr.b_)\n", + "print(\"Slope: %.2f\" % sgd_lr.w_)\n", + "\n", "\n", "def lin_regplot(X, y, model):\n", - " plt.scatter(X, y, c='blue')\n", - " plt.plot(X, model.predict(X), color='red') \n", + " plt.scatter(X, y, c=\"blue\")\n", + " plt.plot(X, model.predict(X), color=\"red\")\n", " return\n", "\n", + "\n", "lin_regplot(X, y, sgd_lr)\n", "plt.show()" ] @@ -607,12 +617,12 @@ } ], "source": [ - "plt.plot(range(1, sgd_lr.epochs+1), sgd_lr.cost_)\n", - "plt.xlabel('Epochs')\n", - "plt.ylabel('Cost')\n", + "plt.plot(range(1, sgd_lr.epochs + 1), sgd_lr.cost_)\n", + "plt.xlabel(\"Epochs\")\n", + "plt.ylabel(\"Cost\")\n", "plt.ylim([0, 0.2])\n", "plt.tight_layout()\n", - "plt.show() " + "plt.show()" ] }, { @@ -798,7 +808,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.regressor/LinearRegression.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.regressor/LinearRegression.md\", \"r\") as f:\n", " print(f.read())" ] }, diff --git a/docs/sources/user_guide/regressor/StackingCVRegressor.ipynb b/docs/sources/user_guide/regressor/StackingCVRegressor.ipynb index 7d7e0160c..0dddae5d7 100644 --- a/docs/sources/user_guide/regressor/StackingCVRegressor.ipynb +++ b/docs/sources/user_guide/regressor/StackingCVRegressor.ipynb @@ -98,31 +98,28 @@ "from sklearn.linear_model import Lasso\n", "from sklearn.ensemble import RandomForestRegressor\n", "from sklearn.model_selection import cross_val_score\n", - "import numpy as np\n", "\n", "RANDOM_SEED = 42\n", "\n", "X, y = load_boston(return_X_y=True)\n", "\n", - "svr = SVR(kernel='linear')\n", + "svr = SVR(kernel=\"linear\")\n", "lasso = Lasso()\n", - "rf = RandomForestRegressor(n_estimators=5, \n", - " random_state=RANDOM_SEED)\n", + "rf = RandomForestRegressor(n_estimators=5, random_state=RANDOM_SEED)\n", "\n", "# Starting from v0.16.0, StackingCVRegressor supports\n", "# `random_state` to get deterministic result.\n", - "stack = StackingCVRegressor(regressors=(svr, lasso, rf),\n", - " meta_regressor=lasso,\n", - " random_state=RANDOM_SEED)\n", + "stack = StackingCVRegressor(\n", + " regressors=(svr, lasso, rf), meta_regressor=lasso, random_state=RANDOM_SEED\n", + ")\n", "\n", - "print('5-fold cross validation scores:\\n')\n", + "print(\"5-fold cross validation scores:\\n\")\n", "\n", - "for clf, label in zip([svr, lasso, rf, stack], ['SVM', 'Lasso', \n", - " 'Random Forest', \n", - " 'StackingCVRegressor']):\n", + "for clf, label in zip(\n", + " [svr, lasso, rf, stack], [\"SVM\", \"Lasso\", \"Random Forest\", \"StackingCVRegressor\"]\n", + "):\n", " scores = cross_val_score(clf, X, y, cv=5)\n", - " print(\"R^2 Score: %0.2f (+/- %0.2f) [%s]\" % (\n", - " scores.mean(), scores.std(), label))" + " print(\"R^2 Score: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))" ] }, { @@ -144,17 +141,17 @@ } ], "source": [ - "stack = StackingCVRegressor(regressors=(svr, lasso, rf),\n", - " meta_regressor=lasso)\n", + "stack = StackingCVRegressor(regressors=(svr, lasso, rf), meta_regressor=lasso)\n", "\n", - "print('5-fold cross validation scores:\\n')\n", + "print(\"5-fold cross validation scores:\\n\")\n", "\n", - "for clf, label in zip([svr, lasso, rf, stack], ['SVM', 'Lasso', \n", - " 'Random Forest', \n", - " 'StackingCVRegressor']):\n", - " scores = cross_val_score(clf, X, y, cv=5, scoring='neg_mean_squared_error')\n", - " print(\"Neg. MSE Score: %0.2f (+/- %0.2f) [%s]\" % (\n", - " scores.mean(), scores.std(), label))" + "for clf, label in zip(\n", + " [svr, lasso, rf, stack], [\"SVM\", \"Lasso\", \"Random Forest\", \"StackingCVRegressor\"]\n", + "):\n", + " scores = cross_val_score(clf, X, y, cv=5, scoring=\"neg_mean_squared_error\")\n", + " print(\n", + " \"Neg. MSE Score: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label)\n", + " )" ] }, { @@ -200,23 +197,24 @@ "lasso = Lasso(random_state=RANDOM_SEED)\n", "rf = RandomForestRegressor(random_state=RANDOM_SEED)\n", "\n", - "stack = StackingCVRegressor(regressors=(lasso, ridge),\n", - " meta_regressor=rf, \n", - " random_state=RANDOM_SEED,\n", - " use_features_in_secondary=True)\n", + "stack = StackingCVRegressor(\n", + " regressors=(lasso, ridge),\n", + " meta_regressor=rf,\n", + " random_state=RANDOM_SEED,\n", + " use_features_in_secondary=True,\n", + ")\n", "\n", - "params = {'lasso__alpha': [0.1, 1.0, 10.0],\n", - " 'ridge__alpha': [0.1, 1.0, 10.0]}\n", + "params = {\"lasso__alpha\": [0.1, 1.0, 10.0], \"ridge__alpha\": [0.1, 1.0, 10.0]}\n", "\n", "grid = GridSearchCV(\n", - " estimator=stack, \n", + " estimator=stack,\n", " param_grid={\n", - " 'lasso__alpha': [x/5.0 for x in range(1, 10)],\n", - " 'ridge__alpha': [x/20.0 for x in range(1, 10)],\n", - " 'meta_regressor__n_estimators': [10, 100]\n", - " }, \n", + " \"lasso__alpha\": [x / 5.0 for x in range(1, 10)],\n", + " \"ridge__alpha\": [x / 20.0 for x in range(1, 10)],\n", + " \"meta_regressor__n_estimators\": [10, 100],\n", + " },\n", " cv=5,\n", - " refit=True\n", + " refit=True,\n", ")\n", "\n", "grid.fit(X, y)\n", @@ -252,19 +250,23 @@ } ], "source": [ - "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", " if r > 10:\n", " break\n", - "print('...')\n", + "print(\"...\")\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_)" ] }, { @@ -629,7 +631,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.regressor/StackingCVRegressor.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.regressor/StackingCVRegressor.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/regressor/StackingRegressor.ipynb b/docs/sources/user_guide/regressor/StackingRegressor.ipynb index 4c3a365a9..17b4fbfd3 100644 --- a/docs/sources/user_guide/regressor/StackingRegressor.ipynb +++ b/docs/sources/user_guide/regressor/StackingRegressor.ipynb @@ -79,7 +79,6 @@ "outputs": [], "source": [ "from mlxtend.regressor import StackingRegressor\n", - "from mlxtend.data import boston_housing_data\n", "from sklearn.linear_model import LinearRegression\n", "from sklearn.linear_model import Ridge\n", "from sklearn.svm import SVR\n", @@ -87,7 +86,7 @@ "import numpy as np\n", "import warnings\n", "\n", - "warnings.simplefilter('ignore')\n", + "warnings.simplefilter(\"ignore\")\n", "\n", "# Generating a sample dataset\n", "np.random.seed(1)\n", @@ -124,12 +123,11 @@ "# Initializing models\n", "\n", "lr = LinearRegression()\n", - "svr_lin = SVR(kernel='linear')\n", + "svr_lin = SVR(kernel=\"linear\")\n", "ridge = Ridge(random_state=1)\n", - "svr_rbf = SVR(kernel='rbf')\n", + "svr_rbf = SVR(kernel=\"rbf\")\n", "\n", - "stregr = StackingRegressor(regressors=[svr_lin, lr, ridge], \n", - " meta_regressor=svr_rbf)\n", + "stregr = StackingRegressor(regressors=[svr_lin, lr, ridge], meta_regressor=svr_rbf)\n", "\n", "# Training the stacking classifier\n", "\n", @@ -138,13 +136,12 @@ "\n", "# Evaluate and visualize the fit\n", "\n", - "print(\"Mean Squared Error: %.4f\"\n", - " % np.mean((stregr.predict(X) - y) ** 2))\n", - "print('Variance Score: %.4f' % stregr.score(X, y))\n", + "print(\"Mean Squared Error: %.4f\" % np.mean((stregr.predict(X) - y) ** 2))\n", + "print(\"Variance Score: %.4f\" % stregr.score(X, y))\n", "\n", - "with plt.style.context(('seaborn-whitegrid')):\n", - " plt.scatter(X, y, c='lightgray')\n", - " plt.plot(X, stregr.predict(X), c='darkgreen', lw=2)\n", + "with plt.style.context((\"seaborn-whitegrid\")):\n", + " plt.scatter(X, y, c=\"lightgray\")\n", + " plt.plot(X, stregr.predict(X), c=\"darkgreen\", lw=2)\n", "\n", "plt.show()" ] @@ -207,24 +204,22 @@ "# Initializing models\n", "\n", "lr = LinearRegression()\n", - "svr_lin = SVR(kernel='linear')\n", + "svr_lin = SVR(kernel=\"linear\")\n", "ridge = Ridge(random_state=1)\n", "lasso = Lasso(random_state=1)\n", - "svr_rbf = SVR(kernel='rbf')\n", + "svr_rbf = SVR(kernel=\"rbf\")\n", "regressors = [svr_lin, lr, ridge, lasso]\n", - "stregr = StackingRegressor(regressors=regressors, \n", - " meta_regressor=svr_rbf)\n", + "stregr = StackingRegressor(regressors=regressors, meta_regressor=svr_rbf)\n", "\n", - "params = {'lasso__alpha': [0.1, 1.0, 10.0],\n", - " 'ridge__alpha': [0.1, 1.0, 10.0],\n", - " 'svr__C': [0.1, 1.0, 10.0],\n", - " 'meta_regressor__C': [0.1, 1.0, 10.0, 100.0],\n", - " 'meta_regressor__gamma': [0.1, 1.0, 10.0]}\n", + "params = {\n", + " \"lasso__alpha\": [0.1, 1.0, 10.0],\n", + " \"ridge__alpha\": [0.1, 1.0, 10.0],\n", + " \"svr__C\": [0.1, 1.0, 10.0],\n", + " \"meta_regressor__C\": [0.1, 1.0, 10.0, 100.0],\n", + " \"meta_regressor__gamma\": [0.1, 1.0, 10.0],\n", + "}\n", "\n", - "grid = GridSearchCV(estimator=stregr, \n", - " param_grid=params, \n", - " cv=5,\n", - " refit=True)\n", + "grid = GridSearchCV(estimator=stregr, param_grid=params, cv=5, refit=True)\n", "grid.fit(X, y)\n", "\n", "print(\"Best: %f using %s\" % (grid.best_score_, grid.best_params_))" @@ -258,19 +253,23 @@ } ], "source": [ - "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", " if r > 10:\n", " break\n", - "print('...')\n", + "print(\"...\")\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_)" ] }, { @@ -299,13 +298,12 @@ ], "source": [ "# Evaluate and visualize the fit\n", - "print(\"Mean Squared Error: %.4f\"\n", - " % np.mean((grid.predict(X) - y) ** 2))\n", - "print('Variance Score: %.4f' % grid.score(X, y))\n", + "print(\"Mean Squared Error: %.4f\" % np.mean((grid.predict(X) - y) ** 2))\n", + "print(\"Variance Score: %.4f\" % grid.score(X, y))\n", "\n", - "with plt.style.context(('seaborn-whitegrid')):\n", - " plt.scatter(X, y, c='lightgray')\n", - " plt.plot(X, grid.predict(X), c='darkgreen', lw=2)\n", + "with plt.style.context((\"seaborn-whitegrid\")):\n", + " plt.scatter(X, y, c=\"lightgray\")\n", + " plt.plot(X, grid.predict(X), c=\"darkgreen\", lw=2)\n", "\n", "plt.show()" ] @@ -637,7 +635,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.regressor/StackingRegressor.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.regressor/StackingRegressor.md\", \"r\") as f:\n", " print(f.read())" ] }, diff --git a/docs/sources/user_guide/text/generalize_names.ipynb b/docs/sources/user_guide/text/generalize_names.ipynb index aa6903358..4b5142da6 100644 --- a/docs/sources/user_guide/text/generalize_names.ipynb +++ b/docs/sources/user_guide/text/generalize_names.ipynb @@ -83,7 +83,7 @@ } ], "source": [ - "generalize_names('Pozo, José Ángel')" + "generalize_names(\"Pozo, José Ángel\")" ] }, { @@ -103,7 +103,7 @@ } ], "source": [ - "generalize_names('José Pozo')" + "generalize_names(\"José Pozo\")" ] }, { @@ -123,7 +123,7 @@ } ], "source": [ - "generalize_names('José Ángel Pozo')" + "generalize_names(\"José Ángel Pozo\")" ] }, { @@ -204,7 +204,7 @@ } ], "source": [ - "generalize_names(\"Eto'o, Samuel\", output_sep=', ')" + "generalize_names(\"Eto'o, Samuel\", output_sep=\", \")" ] }, { @@ -262,7 +262,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.text/generalize_names.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.text/generalize_names.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/text/generalize_names_duplcheck.ipynb b/docs/sources/user_guide/text/generalize_names_duplcheck.ipynb index ca273bd08..4ed74202f 100644 --- a/docs/sources/user_guide/text/generalize_names_duplcheck.ipynb +++ b/docs/sources/user_guide/text/generalize_names_duplcheck.ipynb @@ -122,10 +122,7 @@ "import pandas as pd\n", "from io import StringIO\n", "\n", - "simulated_csv = \"name,some_value\\n\"\\\n", - " \"Samuel Eto'o,1\\n\"\\\n", - " \"Adam Johnson,1\\n\"\\\n", - " \"Andrew Johnson,1\\n\"\n", + "simulated_csv = \"name,some_value\\nSamuel Eto'o,1\\nAdam Johnson,1\\nAndrew Johnson,1\\n\"\n", "\n", "df = pd.read_csv(StringIO(simulated_csv))\n", "df" @@ -189,7 +186,8 @@ ], "source": [ "from mlxtend.text import generalize_names_duplcheck\n", - "df_new = generalize_names_duplcheck(df=df, col_name='name')\n", + "\n", + "df_new = generalize_names_duplcheck(df=df, col_name=\"name\")\n", "df_new" ] }, @@ -248,7 +246,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.text/generalize_names_duplcheck.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.text/generalize_names_duplcheck.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/docs/sources/user_guide/text/tokenizer.ipynb b/docs/sources/user_guide/text/tokenizer.ipynb index 5473fee38..982c7a102 100644 --- a/docs/sources/user_guide/text/tokenizer.ipynb +++ b/docs/sources/user_guide/text/tokenizer.ipynb @@ -78,7 +78,7 @@ } ], "source": [ - "tokenizer_emoticons('This :) is :( a test :-)!')" + "tokenizer_emoticons(\"This :) is :( a test :-)!\")" ] }, { @@ -114,7 +114,7 @@ } ], "source": [ - "tokenizer_words_and_emoticons('This :) is :( a test :-)!')" + "tokenizer_words_and_emoticons(\"This :) is :( a test :-)!\")" ] }, { @@ -169,13 +169,13 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.text/tokenizer_emoticons.md', 'r') as f:\n", - " s = f.read() + '

'\n", + "with open(\"../../api_modules/mlxtend.text/tokenizer_emoticons.md\", \"r\") as f:\n", + " s = f.read() + \"

\"\n", "\n", - " \n", - "with open('../../api_modules/mlxtend.text/tokenizer_words_and_emoticons.md', 'r') as f:\n", + "\n", + "with open(\"../../api_modules/mlxtend.text/tokenizer_words_and_emoticons.md\", \"r\") as f:\n", " s2 = f.readlines()\n", - " s += ''.join(s2[1:])\n", + " s += \"\".join(s2[1:])\n", "print(s)" ] } diff --git a/docs/sources/user_guide/utils/Counter.ipynb b/docs/sources/user_guide/utils/Counter.ipynb index 7f4e676a7..bd82977d3 100644 --- a/docs/sources/user_guide/utils/Counter.ipynb +++ b/docs/sources/user_guide/utils/Counter.ipynb @@ -178,7 +178,7 @@ } ], "source": [ - "with open('../../api_modules/mlxtend.utils/Counter.md', 'r') as f:\n", + "with open(\"../../api_modules/mlxtend.utils/Counter.md\", \"r\") as f:\n", " print(f.read())" ] } diff --git a/mlxtend/_base/tests/test_classifier.py b/mlxtend/_base/tests/test_classifier.py index e8aa34f31..c2d535585 100644 --- a/mlxtend/_base/tests/test_classifier.py +++ b/mlxtend/_base/tests/test_classifier.py @@ -46,7 +46,7 @@ def test_check_labels_not_ok_1(): with pytest.raises(AttributeError) as excinfo: cl._check_target_array(y, {(0, 1), (1, 2)}) assert excinfo.value.message == ( - "Labels not in {(1, 2), (0, 1)}" ".\nFound (1, 2, 3)" + "Labels not in {(1, 2), (0, 1)}.\nFound (1, 2, 3)" ) @@ -55,9 +55,7 @@ def test_check_labels_integer_notok(): cl = BlankClassifier(print_progress=0, random_seed=1) with pytest.raises(AttributeError) as excinfo: cl._check_target_array(y) - assert excinfo.value.message == ( - "y must be an integer" " array.\nFound float64" - ) + assert excinfo.value.message == ("y must be an integer array.\nFound float64") def test_check_labels_positive_notok(): @@ -66,7 +64,7 @@ def test_check_labels_positive_notok(): with pytest.raises(AttributeError) as excinfo: cl._check_target_array(y) assert excinfo.value.message == ( - "y array must not " "contain negative " "labels.\nFound [-1 1]" + "y array must not contain negative labels.\nFound [-1 1]" ) @@ -76,7 +74,7 @@ def test_predict_fail(): est._is_fitted = False with pytest.raises(AttributeError) as excinfo: est.predict(X) - assert excinfo.value.message == ("Model is not " "fitted, yet.") + assert excinfo.value.message == ("Model is not fitted, yet.") def test_predict_pass(): @@ -89,7 +87,7 @@ def test_predict_pass(): with pytest.raises(TypeError) as excinfo: est.fit(X) assert excinfo.value.message == ( - "fit() missing 1 " "required positional " "argument: 'y'" + "fit() missing 1 required positional argument: 'y'" ) diff --git a/mlxtend/_base/tests/test_cluster.py b/mlxtend/_base/tests/test_cluster.py index 8c1b85eed..0782adde0 100644 --- a/mlxtend/_base/tests/test_cluster.py +++ b/mlxtend/_base/tests/test_cluster.py @@ -46,7 +46,7 @@ def test_check_labels_not_ok_1(): with pytest.raises(AttributeError) as excinfo: cl._check_target_array(y, {(0, 1), (1, 2)}) assert excinfo.value.message == ( - "Labels not in" " {(1, 2), (0, 1)}." "\nFound (1, 2, 3)" + "Labels not in {(1, 2), (0, 1)}.\nFound (1, 2, 3)" ) @@ -55,9 +55,7 @@ def test_check_labels_integer_notok(): cl = BlankClassifier(print_progress=0, random_seed=1) with pytest.raises(AttributeError) as excinfo: cl._check_target_array(y) - assert excinfo.value.message == ( - "y must be an integer" " array.\nFound float64" - ) + assert excinfo.value.message == ("y must be an integer array.\nFound float64") def test_check_labels_positive_notok(): @@ -66,7 +64,7 @@ def test_check_labels_positive_notok(): with pytest.raises(AttributeError) as excinfo: cl._check_target_array(y) assert excinfo.value.message == ( - "y array must not " "contain negative " "labels.\nFound [-1 1]" + "y array must not contain negative labels.\nFound [-1 1]" ) @@ -76,7 +74,7 @@ def test_predict_fail(): est._is_fitted = False with pytest.raises(AttributeError) as excinfo: est.predict(X) - assert excinfo.value.message == ("Model is not " "fitted, yet.") + assert excinfo.value.message == ("Model is not fitted, yet.") def test_predict_pass(): @@ -93,7 +91,7 @@ def test_fit_1(): with pytest.raises(TypeError) as excinfo: est.fit(X) assert excinfo.value.message == ( - "fit() missing 1" "required positional argument: 'y'" + "fit() missing 1required positional argument: 'y'" ) diff --git a/mlxtend/classifier/ensemble_vote.py b/mlxtend/classifier/ensemble_vote.py index 8e6003c58..feac6a1ca 100644 --- a/mlxtend/classifier/ensemble_vote.py +++ b/mlxtend/classifier/ensemble_vote.py @@ -149,7 +149,7 @@ def fit(self, X, y, sample_weight=None): """ if isinstance(y, np.ndarray) and len(y.shape) > 1 and y.shape[1] > 1: raise NotImplementedError( - "Multilabel and multi-output" " classification is not supported." + "Multilabel and multi-output classification is not supported." ) if self.voting not in ("soft", "hard"): @@ -168,9 +168,7 @@ def fit(self, X, y, sample_weight=None): self.classes_ = self.le_.classes_ if not self.fit_base_estimators and self.use_clones: - warnings.warn( - "fit_base_estimators=False " "enforces use_clones to be `False`" - ) + warnings.warn("fit_base_estimators=False enforces use_clones to be `False`") self.use_clones = False if self.use_clones: @@ -220,7 +218,7 @@ def predict(self, X): """ if not hasattr(self, "clfs_"): raise NotFittedError( - "Estimator not fitted, " "call `fit` before exploiting the model." + "Estimator not fitted, call `fit` before exploiting the model." ) if self.voting == "soft": @@ -255,7 +253,7 @@ def predict_proba(self, X): """ if not hasattr(self, "clfs_"): raise NotFittedError( - "Estimator not fitted, " "call `fit` before exploiting the model." + "Estimator not fitted, call `fit` before exploiting the model." ) avg = np.average(self._predict_probas(X), axis=0, weights=self.weights) diff --git a/mlxtend/classifier/oner.py b/mlxtend/classifier/oner.py index bb6dae68e..1137a84bf 100644 --- a/mlxtend/classifier/oner.py +++ b/mlxtend/classifier/oner.py @@ -198,7 +198,7 @@ def predict(self, X): """ if not hasattr(self, "prediction_dict_"): raise NotFittedError( - "Estimator not fitted, " "call `fit` before using the model." + "Estimator not fitted, call `fit` before using the model." ) rules = self.prediction_dict_["rules (value: class)"] diff --git a/mlxtend/classifier/stacking_classification.py b/mlxtend/classifier/stacking_classification.py index 0a2c3b0f5..ac2c8063d 100644 --- a/mlxtend/classifier/stacking_classification.py +++ b/mlxtend/classifier/stacking_classification.py @@ -170,9 +170,7 @@ def fit(self, X, y, sample_weight=None): """ if not self.fit_base_estimators: - warnings.warn( - "fit_base_estimators=False " "enforces use_clones to be `False`" - ) + warnings.warn("fit_base_estimators=False enforces use_clones to be `False`") self.use_clones = False if self.use_clones: diff --git a/mlxtend/classifier/tests/test_stacking_classifier.py b/mlxtend/classifier/tests/test_stacking_classifier.py index b9a0f7f5e..e403e877a 100644 --- a/mlxtend/classifier/tests/test_stacking_classifier.py +++ b/mlxtend/classifier/tests/test_stacking_classifier.py @@ -4,7 +4,6 @@ # # License: BSD 3 clause -import platform import random import numpy as np @@ -549,9 +548,9 @@ def test_decision_function(): min_allowed_score = 0.92 max_allowed_score = 0.95 - assert ( - min_allowed_score <= scores_mean <= max_allowed_score - ), "Score is out of the allowed range." + assert min_allowed_score <= scores_mean <= max_allowed_score, ( + "Score is out of the allowed range." + ) # another test meta = SVC(decision_function_shape="ovo") @@ -568,9 +567,9 @@ def test_decision_function(): else: min_allowed_score = 0.92 max_allowed_score = 0.95 - assert ( - min_allowed_score <= scores_mean <= max_allowed_score - ), "Score is out of the allowed range." + assert min_allowed_score <= scores_mean <= max_allowed_score, ( + "Score is out of the allowed range." + ) def test_drop_col_unsupported(): diff --git a/mlxtend/data/multiplexer.py b/mlxtend/data/multiplexer.py index 3e40b448a..1212d678c 100644 --- a/mlxtend/data/multiplexer.py +++ b/mlxtend/data/multiplexer.py @@ -71,11 +71,11 @@ def make_multiplexer_dataset( """ if not isinstance(address_bits, int): raise AttributeError( - "address_bits" " must be an integer. Got %s." % type(address_bits) + "address_bits must be an integer. Got %s." % type(address_bits) ) if address_bits < 1: raise AttributeError( - "Number of address_bits" " must be greater than 0. Got %s." % address_bits + "Number of address_bits must be greater than 0. Got %s." % address_bits ) register_bits = 2**address_bits total_bits = address_bits + register_bits diff --git a/mlxtend/evaluate/accuracy.py b/mlxtend/evaluate/accuracy.py index 13d8cdcb6..4fa8fb451 100644 --- a/mlxtend/evaluate/accuracy.py +++ b/mlxtend/evaluate/accuracy.py @@ -58,7 +58,7 @@ def accuracy_score( if len(y_target) != len(y_predicted): raise AttributeError( - "`y_target` and `y_predicted`" " don't have the same number of elements." + "`y_target` and `y_predicted` don't have the same number of elements." ) if method == "standard": return _compute_metric(target_temp, predicted_temp, normalize) diff --git a/mlxtend/evaluate/bias_variance_decomp.py b/mlxtend/evaluate/bias_variance_decomp.py index e1a173508..4f311b614 100644 --- a/mlxtend/evaluate/bias_variance_decomp.py +++ b/mlxtend/evaluate/bias_variance_decomp.py @@ -25,7 +25,7 @@ def bias_variance_decomp( loss="0-1_loss", num_rounds=200, random_seed=None, - **fit_params + **fit_params, ): """ estimator : object diff --git a/mlxtend/evaluate/bootstrap_point632.py b/mlxtend/evaluate/bootstrap_point632.py index 2cf45b209..bb6bb658b 100644 --- a/mlxtend/evaluate/bootstrap_point632.py +++ b/mlxtend/evaluate/bootstrap_point632.py @@ -27,7 +27,7 @@ def _check_arrays(X, y=None): raise ValueError("y must be a 1D array.") if not len(y) == X.shape[0]: - raise ValueError("X and y must contain the" "same number of samples") + raise ValueError("X and y must contain thesame number of samples") def no_information_rate(targets, predictions, loss_fn): @@ -154,14 +154,12 @@ def bootstrap_point632_score( """ if not isinstance(n_splits, int) or n_splits < 1: - raise ValueError( - "Number of splits must be" " greater than 1. Got %s." % n_splits - ) + raise ValueError("Number of splits must be greater than 1. Got %s." % n_splits) allowed_methods = (".632", ".632+", "oob") if not isinstance(method, str) or method not in allowed_methods: raise ValueError( - "The `method` must " "be in %s. Got %s." % (allowed_methods, method) + "The `method` must be in %s. Got %s." % (allowed_methods, method) ) # Pandas compatibility diff --git a/mlxtend/evaluate/cochrans_q.py b/mlxtend/evaluate/cochrans_q.py index b7de06666..23b6ee36b 100644 --- a/mlxtend/evaluate/cochrans_q.py +++ b/mlxtend/evaluate/cochrans_q.py @@ -49,9 +49,7 @@ def cochrans_q(y_target, *y_model_predictions): model_lens.add(ary.shape[0]) if len(model_lens) > 1: - raise ValueError( - "Each prediction array must have the " "same number of samples." - ) + raise ValueError("Each prediction array must have the same number of samples.") if num_models < 2: raise ValueError("Provide at least 2 model prediction arrays.") diff --git a/mlxtend/evaluate/confusion_matrix.py b/mlxtend/evaluate/confusion_matrix.py index ecc9d2788..41c15aaa6 100644 --- a/mlxtend/evaluate/confusion_matrix.py +++ b/mlxtend/evaluate/confusion_matrix.py @@ -49,7 +49,7 @@ def confusion_matrix(y_target, y_predicted, binary=False, positive_label=1): if len(y_target) != len(y_predicted): raise AttributeError( - "`y_target` and `y_predicted`" " don't have the same number of elements." + "`y_target` and `y_predicted` don't have the same number of elements." ) if binary: diff --git a/mlxtend/evaluate/f_test.py b/mlxtend/evaluate/f_test.py index 2e3f0ae1a..d61b0e6f7 100644 --- a/mlxtend/evaluate/f_test.py +++ b/mlxtend/evaluate/f_test.py @@ -59,9 +59,7 @@ def ftest(y_target, *y_model_predictions): model_lens.add(ary.shape[0]) if len(model_lens) > 1: - raise ValueError( - "Each prediction array must have the " "same number of samples." - ) + raise ValueError("Each prediction array must have the same number of samples.") if num_models < 2: raise ValueError("Provide at least 2 model prediction arrays.") @@ -183,7 +181,7 @@ def combined_ftest_5x2cv(estimator1, estimator2, X, y, scoring=None, random_seed elif est_type == "regressor": scoring = "r2" else: - raise AttributeError("Estimator must " "be a Classifier or Regressor.") + raise AttributeError("Estimator must be a Classifier or Regressor.") if isinstance(scoring, str): scorer = get_scorer(scoring) else: diff --git a/mlxtend/evaluate/lift_score.py b/mlxtend/evaluate/lift_score.py index e6864363f..d8e34300c 100644 --- a/mlxtend/evaluate/lift_score.py +++ b/mlxtend/evaluate/lift_score.py @@ -57,7 +57,7 @@ def lift_score(y_target, y_predicted, binary=True, positive_label=1): if len(pred_tmp) != len(targ_tmp): raise AttributeError( - "`y_target` and `y_predicted`" "don't have the same number of elements." + "`y_target` and `y_predicted`don't have the same number of elements." ) if binary: targ_tmp = np.where(targ_tmp != positive_label, 0, 1) @@ -68,7 +68,7 @@ def lift_score(y_target, y_predicted, binary=True, positive_label=1): if len(binary_check_targ_tmp) or len(binary_check_pred_tmp): raise AttributeError( - "`y_target` and `y_predicted`" " have different elements from 0 and 1." + "`y_target` and `y_predicted` have different elements from 0 and 1." ) return support(targ_tmp, pred_tmp) / (support(targ_tmp) * support(pred_tmp)) diff --git a/mlxtend/evaluate/mcnemar.py b/mlxtend/evaluate/mcnemar.py index 9308e933c..7c316d7fd 100644 --- a/mlxtend/evaluate/mcnemar.py +++ b/mlxtend/evaluate/mcnemar.py @@ -45,12 +45,12 @@ def mcnemar_table(y_target, y_model1, y_model2): if y_target.shape[0] != y_model1.shape[0]: raise ValueError( - "y_target and y_model1 contain a different number" " of elements." + "y_target and y_model1 contain a different number of elements." ) if y_target.shape[0] != y_model2.shape[0]: raise ValueError( - "y_target and y_model2 contain a different number" " of elements." + "y_target and y_model2 contain a different number of elements." ) m1_vs_true = (y_target == y_model1).astype(int) @@ -132,9 +132,7 @@ def mcnemar_tables(y_target, *y_model_predictions): model_lens.add(ary.shape[0]) if len(model_lens) > 1: - raise ValueError( - "Each prediction array must have the " "same number of samples." - ) + raise ValueError("Each prediction array must have the same number of samples.") num_models = len(y_model_predictions) diff --git a/mlxtend/evaluate/permutation.py b/mlxtend/evaluate/permutation.py index 16c8b23c1..f33d40f37 100644 --- a/mlxtend/evaluate/permutation.py +++ b/mlxtend/evaluate/permutation.py @@ -86,9 +86,7 @@ def permutation_test( """ if method not in ("approximate", "exact"): - raise AttributeError( - 'method must be "approximate"' ' or "exact", got %s' % method - ) + raise AttributeError('method must be "approximate" or "exact", got %s' % method) if isinstance(func, str): if func not in ("x_mean != y_mean", "x_mean > y_mean", "x_mean < y_mean"): @@ -120,7 +118,7 @@ def func(x, y): if paired: if m != n: - raise ValueError("x and y must have the same" " length if `paired=True`") + raise ValueError("x and y must have the same length if `paired=True`") sample_x = np.empty(m) sample_y = np.empty(n) diff --git a/mlxtend/evaluate/scoring.py b/mlxtend/evaluate/scoring.py index fad8b3247..3c56fe93a 100644 --- a/mlxtend/evaluate/scoring.py +++ b/mlxtend/evaluate/scoring.py @@ -94,7 +94,7 @@ def scoring( if len(y_target) != len(y_predicted): raise AttributeError( - "`y_target` and `y_predicted`" " don't have the same number of elements." + "`y_target` and `y_predicted` don't have the same number of elements." ) if unique_labels == "auto": @@ -125,9 +125,7 @@ def scoring( else: if len(unique_labels) > 2 or len(np.unique(pred_tmp)) > 2: raise AttributeError( - "Metrics precision, " - "recall, and f1 only support binary" - " class labels" + "Metrics precision, recall, and f1 only support binary class labels" ) # `binary=True` makes sure diff --git a/mlxtend/evaluate/tests/test_cochran_q.py b/mlxtend/evaluate/tests/test_cochran_q.py index 543772eb3..6838874da 100644 --- a/mlxtend/evaluate/tests/test_cochran_q.py +++ b/mlxtend/evaluate/tests/test_cochran_q.py @@ -27,7 +27,7 @@ def test_input_array_lengths_1(): t2 = np.array([1, 2, 3]) assert_raises( ValueError, - ("Each prediction array must have" " the same number of samples."), + ("Each prediction array must have the same number of samples."), cochrans_q, t, t2, @@ -43,7 +43,7 @@ def test_model_have_same_len(): assert_raises( ValueError, - ("Each prediction array must have" " the same number of samples."), + ("Each prediction array must have the same number of samples."), cochrans_q, y_true, y_1, diff --git a/mlxtend/evaluate/tests/test_f_test.py b/mlxtend/evaluate/tests/test_f_test.py index 840cf6373..41790c83f 100644 --- a/mlxtend/evaluate/tests/test_f_test.py +++ b/mlxtend/evaluate/tests/test_f_test.py @@ -22,7 +22,7 @@ def test_input_array_lengths_1(): t2 = np.array([1, 2, 3]) assert_raises( ValueError, - ("Each prediction array must have" " the same number of samples."), + ("Each prediction array must have the same number of samples."), ftest, t, t2, @@ -38,7 +38,7 @@ def test_model_have_same_len(): assert_raises( ValueError, - ("Each prediction array must have" " the same number of samples."), + ("Each prediction array must have the same number of samples."), ftest, y_true, y_1, diff --git a/mlxtend/evaluate/tests/test_mcnemar_table.py b/mlxtend/evaluate/tests/test_mcnemar_table.py index 7868c29c8..52d7af89f 100644 --- a/mlxtend/evaluate/tests/test_mcnemar_table.py +++ b/mlxtend/evaluate/tests/test_mcnemar_table.py @@ -27,7 +27,7 @@ def test_input_array_lengths_1(): t2 = np.array([1, 2, 3]) assert_raises( ValueError, - ("y_target and y_model1 contain a different number" " of elements."), + ("y_target and y_model1 contain a different number of elements."), mcnemar_table, t, t2, @@ -40,7 +40,7 @@ def test_input_array_lengths_2(): t2 = np.array([1, 2, 3]) assert_raises( ValueError, - ("y_target and y_model2 contain a different number" " of elements."), + ("y_target and y_model2 contain a different number of elements."), mcnemar_table, t, t, diff --git a/mlxtend/evaluate/tests/test_mcnemar_tables.py b/mlxtend/evaluate/tests/test_mcnemar_tables.py index cc91148d9..c45258658 100644 --- a/mlxtend/evaluate/tests/test_mcnemar_tables.py +++ b/mlxtend/evaluate/tests/test_mcnemar_tables.py @@ -27,7 +27,7 @@ def test_input_array_lengths_1(): t2 = np.array([1, 2, 3]) assert_raises( ValueError, - ("Each prediction array must have" " the same number of samples."), + ("Each prediction array must have the same number of samples."), mcnemar_tables, t, t2, @@ -43,7 +43,7 @@ def test_model_have_same_len(): assert_raises( ValueError, - ("Each prediction array must have" " the same number of samples."), + ("Each prediction array must have the same number of samples."), mcnemar_tables, y_true, y_1, diff --git a/mlxtend/evaluate/tests/test_paired_ttest_resampled.py b/mlxtend/evaluate/tests/test_paired_ttest_resampled.py index e5b60967f..b1bfe4354 100644 --- a/mlxtend/evaluate/tests/test_paired_ttest_resampled.py +++ b/mlxtend/evaluate/tests/test_paired_ttest_resampled.py @@ -23,7 +23,7 @@ def test_train_size(): clf2 = DecisionTreeClassifier() expected_err_msg = ( - "train_size must be of type int or float. " "Got ." + "train_size must be of type int or float. Got ." ) if sys.version_info < (3, 0): diff --git a/mlxtend/evaluate/tests/test_permutation.py b/mlxtend/evaluate/tests/test_permutation.py index 972448d8f..b5b798522 100644 --- a/mlxtend/evaluate/tests/test_permutation.py +++ b/mlxtend/evaluate/tests/test_permutation.py @@ -95,5 +95,5 @@ def test_paired_runs_exact(): def test_paired_invalid_lengths(): - msg = "x and y must have the same" " length if `paired=True`" + msg = "x and y must have the same length if `paired=True`" assert_raises(ValueError, msg, permutation_test, [1, 2, 3], [3, 4], paired=True) diff --git a/mlxtend/evaluate/ttest.py b/mlxtend/evaluate/ttest.py index 8497a6aa4..a3c531bf7 100644 --- a/mlxtend/evaluate/ttest.py +++ b/mlxtend/evaluate/ttest.py @@ -92,7 +92,7 @@ def paired_ttest_resampled( """ if not isinstance(test_size, int) and not isinstance(test_size, float): raise ValueError( - "train_size must be of " "type int or float. Got %s." % type(test_size) + "train_size must be of type int or float. Got %s." % type(test_size) ) rng = np.random.RandomState(random_seed) @@ -104,7 +104,7 @@ def paired_ttest_resampled( elif est_type == "regressor": scoring = "r2" else: - raise AttributeError("Estimator must " "be a Classifier or Regressor.") + raise AttributeError("Estimator must be a Classifier or Regressor.") if isinstance(scoring, str): scorer = get_scorer(scoring) else: diff --git a/mlxtend/externals/adjust_text.py b/mlxtend/externals/adjust_text.py index ebb2675e9..59c0af842 100644 --- a/mlxtend/externals/adjust_text.py +++ b/mlxtend/externals/adjust_text.py @@ -478,7 +478,7 @@ def adjust_text( add_step_numbers=True, on_basemap=False, *args, - **kwargs + **kwargs, ): """Iteratively adjusts the locations of texts. @@ -761,7 +761,7 @@ def adjust_text( xytext=get_midpoint(bbox), arrowprops=ap, *args, - **kwargs + **kwargs, ) if save_steps: diff --git a/mlxtend/externals/pyprind/prog_class.py b/mlxtend/externals/pyprind/prog_class.py index b814b6a20..e8761242b 100755 --- a/mlxtend/externals/pyprind/prog_class.py +++ b/mlxtend/externals/pyprind/prog_class.py @@ -54,7 +54,7 @@ def __init__( if monitor: if not psutil_import: raise ValueError( - "psutil package is required when using" " the `monitor` option." + "psutil package is required when using the `monitor` option." ) else: self.process = psutil.Process() @@ -186,10 +186,9 @@ def __repr__(self): self._stream_flush() time_info = ( - "Title: {}\n" - " Started: {}\n" - " Finished: {}\n" - " Total time elapsed: ".format(self.title, str_start, str_end) + "Title: {}\n Started: {}\n Finished: {}\n Total time elapsed: ".format( + self.title, str_start, str_end + ) + self._get_time(self.total_time) ) if self.monitor: @@ -200,7 +199,7 @@ def __repr__(self): cpu_total = self.process.get_cpu_percent() mem_total = self.process.get_memory_percent() - cpu_mem_info = " CPU %: {:.2f}\n" " Memory %: {:.2f}".format( + cpu_mem_info = " CPU %: {:.2f}\n Memory %: {:.2f}".format( cpu_total, mem_total ) diff --git a/mlxtend/externals/signature_py27.py b/mlxtend/externals/signature_py27.py index b51910952..31602cf9c 100644 --- a/mlxtend/externals/signature_py27.py +++ b/mlxtend/externals/signature_py27.py @@ -4,6 +4,7 @@ Back port of Python 3.3's function signature tools from the inspect module, modified to be compatible with Python 2.7 and 3.2+. """ + from __future__ import absolute_import, division, print_function import functools @@ -259,7 +260,7 @@ def __init__( if name is None: if kind != _POSITIONAL_ONLY: raise ValueError( - "None is not a valid name for a " "non-positional-only parameter" + "None is not a valid name for a non-positional-only parameter" ) self._name = name else: @@ -760,8 +761,9 @@ def _bind(self, args, kwargs, partial=False): if param.name in kwargs: raise TypeError( - "multiple values for argument " - "{arg!r}".format(arg=param.name) + "multiple values for argument {arg!r}".format( + arg=param.name + ) ) arguments[param.name] = arg_val diff --git a/mlxtend/feature_selection/exhaustive_feature_selector.py b/mlxtend/feature_selection/exhaustive_feature_selector.py index fbc08d165..824c6efa4 100644 --- a/mlxtend/feature_selection/exhaustive_feature_selector.py +++ b/mlxtend/feature_selection/exhaustive_feature_selector.py @@ -598,6 +598,4 @@ def _calc_confidence(self, ary, confidence=0.95): def _check_fitted(self): if not self.fitted: - raise AttributeError( - "ExhaustiveFeatureSelector has not been" " fitted, yet." - ) + raise AttributeError("ExhaustiveFeatureSelector has not been fitted, yet.") diff --git a/mlxtend/feature_selection/sequential_feature_selector.py b/mlxtend/feature_selection/sequential_feature_selector.py index 835d7a3f9..f5d8b52c0 100644 --- a/mlxtend/feature_selection/sequential_feature_selector.py +++ b/mlxtend/feature_selection/sequential_feature_selector.py @@ -451,7 +451,7 @@ def fit(self, X, y, groups=None, **fit_params): and not isinstance(self.k_features, str) ): raise AttributeError( - "k_features must be a positive integer" ", tuple, or string" + "k_features must be a positive integer, tuple, or string" ) eligible_k_values_range = range(self.k_lb, self.k_ub + 1) @@ -906,6 +906,4 @@ def _calc_confidence(self, ary, confidence=0.95): def _check_fitted(self): if not self.fitted: - raise AttributeError( - "SequentialFeatureSelector has not been" " fitted, yet." - ) + raise AttributeError("SequentialFeatureSelector has not been fitted, yet.") diff --git a/mlxtend/feature_selection/tests/test_exhaustive_feature_selector.py b/mlxtend/feature_selection/tests/test_exhaustive_feature_selector.py index 627a6a917..d9b1ce64c 100644 --- a/mlxtend/feature_selection/tests/test_exhaustive_feature_selector.py +++ b/mlxtend/feature_selection/tests/test_exhaustive_feature_selector.py @@ -24,21 +24,21 @@ def dict_compare_utility(d1, d2, decimal=2): assert d1.keys() == d2.keys(), "%s != %s" % (d1, d2) for i in d1: - err_msg1 = "d1[%s]['feature_idx']" " != d2[%s]['feature_idx']" % (i, i) - err_msg2 = "d1[%s]['feature_names']" " != d2[%s]['feature_names']" % (i, i) + err_msg1 = "d1[%s]['feature_idx'] != d2[%s]['feature_idx']" % (i, i) + err_msg2 = "d1[%s]['feature_names'] != d2[%s]['feature_names']" % (i, i) assert d1[i]["feature_idx"] == d2[i]["feature_idx"], err_msg1 assert d1[i]["feature_names"] == d2[i]["feature_names"], err_msg2 assert_almost_equal( d1[i]["avg_score"], d2[i]["avg_score"], decimal=decimal, - err_msg=("d1[%s]['avg_score']" " != d2[%s]['avg_score']" % (i, i)), + err_msg=("d1[%s]['avg_score'] != d2[%s]['avg_score']" % (i, i)), ) assert_almost_equal( d1[i]["cv_scores"], d2[i]["cv_scores"], decimal=decimal, - err_msg=("d1[%s]['cv_scores']" " != d2[%s]['cv_scores']" % (i, i)), + err_msg=("d1[%s]['cv_scores'] != d2[%s]['cv_scores']" % (i, i)), ) diff --git a/mlxtend/feature_selection/tests/test_sequential_feature_selector.py b/mlxtend/feature_selection/tests/test_sequential_feature_selector.py index d97a1ea2a..7f1fdd18a 100644 --- a/mlxtend/feature_selection/tests/test_sequential_feature_selector.py +++ b/mlxtend/feature_selection/tests/test_sequential_feature_selector.py @@ -36,7 +36,7 @@ def nan_roc_auc_score(y_true, y_score, average="macro", sample_weight=None): def dict_compare_utility(d_actual, d_desired, decimal=2): assert d_actual.keys() == d_desired.keys(), "%s != %s" % (d_actual, d_desired) for i in d_actual: - err_msg = "d_actual[%s]['feature_idx']" " != d_desired[%s]['feature_idx']" % ( + err_msg = "d_actual[%s]['feature_idx'] != d_desired[%s]['feature_idx']" % ( i, i, ) @@ -46,7 +46,7 @@ def dict_compare_utility(d_actual, d_desired, decimal=2): desired=d_desired[i]["avg_score"], decimal=decimal, err_msg=( - "d_actual[%s]['avg_score']" " != d_desired[%s]['avg_score']" % (i, i) + "d_actual[%s]['avg_score'] != d_desired[%s]['avg_score']" % (i, i) ), ) assert_almost_equal( @@ -54,7 +54,7 @@ def dict_compare_utility(d_actual, d_desired, decimal=2): desired=d_desired[i]["cv_scores"], decimal=decimal, err_msg=( - "d_actual[%s]['cv_scores']" " != d_desired[%s]['cv_scores']" % (i, i) + "d_actual[%s]['cv_scores'] != d_desired[%s]['cv_scores']" % (i, i) ), ) @@ -126,9 +126,7 @@ def test_kfeatures_type_5(): X = iris.data y = iris.target knn = KNeighborsClassifier() - expect = ( - "The min k_features value must be smaller" " than the max k_features value." - ) + expect = "The min k_features value must be smaller than the max k_features value." sfs = SFS(estimator=knn, verbose=0, k_features=(3, 1)) assert_raises(AttributeError, expect, sfs.fit, X, y) diff --git a/mlxtend/feature_selection/tests/test_sequential_feature_selector_feature_groups.py b/mlxtend/feature_selection/tests/test_sequential_feature_selector_feature_groups.py index a91734813..cac9c793f 100644 --- a/mlxtend/feature_selection/tests/test_sequential_feature_selector_feature_groups.py +++ b/mlxtend/feature_selection/tests/test_sequential_feature_selector_feature_groups.py @@ -8,7 +8,6 @@ from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier from sklearn.linear_model import LinearRegression -from sklearn.metrics import roc_auc_score from sklearn.neighbors import KNeighborsClassifier from mlxtend.data import boston_housing_data @@ -19,7 +18,7 @@ def dict_compare_utility(d_actual, d_desired, decimal=2): assert d_actual.keys() == d_desired.keys(), "%s != %s" % (d_actual, d_desired) for i in d_actual: - err_msg = "d_actual[%s]['feature_idx']" " != d_desired[%s]['feature_idx']" % ( + err_msg = "d_actual[%s]['feature_idx'] != d_desired[%s]['feature_idx']" % ( i, i, ) @@ -29,7 +28,7 @@ def dict_compare_utility(d_actual, d_desired, decimal=2): desired=d_desired[i]["avg_score"], decimal=decimal, err_msg=( - "d_actual[%s]['avg_score']" " != d_desired[%s]['avg_score']" % (i, i) + "d_actual[%s]['avg_score'] != d_desired[%s]['avg_score']" % (i, i) ), ) assert_almost_equal( @@ -37,7 +36,7 @@ def dict_compare_utility(d_actual, d_desired, decimal=2): desired=d_desired[i]["cv_scores"], decimal=decimal, err_msg=( - "d_actual[%s]['cv_scores']" " != d_desired[%s]['cv_scores']" % (i, i) + "d_actual[%s]['cv_scores'] != d_desired[%s]['cv_scores']" % (i, i) ), ) diff --git a/mlxtend/frequent_patterns/association_rules.py b/mlxtend/frequent_patterns/association_rules.py index e6e2021f1..87d42b290 100644 --- a/mlxtend/frequent_patterns/association_rules.py +++ b/mlxtend/frequent_patterns/association_rules.py @@ -8,6 +8,7 @@ # # License: BSD 3 clause + from itertools import combinations from typing import Optional @@ -116,22 +117,23 @@ def association_rules( """ # if null values exist, df_orig must be provided + if null_values and df_orig is None: raise TypeError("If null values exist, df_orig must be provided.") - # if null values exist, num_itemsets must be provided + if null_values and num_itemsets == 1: raise TypeError("If null values exist, num_itemsets must be provided.") - # check for valid input + fpc.valid_input_check(df_orig, null_values) if not df.shape[0]: raise ValueError( - "The input DataFrame `df` containing " "the frequent itemsets is empty." + "The input DataFrame `df` containing the frequent itemsets is empty." ) - # check for mandatory columns + if not all(col in df.columns for col in ["support", "itemsets"]): raise ValueError( "Dataframe needs to contain the\ @@ -163,8 +165,8 @@ def zhangs_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): with np.errstate(divide="ignore", invalid="ignore"): # ignoring the divide by 0 warning since it is addressed in the below np.where - zhangs_metric = np.where(denominator == 0, 0, numerator / denominator) + zhangs_metric = np.where(denominator == 0, 0, numerator / denominator) return zhangs_metric def jaccard_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): @@ -187,6 +189,7 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): return cert_metric # metrics for association rules + metric_dict = { "antecedent support": lambda _, sA, ___, ____, _____, ______, _______, ________: sA, "consequent support": lambda _, __, sC, ____, _____, ______, _______, ________: sC, @@ -228,6 +231,7 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): } # check for metric compliance + if support_only: metric = "support" else: @@ -235,8 +239,8 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): raise ValueError( "Metric must be 'confidence' or 'lift', got '{}'".format(metric) ) - # get dict of {frequent itemset} -> support + keys = df["itemsets"].values values = df["support"].values frozenset_vect = np.vectorize( @@ -247,11 +251,13 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): frequent_items_dict = dict(zip(frozenset_vect(keys), values)) # prepare buckets to collect frequent rules + rule_antecedents = [] rule_consequents = [] rule_supports = [] # Define the disabled df, assign columns from original df to be the same on the disabled. + if null_values: first_itemset = next(iter(frequent_items_dict.keys())) df_orig = df_orig.copy() @@ -262,18 +268,19 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): disabled = pd.DataFrame(disabled) if all(isinstance(key, str) for key in first_itemset): disabled.columns = df_orig.columns - if all(isinstance(key, (np.integer, int)) for key in first_itemset): cols = np.arange(0, len(df_orig.columns), 1) disabled.columns = cols df_orig = df_orig.rename(columns=dict(zip(df_orig.columns, cols))) - # iterate over all frequent itemsets + for k in frequent_items_dict.keys(): sAC = frequent_items_dict[k] # to find all possible combinations + for idx in range(len(k) - 1, 0, -1): # of antecedent and consequent + for c in combinations(k, r=idx): antecedent = frozenset(c) consequent = k.difference(antecedent) @@ -281,65 +288,70 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): if support_only: # support doesn't need these, # hence, placeholders should suffice + sA = None sC = None disAC, disA, disC, dis_int, dis_int_ = 0, 0, 0, 0, 0 - else: try: sA = frequent_items_dict[antecedent] sC = frequent_items_dict[consequent] # if the input dataframe is complete + if not null_values: disAC, disA, disC, dis_int, dis_int_ = 0, 0, 0, 0, 0 - else: an = list(antecedent) con = list(consequent) an.extend(con) # select data of antecedent, consequent and combined from disabled + dec = disabled.loc[:, an] _dec = disabled.loc[:, list(antecedent)] __dec = disabled.loc[:, list(consequent)] # select data of antecedent and consequent from original + dec_ = df_orig.loc[:, list(antecedent)] dec__ = df_orig.loc[:, list(consequent)] # disabled counts + disAC, disA, disC, dis_int, dis_int_ = 0, 0, 0, 0, 0 for i in range(len(dec.index)): # select the i-th iset from the disabled dataset + item_comb = list(dec.iloc[i, :]) item_dis_an = list(_dec.iloc[i, :]) item_dis_con = list(__dec.iloc[i, :]) # select the i-th iset from the original dataset + item_or_an = list(dec_.iloc[i, :]) item_or_con = list(dec__.iloc[i, :]) # check and keep count if there is a null value in combined, antecedent, consequent + if 1 in set(item_comb): disAC += 1 if 1 in set(item_dis_an): disA += 1 if 1 in item_dis_con: disC += 1 - # check and keep count if there is a null value in consequent AND all items are present in antecedent + if (1 in item_dis_con) and all( j == 1 for j in item_or_an ): dis_int += 1 - # check and keep count if there is a null value in antecedent AND all items are present in consequent + if (1 in item_dis_an) and all( j == 1 for j in item_or_con ): dis_int_ += 1 - except KeyError as e: s = ( str(e) + "You are likely getting this error" @@ -351,7 +363,6 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): ) raise KeyError(s) # check for the threshold - score = metric_dict[metric]( sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_ ) @@ -361,13 +372,13 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): rule_supports.append( [sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_] ) - # check if frequent rule was generated + if not rule_supports: return pd.DataFrame(columns=["antecedents", "consequents"] + return_metrics) - else: # generate metrics + rule_supports = np.array(rule_supports).T.astype(float) df_res = pd.DataFrame( data=list(zip(rule_antecedents, rule_consequents)), @@ -379,7 +390,6 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): for m in return_metrics: df_res[m] = np.nan df_res["support"] = sAC - else: sAC = rule_supports[0] sA = rule_supports[1] @@ -394,5 +404,4 @@ def certainty_metric_helper(sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_): df_res[m] = metric_dict[m]( sAC, sA, sC, disAC, disA, disC, dis_int, dis_int_ ) - return df_res diff --git a/mlxtend/frequent_patterns/fpcommon.py b/mlxtend/frequent_patterns/fpcommon.py index 858902749..b1fa13724 100644 --- a/mlxtend/frequent_patterns/fpcommon.py +++ b/mlxtend/frequent_patterns/fpcommon.py @@ -3,7 +3,6 @@ import numpy as np import pandas as pd -from pandas import __version__ as pandas_version warnings.simplefilter("always", DeprecationWarning) diff --git a/mlxtend/frequent_patterns/tests/test_fpbase.py b/mlxtend/frequent_patterns/tests/test_fpbase.py index 3d9bedd30..4dbcc0816 100644 --- a/mlxtend/frequent_patterns/tests/test_fpbase.py +++ b/mlxtend/frequent_patterns/tests/test_fpbase.py @@ -10,7 +10,6 @@ import numpy as np import pandas as pd -from numpy.testing import assert_array_equal from packaging.version import Version from pandas import __version__ as pandas_version from scipy.sparse import csr_matrix diff --git a/mlxtend/plotting/decision_regions.py b/mlxtend/plotting/decision_regions.py index 5efb32560..dd2e16b31 100644 --- a/mlxtend/plotting/decision_regions.py +++ b/mlxtend/plotting/decision_regions.py @@ -283,9 +283,10 @@ def plot_decision_regions( partQuant = len(X_predict) / cpus partitions = [] for n in range(cpus - 1): - start, end = np.floor(partQuant * n).astype(int), np.floor( - partQuant * (n + 1) - ).astype(int) + start, end = ( + np.floor(partQuant * n).astype(int), + np.floor(partQuant * (n + 1)).astype(int), + ) partitions.append(X_predict[start:end]) partitions.append(X_predict[end:]) xtype = X.dtype @@ -349,7 +350,7 @@ def plot_decision_regions( c=colors[idx], marker=next(marker_gen), label=c, - **scatter_kwargs + **scatter_kwargs, ) if hide_spines: diff --git a/mlxtend/plotting/pca_correlation_graph.py b/mlxtend/plotting/pca_correlation_graph.py index c59d38ec6..b3e3cd59e 100644 --- a/mlxtend/plotting/pca_correlation_graph.py +++ b/mlxtend/plotting/pca_correlation_graph.py @@ -3,6 +3,7 @@ # A function for plotting a PCA correlation circle # File Author: Gabriel Azevedo Ferreira + import matplotlib.pyplot as plt import numpy as np import pandas as pd @@ -17,14 +18,17 @@ def corr2_coeff(A, B): """ A, B = np.array(A), np.array(B) # Rowwise mean of input arrays & subtract from input arrays themeselves + A_mA = A - A.mean(1)[:, None] B_mB = B - B.mean(1)[:, None] # Sum of squares across rows + ssA = (A_mA**2).sum(1) ssB = (B_mB**2).sum(1) # Finally get corr coeff + return np.dot(A_mA, B_mB.T) / np.sqrt(np.dot(ssA[:, None], ssB[None])) @@ -42,6 +46,7 @@ def create_correlation_table(A, B, names_cols_A, names_cols_B): """ # corrs = np.corrcoef(np.transpose(A), np.transpose(B) # )[len(names_cols_A):, :len(names_cols_A)] + corrs = corr2_coeff(A.T, B.T).T df_corrs = pd.DataFrame(corrs, columns=names_cols_A, index=names_cols_B) @@ -109,19 +114,16 @@ def plot_pca_correlation_graph( pca.fit(X) X_pca = pca.transform(X) explained_variance = pca.e_vals_ - elif (X_pca is not None) and (explained_variance is None): raise ValueError( "If `X_pca` is not None, the `explained variance`" " values should not be `None`." ) - elif (X_pca is None) and (explained_variance is not None): raise ValueError( "If `explained variance` is not None, the `X_pca`" " values should not be `None`." ) - elif (X_pca is not None) and (explained_variance is not None): if X_pca.shape[1] != len(explained_variance): raise ValueError( @@ -132,7 +134,6 @@ def plot_pca_correlation_graph( f"!= " f"{len(explained_variance)}" ) - if X_pca.shape[1] < n_comp: raise ValueError( f"Input array `X_pca` contains fewer principal" @@ -147,14 +148,16 @@ def plot_pca_correlation_graph( f" {len(explained_variance)} elements, expected" f"`X.shape[1]={X.shape[1]}`." ) - corrs = create_correlation_table( X_pca, X, ["Dim " + str(i + 1) for i in range(n_comp)], variables_names ) + corrs = corrs * -1 + tot = sum(X.var(0)) * X.shape[0] / (X.shape[0] - 1) explained_var_ratio = [(i / tot) * 100 for i in explained_variance] # Plotting circle + fig_res = plt.figure(figsize=(figure_axis_size, figure_axis_size)) plt.Circle((0, 0), radius=1, color="k", fill=False) circle1 = plt.Circle((0, 0), radius=1, color="k", fill=False) @@ -162,6 +165,7 @@ def plot_pca_correlation_graph( fig.gca().add_artist(circle1) # Plotting arrows + texts = [] for name, row in corrs.iterrows(): x = row["Dim " + str(dimensions[0])] @@ -171,12 +175,15 @@ def plot_pca_correlation_graph( plt.plot([0.0, x], [0.0, y], "k-") texts.append(plt.text(x, y, name, fontsize=2 * figure_axis_size)) # Plotting vertical lines + plt.plot([-1.1, 1.1], [0, 0], "k--") plt.plot([0, 0], [-1.1, 1.1], "k--") # Adjusting text + adjust_text(texts) # Setting limits and title + plt.xlim((-1.1, 1.1)) plt.ylim((-1.1, 1.1)) plt.title("Correlation Circle", fontsize=figure_axis_size * 3) diff --git a/mlxtend/plotting/plot_confusion_matrix.py b/mlxtend/plotting/plot_confusion_matrix.py index b1052ea0d..bb6bcc59a 100644 --- a/mlxtend/plotting/plot_confusion_matrix.py +++ b/mlxtend/plotting/plot_confusion_matrix.py @@ -95,7 +95,7 @@ def plot_confusion_matrix( raise AssertionError("Both show_absolute and show_normed are False") if class_names is not None and len(class_names) != len(conf_mat): raise AssertionError( - "len(class_names) should be equal to number of" "classes in the dataset" + "len(class_names) should be equal to number ofclasses in the dataset" ) total_samples = conf_mat.sum(axis=1)[:, np.newaxis] diff --git a/mlxtend/plotting/scatter_hist.py b/mlxtend/plotting/scatter_hist.py index 9b1e6e946..a455c658a 100644 --- a/mlxtend/plotting/scatter_hist.py +++ b/mlxtend/plotting/scatter_hist.py @@ -4,8 +4,6 @@ # # License: BSD 3 clause import matplotlib.pyplot as plt -import numpy as np -import pandas as pd def scatter_hist(x, y, xlabel=None, ylabel=None, figsize=(5, 5)): diff --git a/mlxtend/plotting/tests/test_decision_regions.py b/mlxtend/plotting/tests/test_decision_regions.py index 1690a0a74..482eddbe4 100644 --- a/mlxtend/plotting/tests/test_decision_regions.py +++ b/mlxtend/plotting/tests/test_decision_regions.py @@ -38,7 +38,7 @@ def test_filler_feature_values_fail(): sr.fit(X, y) assert_raises( ValueError, - "Filler values must be provided when " "X has more than 2 training features.", + "Filler values must be provided when X has more than 2 training features.", plot_decision_regions, X, y, @@ -114,9 +114,7 @@ def test_y_ary_dim(): def test_scatter_kwargs_type(): kwargs = "not a dictionary" sr.fit(X[:, :2], y) - message = "d must be of type dict or None, but got " "{} instead".format( - type(kwargs) - ) + message = "d must be of type dict or None, but got {} instead".format(type(kwargs)) assert_raises( TypeError, message, @@ -131,9 +129,7 @@ def test_scatter_kwargs_type(): def test_contourf_kwargs_type(): kwargs = "not a dictionary" sr.fit(X[:, :2], y) - message = "d must be of type dict or None, but got " "{} instead".format( - type(kwargs) - ) + message = "d must be of type dict or None, but got {} instead".format(type(kwargs)) assert_raises( TypeError, message, @@ -148,9 +144,7 @@ def test_contourf_kwargs_type(): def test_scatter_highlight_kwargs_type(): kwargs = "not a dictionary" sr.fit(X[:, :2], y) - message = "d must be of type dict or None, but got " "{} instead".format( - type(kwargs) - ) + message = "d must be of type dict or None, but got {} instead".format(type(kwargs)) assert_raises( TypeError, message, diff --git a/mlxtend/plotting/tests/test_pca_corr_graph.py b/mlxtend/plotting/tests/test_pca_corr_graph.py index 1862626f2..235b9d99c 100644 --- a/mlxtend/plotting/tests/test_pca_corr_graph.py +++ b/mlxtend/plotting/tests/test_pca_corr_graph.py @@ -5,7 +5,7 @@ from mlxtend.data import iris_data from mlxtend.plotting import plot_pca_correlation_graph -if Version(sklearn_version) < "0.22": +if Version(sklearn_version) < Version("0.22"): from sklearn.decomposition.pca import PCA else: from sklearn.decomposition import PCA diff --git a/mlxtend/preprocessing/scaling.py b/mlxtend/preprocessing/scaling.py index e352282cd..1ad173b4a 100644 --- a/mlxtend/preprocessing/scaling.py +++ b/mlxtend/preprocessing/scaling.py @@ -44,7 +44,7 @@ def minmax_scaling(array, columns, min_val=0, max_val=1): elif isinstance(ary_new, np.ndarray): ary_newt = ary_new else: - raise AttributeError("Input array must be a pandas" "DataFrame or NumPy array") + raise AttributeError("Input array must be a pandasDataFrame or NumPy array") numerator = ary_newt[:, columns] - ary_newt[:, columns].min(axis=0) denominator = ary_newt[:, columns].max(axis=0) - ary_newt[:, columns].min(axis=0) @@ -113,7 +113,7 @@ def standardize(array, columns=None, ddof=0, return_params=False, params=None): columns = list(range(ary_new.shape[1])) else: - raise AttributeError("Input array must be a pandas " "DataFrame or NumPy array") + raise AttributeError("Input array must be a pandas DataFrame or NumPy array") if params is not None: parameters = params diff --git a/mlxtend/preprocessing/tests/test_copy_transformer.py b/mlxtend/preprocessing/tests/test_copy_transformer.py index 23265bdff..6e4d8893c 100644 --- a/mlxtend/preprocessing/tests/test_copy_transformer.py +++ b/mlxtend/preprocessing/tests/test_copy_transformer.py @@ -33,7 +33,7 @@ def test_copy_failtype(): copy = CopyTransformer() expect = ( - "X must be a list or NumPy array or SciPy sparse array." " Found " + "X must be a list or NumPy array or SciPy sparse array. Found " ) if sys.version_info < (3, 0): expect = expect.replace("class", "type") diff --git a/mlxtend/utils/checking.py b/mlxtend/utils/checking.py index 38ef1e635..caadc016e 100644 --- a/mlxtend/utils/checking.py +++ b/mlxtend/utils/checking.py @@ -63,7 +63,7 @@ def format_kwarg_dictionaries( for d in [default_kwargs, user_kwargs]: if not isinstance(d, (dict, type(None))): raise TypeError( - "d must be of type dict or None, but " "got {} instead".format(type(d)) + "d must be of type dict or None, but got {} instead".format(type(d)) ) if d is not None: formatted_kwargs.update(d) diff --git a/mlxtend/utils/tests/test_checking_inputs.py b/mlxtend/utils/tests/test_checking_inputs.py index 807aaf2aa..e916fdccc 100644 --- a/mlxtend/utils/tests/test_checking_inputs.py +++ b/mlxtend/utils/tests/test_checking_inputs.py @@ -88,7 +88,7 @@ def test_check_Xy_invalid_dim_X(): def test_check_Xy_unequal_length_X(): assert_raises( ValueError, - ("y and X must contain the same number of samples. " "Got y: 4, X: 3"), + ("y and X must contain the same number of samples. Got y: 4, X: 3"), check_Xy, X[1:], y, @@ -98,7 +98,7 @@ def test_check_Xy_unequal_length_X(): def test_check_Xy_unequal_length_y(): assert_raises( ValueError, - ("y and X must contain the same number of samples. " "Got y: 3, X: 4"), + ("y and X must contain the same number of samples. Got y: 3, X: 4"), check_Xy, X, y[1:], @@ -132,7 +132,7 @@ def test_format_kwarg_dictionaries_no_user_kwargs(): def test_format_kwarg_dictionaries_default_kwargs_invalid_type(): invalid_kwargs = "not a dictionary" - message = "d must be of type dict or None, but got " "{} instead".format( + message = "d must be of type dict or None, but got {} instead".format( type(invalid_kwargs) ) assert_raises( @@ -142,7 +142,7 @@ def test_format_kwarg_dictionaries_default_kwargs_invalid_type(): def test_format_kwarg_dictionaries_user_kwargs_invalid_type(): invalid_kwargs = "not a dictionary" - message = "d must be of type dict or None, but got " "{} instead".format( + message = "d must be of type dict or None, but got {} instead".format( type(invalid_kwargs) ) assert_raises(