<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import linalg

from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
df =pd.read_csv("/content/economicas.csv")
df
df.info()
X=df[['MOD_LECTURA_CRITICA_PUNT','MOD_COMPETEN_CIUDADA_PUNT','MOD_COMUNI_ESCRITA_PUNT']]
X.isna().sum()
X.info()
X = X.to_numpy()
y=df[['PUNT_GLOBAL']]
Y=y.to_numpy()
l = []
for i in X.T:
  u = i.mean()
  s = i.std()
  scal = (i - u) / s
  l.append(scal)

x_scal = np.array(l).T
cov_x = np.cov(x_scal.T)
cov_x
np.linalg.eig(pd.DataFrame(X).corr().to_numpy())
val_p, vec_p = linalg.eig(cov_x)
val_p, vec_p
val_p = val_p[:2]
vec_p = vec_p[:, :2]
W = vec_p
pca_p = x_scal @ W
pca_p = pd.DataFrame(pca_p, columns=[f'PC{i}' for i in range(1, pca_p.shape[1] + 1)])
pca_p.head()
y=Y.T[0]
y@X
y

X
fig,ax = plt.subplots(1,1, figsize=(10,6))
markers = ['s','>','o']
for i,j in zip(np.unique(y), markers):
  ax.scatter(pca_p.iloc[:,0][y==i], pca_p.iloc[:,1][y==i], marker=j)

ax.set_xlabel(pca_p.columns[0], size=14)
ax.set_ylabel(pca_p.columns[1], size=14)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.show()
scal = StandardScaler()
X_scal = scal.fit_transform(X)

pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scal)

pca_2 = pd.DataFrame(X_pca, columns=['PC1','PC2'])
pca3 = PCA()

pca3.fit(X_scal)
pca3.explained_variance_
pca3.explained_variance_ratio_
np.cumsum(pca3.explained_variance_ratio_)
X.shape
y.shape
fig, ax = plt.subplots(1,1, figsize=(10,6))
ax.set_facecolor('#EFFFF5')
plt.plot(np.arange(1,5,1), pca3.explained_variance_ratio_, marker='s', label='varianza_explicada')
plt.plot(np.arange(1,5,1),np.cumsum(pca3.explained_variance_ratio_), marker='^', color='darkgreen', label='varianza_acumulada')
plt.Xlim(0.8,4.5)
plt.ylim(-0.05,1.05)
plt.Xticks(ticks=np.arange(1,5,1))
plt.axhline(1, ls='--', color='g', alpha=0.3)
plt.Xlabel('Número de componentes', size=14)
plt.ylabel('% varianza explicada', size=14)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
plt.grid(alpha=0.2, axis='both')
plt.show()
sklearn_loadings = pca.components_.T * np.sqrt(pca.explained_variance_)

sklearn_loadings<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18585526315789475, 'fraction_numerical': 0.02631578947368421, 'mean_word_length': 6.723809523809524, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '157487', 'license': 'MIT', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1127, 'n_words': 170, 'path': 'proyecto_8_true.ipynb', 'repo_name': 'julianrojas19/julianrojas19.github.io', 'size': '2432'}