<jupyter_start><jupyter_text>2.1 Carga de los datos y librerías<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA 
from sklearn.preprocessing import StandardScaler
%matplotlib inline


my_sheet = 'Sheet1'
file_name = 'PCA_exercise_1.xlsx' # name of your excel file
df_in = pd.read_excel(file_name, sheet_name = my_sheet)
df_in.head() # shows headers with top 5 rows
#Cleaning the data
df_in.drop(df_in.columns[[0,1]],axis=1, inplace = True )
df_in.drop(0,inplace = True)
df_in.rename(columns={'Unnamed: 1':'Band','Unnamed: 2':'1', 'Unnamed: 3':'2', 'Unnamed: 4':'3', 'Unnamed: 5':'4'}, inplace=True)
df_in.rename(index={1: 'Band 1',2: 'Band 2',3: 'Band 3',4: 'Band 4',5: 'Band 5',6: 'Band 6',7: 'Band 7',8: 'Band 8',9: 'Band 9',
                    10: 'Band 10',11: 'Band 11',12: 'Band 12',13: 'Band 13',14: 'Band 14',15: 'Band 15',16: 'Band 16',17: 'Band 17'}, inplace=True)
df_in.head() # shows headers with top 5 rows
df = df_in.transpose()
df.head()
# Se divide la matriz del dataset 
X = df.iloc[:,0:17].values
# la submatriz x contiene los valores de las primeras 4 columnas del dataframe y todas las filas

print(X)<jupyter_output>[[375 57 245 1472 105 54 193 147 1102 720 253 685 488 198 360 1374 156]
 [135 47 267 1494 66 41 209 93 674 1033 143 586 355 187 334 1506 139]
 [458 53 242 1462 103 62 184 122 957 566 171 750 418 220 337 1572 147]
 [475 73 227 1582 103 64 235 160 1137 874 265 803 570 203 365 1256 175]]<jupyter_text>2.2 Normalización<jupyter_code>#Aplicamos una transformación de los datos para poder aplicar las propiedades de la distribución normal
from sklearn.preprocessing import StandardScaler
X_std = StandardScaler().fit_transform(X)
X_std.shape
#Let's check whether the normalized data has a mean of zero and a standard deviation of one.
np.mean(X_std),np.std(X_std)<jupyter_output><empty_output><jupyter_text>2.2.2 PCA analisis<jupyter_code>from sklearn.decomposition import PCA

pca_stars = PCA(n_components =2)
principalComponents_stars = pca_stars.fit_transform(X_std)
principal_stars_Df = pd.DataFrame(data = principalComponents_stars
             , columns = ['principal component 1', 'principal component 2'])
principal_stars_Df.head()
print('Explained variation per principal component: {}'.format(pca_stars.explained_variance_ratio_))
principal_stars_Df.rename(index={0:'Object 1',1:'Object 2', 2:'Object 3', 3:'Object 4'}, inplace=True)
principal_stars_Df.head()<jupyter_output><empty_output><jupyter_text>plot visualization<jupyter_code>objects = ['Object 1', 'Object 2' ,'Object 3', 'Object 4']
plt.figure()
plt.figure(figsize=(10,10))
plt.xticks(fontsize=12)
plt.yticks(fontsize=14)
plt.xlabel('Principal Component - 1',fontsize=20)
plt.ylabel('Principal Component - 2',fontsize=20)
plt.title("Principal Component Analysis of Breast Cancer Dataset",fontsize=20)
targets = ['Object 1', 'Object 2', 'Object 3' ,'Object 4']
colors = ['r', 'g','m','b']
objects
target
lista = []
for target, color in zip(targets,colors):
    indicesToKeep = objects == target
    
    lista.append(indicesToKeep)
    #principal_stars_Df.loc[indicesToKeep, 'principal component 1']
print(lista)
for target, color in zip(targets,colors):
    indicesToKeep = objects == target
    plt.scatter(principal_stars_Df.loc[indicesToKeep, 'principal component 1']
               , principal_stars_Df.loc[indicesToKeep, 'principal component 2'], c = color, s = 50)

plt.legend(targets,prop={'size': 15})<jupyter_output><empty_output><jupyter_text>2.3 Cálculo de autovectores y autovalores (eigenvector/eigenvalue)<jupyter_code># Calculamos la matriz de covarianza
print('NumPy covariance matrix: \n%s' %np.cov(X_std.T))
#Calculamos los autovalores y autovectores de la matriz y los mostramos

cov_mat = np.cov(X_std.T)

#eig_vals, eig_vecs = np.linalg.eig(cov_mat)
from scipy.linalg import svd
U, s, VT = svd(cov_mat)

print('U \n%s' %U)
print('s \n%s' %s)
print('VT \n%s' %VT)



#print('Eigenvectors \n%s' %eig_vecs)
#print('\nEigenvalues \n%s' %eig_vals)
pca = PCA(n_components=2)
pca.fit(X_std)
print(pca.components_)
#  Hacemos una lista de parejas (autovector, autovalor) 
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]

# Ordenamos estas parejas den orden descendiente con la función sort
eig_pairs.sort(key=lambda x: x[0], reverse=True)

# Visualizamos la lista de autovalores en orden desdenciente
print('Autovalores en orden descendiente:')
for i in eig_pairs:
    print(i[0])<jupyter_output><empty_output><jupyter_text>2.4 Seleccionamos los los autovectores correspondientes a las componentes principales<jupyter_code># A partir de los autovalores, calculamos la varianza explicada
tot = sum(eig_vals)
var_exp = [(i / tot)*100 for i in sorted(eig_vals, reverse=True)]
cum_var_exp = np.cumsum(var_exp)

# Representamos en un diagrama de barras la varianza explicada por cada autovalor, y la acumulada
with plt.style.context('seaborn-pastel'):
    plt.figure(figsize=(6, 4))

    plt.bar(range(4), var_exp, alpha=0.5, align='center',
            label='Varianza individual explicada', color='g')
    plt.step(range(4), cum_var_exp, where='mid', linestyle='--', label='Varianza explicada acumulada')
    plt.ylabel('Ratio de Varianza Explicada')
    plt.xlabel('Componentes Principales')
    plt.legend(loc='best')
    plt.tight_layout()<jupyter_output><empty_output><jupyter_text>2.5  Proyectamos los datos sobre un espacio de dimensionalidad menor.<jupyter_code>#Generamos la matríz a partir de los pares autovalor-autovector
matrix_w = np.hstack((eig_pairs[0][1].reshape(4,1),
                      eig_pairs[1][1].reshape(4,1)))

print('Matriz W:\n', matrix_w)

Y = X_std.dot(matrix_w)
df.insert(17, "Objects", ["Object 1","Object 2","Object 3","Object 4"]) 
df.head()
y = df.iloc[:,17].values
# El vector y contiene los valores de la 4 columna (especie)para todas las filas
with plt.style.context('seaborn-whitegrid'):
    plt.figure(figsize=(6, 4))
    for lab, col in zip(('Object 1', 'Object 2', 'Object 3','Object 4'),
                        ('magenta', 'cyan', 'limegreen','blue')):
        plt.scatter(Y[y==lab, 0],
                    Y[y==lab, 1],
                    label=lab,
                    c=col)
    plt.xlabel('Componente Principal 1')
    plt.ylabel('Componente Principal 2')
    plt.legend(loc='lower center')
    plt.tight_layout()
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13376885985378753, 'fraction_numerical': 0.057551718774303935, 'mean_word_length': 5.0546139359698685, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '382231', 'license': 'MIT', 'n_tokens_mistral': 2674, 'n_tokens_neox': 2342, 'n_words': 618, 'path': 'PCA/.ipynb', 'repo_name': 'DavidAguilarParedes/DATAH_challenge2020', 'size': '6429'}