Repository: DistrictDataLabs/yellowbrick

Issue:
Update Model Selection Tutorial example to use Yellowbrick datasets/download module
The model selection tutorial datasets do not currently the Yellowbrick hosted and modified datasets. For consistency, examples should leverage the core sets as per the 'example datasets' documentation http://www.scikit-yb.org/en/latest/api/datasets.html. 

- [x] Modify docs on model selection tutorial to align with the example datasets documentation


Discussion:
related to this documentation issue: https://github.com/DistrictDataLabs/yellowbrick/issues/441
This is a duplicate issue of https://github.com/DistrictDataLabs/yellowbrick/issues/424

Not going to close either issue, but @joelvanveluwen please note that I reworked the datasets api in https://github.com/DistrictDataLabs/yellowbrick/pull/501


Resolving patch:
diff --git a/docs/tutorial.py b/docs/tutorial.py
--- a/docs/tutorial.py
+++ b/docs/tutorial.py
@@ -1,111 +1,87 @@
 #!/usr/bin/env python
 # Generate the classification report images for the tutorial
 
-import os
-import pandas as pd
 import matplotlib.pyplot as plt
 
-from yellowbrick.classifier import ClassificationReport
-
 from sklearn.pipeline import Pipeline
-from sklearn.base import BaseEstimator, TransformerMixin
-from sklearn.preprocessing import LabelEncoder, OneHotEncoder
-
 from sklearn.svm import LinearSVC, NuSVC, SVC
 from sklearn.neighbors import KNeighborsClassifier
+from sklearn.preprocessing import OneHotEncoder, LabelEncoder
 from sklearn.linear_model import LogisticRegressionCV, LogisticRegression, SGDClassifier
 from sklearn.ensemble import BaggingClassifier, ExtraTreesClassifier, RandomForestClassifier
-
-
-DATA = os.path.join(
-    os.path.dirname(__file__),  "..", "examples", "data", "mushroom", "mushroom.csv"
-)
+ 
+from yellowbrick.datasets import load_mushroom
+from yellowbrick.classifier import ClassificationReport
 
 
 ESTIMATORS = {
-    LinearSVC: "images/tutorial/modelselect_linear_svc.png",
-    NuSVC: "images/tutorial/modelselect_nu_svc.png",
-    SVC: "images/tutorial/modelselect_svc.png",
-    SGDClassifier: "images/tutorial/modelselect_sgd_classifier.png",
-    KNeighborsClassifier: "images/tutorial/modelselect_kneighbors_classifier.png",
-    LogisticRegressionCV: "images/tutorial/modelselect_logistic_regression_cv.png",
-    LogisticRegression: "images/tutorial/modelselect_logistic_regression.png",
-    BaggingClassifier: "images/tutorial/modelselect_bagging_classifier.png",
-    ExtraTreesClassifier: "images/tutorial/modelselect_extra_trees_classifier.png",
-    RandomForestClassifier: "images/tutorial/modelselect_random_forest_classifier.png",
+    'SVC': {
+        'model': SVC(gamma='auto'), 
+        'path': 'images/tutorial/modelselect_svc.png'
+    },
+    'NuSVC': {
+        'model': NuSVC(gamma='auto'), 
+        'path': 'images/tutorial/modelselect_nu_svc.png'
+    },
+    'LinearSVC': {
+        'model': LinearSVC(), 
+        'path': 'images/tutorial/modelselect_linear_svc.png'
+    },
+    'SGD': {
+        'model': SGDClassifier(max_iter=100, tol=1e-3), 
+        'path': 'images/tutorial/modelselect_sgd_classifier.png'
+    },
+    'KNN': {
+        'model': KNeighborsClassifier(), 
+        'path': 'images/tutorial/modelselect_kneighbors_classifier.png'
+    },
+    'LR': {
+        'model': LogisticRegression(solver='lbfgs'), 
+        'path': 'images/tutorial/modelselect_logistic_regression.png'
+    },
+    'LRCV': {
+        'model': LogisticRegressionCV(cv=3), 
+        'path': 'images/tutorial/modelselect_logistic_regression_cv.png'
+    },
+    'Bags': {
+        'model': BaggingClassifier(), 
+        'path': 'images/tutorial/modelselect_bagging_classifier.png'
+    },
+    'XTrees': {
+        'model': ExtraTreesClassifier(n_estimators=100), 
+        'path': 'images/tutorial/modelselect_extra_trees_classifier.png'
+    },
+    'RF': {
+        'model': RandomForestClassifier(n_estimators=100), 
+        'path': 'images/tutorial/modelselect_random_forest_classifier.png'
+    },
 }
 
 
-
-class EncodeCategorical(BaseEstimator, TransformerMixin):
-    """
-    Encodes a specified list of columns or all columns if None.
-    """
-
-    def __init__(self, columns=None):
-        self.columns  = [col for col in columns]
-        self.encoders = None
-
-    def fit(self, data, target=None):
-        """
-        Expects a data frame with named columns to encode.
-        """
-        # Encode all columns if columns is None
-        if self.columns is None:
-            self.columns = data.columns
-
-        # Fit a label encoder for each column in the data frame
-        self.encoders = {
-            column: LabelEncoder().fit(data[column])
-            for column in self.columns
-        }
-        return self
-
-    def transform(self, data):
-        """
-        Uses the encoders to transform a data frame.
-        """
-        output = data.copy()
-        for column, encoder in self.encoders.items():
-            output[column] = encoder.transform(data[column])
-
-        return output
-
-
-def load_data(path=DATA):
-    dataset  = pd.read_csv(path)
-    features = ['shape', 'surface', 'color']
-    target   = ['target']
-
-    X = dataset[features]
-    y = dataset[target]
-
-    y = LabelEncoder().fit_transform(y.values.ravel())
-
-    return X, y
-
-
-def visual_model_selection(X, y, estimator, path):
+def visualize_model(X, y, estimator, path, **kwargs):
     """
     Test various estimators.
-    """
+    """ 
+    y = LabelEncoder().fit_transform(y)
     model = Pipeline([
-         ('label_encoding', EncodeCategorical(X.keys())),
-         ('one_hot_encoder', OneHotEncoder()),
+         ('one_hot_encoder', OneHotEncoder()), 
          ('estimator', estimator)
     ])
 
     _, ax = plt.subplots()
 
     # Instantiate the classification model and visualizer
-    visualizer = ClassificationReport(model, ax=ax, classes=['edible', 'poisonous'])
-    visualizer.fit(X, y)
+    visualizer = ClassificationReport(
+        model, classes=['edible', 'poisonous'], 
+        cmap="YlGn", size=(600, 360), ax=ax, **kwargs
+    )
+    visualizer.fit(X, y)  
     visualizer.score(X, y)
-    visualizer.poof(outpath=path)
+    visualizer.poof(outpath=path)  
 
 
 if __name__ == '__main__':
-    X, y = load_data()
+    X, y = load_mushroom()
 
-    for clf, path in ESTIMATORS.items():
-        visual_model_selection(X, y, clf(), path)
+    for clf in ESTIMATORS.values():
+        visualize_model(X, y, clf['model'], clf['path'])


Test patch: