Reputation: 2789
I have the following data and labels I am transforming through PCA. The labels are only 0 or 1.
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import seaborn as sns
import numpy as np
fields = ["Occupancy", "Temperature", "Humidity", "Light", "CO2", "HumidityRatio", "NSM", "WeekStatus"]
df = pd.read_csv('datatraining-updated.csv', skipinitialspace=True, usecols=fields, sep=',')
#Get the output from pandas as a numpy matrix
final_data=df.values
#Data
X = final_data[:,1:8]
#Labels
y = final_data[:,0]
#Normalize features
X_scaled = StandardScaler().fit_transform(X)
#Apply PCA on them
pca = PCA(n_components=7).fit(X_scaled)
#Transform them with PCA
X_reduced = pca.transform(X_scaled)
Then, I just want to show, in a 3D graph, the 3 PCA features with highest variance, I can find them as follows
#Show variable importance
importance = pca.explained_variance_ratio_
print('Explained variation per principal component:
{}'.format(importance))
After that, I want to plot only the top-3 highest variance features. So, I previously select them in the code below
X_reduced=X_reduced[:, [0, 4, 5]]
Ok, here is my problem: I can plot them without the legend. When I try to plot them using the following code
# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax = fig.gca(projection='3d')
colors = ("red", "gray")
for data, color, group in zip(X_reduced, colors, y):
dim1,dim2,dim3=data
ax.scatter(dim1, dim2, dim3, c=color, edgecolors='none',
label=group)
plt.title('Matplot 3d scatter plot')
plt.legend(y)
plt.show()
I get the following error:
plot_data-3d-pca.py:56: UserWarning: Requested projection is different from current axis projection, creating new axis with requested projection.
ax = fig.gca(projection='3d')
plot_data-3d-pca.py:56: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
ax = fig.gca(projection='3d')
Traceback (most recent call last):
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3.py", line 307, in idle_draw
self.draw()
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3agg.py", line 76, in draw
self._render_figure(allocation.width, allocation.height)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3agg.py", line 20, in _render_figure
backend_agg.FigureCanvasAgg.draw(self)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1709, in draw
renderer, self, artists, self.suppressComposite)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 292, in draw
reverse=True)):
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 291, in <lambda>
key=lambda col: col.do_3d_projection(renderer),
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 545, in do_3d_projection
ecs = (_zalpha(self._edgecolor3d, vzs) if self._depthshade else
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 847, in _zalpha
rgba = np.broadcast_to(mcolors.to_rgba_array(colors), (len(zs), 4))
File "<__array_function__ internals>", line 6, in broadcast_to
File "/home/unica-server/.local/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 182, in broadcast_to
return _broadcast_to(array, shape, subok=subok, readonly=True)
File "/home/unica-server/.local/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 127, in _broadcast_to
op_flags=['readonly'], itershape=shape, order='C')
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (0,4) and requested shape (1,4)
My y's shape is (8143,) and my X_reduced's shape is (8143,3)
What is my mistake?
EDIT: The data I am using can be found here
Upvotes: 0
Views: 836
Reputation: 394
The first warning Requested projection is different from current axis projection
is because you are trying to change the projection of an axis after its creation with ax = fig.gca(projection='3d')
but you cannot. Set the projection at creation instead.
To fix the second error, replace edgecolors='none'
by edgecolors=None
.
The following corrected code works for me.
# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d') # set projection at creation of axis
# ax = fig.gca(projection='3d') # you cannot change the projection after creation
colors = ("red", "gray")
for data, color, group in zip(X_reduced, colors, y):
dim1,dim2,dim3=data
# replace 'none' by None
ax.scatter(dim1, dim2, dim3, c=color, edgecolors=None, label=group)
plt.title('Matplot 3d scatter plot')
plt.legend(y)
plt.show()
EDIT : Above is my answer to what I understood of the original question. Below is a looped version of mad's own answer.
class_values = [0, 1]
labels = ['Empty', 'Full']
n_class = len(class_values)
# allocate lists
index_class = [None] * n_class
X_reduced_class = [None] * n_class
for i, class_i in enumerate(class_values) :
# get where are the 0s and 1s labels
index_class[i] = np.where(np.isin(y, class_i))
# get reduced PCA for each label
X_reduced_class[i] = X_reduced[index_class[i]]
colors = ['blue', 'red']
# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
ids_plot = [0, 4, 5]
for i in range(n_class) :
# get the three interesting columns
data = X_reduced_class[i][:, ids_plot]
ax.scatter(data[:,0], data[:,1], data[:,2], c=colors[i], edgecolor='k', s=40, label=labels[i])
ax.set_title("Data Visualization with 3 highest variance dimensions with PCA")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])
ax.legend()
plt.show()
Upvotes: 1
Reputation: 2789
I solved the error in a different way.
I did not know that, for each label, I had to do a different scatterplot. Thanks to this post I found the answer.
My solution was first to separate the labels and data from one class, and then do the same for the other class. Finally, I plot them separately with different scatterplots. So, firstly I identify the different labels (I have only two labels, 0 or 1) and their data (their corresponding Xs).
#Get where are the 0s and 1s labels
index_class1 = np.where(np.isin(y, 0))
index_class2 = np.where(np.isin(y, 1))
#Get reduced PCA for each label
X_reducedclass1=X_reduced[index_class1][:]
X_reducedclass2=X_reduced[index_class2][:]
Then, I will plot each PCA reduced vectors from each class in different scatterplots
colors = ['blue', 'red']
# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
scatter1=ax.scatter(X_reducedclass1[:, 0], X_reducedclass1[:, 4], X_reducedclass1[:, 5], c=colors[0], cmap=plt.cm.Set1, edgecolor='k', s=40)
scatter2=ax.scatter(X_reducedclass2[:, 0], X_reducedclass2[:, 4], X_reducedclass2[:, 5], c=colors[1], cmap=plt.cm.Set1, edgecolor='k', s=40)
ax.set_title("Data Visualization with 3 highest variance dimensions with PCA")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])
#ax.legend(np.unique(y))
ax.legend([scatter1, scatter2], ['Empty', 'Full'], loc="upper right")
plt.show()
Which gives me this beautiful image
Of course, such a code can be simplified with a for loop too (altough I have no idea how to do that).
Upvotes: 0