Instruction:
Create a Python 3 function to
create an internal transformer pipeline that projects the data set into the 2D space using UMAP.

Response:
def make_transformer(self, umap_kwargs={}):
        """
        Creates an internal transformer pipeline to project the data set into
        2D space using UMAP. This method will reset the transformer on the
        class.

        Parameters
        ----------

        Returns
        -------
        transformer : Pipeline
            Pipelined transformer for UMAP projections
        """

        # Create the pipeline steps
        steps = []

        # Add the UMAP manifold
        steps.append(('umap', UMAP(
            n_components=2, random_state=self.random_state, **umap_kwargs)))

        # return the pipeline
        return Pipeline(steps)


Instruction:
Can you generate the documentation for the following Python 3 function
def fit(self, X, y=None, **kwargs):

        # Store the classes we observed in y
        if y is not None:
            self.classes_ = np.unique(y)
        elif y is None and self.labels is not None:
            self.classes_ = np.array([self.labels[0]])
        else:
            self.classes_ = np.array([self.NULL_CLASS])

        # Fit our internal transformer and transform the data.
        vecs = self.transformer_.fit_transform(X)
        self.n_instances_ = vecs.shape[0]

        # Draw the vectors
        self.draw(vecs, y, **kwargs)

        # Fit always returns self.
        return self

Response: Fit the UMAP to the data X and draw the vectors and return the instance of the UMAP class.


Instruction:
Explain what the following Python 3 code does
def draw(self, points, target=None, **kwargs):
        # Resolve the labels with the classes
        labels = self.labels if self.labels is not None else self.classes_
        if len(labels) != len(self.classes_):
            raise YellowbrickValueError((
                "number of supplied labels ({}) does not "
                "match the number of classes ({})"
            ).format(len(labels), len(self.classes_)))

        # Create the color mapping for the labels.
        self.color_values_ = resolve_colors(
            n_colors=len(labels), colormap=self.colormap, colors=self.colors)
        colors = dict(zip(labels, self.color_values_))

        # Transform labels into a map of class to label
        labels = dict(zip(self.classes_, labels))

        # Expand the points into vectors of x and y for scatter plotting,
        # assigning them to their label if the label has been passed in.
        # Additionally, filter classes not specified directly by the user.
        series = defaultdict(lambda: {'x':[], 'y':[]})

        if target is not None:
            for t, point in zip(target, points):
                label = labels[t]
                series[label]['x'].append(point[0])
                series[label]['y'].append(point[1])
        else:
            label = self.classes_[0]
            for x,y in points:
                series[label]['x'].append(x)
                series[label]['y'].append(y)

        # Plot the points
        for label, points in series.items():
            self.ax.scatter(
                points['x'], points['y'], c=colors[label],
                alpha=self.alpha, label=label
            )

Response: This method draws the UMAP scatter plot from a set of decomposed points.


Instruction:
Create a Python 3 function to
generate an explained variance plot of the dimensionality reduction algorithm using n = 1 to n_components dimensions. This is a single plot that shows the best trade off between the number of features retained within the data.

Response:
def explained_variance_visualizer(X, y=None, ax=None, scale=True,
                                  center=True, colormap=palettes.DEFAULT_SEQUENCE,
                                  **kwargs):
        """Produce a plot of the explained variance produced by a dimensionality
        reduction algorithm using n=1 to n=n_components dimensions. This is a single
        plot to help identify the best trade off between number of dimensions
        and amount of information retained within the data.

        Parameters
        ----------
        X : ndarray or DataFrame of shape n x m
            A matrix of n rows with m features

        y : ndarray or Series of length n
            An array or Series of target or class values

        ax : matplotlib Axes, default: None
            The aces to plot the figure on

        scale : bool, default: True
            Boolean that indicates if the values of X should be scaled.

        colormap : string or cmap, default: None
            optional string or matplotlib cmap to colorize lines
            Use either color to colorize the lines on a per class basis or
            colormap to color them on a continuous scale.

        kwargs : dict
            Keyword arguments that are passed to the base class and may influence
            the visualization as defined in other Visualizers.

        Examples
        --------
        >>> from sklearn import datasets
        >>> bc = datasets.load_breast_cancer()
        >>> X = bc = bc.data
        >>> explained_variance_visualizer(X, scale=True, center=True, colormap='RdBu_r')

        """

        # Instantiate the visualizer
        visualizer = ExplainedVariance(X=X)

        # Fit and transform the visualizer (calls draw)
        visualizer.fit(X, y, **kwargs)
        visualizer.transform(X)

        # Return the axes object on the visualizer
        return visualizer.poof()


Instruction:
Make a summary of the following Python 3 code
def roc_auc(model, X, y=None, ax=None, **kwargs):
    # Instantiate the visualizer
    visualizer = ROCAUC(model, ax, **kwargs)

    # Create the train and test splits
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X_train, y_train, **kwargs)
    visualizer.score(X_test, y_test)
    visualizer.finalize()

    # Return the axes object on the visualizer
    return visualizer.ax

Response: This function is used to plot ROCAUC curves for the classifier with the same target vector and the same class. The ROC curves are plotted from the sum of all true positives and false positives across all classes.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def score(self, X, y=None, **kwargs):

        # Compute the predictions for the test data
        y_pred = self._get_y_scores(X)

        # Note: In the above, _get_y_scores calls either a decision_function or
        # predict_proba, which should return a 2D array. But in a binary
        # classification using an estimator with only a decision_function, y_pred
        # will instead be 1D, meaning only one curve can be plotted. In this case,
        # we set the _binary_decision attribute to True to ensure only one curve is
        # computed and plotted later on.
        if y_pred.ndim == 1:
            self._binary_decision = True

            # Raise an error if it's a binary decision and user has set micro,
            # macro, or per_class to True
            if self.micro or self.macro or self.per_class:
                raise ModelError(
                    "Micro, macro, and per-class scores are not defined for "
                    "binary classification for estimators with only "
                    "decision_function methods; set micro, macro, and "
                    "per-class params to False."
                )
        else:
            self._binary_decision = False
            # If it's not a binary decision, at least one of micro, macro, or
            # per_class must be True
            if not self.micro and not self.macro and not self.per_class:
                raise YellowbrickValueError(
                    "no curves will be drawn; specify micro, macro, or per_class"
                )

        # Classes may be label encoded so only use what's in y to compute.
        # The self.classes_ attribute will be used as names for labels.
        classes = np.unique(y)
        n_classes = len(classes)

        # Store the false positive rate, true positive rate and curve info.
        self.fpr = dict()
        self.tpr = dict()
        self.roc_auc = dict()

        # If the decision is binary, compute the ROC curve and ROC area
        if self._binary_decision == True:
            self.fpr[0], self.tpr[0], _ = roc_curve(y, y_pred)
            self.roc_auc[0] = auc(self.fpr[0], self.tpr[0])
        else:
            # Otherwise compute the ROC curve and ROC area for each class
            for i, c in enumerate(classes):
                self.fpr[i], self.tpr[i], _ = roc_curve(y, y_pred[:,i], pos_label=c)
                self.roc_auc[i] = auc(self.fpr[i], self.tpr[i])

        # Compute micro average
        if self.micro:
            self._score_micro_average(y, y_pred, classes, n_classes)

        # Compute macro average
        if self.macro:
            self._score_macro_average(n_classes)

        # Draw the Curves
        self.draw()

        # Set score to micro average if specified
        if self.micro:
            self.score_ = self.roc_auc[MICRO]

        # Set score to macro average if not micro
        if self.macro:
            self.score_ = self.roc_auc[MACRO]

        # Set score to the base score if neither macro nor micro
        self.score_ = self.estimator.score(X, y)

        return self.score_

Response: Generates the predicted target values using the Scikit - Learn - Critical Log - Learning estimator.


Instruction:
How would you code a function in Python 3 to
render the ROC curves for the current class and returns the plotted figure.

Response:
def draw(self):
        """
        Renders ROC-AUC plot.
        Called internally by score, possibly more than once

        Returns
        -------
        ax : the axis with the plotted figure
        """
        colors = self.colors[0:len(self.classes_)]
        n_classes = len(colors)

        # If it's a binary decision, plot the single ROC curve
        if self._binary_decision == True:
            self.ax.plot(
                self.fpr[0], self.tpr[0],
                label='ROC for binary decision, AUC = {:0.2f}'.format(
                        self.roc_auc[0]
                )
            )

        # If per-class plotting is requested, plot ROC curves for each class
        if self.per_class:
            for i, color in zip(range(n_classes), colors):
                self.ax.plot(
                    self.fpr[i], self.tpr[i], color=color,
                    label='ROC of class {}, AUC = {:0.2f}'.format(
                        self.classes_[i], self.roc_auc[i],
                    )
                )

        # If requested, plot the ROC curve for the micro average
        if self.micro:
            self.ax.plot(
                self.fpr[MICRO], self.tpr[MICRO], linestyle="--",
                color= self.colors[len(self.classes_)-1],
                label='micro-average ROC curve, AUC = {:0.2f}'.format(
                    self.roc_auc["micro"],
                )
            )

        # If requested, plot the ROC curve for the macro average
        if self.macro:
            self.ax.plot(
                self.fpr[MACRO], self.tpr[MACRO], linestyle="--",
                color= self.colors[len(self.classes_)-1],
                label='macro-average ROC curve, AUC = {:0.2f}'.format(
                    self.roc_auc["macro"],
                )
            )

        # Plot the line of no discrimination to compare the curve to.
        self.ax.plot([0,1], [0,1], linestyle=':', c=LINE_COLOR)
        return self.ax


Instruction:
Can you tell what is the following Python 3 function doing
def finalize(self, **kwargs):
        # Set the title and add the legend
        self.set_title('ROC Curves for {}'.format(self.name))
        self.ax.legend(loc='lower right', frameon=True)

        # Set the limits for the ROC/AUC (always between 0 and 1)
        self.ax.set_xlim([0.0, 1.0])
        self.ax.set_ylim([0.0, 1.0])

        # Set x and y axis labels
        self.ax.set_ylabel('True Postive Rate')
        self.ax.set_xlabel('False Positive Rate')

Response: Executes any subclass - specific axes finalization steps.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_y_scores(self, X):
        # The resolution order of scoring functions
        attrs = (
            'predict_proba',
            'decision_function',
        )

        # Return the first resolved function
        for attr in attrs:
            try:
                method = getattr(self.estimator, attr, None)
                if method:
                    return method(X)
            except AttributeError:
                # Some Scikit-Learn estimators have both probability and
                # decision functions but override __getattr__ and raise an
                # AttributeError on access.
                # Note that because of the ordering of our attrs above,
                # estimators with both will *only* ever use probability.
                continue

        # If we've gotten this far, raise an error
        raise ModelError(
            "ROCAUC requires estimators with predict_proba or "
            "decision_function methods."
        )

Response: This method computes the target scores for the ROCAUC metric.


Instruction:
Given the following Python 3 function, write the documentation
def _score_micro_average(self, y, y_pred, classes, n_classes):
        # Convert y to binarized array for micro and macro scores
        y = label_binarize(y, classes=classes)
        if n_classes == 2:
            y = np.hstack((1-y, y))

        # Compute micro-average
        self.fpr[MICRO], self.tpr[MICRO], _ = roc_curve(y.ravel(), y_pred.ravel())
        self.roc_auc[MICRO] = auc(self.fpr[MICRO], self.tpr[MICRO])

Response: Compute the micro average scores for the ROCAUC curves.


Instruction:
Given the following Python 3 function, write the documentation
def _score_macro_average(self, n_classes):
        # Gather all FPRs
        all_fpr = np.unique(np.concatenate([self.fpr[i] for i in range(n_classes)]))
        avg_tpr = np.zeros_like(all_fpr)

        # Compute the averages per class
        for i in range(n_classes):
            avg_tpr += interp(all_fpr, self.fpr[i], self.tpr[i])

        # Finalize the average
        avg_tpr /= n_classes

        # Store the macro averages
        self.fpr[MACRO] = all_fpr
        self.tpr[MACRO] = avg_tpr
        self.roc_auc[MACRO] = auc(self.fpr[MACRO], self.tpr[MACRO])

Response: Compute the macro average scores for the ROCAUC curves.


Instruction:
Explain what the following Python 3 code does
def is_estimator(model):
    if inspect.isclass(model):
        return issubclass(model, BaseEstimator)

    return isinstance(model, BaseEstimator)

Response: Determines if a model is an estimator using issubclass and isinstance.


Instruction:
How would you explain what the following Python 3 function does
def is_gridsearch(estimator):

    from sklearn.model_selection import GridSearchCV, RandomizedSearchCV

    if inspect.isclass(estimator):
        return issubclass(estimator, (GridSearchCV, RandomizedSearchCV))

    return isinstance(estimator, (GridSearchCV, RandomizedSearchCV))

Response: Returns True if the given estimator is a Scikit - Learn clusterer.


Instruction:
Write a Python 3 script to
return True if the given object is a Pandas Data Frame.

Response:
def is_dataframe(obj):
    """
    Returns True if the given object is a Pandas Data Frame.

    Parameters
    ----------
    obj: instance
        The object to test whether or not is a Pandas DataFrame.
    """
    try:
        # This is the best method of type checking
        from pandas import DataFrame
        return isinstance(obj, DataFrame)
    except ImportError:
        # Pandas is not a dependency, so this is scary
        return obj.__class__.__name__ == "DataFrame"


Instruction:
Can you generate the documentation for the following Python 3 function
def is_series(obj):
    try:
        # This is the best method of type checking
        from pandas import Series
        return isinstance(obj, Series)
    except ImportError:
        # Pandas is not a dependency, so this is scary
        return obj.__class__.__name__ == "Series"

Response: Returns True if the given object is a Pandas Series.


Instruction:
How would you code a function in Python 3 to
return True if the given object is a Numpy Structured Array.

Response:
def is_structured_array(obj):
    """
    Returns True if the given object is a Numpy Structured Array.

    Parameters
    ----------
    obj: instance
        The object to test whether or not is a Numpy Structured Array.
    """
    if isinstance(obj, np.ndarray) and hasattr(obj, 'dtype'):
        if obj.dtype.names is not None:
            return True
    return False


Instruction:
Write a Python 3 script for
plotting the actual targets from the dataset against the predicted values generated by the model.

Response:
def prediction_error(model, X, y=None, ax=None, alpha=0.75, **kwargs):
    """
    Quick method:

    Plot the actual targets from the dataset against the
    predicted values generated by our model(s).

    This helper function is a quick wrapper to utilize the PredictionError
    ScoreVisualizer for one-off analysis.

    Parameters
    ----------
    model : the Scikit-Learn estimator (should be a regressor)

    X  : ndarray or DataFrame of shape n x m
        A matrix of n instances with m features.

    y  : ndarray or Series of length n
        An array or series of target or class values.

    ax : matplotlib Axes
        The axes to plot the figure on.

    shared_limits : bool, default: True
        If shared_limits is True, the range of the X and Y axis limits will
        be identical, creating a square graphic with a true 45 degree line.
        In this form, it is easier to diagnose under- or over- prediction,
        though the figure will become more sparse. To localize points, set
        shared_limits to False, but note that this will distort the figure
        and should be accounted for during analysis.

    besfit : bool, default: True
        Draw a linear best fit line to estimate the correlation between the
        predicted and measured value of the target variable. The color of
        the bestfit line is determined by the ``line_color`` argument.

    identity: bool, default: True
        Draw the 45 degree identity line, y=x in order to better show the
        relationship or pattern of the residuals. E.g. to estimate if the
        model is over- or under- estimating the given values. The color of the
        identity line is a muted version of the ``line_color`` argument.

    point_color : color
        Defines the color of the error points; can be any matplotlib color.

    line_color : color
        Defines the color of the best fit line; can be any matplotlib color.

    alpha : float, default: 0.75
        Specify a transparency where 1 is completely opaque and 0 is completely
        transparent. This property makes densely clustered points more visible.

    kwargs : dict
        Keyword arguments that are passed to the base class and may influence
        the visualization as defined in other Visualizers.

    Returns
    -------
    ax : matplotlib Axes
        Returns the axes that the prediction error plot was drawn on.
    """
    # Instantiate the visualizer
    visualizer = PredictionError(model, ax, alpha=alpha, **kwargs)

    # Create the train and test splits
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X_train, y_train, **kwargs)
    visualizer.score(X_test, y_test)
    visualizer.finalize()

    # Return the axes object on the visualizer
    return visualizer.ax


Instruction:
Create a Python 3 function to
plot the residuals for the given model on the given axes.

Response:
def residuals_plot(model,
                   X,
                   y,
                   ax=None,
                   hist=True,
                   test_size=0.25,
                   train_color='b',
                   test_color='g',
                   line_color=LINE_COLOR,
                   random_state=None,
                   train_alpha=0.75,
                   test_alpha=0.75,
                   **kwargs):
    """Quick method:

    Divides the dataset X, y into a train and test split (the size of the
    splits determined by test_size) then plots the training and test residuals
    agains the predicted value for the given model.

    This helper function is a quick wrapper to utilize the ResidualsPlot
    ScoreVisualizer for one-off analysis.

    Parameters
    ----------
    model : a Scikit-Learn regressor
        Should be an instance of a regressor, otherwise will raise a
        YellowbrickTypeError exception on instantiation.

    X  : ndarray or DataFrame of shape n x m
        A matrix of n instances with m features.

    y  : ndarray or Series of length n
        An array or series of target or class values.

    ax : matplotlib Axes, default: None
        The axes to plot the figure on. If None is passed in the current axes
        will be used (or generated if required).

    hist : {True, False, None, 'density', 'frequency'}, default: True
        Draw a histogram showing the distribution of the residuals on the
        right side of the figure. Requires Matplotlib >= 2.0.2.
        If set to 'density', the probability density function will be plotted.
        If set to True or 'frequency' then the frequency will be plotted.

    test_size : float, int default: 0.25
        If float, should be between 0.0 and 1.0 and represent the proportion
        of the dataset to include in the test split. If int, represents the
        absolute number of test samples.

    train_color : color, default: 'b'
        Residuals for training data are ploted with this color but also
        given an opacity of 0.5 to ensure that the test data residuals
        are more visible. Can be any matplotlib color.

    test_color : color, default: 'g'
        Residuals for test data are plotted with this color. In order to
        create generalizable models, reserved test data residuals are of
        the most analytical interest, so these points are highlighted by
        having full opacity. Can be any matplotlib color.

    line_color : color, default: dark grey
        Defines the color of the zero error line, can be any matplotlib color.

    random_state : int, RandomState instance or None, optional
        Passed to the train_test_split function.

    train_alpha : float, default: 0.75
        Specify a transparency for traininig data, where 1 is completely opaque
        and 0 is completely transparent. This property makes densely clustered
        points more visible.

    test_alpha : float, default: 0.75
        Specify a transparency for test data, where 1 is completely opaque and
        0 is completely transparent. This property makes densely clustered
        points more visible.

    kwargs : dict
        Keyword arguments that are passed to the base class and may influence
        the visualization as defined in other Visualizers.

    Returns
    -------
    ax : matplotlib axes
        Returns the axes that the residuals plot was drawn on.
    """
    # Instantiate the visualizer

    visualizer = ResidualsPlot(
        model=model, ax=ax, hist=hist, train_color=train_color,
        test_color=test_color, line_color=line_color,
        train_alpha=train_alpha,test_alpha=test_alpha,
        **kwargs
    )

    # Create the train and test splits
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=test_size, random_state=random_state
    )

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X_train, y_train, **kwargs)
    visualizer.score(X_test, y_test)
    visualizer.finalize()

    # Return the axes object on the visualizer
    return visualizer.ax


Instruction:
Can you generate the documentation for the following Python 3 function
def draw(self, y, y_pred):

        """
        Parameters
        ----------
        y : ndarray or Series of length n
            An array or series of target or class values

        y_pred : ndarray or Series of length n
            An array or series of predicted target values

        Returns
        ------
        ax : the axis with the plotted figure
        """
        label = "$R^2 = {:0.3f}$".format(self.score_)
        self.ax.scatter(
            y,
            y_pred,
            c=self.colors['point'],
            alpha=self.alpha,
            label=label)

        # TODO If score is happening inside a loop, draw would get called multiple times.
        # Ideally we'd want the best fit line to be drawn only once
        if self.bestfit:
            draw_best_fit(
                y, y_pred, self.ax, 'linear', ls='--', lw=2,
                c=self.colors['line'], label='best fit'
            )

        # Set the axes limits based on the range of X and Y data
        # NOTE: shared_limits will be accounted for in finalize()
        # TODO: do better than add one for really small residuals
        self.ax.set_xlim(y.min()-1, y.max()+1)
        self.ax.set_ylim(y_pred.min()-1, y_pred.max()+1)

        return self.ax

Response: Draws the n - term data for the current object.


Instruction:
Create a Python 3 function to
finalize executes any subclass - specific axes finalization steps.

Response:
def finalize(self, **kwargs):
        """
        Finalize executes any subclass-specific axes finalization steps.
        The user calls poof and poof calls finalize.

        Parameters
        ----------
        kwargs: generic keyword arguments.
        """
        # Set the title on the plot
        self.set_title(
            'Prediction Error for {}'.format(self.name)
        )

        # Square the axes to ensure a 45 degree line
        if self.shared_limits:
            # Get the current limits
            ylim = self.ax.get_ylim()
            xlim = self.ax.get_xlim()

            # Find the range that captures all data
            bounds = (
                min(ylim[0], xlim[0]),
                max(ylim[1], xlim[1]),
            )

            # Reset the limits
            self.ax.set_xlim(bounds)
            self.ax.set_ylim(bounds)

            # Ensure the aspect ratio is square
            self.ax.set_aspect('equal', adjustable='box')

        # Draw the 45 degree line
        if self.identity:
            draw_identity_line(
                ax=self.ax, ls='--', lw=2, c=self.colors['line'],
                alpha=0.5, label="identity"
            )

        # Set the axes labels
        self.ax.set_ylabel(r'$\hat{y}$')
        self.ax.set_xlabel(r'$y$')

        # Set the legend
        # Note: it would be nice to be able to use the manual_legend utility
        # here, since if the user sets a low alpha value, the R2 color in the
        # legend will also become more translucent. Unfortunately this is a
        # bit tricky because adding a manual legend here would override the
        # best fit and 45 degree line legend components. In particular, the
        # best fit is plotted in draw because it depends on y and y_pred.
        self.ax.legend(loc='best', frameon=True)


Instruction:
Make a summary of the following Python 3 code
def hax(self):
        if make_axes_locatable is None:
            raise YellowbrickValueError((
                "residuals histogram requires matplotlib 2.0.2 or greater "
                "please upgrade matplotlib or set hist=False on the visualizer"
            ))

        divider = make_axes_locatable(self.ax)

        hax = divider.append_axes("right", size=1, pad=0.1, sharey=self.ax)
        hax.yaxis.tick_right()
        hax.grid(False, axis='x')

        return hax

Response: Returns the axes that are used to create the histogram for the current instance of the class.


Instruction:
Write a Python 3 script for
fitting the residuals plot to the set of target values.

Response:
def fit(self, X, y, **kwargs):
        """
        Parameters
        ----------
        X : ndarray or DataFrame of shape n x m
            A matrix of n instances with m features

        y : ndarray or Series of length n
            An array or series of target values

        kwargs: keyword arguments passed to Scikit-Learn API.

        Returns
        -------
        self : visualizer instance
        """
        super(ResidualsPlot, self).fit(X, y, **kwargs)
        self.score(X, y, train=True)
        return self


Instruction:
How would you explain what the following Python 3 function does
def score(self, X, y=None, train=False, **kwargs):
        score = self.estimator.score(X, y, **kwargs)
        if train:
            self.train_score_ = score
        else:
            self.test_score_ = score

        y_pred = self.predict(X)
        scores = y_pred - y
        self.draw(y_pred, scores, train=train)

        return score

Response:
Generates predicted target values using the Scikit - Learn
            estimator.


Instruction:
Make a summary of the following Python 3 code
def draw(self, y_pred, residuals, train=False, **kwargs):

        if train:
            color = self.colors['train_point']
            label = "Train $R^2 = {:0.3f}$".format(self.train_score_)
            alpha = self.alphas['train_point']
        else:
            color = self.colors['test_point']
            label = "Test $R^2 = {:0.3f}$".format(self.test_score_)
            alpha = self.alphas['test_point']

        # Update the legend information
        self._labels.append(label)
        self._colors.append(color)

        # Draw the residuals scatter plot
        self.ax.scatter(
            y_pred, residuals, c=color, alpha=alpha, label=label
        )

        # Add residuals histogram
        if self.hist in {True, 'frequency'}:
            self.hax.hist(residuals, bins=50, orientation="horizontal", color=color)
        elif self.hist == 'density':
            self.hax.hist(
                residuals, bins=50, orientation="horizontal", density=True, color=color
            )

        # Ensure the current axes is always the main residuals axes
        plt.sca(self.ax)
        return self.ax

Response: Draw the residuals against the predicted value for the specified split.


Instruction:
Implement a function in Python 3 to
finalize executes any subclass - specific axes finalization steps.

Response:
def finalize(self, **kwargs):
        """
        Finalize executes any subclass-specific axes finalization steps.
        The user calls poof and poof calls finalize.

        Parameters
        ----------
        kwargs: generic keyword arguments.
        """
        # Add the title to the plot
        self.set_title('Residuals for {} Model'.format(self.name))

        # Set the legend with full opacity patches using manual legend
        manual_legend(
            self, self._labels, self._colors, loc='best', frameon=True
        )

        # Create a full line across the figure at zero error.
        self.ax.axhline(y=0, c=self.colors['line'])

        # Set the axes labels
        self.ax.set_ylabel('Residuals')
        self.ax.set_xlabel("Predicted Value")

        # Finalize the histogram axes
        if self.hist:
            self.hax.axhline(y=0, c=self.colors['line'])
            self.hax.set_xlabel("Distribution")


Instruction:
Can you implement a function in Python 3 that
fits the visualizer to the given set of instances described by X and binary classes described in y.

Response:
def fit(self, X, y, **kwargs):
        """
        Fit is the entry point for the visualizer. Given instances described
        by X and binary classes described in the target y, fit performs n
        trials by shuffling and splitting the dataset then computing the
        precision, recall, f1, and queue rate scores for each trial. The
        scores are aggregated by the quantiles expressed then drawn.

        Parameters
        ----------
        X : ndarray or DataFrame of shape n x m
            A matrix of n instances with m features

        y : ndarray or Series of length n
            An array or series of target or class values. The target y must
            be a binary classification target.

        kwargs: dict
            keyword arguments passed to Scikit-Learn API.

        Returns
        -------
        self : instance
            Returns the instance of the visualizer

        raises: YellowbrickValueError
            If the target y is not a binary classification target.
        """
        # Check target before metrics raise crazy exceptions
        if type_of_target(y) != 'binary':
            raise YellowbrickValueError("multiclass format is not supported")

        # Make arrays indexable for cross validation
        X, y = indexable(X, y)

        # TODO: parallelize trials with joblib (using sklearn utility)
        # NOTE: parallelization with matplotlib is tricy at best!
        trials = [
            metric
            for idx in range(self.n_trials)
            for metric in self._split_fit_score_trial(X, y, idx)
        ]

        # Compute maximum number of uniform thresholds across all trials
        n_thresholds = np.array([len(t['thresholds']) for t in trials]).min()
        self.thresholds_ = np.linspace(0.0, 1.0, num=n_thresholds)

        # Filter metrics and collect values for uniform thresholds
        metrics = frozenset(METRICS) - self._check_exclude(self.exclude)
        uniform_metrics = defaultdict(list)

        for trial in trials:
            rows = defaultdict(list)
            for t in self.thresholds_:
                idx = bisect.bisect_left(trial['thresholds'], t)
                for metric in metrics:
                    rows[metric].append(trial[metric][idx])

            for metric, row in rows.items():
                uniform_metrics[metric].append(row)

        # Convert metrics to metric arrays
        uniform_metrics = {
            metric: np.array(values)
            for metric, values in uniform_metrics.items()
        }

        # Perform aggregation and store cv_scores_
        quantiles = self._check_quantiles(self.quantiles)
        self.cv_scores_ = {}

        for metric, values in uniform_metrics.items():
            # Compute the lower, median, and upper plots
            lower, median, upper = mstats.mquantiles(
                values, prob=quantiles, axis=0
            )

            # Store the aggregates in cv scores
            self.cv_scores_[metric] = median
            self.cv_scores_["{}_lower".format(metric)] = lower
            self.cv_scores_["{}_upper".format(metric)] = upper

        # Draw and always return self
        self.draw()
        return self


Instruction:
Write a Python 3 script for
splitting the dataset X y and scores it by fitting the model and then scores it according to the required metrics.

Response:
def _split_fit_score_trial(self, X, y, idx=0):
        """
        Splits the dataset, fits a clone of the estimator, then scores it
        according to the required metrics.

        The index of the split is added to the random_state if the
        random_state is not None; this ensures that every split is shuffled
        differently but in a deterministic fashion for testing purposes.
        """
        random_state = self.random_state
        if random_state is not None:
            random_state += idx

        splitter = self._check_cv(self.cv, random_state)

        for train_index, test_index in splitter.split(X, y):
            # Safe indexing handles multiple types of inputs including
            # DataFrames and structured arrays - required for generic splits.
            X_train = safe_indexing(X, train_index)
            y_train = safe_indexing(y, train_index)
            X_test = safe_indexing(X, test_index)
            y_test = safe_indexing(y, test_index)

            model = clone(self.estimator)
            model.fit(X_train, y_train)

            if hasattr(model, "predict_proba"):
                # Get the probabilities for the positive class
                y_scores = model.predict_proba(X_test)[:,1]
            else:
                # Use the decision function to get the scores
                y_scores = model.decision_function(X_test)

            # Compute the curve metrics and thresholds
            curve_metrics = precision_recall_curve(y_test, y_scores)
            precision, recall, thresholds = curve_metrics

            # Compute the F1 score from precision and recall
            # Don't need to warn for F, precision/recall would have warned
            with np.errstate(divide='ignore', invalid='ignore'):
                beta = self.fbeta ** 2
                f_score = ((1 + beta) * precision * recall /
                   (beta * precision + recall))

            # Ensure thresholds ends at 1
            thresholds = np.append(thresholds, 1)

            # Compute the queue rate
            queue_rate = np.array([
                (y_scores >= threshold).mean()
                for threshold in thresholds
            ])

            yield {
                'thresholds': thresholds,
                'precision': precision,
                'recall': recall,
                'fscore': f_score,
                'queue_rate': queue_rate
            }


Instruction:
Can you tell what is the following Python 3 function doing
def draw(self):
        # Set the colors from the supplied values or reasonable defaults
        color_values = resolve_colors(n_colors=4, colors=self.color)

        for idx, metric in enumerate(METRICS):
            # Skip any excluded labels
            if metric not in self.cv_scores_:
                continue

            # Get the color ensuring every metric has a static color
            color = color_values[idx]

            # Make the label pretty
            if metric == "fscore":
                if self.fbeta == 1.0:
                    label = "$f_1$"
                else:
                    label = "$f_{{\beta={:0.1f}}}".format(self.fbeta)
            else:
                label = metric.replace("_", " ")

            # Draw the metric values
            self.ax.plot(
                self.thresholds_, self.cv_scores_[metric],
                color=color, label=label
            )

            # Draw the upper and lower bounds
            lower = self.cv_scores_["{}_lower".format(metric)]
            upper = self.cv_scores_["{}_upper".format(metric)]

            self.ax.fill_between(
                self.thresholds_, upper, lower,
                alpha=0.35, linewidth=0, color=color
            )

            # Annotate the graph with the maximizing value
            if self.argmax.lower() == metric:
                argmax = self.cv_scores_[metric].argmax()
                threshold = self.thresholds_[argmax]
                self.ax.axvline(
                    threshold, ls='--', c='k', lw=1,
                    label="$t_{}={:0.2f}$".format(metric[0], threshold)
                )

        return self.ax

Response: Draws cv scores as a line chart on the current axes.


Instruction:
Here you have a function in Python 3, explain what it does
def _check_quantiles(self, val):
        if len(val) != 3 or not is_monotonic(val) or not np.all(val < 1):
            raise YellowbrickValueError(
                "quantiles must be a sequence of three "
                "monotonically increasing values less than 1"
            )
        return np.asarray(val)

Response: Validate the quantiles passed in. Returns the np array if valid.


Instruction:
Can you implement a function in Python 3 that
validates the cv method passed in. Returns the split strategy if validation exception is raised.

Response:
def _check_cv(self, val, random_state=None):
        """
        Validate the cv method passed in. Returns the split strategy if no
        validation exception is raised.
        """
        # Use default splitter in this case
        if val is None: val = 0.1

        if isinstance(val, float) and val <= 1.0:
            return ShuffleSplit(
                n_splits=1, test_size=val, random_state=random_state
            )

        if hasattr(val, "split") and hasattr(val, "get_n_splits"):
            if random_state is not None and hasattr(val, "random_state"):
                val.random_state = random_state
            return val

        raise YellowbrickValueError(
            "'{}' is not a valid cv splitter".format(type(val))
        )


Instruction:
Given the following Python 3 function, write the documentation
def _check_exclude(self, val):
        if val is None:
            exclude = frozenset()
        elif isinstance(val, str):
            exclude = frozenset([val.lower()])
        else:
            exclude = frozenset(map(lambda s: s.lower(), val))

        if len(exclude - frozenset(METRICS)) > 0:
            raise YellowbrickValueError(
                "'{}' is not a valid metric to exclude".format(repr(val))
            )

        return exclude

Response: Validate the excluded metrics. Returns the set of excluded params.


Instruction:
Here you have a function in Python 3, explain what it does
def decisionviz(model,
                X,
                y,
                colors=None,
                classes=None,
                features=None,
                show_scatter=True,
                step_size=0.0025,
                markers=None,
                pcolormesh_alpha=0.8,
                scatter_alpha=1.0,
                title=None,
                **kwargs):
    """DecisionBoundariesVisualizer is a bivariate data visualization algorithm
        that plots the decision boundaries of each class.

    This helper function is a quick wrapper to utilize the
    DecisionBoundariesVisualizers for one-off analysis.

    Parameters
    ----------
    model : the Scikit-Learn estimator, required
        Should be an instance of a classifier, else the __init__ will
        return an error.

    x : matrix, required
        The feature name that corresponds to a column name or index postion
        in the matrix that will be plotted against the x-axis

    y : array, required
        The feature name that corresponds to a column name or index postion
        in the matrix that will be plotted against the y-axis

    classes : a list of class names for the legend, default: None
        If classes is None and a y value is passed to fit then the classes
        are selected from the target vector.

    features : list of strings, default: None
        The names of the features or columns

    show_scatter : boolean, default: True
        If boolean is True, then a scatter plot with points will be drawn
        on top of the decision boundary graph

    step_size : float percentage, default: 0.0025
        Determines the step size for creating the numpy meshgrid that will
        later become the foundation of the decision boundary graph. The
        default value of 0.0025 means that the step size for constructing
        the meshgrid will be 0.25%% of differenes of the max and min of x
        and y for each feature.

    markers : iterable of strings, default: ,od*vh+
        Matplotlib style markers for points on the scatter plot points

    pcolormesh_alpha : float, default: 0.8
        Sets the alpha transparency for the meshgrid of model boundaries

    scatter_alpha : float, default: 1.0
        Sets the alpha transparency for the scatter plot points

    title : string, default: stringified feature_one and feature_two
        Sets the title of the visualization

    kwargs : keyword arguments passed to the super class.

    Returns
    -------
    ax : matplotlib axes
        Returns the axes that the decision boundaries graph were drawn on.
    """
    # Instantiate the visualizer
    visualizer = DecisionBoundariesVisualizer(model,
                    X,
                    y,
                    colors=colors,
                    classes=classes,
                    features=features,
                    show_scatter=show_scatter,
                    step_size=step_size,
                    markers=markers,
                    pcolormesh_alpha=pcolormesh_alpha,
                    scatter_alpha=scatter_alpha,
                    title=title,
                    **kwargs)

    # Fit, draw and poof the visualizer
    visualizer.fit_draw_poof(X, y, **kwargs)

    # Return the axes object on the visualizer
    return visualizer.ax

Response: This function is a quick wrapper to utilize the decision boundaries visualizer for one - off analysis.


Instruction:
Can you generate the documentation for the following Python 3 function
def find_text_color(base_color, dark_color="black", light_color="white", coef_choice=0):

    #Coefficients:
        # option 0: http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
        # option 1: http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
    coef_options = [np.array((.241, .691, .068, 0)),
                    np.array((.299, .587, .114, 0))
                    ]

    coefs= coef_options[coef_choice]
    rgb = np.array(base_color) * 255
    brightness = np.sqrt(np.dot(coefs, rgb**2))

    #Threshold from option 0 link; determined by trial and error.
    #base is light
    if brightness > 130:
        return dark_color
    return light_color

Response: This function returns the appropriate light or dark text color for the current locale.


Instruction:
Here you have a function in Python 3, explain what it does
def feature_importances(model, X, y=None, ax=None, labels=None,
                        relative=True, absolute=False, xlabel=None,
                        stack=False, **kwargs):
    """
    Displays the most informative features in a model by showing a bar chart
    of features ranked by their importances. Although primarily a feature
    engineering mechanism, this visualizer requires a model that has either a
    ``coef_`` or ``feature_importances_`` parameter after fit.

    Parameters
    ----------
    model : Estimator
        A Scikit-Learn estimator that learns feature importances. Must support
        either ``coef_`` or ``feature_importances_`` parameters.

    X : ndarray or DataFrame of shape n x m
        A matrix of n instances with m features

    y : ndarray or Series of length n, optional
        An array or series of target or class values

    ax : matplotlib Axes, default: None
        The axis to plot the figure on. If None is passed in the current axes
        will be used (or generated if required).

    labels : list, default: None
        A list of feature names to use. If a DataFrame is passed to fit and
        features is None, feature names are selected as the column names.

    relative : bool, default: True
        If true, the features are described by their relative importance as a
        percentage of the strongest feature component; otherwise the raw
        numeric description of the feature importance is shown.

    absolute : bool, default: False
        Make all coeficients absolute to more easily compare negative
        coeficients with positive ones.

    xlabel : str, default: None
        The label for the X-axis. If None is automatically determined by the
        underlying model and options provided.

    stack : bool, default: False
        If true and the classifier returns multi-class feature importance,
        then a stacked bar plot is plotted; otherwise the mean of the
        feature importance across classes are plotted.

    kwargs : dict
        Keyword arguments that are passed to the base class and may influence
        the visualization as defined in other Visualizers.

    Returns
    -------
    ax : matplotlib axes
        Returns the axes that the parallel coordinates were drawn on.
    """
    # Instantiate the visualizer
    visualizer = FeatureImportances(
        model, ax, labels, relative, absolute, xlabel, stack, **kwargs)

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X, y)
    visualizer.finalize()

    # Return the axes object on the visualizer
    return visualizer.ax

Response: Displays the most informative features in a model by ranking them by their importances.


Instruction:
Explain what the following Python 3 code does
def fit(self, X, y=None, **kwargs):
        super(FeatureImportances, self).fit(X, y, **kwargs)

        # Get the feature importances from the model
        self.feature_importances_ = self._find_importances_param()

        # Get the classes from the model
        if is_classifier(self):
            self.classes_ = self._find_classes_param()
        else:
            self.classes_ = None
            self.stack = False

        # If self.stack = True and feature importances is a multidim array,
        # we're expecting a shape of (n_classes, n_features)
        # therefore we flatten by taking the average by
        # column to get shape (n_features,)  (see LogisticRegression)
        if not self.stack and self.feature_importances_.ndim > 1:
            self.feature_importances_ = np.mean(self.feature_importances_, axis=0)
            warnings.warn((
                "detected multi-dimensional feature importances but stack=False, "
                "using mean to aggregate them."
            ), YellowbrickWarning)

        # Apply absolute value filter before normalization
        if self.absolute:
            self.feature_importances_ = np.abs(self.feature_importances_)

        # Normalize features relative to the maximum
        if self.relative:
            maxv = np.abs(self.feature_importances_).max()
            self.feature_importances_ /= maxv
            self.feature_importances_ *= 100.0

        # Create labels for the feature importances
        # NOTE: this code is duplicated from MultiFeatureVisualizer
        if self.labels is None:
            # Use column names if a dataframe
            if is_dataframe(X):
                self.features_ = np.array(X.columns)

            # Otherwise use the column index as the labels
            else:
                _, ncols = X.shape
                self.features_ = np.arange(0, ncols)
        else:
            self.features_ = np.array(self.labels)

        # Sort the features and their importances
        if self.stack:
            sort_idx = np.argsort(np.mean(self.feature_importances_, 0))
            self.features_ = self.features_[sort_idx]
            self.feature_importances_ = self.feature_importances_[:, sort_idx]
        else:
            sort_idx = np.argsort(self.feature_importances_)
            self.features_ = self.features_[sort_idx]
            self.feature_importances_ = self.feature_importances_[sort_idx]

        # Draw the feature importances
        self.draw()
        return self

Response: Fits the estimator to discover the feature importances described by the data X then draws those importances as a bar plot.


Instruction:
Can you create a Python 3 function that
draws the feature importances as a bar chart ; called from fit.

Response:
def draw(self, **kwargs):
        """
        Draws the feature importances as a bar chart; called from fit.
        """
        # Quick validation
        for param in ('feature_importances_', 'features_'):
            if not hasattr(self, param):
                raise NotFitted("missing required param '{}'".format(param))

        # Find the positions for each bar
        pos = np.arange(self.features_.shape[0]) + 0.5

        # Plot the bar chart
        if self.stack:
            colors = color_palette(kwargs.pop('colors', None),
                                   len(self.classes_))
            zeros = np.zeros(self.feature_importances_.shape[1])
            left_arr = np.zeros((self.feature_importances_.shape[1], 2))

            for idx in range(len(self.feature_importances_)):
                left = [
                    left_arr[j, int(self.feature_importances_[idx][j] > 0)]
                    for j in range(len(self.feature_importances_[idx]))
                ]

                self.ax.barh(pos, self.feature_importances_[idx], left=left,
                             color=colors[idx], label=self.classes_[idx])

                left_arr[:, 0] += np.minimum(self.feature_importances_[idx],
                                             zeros)
                left_arr[:, 1] += np.maximum(self.feature_importances_[idx],
                                             zeros)
        else:
            self.ax.barh(pos, self.feature_importances_, align='center')

        # Set the labels for the bars
        self.ax.set_yticks(pos)
        self.ax.set_yticklabels(self.features_)

        return self.ax


Instruction:
Can you implement a function in Python 3 that
finalizes the drawing setting labels and title.

Response:
def finalize(self, **kwargs):
        """
        Finalize the drawing setting labels and title.
        """
        # Set the title
        self.set_title('Feature Importances of {} Features using {}'.format(
                len(self.features_), self.name))

        # Set the xlabel
        self.ax.set_xlabel(self._get_xlabel())

        # Remove the ygrid
        self.ax.grid(False, axis='y')

        if self.stack:
            plt.legend(bbox_to_anchor=(1.04, 0.5), loc="center left")
        # Ensure we have a tight fit
        plt.tight_layout()


Instruction:
Implement a function in Python 3 to
search the wrapped model for the classes_ parameter.

Response:
def _find_classes_param(self):
        """
        Searches the wrapped model for the classes_ parameter.
        """
        for attr in ["classes_"]:
            try:
                return getattr(self.estimator, attr)
            except AttributeError:
                continue

        raise YellowbrickTypeError(
            "could not find classes_ param on {}".format(
                self.estimator.__class__.__name__
            )
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_xlabel(self):
        # Return user-specified label
        if self.xlabel:
            return self.xlabel

        # Label for coefficients
        if hasattr(self.estimator, "coef_"):
            if self.relative:
                return "relative coefficient magnitude"
            return "coefficient value"

        # Default label for feature_importances_
        if self.relative:
            return "relative importance"
        return "feature importance"

Response: Determines the xlabel based on the underlying data structure


Instruction:
Given the following Python 3 function, write the documentation
def target_color_type(y):
    ttype = type_of_target(y)

    if ttype.startswith(CONTINUOUS):
        return CONTINUOUS

    if ttype.startswith("binary"):
        return DISCRETE

    if ttype.startswith("multiclass"):
        if len(np.unique(y)) > MAX_DISCRETE_CLASSES:
            return CONTINUOUS
        return DISCRETE

    return UNKNOWN

Response: Determines the type of color space that will best represent the target variable y.


Instruction:
Implement a Python 3 function for
creating the grid layout for the joint plot.

Response:
def _layout(self):
        """
        Creates the grid layout for the joint plot, adding new axes for the histograms
        if necessary and modifying the aspect ratio. Does not modify the axes or the
        layout if self.hist is False or None.
        """
        # Ensure the axes are created if not hist, then return.
        if not self.hist:
            self.ax
            return

        # Ensure matplotlib version compatibility
        if make_axes_locatable is None:
            raise YellowbrickValueError((
                "joint plot histograms requires matplotlib 2.0.2 or greater "
                "please upgrade matplotlib or set hist=False on the visualizer"
            ))

        # Create the new axes for the histograms
        divider = make_axes_locatable(self.ax)
        self._xhax = divider.append_axes("top", size=1, pad=0.1, sharex=self.ax)
        self._yhax = divider.append_axes("right", size=1, pad=0.1, sharey=self.ax)

        # Modify the display of the axes
        self._xhax.xaxis.tick_top()
        self._yhax.yaxis.tick_right()
        self._xhax.grid(False, axis='y')
        self._yhax.grid(False, axis='x')


Instruction:
How would you explain what the following Python 3 function does
def fit(self, X, y=None):
        # Convert python objects to numpy arrays
        if isinstance(X, (list, tuple)):
            X = np.array(X)

        if y is not None and isinstance(y, (list, tuple)):
            y = np.array(y)

        # Case where no columns are specified
        if self.columns is None:
            if (y is None and (X.ndim != 2 or X.shape[1] != 2)) or (y is not None and (X.ndim != 1 or y.ndim != 1)):
                raise YellowbrickValueError((
                    "when self.columns is None specify either X and y as 1D arrays "
                    "or X as a matrix with 2 columns"
                ))

            if y is None:
                # Draw the first column as x and the second column as y
                self.draw(X[:,0], X[:,1], xlabel="0", ylabel="1")
                return self

            # Draw x against y
            self.draw(X, y, xlabel="x", ylabel="y")
            return self

        # Case where a single string or int index is specified
        if isinstance(self.columns, (int,str)):
            if y is None:
                raise YellowbrickValueError(
                    "when self.columns is a single index, y must be specified"
                )

            # fetch the index from X -- raising index error if not possible
            x = self._index_into(self.columns, X)
            self.draw(x, y, xlabel=str(self.columns), ylabel="target")
            return self

        # Case where there is a double index for both columns
        columns = tuple(self.columns)
        if len(columns) != 2:
            raise YellowbrickValueError((
                    "'{}' contains too many indices or is invalid for joint plot"
                ).format(columns))

        # TODO: color the points based on the target if it is given
        x = self._index_into(columns[0], X)
        y = self._index_into(columns[1], X)
        self.draw(x, y, xlabel=str(columns[0]), ylabel=str(columns[1]))
        return self

Response: Fits the JointPlot to create a correlative visualization between the columns X and y.


Instruction:
Can you write a function in Python 3 where it
draws the main joint plot for the data in x and y.

Response:
def draw(self, x, y, xlabel=None, ylabel=None):
        """
        Draw the joint plot for the data in x and y.

        Parameters
        ----------
        x, y : 1D array-like
            The data to plot for the x axis and the y axis

        xlabel, ylabel : str
            The labels for the x and y axes.
        """
        # This is a little weird to be here, but it is the best place to perform
        # this computation given how fit calls draw and returns.
        self.corr_ = self.correlation_methods[self.correlation](x, y)

        # First draw the joint plot
        joint_kws = self.joint_kws or {}
        joint_kws.setdefault("alpha", self.alpha)
        joint_kws.setdefault("label", "{}={:0.3f}".format(self.correlation, self.corr_))

        # Draw scatter joint plot
        if self.kind == "scatter":
            self.ax.scatter(x, y, **joint_kws)

            # TODO: Draw best fit line (or should this be kind='reg'?)

        # Draw hexbin joint plot
        elif self.kind in ('hex', 'hexbin'):
            joint_kws.setdefault("mincnt", 1)
            joint_kws.setdefault("gridsize", 50)
            self.ax.hexbin(x, y, **joint_kws)

        # Something bad happened
        else:
            raise ValueError("unknown joint plot kind '{}'".format(self.kind))

        # Set the X and Y axis labels on the plot
        self.ax.set_xlabel(xlabel)
        self.ax.set_ylabel(ylabel)

        # If we're not going to draw histograms, stop here
        if not self.hist:
            # Ensure the current axes is always the main joint plot axes
            plt.sca(self.ax)
            return self.ax

        # Draw the histograms
        hist_kws = self.hist_kws or {}
        hist_kws.setdefault("bins", 50)
        if self.hist == "density":
            hist_kws.setdefault("density", True)

        self.xhax.hist(x, **hist_kws)
        self.yhax.hist(y, orientation="horizontal", **hist_kws)

        # Ensure the current axes is always the main joint plot axes
        plt.sca(self.ax)
        return self.ax


Instruction:
Given the following Python 3 function, write the documentation
def finalize(self, **kwargs):
        # Set the aspect ratio to make the visualization square
        # TODO: still unable to make plot square using make_axes_locatable
        # x0,x1 = self.ax.get_xlim()
        # y0,y1 = self.ax.get_ylim()
        # self.ax.set_aspect(abs(x1-x0)/abs(y1-y0))

        # Add the title to the plot if the user has set one.
        self.set_title("")

        # Set the legend with full opacity patches using manual legend.
        # Or Add the colorbar if this is a continuous plot.
        self.ax.legend(loc="best", frameon=True)

        # Finalize the histograms
        if self.hist:
            plt.setp(self.xhax.get_xticklabels(), visible=False)
            plt.setp(self.yhax.get_yticklabels(), visible=False)
            plt.sca(self.ax)

        # Call tight layout to maximize readability
        plt.tight_layout()

Response: Finalize executes any remaining image modifications making it ready to show.


Instruction:
Can you create a Python 3 function that
attempts to get the column from the data using the specified index raises an IndexError if this is not possible from this point in the stack.

Response:
def _index_into(self, idx, data):
        """
        Attempts to get the column from the data using the specified index, raises an
        exception if this is not possible from this point in the stack.
        """
        try:
            if is_dataframe(data):
                # Assume column indexing
                return data[idx]
            # Otherwise assume numpy array-like indexing
            return data[:,idx]
        except Exception as e:
            raise IndexError(
                "could not index column '{}' into type {}: {}".format(
                    self.columns, data.__class__.__name__, e
            ))


Instruction:
Can you generate the documentation for the following Python 3 function
def fit(self, X, y=None, **kwargs):
        if is_dataframe(X):
            self.X = X.values
            if self.features_ is None:
                self.features_ = X.columns
        else:
            self.X = X

        self.y = y

        super(MissingDataVisualizer, self).fit(X, y, **kwargs)

Response: Fit the missing data visualization to the data X and y.


Instruction:
Can you tell what is the following Python 3 function doing
def cv_scores(model, X, y, ax=None, cv=None, scoring=None, **kwargs):

    # Initialize the visualizer
    visualizer = CVScores(model, ax=ax, cv=cv, scoring=scoring)

    # Fit and poof the visualizer
    visualizer.fit(X, y)
    visualizer.poof(**kwargs)
    return visualizer.ax

Response: Generates a CVScores visualizer for one - off analysis of the base class.


Instruction:
How would you implement a function in Python 3 that
fits the learning curve with the wrapped model to the specified data. Draws training and test score curves and saves the scores to the estimator.

Response:
def fit(self, X, y, **kwargs):
        """
        Fits the learning curve with the wrapped model to the specified data.
        Draws training and test score curves and saves the scores to the
        estimator.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training vector, where n_samples is the number of samples and
            n_features is the number of features.

        y : array-like, shape (n_samples) or (n_samples, n_features), optional
            Target relative to X for classification or regression;
            None for unsupervised learning.

        Returns
        -------
        self : instance

        """

        self.cv_scores_ = cross_val_score(
            self.estimator, X, y, cv=self.cv, scoring=self.scoring
        )
        self.cv_scores_mean_ = self.cv_scores_.mean()

        self.draw()
        return self


Instruction:
Implement a function in Python 3 to
draw the cross - validated scores for the current locale.

Response:
def draw(self, **kwargs):
        """
        Creates the bar chart of the cross-validated scores generated from the
        fit method and places a dashed horizontal line that represents the
        average value of the scores.
        """

        color = kwargs.pop("color", "b")
        width = kwargs.pop("width", 0.3)
        linewidth = kwargs.pop("linewidth", 1)

        xvals = np.arange(1, len(self.cv_scores_) + 1, 1)
        self.ax.bar(xvals, self.cv_scores_, width=width)
        self.ax.axhline(
            self.cv_scores_mean_, color=color,
            label="Mean score = {:0.3f}".format(self.cv_scores_mean_),
            linestyle='--', linewidth=linewidth
        )

        return self.ax


Instruction:
Write a Python 3 script to
add the title, legend, and other visual final touches to the plot.

Response:
def finalize(self, **kwargs):
        """
        Add the title, legend, and other visual final touches to the plot.
        """

        # Set the title of the figure
        self.set_title('Cross Validation Scores for {}'.format(self.name))

        # Add the legend
        loc = kwargs.pop("loc", "best")
        edgecolor = kwargs.pop("edgecolor", "k")
        self.ax.legend(frameon=True, loc=loc, edgecolor=edgecolor)

        # set spacing between the x ticks
        self.ax.xaxis.set_major_locator(ticker.MultipleLocator(1))

        # Set the axis labels
        self.ax.set_xlabel('Training Instances')
        self.ax.set_ylabel('Score')


Instruction:
Make a summary of the following Python 3 code
def kendalltau(X):
    corrs = np.zeros((X.shape[1], X.shape[1]))
    for idx, cola in enumerate(X.T):
        for jdx, colb in enumerate(X.T):
            corrs[idx, jdx] = sp_kendalltau(cola,colb)[0]
    return corrs

Response: Returns a correlation matrix so that each column is the variable and each row is the observations.


Instruction:
Can you implement a function in Python 3 that
generates a 1D ranking of each feature with the algorithm and ranks them in a bar plot.

Response:
def rank1d(X, y=None, ax=None, algorithm='shapiro', features=None,
           orient='h', show_feature_names=True, **kwargs):
    """Scores each feature with the algorithm and ranks them in a bar plot.

    This helper function is a quick wrapper to utilize the Rank1D Visualizer
    (Transformer) for one-off analysis.

    Parameters
    ----------
    X : ndarray or DataFrame of shape n x m
        A matrix of n instances with m features

    y : ndarray or Series of length n
        An array or series of target or class values

    ax : matplotlib axes
        the axis to plot the figure on.

    algorithm : one of {'shapiro', }, default: 'shapiro'
        The ranking algorithm to use, default is 'Shapiro-Wilk.

    features : list
        A list of feature names to use.
        If a DataFrame is passed to fit and features is None, feature
        names are selected as the columns of the DataFrame.

    orient : 'h' or 'v'
        Specifies a horizontal or vertical bar chart.

    show_feature_names : boolean, default: True
        If True, the feature names are used to label the axis ticks in the
        plot.

    Returns
    -------
    ax : matplotlib axes
        Returns the axes that the parallel coordinates were drawn on.

    """
    # Instantiate the visualizer
    visualizer = Rank1D(ax, algorithm, features, orient, show_feature_names,
                        **kwargs)

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X, y, **kwargs)
    visualizer.transform(X)

    # Return the axes object on the visualizer
    return visualizer.ax


Instruction:
Given the following Python 3 function, write the documentation
def rank2d(X, y=None, ax=None, algorithm='pearson', features=None,
           show_feature_names=True, colormap='RdBu_r', **kwargs):
    """Displays pairwise comparisons of features with the algorithm and ranks
    them in a lower-left triangle heatmap plot.

    This helper function is a quick wrapper to utilize the Rank2D Visualizer
    (Transformer) for one-off analysis.

    Parameters
    ----------
    X : ndarray or DataFrame of shape n x m
        A matrix of n instances with m features

    y : ndarray or Series of length n
        An array or series of target or class values

    ax : matplotlib axes
        the axis to plot the figure on.

    algorithm : one of {pearson, covariance, spearman, kendalltau}
        the ranking algorithm to use, default is Pearson correlation.

    features : list
        A list of feature names to use.
        If a DataFrame is passed to fit and features is None, feature
        names are selected as the columns of the DataFrame.

    show_feature_names : boolean, default: True
        If True, the feature names are used to label the axis ticks in the
        plot.

    colormap : string or cmap
        optional string or matplotlib cmap to colorize lines
        Use either color to colorize the lines on a per class basis or
        colormap to color them on a continuous scale.

    Returns
    -------
    ax : matplotlib axes
        Returns the axes that the parallel coordinates were drawn on.

    """
    # Instantiate the visualizer
    visualizer = Rank2D(ax, algorithm, features, colormap, show_feature_names,
                        **kwargs)

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X, y, **kwargs)
    visualizer.transform(X)

    # Return the axes object on the visualizer
    return visualizer.ax

Response: Displays pairwise comparisons of features with the algorithm and ranks them in a lower - left triangle heatmap plot.


Instruction:
Here you have a function in Python 3, explain what it does
def rank(self, X, algorithm=None):
        algorithm = algorithm or self.ranking_
        algorithm = algorithm.lower()

        if algorithm not in self.ranking_methods:
            raise YellowbrickValueError(
                "'{}' is unrecognized ranking method".format(algorithm)
            )

        # Extract matrix from dataframe if necessary
        if is_dataframe(X):
            X = X.values

        return self.ranking_methods[algorithm](X)

Response: Returns the feature ranking for the given set of n - dimensional n - dimensional n - dimensional molecular features.


Instruction:
Here you have a function in Python 3, explain what it does
def finalize(self, **kwargs):
        # Set the title
        self.set_title(
            "{} Ranking of {} Features".format(
                self.ranking_.title(), len(self.features_)
            )
        )

Response: Executes any subclass - specific axes finalization steps.


Instruction:
Make a summary of the following Python 3 code
def draw(self, **kwargs):
        if self.orientation_ == 'h':
            # Make the plot
            self.ax.barh(np.arange(len(self.ranks_)), self.ranks_, color='b')

            # Add ticks and tick labels
            self.ax.set_yticks(np.arange(len(self.ranks_)))
            if self.show_feature_names_:
                self.ax.set_yticklabels(self.features_)
            else:
                self.ax.set_yticklabels([])

            # Order the features from top to bottom on the y axis
            self.ax.invert_yaxis()

            # Turn off y grid lines
            self.ax.yaxis.grid(False)

        elif self.orientation_ == 'v':
            # Make the plot
            self.ax.bar(np.arange(len(self.ranks_)), self.ranks_, color='b')

            # Add ticks and tick labels
            self.ax.set_xticks(np.arange(len(self.ranks_)))
            if self.show_feature_names_:
                self.ax.set_xticklabels(self.features_, rotation=90)
            else:
                self.ax.set_xticklabels([])

            # Turn off x grid lines
            self.ax.xaxis.grid(False)

        else:
            raise YellowbrickValueError(
                "Orientation must be 'h' or 'v'"
            )

Response: Draws the bar plot of the ranking array of features.


Instruction:
Implement a Python 3 function for
drawing the heatmap of the ranking matrix of variables.

Response:
def draw(self, **kwargs):
        """
        Draws the heatmap of the ranking matrix of variables.
        """
        # Set the axes aspect to be equal
        self.ax.set_aspect("equal")

        # Generate a mask for the upper triangle
        mask = np.zeros_like(self.ranks_, dtype=np.bool)
        mask[np.triu_indices_from(mask)] = True

        # Draw the heatmap
        # TODO: Move mesh to a property so the colorbar can be finalized
        data = np.ma.masked_where(mask, self.ranks_)
        mesh = self.ax.pcolormesh(data, cmap=self.colormap, vmin=-1, vmax=1)

        # Set the Axis limits
        self.ax.set(
            xlim=(0, data.shape[1]), ylim=(0, data.shape[0])
        )

        # Add the colorbar
        cb = self.ax.figure.colorbar(mesh, None, self.ax)
        cb.outline.set_linewidth(0)

        # Reverse the rows to get the lower left triangle
        self.ax.invert_yaxis()

        # Add ticks and tick labels
        self.ax.set_xticks(np.arange(len(self.ranks_)) + 0.5)
        self.ax.set_yticks(np.arange(len(self.ranks_)) + 0.5)
        if self.show_feature_names_:
            self.ax.set_xticklabels(self.features_, rotation=90)
            self.ax.set_yticklabels(self.features_)
        else:
            self.ax.set_xticklabels([])
            self.ax.set_yticklabels([])


Instruction:
Here you have a function in Python 3, explain what it does
def class_prediction_error(
    model,
    X,
    y=None,
    ax=None,
    classes=None,
    test_size=0.2,
    random_state=None,
    **kwargs):
    """Quick method:
    Divides the dataset X and y into train and test splits, fits the model on
    the train split, then scores the model on the test split. The visualizer
    displays the support for each class in the fitted classification model
    displayed as a stacked bar plot Each bar is segmented to show the
    distribution of predicted classes for each class.

    This helper function is a quick wrapper to utilize the ClassPredictionError
    ScoreVisualizer for one-off analysis.

    Parameters
    ----------
    model : the Scikit-Learn estimator (should be a classifier)

    X  : ndarray or DataFrame of shape n x m
        A matrix of n instances with m features.

    y  : ndarray or Series of length n
        An array or series of target or class values.

    ax : matplotlib axes
        The axes to plot the figure on.

    classes : list of strings
        The names of the classes in the target

    test_size : float, default=0.2
        The percentage of the data to reserve as test data.

    random_state : int or None, default=None
        The value to seed the random number generator for shuffling data.

    Returns
    -------
    ax : matplotlib axes
        Returns the axes that the class prediction error plot was drawn on.
    """
    # Instantiate the visualizer
    visualizer = ClassPredictionError(model, ax, classes, **kwargs)

    # Create the train and test splits
    X_train, X_test, y_train, y_test = tts(
        X, y, test_size=test_size, random_state=random_state
    )

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X_train, y_train, **kwargs)
    visualizer.score(X_test, y_test)

    # Return the axes object on the visualizer
    return visualizer.ax

Response: Plots a ClassPredictionError plot on the specified axes.


Instruction:
Given the following Python 3 function, write the documentation
def draw(self):

        indices = np.arange(len(self.classes_))
        prev = np.zeros(len(self.classes_))

        colors = resolve_colors(
            colors=self.colors,
            n_colors=len(self.classes_))

        for idx, row in enumerate(self.predictions_):
            self.ax.bar(indices, row, label=self.classes_[idx],
                        bottom=prev, color=colors[idx])
            prev += row

        return self.ax

Response: Draws the class prediction error across the axis.


Instruction:
Write a Python 3 script for
finalizing executes any subclass - specific axes finalization steps.

Response:
def finalize(self, **kwargs):
        """
        Finalize executes any subclass-specific axes finalization steps.
        The user calls poof and poof calls finalize.
        """

        indices = np.arange(len(self.classes_))

        # Set the title
        self.set_title("Class Prediction Error for {}".format(self.name))

        # Set the x ticks with the class names
        self.ax.set_xticks(indices)
        self.ax.set_xticklabels(self.classes_)

        # Set the axes labels
        self.ax.set_xlabel("actual class")
        self.ax.set_ylabel("number of predicted class")

        # Compute the ceiling for the y limit
        cmax = max([sum(predictions) for predictions in self.predictions_])
        self.ax.set_ylim(0, cmax + cmax * 0.1)

        # Put the legend outside of the graph
        plt.legend(bbox_to_anchor=(1.04, 0.5), loc="center left")
        plt.tight_layout(rect=[0, 0, 0.85, 1])


Instruction:
How would you code a function in Python 3 to
create a MissingValues Bar visualizer that draws the missing values for each selected feature column.

Response:
def missing_bar(X, y=None, ax=None, classes=None, width=0.5, color='black', **kwargs):
    """The MissingValues Bar visualizer creates a bar graph that lists the total
    count of missing values for each selected feature column.

    When y targets are supplied to fit, the output is a stacked bar chart where
    each color corresponds to the total NaNs for the feature in that column.

    Parameters
    ----------
    alpha : float, default: 0.5
        A value for bending elments with the background.

    marker : matplotlib marker, default: |
        The marker used for each element coordinate in the plot

    color : string, default: black
        The color for drawing the bar chart when the y targets are not passed to
        fit.

    colors : list, default: None
        The color pallette for drawing a stack bar chart when the y targets
        are passed to fit.

    classes : list, default: None
        A list of class names for the legend.
        If classes is None and a y value is passed to fit then the classes
        are selected from the target vector.

    kwargs : dict
        Keyword arguments that are passed to the base class and may influence
        the visualization as defined in other Visualizers.

    Attributes
    ----------
    features_ : np.array
        The feature labels ranked according to their importance

    classes_ : np.array
        The class labels for each of the target values

    Examples
    --------

    >>> from yellowbrick.contrib.missing import missing_bar
    >>> visualizer = missing_bar(X, y=y)
    """
    # Instantiate the visualizer
    visualizer = MissingValuesBar(
        ax=ax, classes=classes, width=width, color=color, **kwargs
    )

    # Fit and transform the visualizer (calls draw)
    visualizer.fit(X, y)
    visualizer.poof()

    # Return the axes object on the visualizer
    return visualizer.ax


Instruction:
Can you generate a brief explanation for the following Python 3 code
def draw(self, X, y, **kwargs):
        nan_col_counts = self.get_nan_col_counts()

        # the x locations for the groups
        self.ind = np.arange(len(self.features_))

        if y is None:
            self.ax.barh(self.ind - self.width / 2, nan_col_counts, self.width,
                            color=self.color, label=None)
        else:
            self.draw_stacked_bar(nan_col_counts)

Response:
Called from the fit method this method generates a horizontal bar chart for each nan count per
        target values.


Instruction:
How would you implement a function in Python 3 that
draws a horizontal stacked bar chart with different colors for each count of nan values per label.

Response:
def draw_stacked_bar(self, nan_col_counts):
        """Draws a horizontal stacked bar chart with different colors
        for each count of nan values per label.
        """
        for index, nan_values in enumerate(nan_col_counts):
            label, nan_col_counts = nan_values

            if index == 0:
                # first draw should be at zero
                bottom_chart = np.zeros(nan_col_counts.shape)

            # if features passed in then, label as such
            if self.classes_ is not None:
                label = self.classes_[index]

            color = self.colors[index]

            self.ax.barh(self.ind - self.width / 2, nan_col_counts, self.width,
                        color=color, label=label, left=bottom_chart)

            # keep track of counts to build on stacked
            bottom_chart = nan_col_counts


Instruction:
Create a Python 3 function to
finalize executes any subclass - specific axes finalization steps.

Response:
def finalize(self, **kwargs):
        """
        Finalize executes any subclass-specific axes finalization steps.
        The user calls poof and poof calls finalize.

        Parameters
        ----------
        kwargs: generic keyword arguments.

        """
        # Set the title
        self.set_title(
            'Count of Missing Values by Column'
        )
        tick_locations = np.arange(len(self.features_))  # the x locations for the groups
        self.ax.set_yticks(tick_locations)
        self.ax.set_yticklabels(self.get_feature_names())
        # Remove the ticks from the graph
        self.ax.set_xlabel('Count')

        self.ax.legend(loc='best')


Instruction:
Make a summary of the following Python 3 code
def feature_correlation(X, y, ax=None, method='pearson',
                        labels=None, sort=False, feature_index=None,
                        feature_names=None, **kwargs):
    """
    Displays the correlation between features and dependent variables.

    This visualizer can be used side-by-side with
    yellowbrick.features.JointPlotVisualizer that plots a feature
    against the target and shows the distribution of each via a
    histogram on each axis.

    Parameters
    ----------
    X : ndarray or DataFrame of shape n x m
        A matrix of n instances with m features

    y : ndarray or Series of length n
        An array or series of target or class values

    ax : matplotlib Axes, default: None
        The axis to plot the figure on. If None is passed in the current axes
        will be used (or generated if required).

    method : str, default: 'pearson'
        The method to calculate correlation between features and target.
        Options include:

            - 'pearson', which uses ``scipy.stats.pearsonr``
            - 'mutual_info-regression', which uses ``mutual_info-regression``
              from ``sklearn.feature_selection``
            - 'mutual_info-classification', which uses ``mutual_info_classif``
              from ``sklearn.feature_selection``
            'mutual_info-classification'], default: 'pearson'

    labels : list, default: None
        A list of feature names to use. If a DataFrame is passed to fit and
        features is None, feature names are selected as the column names.

    sort : boolean, default: False
        If false, the features are are not sorted in the plot; otherwise
        features are sorted in ascending order of correlation.

    feature_index : list,
        A list of feature index to include in the plot.

    feature_names : list of feature names
        A list of feature names to include in the plot.
        Must have labels or the fitted data is a DataFrame with column names.
        If feature_index is provided, feature_names will be ignored.

    kwargs : dict
        Keyword arguments that are passed to the base class and may influence
        the visualization as defined in other Visualizers.

    Returns
    -------
    ax : matplotlib axes
        Returns the axes that the parallel coordinates were drawn on.
    """

    # Instantiate the visualizer
    viz = FeatureCorrelation(ax, method, labels, sort,
                             feature_index, feature_names, **kwargs)

    # Fit and transform the visualizer (calls draw)
    viz.fit(X, y, **kwargs)
    viz.finalize()

    # Return the axes object on the visualizer
    return viz.ax

Response: Visualize the feature correlation between two sets of target and class values.


Instruction:
Can you tell what is the following Python 3 function doing
def fit(self, X, y, **kwargs):
        self._create_labels_for_features(X)

        self._select_features_to_plot(X)

        # Calculate Features correlation with target variable
        if self.method == "pearson":
            self.scores_ = np.array(
                [pearsonr(x, y, **kwargs)[0] for x in np.asarray(X).T]
            )
        else:
            self.scores_ = np.array(
                self.correlation_methods[self.method](X, y, **kwargs)
        )

        # If feature indices are given, plot only the given features
        if self.feature_index:
            self.scores_ = self.scores_[self.feature_index]
            self.features_ = self.features_[self.feature_index]

        # Sort features by correlation
        if self.sort:
            sort_idx = np.argsort(self.scores_)
            self.scores_ = self.scores_[sort_idx]
            self.features_ = self.features_[sort_idx]

        self.draw()
        return self

Response: Fits the estimator to calculate feature correlation to the dependent variable.


Instruction:
Implement a function in Python 3 to
draw the feature correlation to dependent variable called from fit.

Response:
def draw(self):
        """
        Draws the feature correlation to dependent variable, called from fit.
        """
        pos = np.arange(self.scores_.shape[0]) + 0.5

        self.ax.barh(pos, self.scores_)

        # Set the labels for the bars
        self.ax.set_yticks(pos)
        self.ax.set_yticklabels(self.features_)

        return self.ax


Instruction:
Can you tell what is the following Python 3 function doing
def finalize(self):
        self.set_title('Features correlation with dependent variable')

        self.ax.set_xlabel(self.correlation_labels[self.method])

        self.ax.grid(False, axis='y')

Response: Finalize the drawing setting labels and title.


Instruction:
Explain what the following Python 3 code does
def _create_labels_for_features(self, X):
        if self.labels is None:
            # Use column names if a dataframe
            if is_dataframe(X):
                self.features_ = np.array(X.columns)
            # Otherwise use the column index as the labels
            else:
                _, ncols = X.shape
                self.features_ = np.arange(0, ncols)
        else:
            self.features_ = np.array(self.labels)

Response: Create labels for the features in the features_ attribute of the features_ attribute.


Instruction:
Implement a function in Python 3 to
select features to plot.

Response:
def _select_features_to_plot(self, X):
        """
        Select features to plot.

        feature_index is always used as the filter and
        if filter_names is supplied, a new feature_index
        is computed from those names.
        """
        if self.feature_index:
            if self.feature_names:
                raise YellowbrickWarning(
                    'Both feature_index and feature_names '
                    'are specified. feature_names is ignored'
                )
            if (min(self.feature_index) < 0
                    or max(self.feature_index) >= X.shape[1]):
                raise YellowbrickValueError('Feature index is out of range')
        elif self.feature_names:
            self.feature_index = []
            features_list = self.features_.tolist()
            for feature_name in self.feature_names:
                try:
                    self.feature_index.append(
                        features_list.index(feature_name)
                    )
                except ValueError:
                    raise YellowbrickValueError(
                        '{} not in labels'.format(feature_name)
                    )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def feature_analysis(fname="feature_analysis.png"):

    # Create side-by-side axes grid
    _, axes = plt.subplots(ncols=2, figsize=(18,6))

    # Draw RadViz on the left
    data = load_occupancy(split=False)
    oz = RadViz(ax=axes[0], classes=["unoccupied", "occupied"])
    oz.fit(data.X, data.y)
    oz.finalize()

    # Draw Rank2D on the right
    data = load_concrete(split=False)
    oz = Rank2D(ax=axes[1])
    oz.fit_transform(data.X, data.y)
    oz.finalize()

    # Save figure
    path = os.path.join(FIGURES, fname)
    plt.tight_layout()
    plt.savefig(path)

Response: Create figures for feature analysis


Instruction:
Can you generate a brief explanation for the following Python 3 code
def regression(fname="regression.png"):
    _, axes = plt.subplots(ncols=2, figsize=(18, 6))
    alphas = np.logspace(-10, 1, 300)
    data = load_concrete(split=True)

    # Plot prediction error in the middle
    oz = PredictionError(LassoCV(alphas=alphas), ax=axes[0])
    oz.fit(data.X.train, data.y.train)
    oz.score(data.X.test, data.y.test)
    oz.finalize()

    # Plot residuals on the right
    oz = ResidualsPlot(RidgeCV(alphas=alphas), ax=axes[1])
    oz.fit(data.X.train, data.y.train)
    oz.score(data.X.test, data.y.test)
    oz.finalize()

    # Save figure
    path = os.path.join(FIGURES, fname)
    plt.tight_layout()
    plt.savefig(path)

Response: Create figures for regression models


Instruction:
Here you have a function in Python 3, explain what it does
def ax(self):
        if not hasattr(self, "_ax") or self._ax is None:
            self._ax = plt.gca()
        return self._ax

Response: The matplotlib axes that the visualizer draws upon.


Instruction:
Create a Python 3 function to
return the actual size in pixels as set by matplotlib or the user provided size if available.

Response:
def size(self):
        """
        Returns the actual size in pixels as set by matplotlib, or
        the user provided size if available.
        """
        if not hasattr(self, "_size") or self._size is None:
            fig = plt.gcf()
            self._size = fig.get_size_inches()*fig.dpi
        return self._size


Instruction:
Implement a Python 3 function for
making the magic happen and a visualizer appear.

Response:
def poof(self, outpath=None, clear_figure=False, **kwargs):
        """
        Poof makes the magic happen and a visualizer appear! You can pass in
        a path to save the figure to disk with various backends, or you can
        call it with no arguments to show the figure either in a notebook or
        in a GUI window that pops up on screen.

        Parameters
        ----------
        outpath: string, default: None
            path or None. Save figure to disk or if None show in window

        clear_figure: boolean, default: False
            When True, this flag clears the figure after saving to file or
            showing on screen. This is useful when making consecutive plots.

        kwargs: dict
            generic keyword arguments.

        Notes
        -----
        Developers of visualizers don't usually override poof, as it is
        primarily called by the user to render the visualization.
        """
        # Ensure that draw has been called
        if self._ax is None:
            warn_message = (
                "{} does not have a reference to a matplotlib.Axes "
                "the figure may not render as expected!"
            )
            warnings.warn(
                warn_message.format(self.__class__.__name__), YellowbrickWarning
            )

        # Finalize the figure
        self.finalize()

        if outpath is not None:
            plt.savefig(outpath, **kwargs)
        else:
            plt.show()

        if clear_figure:
            plt.gcf().clear()


Instruction:
Can you write a function in Python 3 where it
sets the title on the current axes.

Response:
def set_title(self, title=None):
        """
        Sets the title on the current axes.

        Parameters
        ----------
        title: string, default: None
            Add title to figure or if None leave untitled.
        """
        title = self.title or title
        if title is not None:
            self.ax.set_title(title)


Instruction:
Write a Python 3 function for
generating the subplots for the number of given models.

Response:
def generate_subplots(self):
        """
        Generates the subplots for the number of given models.
        """
        _, axes = plt.subplots(len(self.models), sharex=True, sharey=True)
        return axes


Instruction:
Can you tell what is the following Python 3 function doing
def predict(self, X, y):
        for model in self.models:
            yield cvp(model, X, y, cv=12)

Response: Returns a generator containing the predictions for each of the internal models in the set of target or class values.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_corpus(path):

    # Check if the data exists, otherwise download or raise
    if not os.path.exists(path):
        raise ValueError((
            "'{}' dataset has not been downloaded, "
            "use the yellowbrick.download module to fetch datasets"
        ).format(path))

    # Read the directories in the directory as the categories.
    categories = [
        cat for cat in os.listdir(path)
        if os.path.isdir(os.path.join(path, cat))
    ]

    files  = [] # holds the file names relative to the root
    data   = [] # holds the text read from the file
    target = [] # holds the string of the category

    # Load the data from the files in the corpus
    for cat in categories:
        for name in os.listdir(os.path.join(path, cat)):
            files.append(os.path.join(path, cat, name))
            target.append(cat)

            with open(os.path.join(path, cat, name), 'r') as f:
                data.append(f.read())

    # Return the data bunch for use similar to the newsgroups example
    return Bunch(
        categories=categories,
        files=files,
        data=data,
        target=target,
    )

Response: Loads and wrangles the passed in text corpus by path.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_data_home(path=None):
    if path is None:
        path = os.environ.get('YELLOWBRICK_DATA', FIXTURES)

    path = os.path.expanduser(path)
    path = os.path.expandvars(path)

    if not os.path.exists(path):
        os.makedirs(path)

    return path

Response: Returns the path of the Yellowbrick data directory.