Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return self
def draw(self, **kwargs):
pass
class MockTransformer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None, **kwargs):
return self
def transform(self, X, **kwargs):
return X
class MockVisualTransformer(Visualizer, TransformerMixin):
def fit(self, X, y=None, **kwargs):
self.draw(**kwargs)
return self
def transform(self, X, **kwargs):
return X
def draw(self, **kwargs):
pass
##########################################################################
## VisualPipeline Tests
##########################################################################
##########################################################################
## Mock Objects
##########################################################################
class Thing(object):
pass
class MockEstimator(BaseEstimator):
def fit(self, X, y=None, **kwargs):
return self
class MockVisualEstimator(Visualizer):
def fit(self, X, y=None, **kwargs):
self.draw(**kwargs)
return self
def draw(self, **kwargs):
pass
class MockTransformer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None, **kwargs):
return self
def transform(self, X, **kwargs):
return X
from yellowbrick.base import Visualizer
from yellowbrick.utils.wrapper import *
from sklearn.naive_bayes import MultinomialNB
try:
from unittest import mock
except ImportError:
import mock
##########################################################################
## Fixture
##########################################################################
class MockVisualizer(Visualizer):
def __init__(self, ax=None, **kwargs):
self.ax = ax
self.fit = mock.MagicMock()
self.finalize = mock.MagicMock()
self.poof = mock.MagicMock()
self.set_title = mock.MagicMock()
@property
def ax(self):
return self._ax
@ax.setter
def ax(self, val):
self._ax = val
## Imports
##########################################################################
import numpy as np
import scipy as sp
from yellowbrick.base import Visualizer
from sklearn.linear_model import LinearRegression
##########################################################################
## Cook's Distance
##########################################################################
class CooksDistance(Visualizer):
"""
Cook's Distance is a measure of how influential an instance is to the computation of
a regression, e.g. if the instance is removed would the estimated coeficients of the
underlying model be substantially changed? Because of this, Cook's Distance is
generally used to detect outliers in standard, OLS regression. In fact, a general
rule of thumb is that D(i) > 4/n is a good threshold for determining highly
influential points as outliers and this visualizer can report the percentage of data
that is above that threshold.
This implementation of Cook's Distance assumes Ordinary Least Squares regression,
and therefore embeds a ``sklearn.linear_model.LinearRegression`` under the hood.
Distance is computed via the non-whitened leverage of the projection matrix,
computed inside of ``fit()``. The results of this visualizer are therefore similar
to, but not as advanced, as a similar computation using statsmodels. Computing the
influence for other regression models requires leave one out validation and can be
expensive to compute.
def __init__(self, model, ax=None, fig=None, is_fitted="auto", **kwargs):
self.estimator = model
self.is_fitted = is_fitted
self.name = get_model_name(self.estimator)
# Initialize base classes independently
Wrapper.__init__(self, self.estimator)
Visualizer.__init__(self, ax=ax, fig=fig, **kwargs)
Base classes for target visualizers
"""
##########################################################################
# Imports
##########################################################################
from yellowbrick.base import Visualizer
##########################################################################
# TargetVisualizer Base Class
##########################################################################
class TargetVisualizer(Visualizer):
"""
The base class for target visualizers, generic enough to support any
computation on a single vector, y. This Visualizer is based on the
LabelEncoder in sklearn.preprocessing, which only accepts a target y.
Parameters
----------
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).
fig : matplotlib Figure, default: None
The figure to plot the Visualizer on. If None is passed in the current
plot will be used (or generated if required).
kwargs : dict
-------
legend: Legend artist
The artist created by the ax.legend() call, returned for further
manipulation if required by the caller.
Notes
-----
Right now this method simply draws the patches as rectangles and cannot
take into account the line or scatter plot properties (e.g. line style or
marker style). It is possible to add Line2D patches to the artist that do
add manual styles like this, which we can explore in the future.
.. seealso:: https://matplotlib.org/gallery/text_labels_and_annotations/custom_legends.html
"""
# Get access to the matplotlib Axes
if isinstance(g, Visualizer):
g = g.ax
elif g is None:
g = plt.gca()
# Ensure that labels and colors are the same length to prevent odd behavior.
if len(colors) != len(labels):
raise YellowbrickValueError(
"please specify the same number of colors as labels!"
)
# Create the legend handles with the associated colors and labels
handles = [
patches.Patch(color=color, label=label)
for color, label in zip(colors, labels)
]
Returns
-------
score : float or array-like
Returns the score of the underlying model, which is model-specific,
e.g. accuracy for classifiers, R2 for regressors, etc.
"""
raise NotImplementedError("ScoreVisualizer subclasses should implement score")
##########################################################################
## Multiple Models
##########################################################################
class VisualizerGrid(Visualizer):
"""
Used as a base class for visualizers that use subplots.
Parameters
----------
visualizers : A list of instantiated visualizers
nrows: integer, default: None
The number of rows desired, if you would like a fixed number of rows.
Specify only one of nrows and ncols, the other should be None. If you
specify nrows, there will be enough columns created to fit all the
visualizers specified in the visualizers list.
ncols: integer, default: None
The number of columns desired, if you would like a fixed number of columns.
Specify only one of nrows and ncols, the other should be None. If you
def visual_steps(self):
return dict(step for step in self.steps if isinstance(step[1], Visualizer))
from yellowbrick.style import resolve_colors
from yellowbrick.exceptions import NotFitted
from yellowbrick.utils.target import target_color_type, TargetType
from yellowbrick.exceptions import YellowbrickKeyError, YellowbrickValueError
from yellowbrick.style import palettes
from matplotlib.colors import Normalize
from sklearn.base import TransformerMixin
##########################################################################
## Feature Visualizers
##########################################################################
class FeatureVisualizer(Visualizer, TransformerMixin):
"""Base class for feature visualization.
Feature engineering is primarily conceptualized as a transformation or
extraction operation, e.g. some raw data is passed through a series of
transformers and mappings to result in some final dataset which can be
directly fitted to a model. Therefore feature visualizers are
transformers and support the sklearn transformer interface by implementing
a transform method.
Subclasses of the FeatureVisualizer may call draw either from fit or from
transform but must implement both so that they can be supported in pipeline
objects. By default, the transform method of the visualizer is just a data
pass through that ensures the visualizer can be placed into a feature
extraction workflow.
Parameters