rasbt / machine-learning-book

Code Repository for Machine Learning with PyTorch and Scikit-Learn
https://sebastianraschka.com/books/#machine-learning-with-pytorch-and-scikit-learn
MIT License
3.64k stars 1.31k forks source link

ValueError: 'red' is not a valid color value. #178

Closed kcolombe closed 6 months ago

kcolombe commented 6 months ago

Hello. It should be simple but for some reason, I am having trouble getting the plot_decision_regions function to work. Any reason why red wouldn't be a valid color?

def plot_decision_regions(X, y, classifier, resolution=0.02):

    # setup marker generator and color map
    markers = ('o', 's', '^', 'v', '<')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])

    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    lab = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    lab = lab.reshape(xx1.shape)
    plt.contourf(xx1, xx2, lab, alpha=0.3, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())

    # plot class examples
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], 
                    y=X[y == cl, 1],
                    alpha=0.8, 
                    c=colors[idx],
                    marker=markers[idx], 
                    label=f'Class {cl}', 
                    edgecolor='black')
plot_decision_regions(X, y, classifier=ppn)
plt.xlabel('Sepal length [cm]')
plt.ylabel('Petal length [cm]')
plt.legend(loc='upper left')
plt.show()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[13], [line 31](vscode-notebook-cell:?execution_count=13&line=31)
     [23](vscode-notebook-cell:?execution_count=13&line=23)     for idx, cl in enumerate(np.unique(y)):
     [24](vscode-notebook-cell:?execution_count=13&line=24)         plt.scatter(x=X[y == cl, 0], 
     [25](vscode-notebook-cell:?execution_count=13&line=25)                     y=X[y == cl, 1],
     [26](vscode-notebook-cell:?execution_count=13&line=26)                     alpha=0.8, 
   (...)
     [29](vscode-notebook-cell:?execution_count=13&line=29)                     label=f'Class {cl}', 
     [30](vscode-notebook-cell:?execution_count=13&line=30)                     edgecolor='black')
---> [31](vscode-notebook-cell:?execution_count=13&line=31) plot_decision_regions(X, y, classifier=ppn)
     [32](vscode-notebook-cell:?execution_count=13&line=32) plt.xlabel('Sepal length [cm]')
     [33](vscode-notebook-cell:?execution_count=13&line=33) plt.ylabel('Petal length [cm]')

Cell In[13], [line 18](vscode-notebook-cell:?execution_count=13&line=18)
     [16](vscode-notebook-cell:?execution_count=13&line=16) lab = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
     [17](vscode-notebook-cell:?execution_count=13&line=17) lab = lab.reshape(xx1.shape)
---> [18](vscode-notebook-cell:?execution_count=13&line=18) plt.contourf(xx1, xx2, lab, alpha=0.3, cmap=cmap)
     [19](vscode-notebook-cell:?execution_count=13&line=19) plt.xlim(xx1.min(), xx1.max())
     [20](vscode-notebook-cell:?execution_count=13&line=20) plt.ylim(xx2.min(), xx2.max())

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2938, in contourf(data, *args, **kwargs)
   [2936](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2936) @_copy_docstring_and_deprecators(Axes.contourf)
   [2937](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2937) def contourf(*args, data=None, **kwargs) -> QuadContourSet:
-> [2938](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2938)     __ret = gca().contourf(
   [2939](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2939)         *args, **({"data": data} if data is not None else {}), **kwargs
   [2940](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2940)     )
   [2941](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2941)     if __ret._A is not None:  # type: ignore[attr-defined]
   [2942](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py:2942)         sci(__ret)

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1465, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   [1462](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1462) @functools.wraps(func)
   [1463](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1463) def inner(ax, *args, data=None, **kwargs):
   [1464](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1464)     if data is None:
-> [1465](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1465)         return func(ax, *map(sanitize_sequence, args), **kwargs)
   [1467](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1467)     bound = new_sig.bind(ax, *args, **kwargs)
   [1468](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1468)     auto_label = (bound.arguments.get(label_namer)
   [1469](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py:1469)                   or bound.kwargs.get(label_namer))

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6528, in Axes.contourf(self, *args, **kwargs)
   [6519](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6519) """
   [6520](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6520) Plot filled contours.
   [6521](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6521) 
   (...)
   [6525](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6525) %(contour_doc)s
   [6526](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6526) """
   [6527](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6527) kwargs['filled'] = True
-> [6528](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6528) contours = mcontour.QuadContourSet(self, *args, **kwargs)
   [6529](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6529) self._request_autoscale_view()
   [6530](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6530) return contours

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:887, in ContourSet.__init__(self, ax, levels, filled, linewidths, linestyles, hatches, alpha, origin, extent, cmap, colors, norm, vmin, vmax, extend, antialiased, nchunk, locator, transform, negative_linestyles, clip_path, *args, **kwargs)
    [884](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:884) self.labelTexts = []
    [885](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:885) self.labelCValues = []
--> [887](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:887) self.set_cmap(cmap)
    [888](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:888) if norm is not None:
    [889](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:889)     self.set_norm(norm)

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:602, in ScalarMappable.set_cmap(self, cmap)
    [600](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:600) self.cmap = _ensure_cmap(cmap)
    [601](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:601) if not in_init:
--> [602](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:602)     self.changed()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1145, in ContourSet.changed(self)
   [1143](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1143) def changed(self):
   [1144](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1144)     if not hasattr(self, "cvalues"):
-> [1145](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1145)         self._process_colors()  # Sets cvalues.
   [1146](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1146)     # Force an autoscale immediately because self.to_rgba() calls
   [1147](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1147)     # autoscale_None() internally with the data passed to it,
   [1148](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1148)     # so if vmin/vmax are not set yet, this would override them with
   [1149](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1149)     # content from *cvalues* rather than levels like we want
   [1150](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1150)     self.norm.autoscale_None(self.levels)

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1302, in ContourSet._process_colors(self)
   [1300](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1300) else:
   [1301](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1301)     self.cvalues = self.layers
-> [1302](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1302) self.norm.autoscale_None(self.levels)
   [1303](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1303) self.set_array(self.cvalues)
   [1304](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1304) self.update_scalarmappable()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1408, in Normalize.autoscale_None(self, A)
   [1405](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1405)         A = A.data
   [1407](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1407) if self.vmin is None and A.size:
-> [1408](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1408)     self.vmin = A.min()
   [1409](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1409) if self.vmax is None and A.size:
   [1410](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1410)     self.vmax = A.max()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1264, in Normalize.vmin(self, value)
   [1262](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1262) if value != self._vmin:
   [1263](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1263)     self._vmin = value
-> [1264](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1264)     self._changed()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1292, in Normalize._changed(self)
   [1287](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1287) def _changed(self):
   [1288](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1288)     """
   [1289](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1289)     Call this whenever the norm is changed to notify all the
   [1290](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1290)     callback listeners to the 'changed' signal.
   [1291](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1291)     """
-> [1292](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1292)     self.callbacks.process('changed')

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:303, in CallbackRegistry.process(self, s, *args, **kwargs)
    [301](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:301) except Exception as exc:
    [302](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:302)     if self.exception_handler is not None:
--> [303](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:303)         self.exception_handler(exc)
    [304](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:304)     else:
    [305](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:305)         raise

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:87, in _exception_printer(exc)
     [85](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:85) def _exception_printer(exc):
     [86](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:86)     if _get_running_interactive_framework() in ["headless", None]:
---> [87](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:87)         raise exc
     [88](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:88)     else:
     [89](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:89)         traceback.print_exc()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:298, in CallbackRegistry.process(self, s, *args, **kwargs)
    [296](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:296) if func is not None:
    [297](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:297)     try:
--> [298](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:298)         func(*args, **kwargs)
    [299](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:299)     # this does not capture KeyboardInterrupt, SystemExit,
    [300](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:300)     # and GeneratorExit
    [301](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:301)     except Exception as exc:

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1150, in ContourSet.changed(self)
   [1145](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1145)     self._process_colors()  # Sets cvalues.
   [1146](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1146) # Force an autoscale immediately because self.to_rgba() calls
   [1147](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1147) # autoscale_None() internally with the data passed to it,
   [1148](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1148) # so if vmin/vmax are not set yet, this would override them with
   [1149](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1149) # content from *cvalues* rather than levels like we want
-> [1150](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1150) self.norm.autoscale_None(self.levels)
   [1151](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1151) self.set_array(self.cvalues)
   [1152](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1152) self.update_scalarmappable()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1410, in Normalize.autoscale_None(self, A)
   [1408](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1408)     self.vmin = A.min()
   [1409](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1409) if self.vmax is None and A.size:
-> [1410](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1410)     self.vmax = A.max()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1275, in Normalize.vmax(self, value)
   [1273](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1273) if value != self._vmax:
   [1274](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1274)     self._vmax = value
-> [1275](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1275)     self._changed()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1292, in Normalize._changed(self)
   [1287](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1287) def _changed(self):
   [1288](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1288)     """
   [1289](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1289)     Call this whenever the norm is changed to notify all the
   [1290](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1290)     callback listeners to the 'changed' signal.
   [1291](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1291)     """
-> [1292](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1292)     self.callbacks.process('changed')

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:303, in CallbackRegistry.process(self, s, *args, **kwargs)
    [301](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:301) except Exception as exc:
    [302](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:302)     if self.exception_handler is not None:
--> [303](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:303)         self.exception_handler(exc)
    [304](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:304)     else:
    [305](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:305)         raise

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:87, in _exception_printer(exc)
     [85](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:85) def _exception_printer(exc):
     [86](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:86)     if _get_running_interactive_framework() in ["headless", None]:
---> [87](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:87)         raise exc
     [88](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:88)     else:
     [89](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:89)         traceback.print_exc()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:298, in CallbackRegistry.process(self, s, *args, **kwargs)
    [296](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:296) if func is not None:
    [297](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:297)     try:
--> [298](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:298)         func(*args, **kwargs)
    [299](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:299)     # this does not capture KeyboardInterrupt, SystemExit,
    [300](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:300)     # and GeneratorExit
    [301](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cbook.py:301)     except Exception as exc:

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1152, in ContourSet.changed(self)
   [1150](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1150) self.norm.autoscale_None(self.levels)
   [1151](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1151) self.set_array(self.cvalues)
-> [1152](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1152) self.update_scalarmappable()
   [1153](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1153) alphas = np.broadcast_to(self.get_alpha(), len(self.cvalues))
   [1154](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/contour.py:1154) for label, cv, alpha in zip(self.labelTexts, self.labelCValues, alphas):

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py:920, in Collection.update_scalarmappable(self)
    [918](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py:918)         # pcolormesh, scatter, maybe others flatten their _A
    [919](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py:919)         self._alpha = self._alpha.reshape(self._A.shape)
--> [920](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py:920)     self._mapped_colors = self.to_rgba(self._A, self._alpha)
    [922](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py:922) if self._face_is_mapped:
    [923](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py:923)     self._facecolors = self._mapped_colors

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:509, in ScalarMappable.to_rgba(self, x, alpha, bytes, norm)
    [507](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:507) if norm:
    [508](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:508)     x = self.norm(x)
--> [509](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:509) rgba = self.cmap(x, alpha=alpha, bytes=bytes)
    [510](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py:510) return rgba

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:725, in Colormap.__call__(self, X, alpha, bytes)
    [702](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:702) r"""
    [703](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:703) Parameters
    [704](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:704) ----------
   (...)
    [722](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:722) RGBA values with a shape of ``X.shape + (4, )``.
    [723](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:723) """
    [724](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:724) if not self._isinit:
--> [725](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:725)     self._init()
    [727](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:727) xa = np.array(X, copy=True)
    [728](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:728) if not xa.dtype.isnative:
    [729](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:729)     # Native byteorder is faster.

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1175, in ListedColormap._init(self)
   [1173](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1173) def _init(self):
   [1174](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1174)     self._lut = np.zeros((self.N + 3, 4), float)
-> [1175](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1175)     self._lut[:-3] = to_rgba_array(self.colors)
   [1176](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1176)     self._isinit = True
   [1177](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:1177)     self._set_extremes()

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:489, in to_rgba_array(c, alpha)
    [487](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:487)         raise e
    [488](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:488) if isinstance(c, str):
--> [489](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:489)     raise ValueError(f"{c!r} is not a valid color value.")
    [491](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:491) if len(c) == 0:
    [492](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py:492)     return np.zeros((0, 4), float)

ValueError: 'red' is not a valid color value.`
rasbt commented 6 months ago

Hm, this looks kind of familiar. I think there was a particular matplotlib version that had that issue. E.g. as discussed in #149. Could you print your matplotlib version here so we can further dig into what's going on here?

kcolombe commented 6 months ago

import matplotlib matplotlib.version '3.8.0'

I did seem to solve it with from matplotlib.colors import to_rgba, ListedColormap ` def plot_decision_regions(X, y, classifier, resolution=0.02):

setup marker generator and color map

markers = ('o', 's', '^', 'v', '<')  # Define marker styles for different classes
colors = ['red', 'blue', 'lightgreen', 'gray', 'cyan']  # Define colors for different classes
rgba_colors = [to_rgba(color) for color in colors]  # Convert color names to RGBA format
cmap = ListedColormap(rgba_colors[:len(np.unique(y))])  # Create a colormap from the list of colors`
rasbt commented 6 months ago

Oh I see, thanks for reporting! I think this is specifically a matplotlib 3.8 bug. If you downgrade to 3.7.2 or upgrade to 3.9, it should work fine. E.g. pip install matplotlib --upgrade.

I added an extra check for Matplotlib 3.8 here, which is imported at the top of each notebook: https://github.com/rasbt/machine-learning-book/blob/be3ad1f5bc6ab614dfdaccaa085ba9d69074853c/python_environment_check.py#L40

kcolombe commented 6 months ago

Thank you very much