Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_fit_class_labels_class_names_edge_case(self):
"""
Edge case that more class labels are defined than in datatset
"""
model = neighbors.KNeighborsClassifier(3)
viz = DecisionBoundariesVisualizer(
model, classes=['one', 'two', 'three', 'four', 'five'])
with pytest.raises(YellowbrickTypeError):
viz.fit(X_two_cols, y=y)
def test_only_regressors(self, model):
"""
Assert AlphaSelection only works with regressors
"""
with pytest.raises(YellowbrickTypeError):
AlphaSelection(model())
# Assign each class a unique number for drawing
if self.classes_ is None:
self.classes_ = {
label: str(kls_num)
for kls_num, label in enumerate(np.unique(y))
}
self.class_labels = None
elif len(set(y)) == len(self.classes_):
self.classes_ = {
label: str(kls_num)
for kls_num, label in enumerate(self.classes_)
}
self.class_labels = dict(zip(set(y), self.classes_))
else:
raise YellowbrickTypeError(
"""Number of classes must be the same length of number of
target y"""
)
# ensure that only
self.estimator.fit(X, y)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - (X[:, 0].min() * .01), X[:, 0].max() + (X[:, 0].max() * .01)
y_min, y_max = X[:, 1].min() - (X[:, 1].min() * .01), X[:, 1].max() + (X[:, 1].max() * .01)
self.ax.set_xlim([x_min, x_max])
self.ax.set_ylim([y_min, y_max])
# set the step increment for drawing the boundary graph
x_step = (x_max - x_min) * self.step_size
else:
raise YellowbrickTypeError("`sample` parameter must be int or float")
self.sample = sample
# Set sample parameters
if isinstance(shuffle, bool):
self.shuffle = shuffle
else:
raise YellowbrickTypeError("`shuffle` parameter must be boolean")
if self.shuffle:
if (random_state is None) or isinstance(random_state, int):
self._rng = RandomState(random_state)
elif isinstance(random_state, RandomState):
self._rng = random_state
else:
raise YellowbrickTypeError(
"`random_state` must be None, int, or np.random.RandomState"
)
else:
self._rng = None
# Visual and drawing parameters
self.fast = fast
self.alpha = alpha
self.show_vlines = vlines
self.vlines_kwds = vlines_kwds or {"linewidth": 1, "color": "black"}
# Internal properties
self._increments = None
self._colors = None
def __init__(self, model, ax=None, fig=None, force_model=False, **kwargs):
if not force_model and not isregressor(model):
raise YellowbrickTypeError(
"This estimator is not a regressor; try a classifier or "
"clustering score visualizer instead!"
)
self.force_model = force_model
super(RegressionScoreVisualizer, self).__init__(model, ax=ax, fig=fig, **kwargs)
Detects the model name for a Scikit-Learn model or pipeline.
Parameters
----------
model: class or instance
The object to determine the name for. If the model is an estimator it
returns the class name; if it is a Pipeline it returns the class name
of the final transformer or estimator in the Pipeline.
Returns
-------
name : string
The name of the model or pipeline.
"""
if not is_estimator(model):
raise YellowbrickTypeError(
"Cannot detect the model name for non estimator: '{}'".format(type(model))
)
else:
if isinstance(model, Pipeline):
return get_model_name(model.steps[-1][-1])
else:
return model.__class__.__name__
def __init__(
self,
model,
ax=None,
fig=None,
classes=None,
encoder=None,
is_fitted="auto",
force_model=False,
**kwargs,
):
# A bit of type checking
if not force_model and not isclassifier(model):
raise YellowbrickTypeError(
"This estimator is not a classifier; "
"try a regression or clustering score visualizer instead!"
)
# Initialize the super method.
super(ClassificationScoreVisualizer, self).__init__(
model, ax=ax, fig=fig, is_fitted=is_fitted, **kwargs
)
self.set_params(classes=classes, encoder=encoder, force_model=force_model)
def __init__(self, model, ax=None, fig=None, force_model=False, **kwargs):
if not force_model and not isclusterer(model):
raise YellowbrickTypeError(
"The supplied model is not a clustering estimator; try a "
"classifier or regression score visualizer instead!"
)
self.force_model = force_model
super(ClusteringScoreVisualizer, self).__init__(model, ax=ax, fig=fig, **kwargs)
def _find_importances_param(self):
"""
Searches the wrapped model for the feature importances parameter.
"""
for attr in ("feature_importances_", "coef_"):
try:
return getattr(self.estimator, attr)
except AttributeError:
continue
raise YellowbrickTypeError(
"could not find feature importances param on {}".format(
self.estimator.__class__.__name__
)
"`sample` parameter of type `int` must be greater than 1"
)
elif isinstance(sample, float):
if sample <= 0 or sample > 1:
raise YellowbrickValueError(
"`sample` parameter of type `float` must be between 0 and 1"
)
else:
raise YellowbrickTypeError("`sample` parameter must be int or float")
self.sample = sample
# Set sample parameters
if isinstance(shuffle, bool):
self.shuffle = shuffle
else:
raise YellowbrickTypeError("`shuffle` parameter must be boolean")
if self.shuffle:
if (random_state is None) or isinstance(random_state, int):
self._rng = RandomState(random_state)
elif isinstance(random_state, RandomState):
self._rng = random_state
else:
raise YellowbrickTypeError(
"`random_state` must be None, int, or np.random.RandomState"
)
else:
self._rng = None
# Visual and drawing parameters
self.fast = fast
self.alpha = alpha
self.show_vlines = vlines