<jupyter_start><jupyter_text>Ejercicio Aplicando PCA: Principal Component Analysis  Importamos librerias<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (16, 9)
plt.style.use('ggplot')

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier<jupyter_output><empty_output><jupyter_text>Cargamos datos de entrada<jupyter_code>dataframe = pd.read_csv(r"comprar_alquilar.csv")

dataframe.tail(10)
for col in dataframe:
    print(col)
    print(sorted(dataframe[col].unique()))
dataframe.describe()
dataframe.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 202 entries, 0 to 201
Data columns (total 10 columns):
 #   Column          Non-Null Count  Dtype
---  ------          --------------  -----
 0   ingresos        202 non-null    int64
 1   gastos_comunes  202 non-null    int64
 2   pago_coche      202 non-null    int64
 3   gastos_otros    202 non-null    int64
 4   ahorros         202 non-null    int64
 5   vivienda        202 non-null    int64
 6   estado_civil    202 non-null    int64
 7   hijos           202 non-null    int64
 8   trabajo         202 non-null    int64
 9   comprar         202 non-null    int64
dtypes: int64(10)
memory usage: 15.9 KB<jupyter_text>Visualicemos las dimensiones<jupyter_code>fig,axes =plt.subplots(5,2, figsize=(12, 9)) # 2 columnas cada una con 5 figuras, total 10 features
alquilar=dataframe[dataframe['comprar']==0] # conviene alquilar casa
comprar=dataframe[dataframe['comprar']==1] # conviene comprar casa
ax=axes.ravel()
for i in range(len(dataframe.columns)):
  _,bins=np.histogram(dataframe[dataframe.columns[i]],bins=40)
  ax[i].hist(alquilar.values[:,i],bins=bins,color='r',alpha=.5)# rojo alquilar
  ax[i].hist(comprar.values[:,i],bins=bins,color='g',alpha=0.3)# verde para comprar
  ax[i].set_title(dataframe.columns[i],fontsize=9)
  ax[i].axes.get_xaxis().set_visible(False)
  ax[i].set_yticks(())
ax[0].legend(['alquilar','comprar'],loc='best',fontsize=8)
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Ejemplo de cómo se correlacionan<jupyter_code>plt.subplot(1,2,1)#fisrt plot
plt.scatter(dataframe['ingresos'], dataframe['ahorros'], s=dataframe['vivienda']*0.0005, color='magenta', label='check', alpha=0.3)
plt.xlabel('Ingresos',fontsize=12)
plt.ylabel('Ahorros',fontsize=12)
plt.subplot(1,2,2)# 2nd plot
plt.scatter(dataframe['gastos_comunes'], dataframe['gastos_otros'], s=dataframe['vivienda']*0.0005, color='purple', label='check', alpha=0.3)
plt.xlabel('Gastos Comunes',fontsize=12)
plt.ylabel('Vivienda',fontsize=12)
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Vamos a Normalizar o Estandarizar los datos<jupyter_code>scaler = StandardScaler()
df = dataframe.drop(['comprar'], axis=1)
scaler.fit(df)
X_scaled = scaler.transform(df)<jupyter_output><empty_output><jupyter_text>Aplicamos PCA<jupyter_code>pca = PCA(n_components = 9)
pca.fit(X_scaled)
X_pca = pca.transform(X_scaled)
print("shape of X_pca", X_pca.shape)<jupyter_output>shape of X_pca (202, 9)<jupyter_text>Veamos cuántos buenos predictores son las nuevas dimensiones<jupyter_code>expl = pca.explained_variance_ratio_
print(expl)
print("suma", np.sum(expl[0:5]))
print("suma acumulada", np.cumsum(expl[0:-1]))<jupyter_output>[0.29913557 0.23294923 0.11711241 0.10685622 0.09635279 0.05629866
 0.04145579 0.03017819 0.01966115]
suma 0.852406211771431
suma acumulada [0.29913557 0.5320848  0.64919721 0.75605343 0.85240621 0.90870487
 0.95016066 0.98033885]<jupyter_text>graficamos la variacion explicada acumulada<jupyter_code>plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel("number of components")
plt.ylabel("cumulative explained variance")<jupyter_output><empty_output><jupyter_text>Veamos la correlación entre las nuevas componentes y las dimensiones originales<jupyter_code>plt.matshow(pca.components_[0:5],cmap='viridis')
plt.yticks([0,1,2,3,4],['1st Comp','2nd Comp','3rd Comp','4th Comp','5th Comp'],fontsize=10)
plt.colorbar()
plt.xticks(range(len(df.columns)),df.columns,rotation=65,ha='left')
#plt.tight_layout()
plt.show()#<jupyter_output>C:\Users\Admin\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:3: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
  This is separate from the ipykernel package so we can avoid doing imports until<jupyter_text>Preparo nuevas entradas para validar el algoritmo<jupyter_code># Split dataset in training and test datasets
# Split dataset in training and test datasets
dfScaledPca = pd.DataFrame(X_pca, columns = df.columns)
dfScaledPca["comprar"]=dataframe["comprar"]
print (dfScaledPca.head())
print(dfScaledPca.shape)
X_train, X_test = train_test_split(dfScaledPca, test_size=0.2, random_state=4) #6 
y_train =X_train["comprar"]
y_test = X_test["comprar"]
X_train = X_train.drop(['comprar'], axis=1)
X_test = X_test.drop(['comprar'], axis=1)<jupyter_output>ingresos  gastos_comunes  pago_coche  gastos_otros   ahorros  vivienda  \
0 -1.321915       -0.222950   -1.599863     -0.885014  0.646617 -0.700208   
1 -1.147878        1.716967   -0.395722     -0.447960 -0.687805 -0.231969   
2 -0.090369        1.836066   -1.359382      0.337505  0.742387  0.593030   
3 -3.455898       -0.078279    0.202382     -1.573584 -0.068551 -0.051945   
4 -0.916933       -1.184676   -0.190036      0.117804 -0.147753 -0.619750   

   estado_civil     hijos   trabajo  comprar  
0     -0.318031  0.751809  0.269984        1  
1     -1.542487 -0.221799  0.438425        0  
2      1.540495 -0.668120  0.153463        1  
3     -0.909409 -0.684486 -0.129045        0  
4      0.450419 -0.194341  0.687452        1  
(202, 10)<jupyter_text>Prediccion con KNN  Con datos de entrada con reducción de dimensiones por PCA<jupyter_code>knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit(X_train.values, y_train)
y_pred = knn.predict(X_test)<jupyter_output>C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\base.py:439: UserWarning: X has feature names, but KNeighborsClassifier was fitted without feature names
  f"X has feature names, but {self.__class__.__name__} was fitted without"<jupyter_text>Resultados Obtenidos<jupyter_code>print("precision set entrenamiento", knn.score (X_train, y_train))
print("precision set test", knn.score(X_test, y_test))<jupyter_output>precision set entrenamiento 0.9440993788819876
precision set test 0.8048780487804879
{'fraction_non_alphanumeric': 0.12432035268185158, 'fraction_numerical': 0.10418809698750918, 'mean_word_length': 4.980667838312829, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '418937', 'license': 'MIT', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2626, 'n_words': 504, 'path': '4-Machine_Learning/2-No Supervisado/1-PCA/Ejercicio_PCA_teoria - Clase.ipynb', 'repo_name': 'erfederuiz/thebridge_ft_nov21', 'size': '6805'}