У меня есть следующие данные и метки, которые я преобразовываю через PCA. Метки только 0 или 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)
Затем я просто хочу показать на трехмерном графике 3 функции PCA с наибольшей дисперсией, я могу найти их следующим образом
#Show variable importance
importance = pca.explained_variance_ratio_
print('Explained variation per principal component:
{}'.format(importance))
После этого я хочу отобразить только топ-3 высочайших отклонений. Итак, я предварительно выбрал их в коде ниже
X_reduced=X_reduced[:, [0, 4, 5]]
Хорошо, вот моя проблема: я могу построить их без легенды. Когда я пытаюсь построить их, используя следующий код
# 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()
, я получаю следующую ошибку:
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)
Моя форма у (8143,) и форма моего X_reduced (8143,3)
В чем моя ошибка?
РЕДАКТИРОВАТЬ: Используемые мной данные можно найти здесь