Repository: scikit-learn/scikit-learn
File: sklearn/inspection/_plot/decision_boundary.py (Python)

Code before:
                "'multiclass_colors'."
            )
        if cmap.N < 256:
            # Special case for the qualitative colormaps that encode a
            # discrete set of colors that are easily distinguishable, contrary to other
            # colormaps that are continuous (for which `cmap.N` >= 256).
            return mpl.colors.to_rgba_array(cmap.colors[:n_classes])
        else:
            return cmap(np.linspace(0, 1, n_classes))

    elif isinstance(multiclass_colors, list):
        if len(multiclass_colors) != n_classes:
            raise ValueError(
                "When 'multiclass_colors' is a list, it must be of the same "
                f"length as the classes or labels to plot ({n_classes}), got: "
                f"{len(multiclass_colors)}."
            )
        elif any(not mpl.colors.is_color_like(col) for col in multiclass_colors):
            raise ValueError(
                "When 'multiclass_colors' is a list, it can only contain valid"
                f" Matplotlib color names. Got: {multiclass_colors}"
            )
        return [mpl.colors.to_rgba(color) for color in multiclass_colors]

    else:
        raise ValueError("'multiclass_colors' must be a list or a str.")


class DecisionBoundaryDisplay:
    """Decisions boundary visualization.

    It is recommended to use
    :func:`~sklearn.inspection.DecisionBoundaryDisplay.from_estimator`
    to create a :class:`DecisionBoundaryDisplay`. All parameters are stored as
    attributes.

    Read more in the :ref:`User Guide <visualizations>`.

    For a detailed example comparing the decision boundaries of multinomial and
    one-vs-rest logistic regression, please see
    :ref:`sphx_glr_auto_examples_linear_model_plot_logistic_multinomial.py`.

    .. versionadded:: 1.1

    Parameters
    ----------
    xx0 : ndarray of shape (grid_resolution, grid_resolution)
        First output of :func:`meshgrid <numpy.meshgrid>`.

    xx1 : ndarray of shape (grid_resolution, grid_resolution)
        Second output of :func:`meshgrid <numpy.meshgrid>`.

Review comment (suggestion):
When the type is invalid, it's more correct to raise `TypeError` instead of `ValueError`.

```suggestion
        raise TypeError("'multiclass_colors' must be a list or a str.")
```

Code after:
                "'multiclass_colors'."
            )
        if cmap.N < 256:
            # Special case for the qualitative colormaps that encode a
            # discrete set of colors that are easily distinguishable, contrary to other
            # colormaps that are continuous (for which `cmap.N` >= 256).
            return mpl.colors.to_rgba_array(cmap.colors[:n_classes])
        else:
            return cmap(np.linspace(0, 1, n_classes))

    elif isinstance(multiclass_colors, list):
        if len(multiclass_colors) != n_classes:
            raise ValueError(
                "When 'multiclass_colors' is a list, it must be of the same "
                f"length as the classes or labels to plot ({n_classes}), got: "
                f"{len(multiclass_colors)}."
            )
        elif any(not mpl.colors.is_color_like(col) for col in multiclass_colors):
            raise ValueError(
                "When 'multiclass_colors' is a list, it can only contain valid"
                f" Matplotlib color names. Got: {multiclass_colors}"
            )
        return [mpl.colors.to_rgba(color) for color in multiclass_colors]

    else:
        raise TypeError("'multiclass_colors' must be a list or a str.")


class DecisionBoundaryDisplay:
    """Decisions boundary visualization.

    It is recommended to use
    :func:`~sklearn.inspection.DecisionBoundaryDisplay.from_estimator`
    to create a :class:`DecisionBoundaryDisplay`. All parameters are stored as
    attributes.

    Read more in the :ref:`User Guide <visualizations>`.

    For a detailed example comparing the decision boundaries of multinomial and
    one-vs-rest logistic regression, please see
    :ref:`sphx_glr_auto_examples_linear_model_plot_logistic_multinomial.py`.

    .. versionadded:: 1.1

    Parameters
    ----------
    xx0 : ndarray of shape (grid_resolution, grid_resolution)
        First output of :func:`meshgrid <numpy.meshgrid>`.

    xx1 : ndarray of shape (grid_resolution, grid_resolution)
        Second output of :func:`meshgrid <numpy.meshgrid>`.