Python - Problem with PCA 3d plot

 
Vista:
sin imagen de perfil

Problem with PCA 3d plot

Publicado por Juan Diego (1 intervención) el 05/01/2023 22:59:13
I have this code with pip install pca, and the biplot 2d is great, but when i try to turn it into 3d plot is empty, and i already read the guide but i don't know what to do


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')

from pca import pca

df = pd.read_excel('Empezando/1.1 Matrices.xlsx', sheet_name= 'PCA' )
df.head()
df.info()
df.columns #sin()
df.shape
df['Meso'].value_counts() #tuve que cambiar el formato de meso a texto, sino no da
df

x = df.drop('Meso',axis=1) #scaling the feature will be automaticlly done
x
Labels = df.Meso
Labels


df.groupby(['Meso']).mean().round(2) #arroja las medias, en mi caso no sirve por que hay solo de a un dato

df.info()



model = pca(n_components=3, normalize=True, alpha=0.05, detect_outliers=['ht2', 'spe'])
results = model.fit_transform(x)
x
#resultados
loadings= results['loadings'] #ojo con los corchetes
loadings

scores = results['PC'] #ojo con los corchetes
scores['Labels'] = Labels
scores.head()

results['explained_var']

model.plot(figsize=(10,8))
plt.show()



model.biplot(n_feat=24, legend = False, figsize = (16,12), y = Labels, label= True, PC=[0,1,2], color_arrow='r', cmap='Set1', visible=True, verbose=3, d3 = False)
#24 n feat
plt.show()
problema
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Problem with PCA 3d plot

Publicado por antonio (65 intervenciones) el 07/01/2023 12:27:02
Hola buenas este seria un ejemplo sencillo para plots de 2d, 3d y pasar de 2d a 3d, añado excel ejemplo (Hi, good, this would be a simple example for 2d, 3d plots and going from 2d to 3d, add excel example)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import matplotlib.pyplot as plt
import pandas as pd
#read excel
var = pd.read_excel("Matrices.xlsx")
#obtain variables
x = list(var['X values'])
y = list(var['Y values'])
z = list(var['Z values'])
#example plot 2d x variable and y variable
plt.plot(x,y)
plt.ylabel('some numbers')
plt.show()
#new figure
fig = plt.figure(figsize=(4,4))
#use 3d
ax = fig.add_subplot(111, projection='3d')
#Points de 3d
ax.scatter(x,y,z)
plt.show()
 
#example pase 2d to 3d x and y variable
#new figure
fig = plt.figure(figsize=(4,4))
#use 3d
ax = fig.add_subplot(111, projection='3d')
#Changue de variables to z direction
ax.plot(x, y, zs=0, zdir='z', label='curve in (x, y)')
plt.show()
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar