amueller / introduction_to_ml_with_python

Notebooks and code for the book "Introduction to Machine Learning with Python"
7.45k stars 4.57k forks source link

Figure 2.38 - UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored #168

Open Jcolinese opened 2 years ago

Jcolinese commented 2 years ago

in chapter 2 the In[76] does not produce a graph - and i get this Warning UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored

rolandlamar commented 1 year ago

Am also working thru the scripts in chapter 2 using Jupyter Lab in Windows 11. Tried numerous various but couldn't solve the cmap problem so I rewrote it using scatter3D. This works:

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits import mplot3d

X_new = np.hstack([X, X[:, 1:] ** 2])

ax = plt.figure().add_subplot(projection='3d') ax.view_init(elev=-152, azim=-26)

mask = y == 0

ax.scatter3D(X_new[mask, 0], X_new[mask, 1], X_new[mask, 2], c='b', s=60, edgecolor='k') ax.scatter3D(X_new[~mask, 0], X_new[~mask, 1], X_new[~mask, 2], c='r', marker='^', s=60, edgecolor='k') ax.set_xlabel("feature0") ax.set_ylabel("feature1") ax.set_zlabel("feature1 ** 2") plt.show()

Script [77] has the same problem. If you run these sequentially in Jupyter do not need the first three lines. This works:

linear_svm_3d = LinearSVC().fit(X_new, y) coef, intercept = linear_svm3d.coef.ravel(), linear_svm3d.intercept

ax = plt.figure().add_subplot(projection='3d') ax.view_init(elev=-152, azim=-26) xx = np.linspace(X_new[:, 0].min() - 2, X_new[:, 0].max() + 2, 50) yy = np.linspace(X_new[:, 1].min() - 2, X_new[:, 1].max() + 2, 50)

XX, YY = np.meshgrid(xx, yy) ZZ = (coef[0] XX + coef[1] YY + intercept) / -coef[2] ax.plot_surface(XX, YY, ZZ, rstride=8, cstride=8, alpha=0.3) ax.scatter3D(X_new[mask, 0], X_new[mask, 1], X_new[mask, 2], c='b', s=60, edgecolor='k') ax.scatter3D(X_new[~mask, 0], X_new[~mask, 1], X_new[~mask, 2], c='r', marker='^', s=60, edgecolor='k')

ax.set_xlabel("feature0") ax.set_ylabel("feature1") ax.set_zlabel("feature1 ** 2")

Jcolinese commented 1 year ago

Legend thankyou

Arnold815 commented 1 year ago

thx for the solution

wanghedy commented 1 year ago

您的邮件已收到,我将会尽快处理,谢谢。王宇凝

wanghedy commented 1 year ago

您的邮件已收到,我将会尽快处理,谢谢。王宇凝

Irvenlikcrea commented 1 year ago

you, my friend, hero! thanks for the solution.