My problem is that I have 3 features, but I only want to plot a 2D graphic while using 2 features at a time and show all the possible combinations.
The problem is that I did classifier.fit(X_train, Y_train)
so it expects to be trained with 3 features, not just 2. X_train
is the size (70, 3) which is (n_samples, n_features).
So far I tweaked the original code to add z_min
and z_max
, since I do need to have this third feature that I need to be able to use classifier.predict()
.
The error I get at the plt.contourf
instruction is Input z must be a 2D array.
import matplotlib as pl
import matplotlib.colors as colors
import matplotlib.cm as cmx
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
z_min, z_max = X_train[:, 2].min() - 1, X_train[:, 2].max() + 1
xx, yy, zz = np.meshgrid(np.arange(x_min, x_max, 0.1),
np.arange(y_min, y_max, 0.1),
np.arange(z_min, z_max, 0.1))
fig, ax = plt.subplots()
# here "model" is your model's prediction (classification) function
Z = classifier.predict(np.c_[np.c_[xx.ravel(), yy.ravel()], zz.ravel()])
# Put the result into a color plot
Z = Z.reshape(len(Z.shape), 2)
plt.contourf(xx, yy, Z, cmap=pl.cm.Paired)
plt.axis('off')
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)
print(z.shape)
= (4612640,)
print(xx.shape)
= (20, 454, 508)
How can I plot a 2D array + train with 3 features but only plot 2 features and keep the right shape for my array Z
? How can I get Z
to the right size?
What I tried so far:
I want something like this, bus instead I have 2 features and I can only predict 2 values, not 3 like the example.
But again all the examples I'm seeing, they are only training with 2 features so they are good to go from my understanding, they are not facing my problem with the Z
shape that's not the right one.
Would it also be possible to visualize this with a 3D graphic so we can see the 3 features ?