Issue
So I'm trying to recreate a 3D plot from an example in my homework. It seems that something's missing, in the end I only see a blank window, but there supposed to be a 3d plot as well.
So this should be right code, but somehow it doesn't work, I wonder why..
import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from mpl_toolkits.mplot3d import Axes3D
X, y = make_blobs(centers=4, random_state=8)
# додаємо другу ознаку, зведену в квадрат
X_new = np.hstack([X, X[:, 1:] ** 2])
# print(X, y, X_new)
linear_svm_3d = LinearSVC().fit(X_new, y)
coef, intercept = linear_svm_3d.coef_.ravel(), linear_svm_3d.intercept_
# показати розділяючу поверхню лінійного класифікатора
figure = plt.figure()
ax = Axes3D(figure, elev=-152, azim=-26)
xx = np.linspace(X_new[:, 0].min() - 2, X_new[:, 0].max() + 2)
yy = np.linspace(X_new[:, 1].min() - 2, X_new[:, 1].max() + 2)
XX, YY = np.meshgrid(xx, yy)
ZZ = (coef[0] * XX + coef[1] * YY + intercept[0])/-coef[2]
ax.plot_surface(XX, YY, ZZ, rstride=8, cstride=8, alpha=0.3)
mask=y==0
ax.scatter(X_new[mask, 0], X_new[mask, 1], X_new[mask, 2], c='b', marker='o', s=60)
ax.scatter(X_new[~mask, 0], X_new[~mask, 1], X_new[~mask, 2], c='r', marker='^', s=60)
ax.set_xlabel('Ознака 0')
ax.set_ylabel('Ознака 1')
ax.set_zlabel('Ознака 2')
plt.show()
In the result I just get this:
Blank window
Solution
I've changed the way in which you declare the 3D figure (I think you were using a method from older versions of matplotlib).
Replace ax = Axes3D(figure, elev=-152, azim=-26)
with:
ax = figure.add_subplot(projection='3d')
ax.view_init(elev=-152, azim=-26)
Answered By - user3128
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.