<jupyter_start><jupyter_text>PCA  Calculate the number of principal components<jupyter_code>import numpy as np
import pandas as pd
import matplotlib as mpl
from matplotlib import pyplot as plt
from sklearn.datasets import load_wine
from sklearn.preprocessing import StandardScaler<jupyter_output><empty_output><jupyter_text>Data import and scaling<jupyter_code>wineData = load_wine().data
wineData = StandardScaler().fit_transform(wineData)<jupyter_output><empty_output><jupyter_text>Covariance matrix calculation<jupyter_code>features = wineData.T
cov_matrix = np.cov(features)
values, vectors = np.linalg.eig(cov_matrix)
valueSum = np.sum(values)

values = sorted(((v, i) for i, v in enumerate(values)), reverse=True)
variancesSum = []
partSum = 0
for i, j in values:
    variancesSum.append(i/valueSum+partSum)
    partSum += i/valueSum

plt.title("Variances sum")
plt.scatter(range(1,14), variancesSum)
plt.axhline(y=0.9)
plt.show()<jupyter_output><empty_output><jupyter_text>Based on this plot, 7 vectors are needed. The vectors are as following:<jupyter_code>for i, num in values[:7]:
    print(vectors[num])<jupyter_output>[-0.1443294   0.48365155 -0.20738262  0.0178563  -0.26566365  0.21353865
  0.05639636 -0.01496997  0.39613926 -0.26628645 -0.50861912 -0.22591696
  0.21160473]
[ 0.24518758  0.22493093  0.08901289 -0.53689028  0.03521363  0.53681385
 -0.42052391 -0.02596375  0.06582674  0.12169604  0.07528304  0.07648554
 -0.30907994]
[ 0.00205106  0.31606881  0.6262239   0.21417556 -0.14302547  0.15447466
  0.14917061  0.14121803 -0.17026002 -0.04962237  0.30769445 -0.49869142
 -0.02712539]
[ 0.23932041 -0.0105905   0.61208035 -0.06085941  0.06610294 -0.10082451
  0.28696914 -0.09168285  0.42797018 -0.05574287 -0.20044931  0.47931378
  0.05279942]
[-0.14199204  0.299634    0.13075693  0.35179658  0.72704851  0.03814394
 -0.3228833  -0.05677422 -0.15636143  0.06222011 -0.27140257  0.07128891
  0.06787022]
[-0.39466085  0.06503951  0.14617896 -0.19806835 -0.14931841 -0.0841223
  0.02792498  0.46390791 -0.40593409 -0.30388245 -0.28603452  0.30434119
 -0.32013135]
[-0.4229343  -0.00335981  0.1506819  -0.1[...]<jupyter_text>US Arrest dataset analysis<jupyter_code>df = pd.read_csv('usarrests.csv', index_col=0)
df.head()
df.info()
df.mean()
df.var()
from sklearn.preprocessing import scale
X = pd.DataFrame(scale(df), index=df.index, columns=df.columns)
from sklearn.decomposition import PCA
pca_loadings = pd.DataFrame(PCA().fit(X).components_.T, index=df.columns, columns=['V1', 'V2', 'V3', 'V4'])
pca_loadings
# Fit the PCA model and transform X to get the principal components
pca = PCA()
df_plot = pd.DataFrame(pca.fit_transform(X), columns=['PC1', 'PC2', 'PC3', 'PC4'], index=X.index)
df_plot.head()
fig , ax1 = plt.subplots(figsize=(9,7))
ax1.set_xlim(-3.5,3.5)
ax1.set_ylim(-3.5,3.5)
# Plot Principal Components 1 and 2
for i in df_plot.index:
    ax1.annotate(i, (-df_plot.PC1.loc[i], -df_plot.PC2.loc[i]), ha='center')
# Plot reference lines
ax1.hlines(0,-3.5,3.5, linestyles='dotted', colors='grey')
ax1.vlines(0,-3.5,3.5, linestyles='dotted', colors='grey')
ax1.set_xlabel('First Principal Component')
ax1.set_ylabel('Second Principal Component')
# Plot Principal Component loading vectors, using a second y-axis.
ax2 = ax1.twinx().twiny()
ax2.set_ylim(-1,1)
ax2.set_xlim(-1,1)
ax2.set_xlabel('Principal Component loading vectors', color='red')
# Plot labels for vectors. Variable 'a' is a small offset parameter to separate arrow tip and text.
a = 1.07
for i in pca_loadings[['V1', 'V2']].index:
    ax2.annotate(i, (-pca_loadings.V1.loc[i]*a, -pca_loadings.V2.loc[i]*a), color='red')
# Plot vectors
ax2.arrow(0,0,-pca_loadings.V1[0], -pca_loadings.V2[0])
ax2.arrow(0,0,-pca_loadings.V1[1], -pca_loadings.V2[1])
ax2.arrow(0,0,-pca_loadings.V1[2], -pca_loadings.V2[2])
ax2.arrow(0,0,-pca_loadings.V1[3], -pca_loadings.V2[3])
plt.show()
pca.explained_variance_
pca.explained_variance_ratio_
plt.figure(figsize=(7,5))
plt.plot([1,2,3,4], pca.explained_variance_ratio_, '-o')
plt.ylabel('Proportion of Variance Explained')
plt.xlabel('Principal Component')
plt.xlim(0.75,4.25)
plt.ylim(0,1.05)
plt.xticks([1,2,3,4])
plt.show()
plt.figure(figsize=(7,5))
plt.plot([1,2,3,4], np.cumsum(pca.explained_variance_ratio_), '-s')
plt.ylabel('Proportion of Variance Explained')
plt.xlabel('Principal Component')
plt.xlim(0.75,4.25)
plt.ylim(0,1.05)
plt.xticks([1,2,3,4])
plt.show()<jupyter_output><empty_output><jupyter_text>Cancer cell data analysis<jupyter_code>df2 = pd.read_csv('nci60.csv').drop('Unnamed: 0', axis=1)
df2.columns = np.arange(df2.columns.size)
y = pd.read_csv('nci60_y.csv', usecols=[1], skiprows=1, names=['type'])
df2.info()
# Scale the data
X = pd.DataFrame(scale(df2))
X.shape
# Fit the PCA model and transform X to get the principal components
pca2 = PCA()
df2_plot = pd.DataFrame(pca2.fit_transform(X))
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(15,6))
color_idx = pd.factorize(y.type)[0]
cmap = mpl.cm.hsv
# Left plot
ax1.scatter(df2_plot.iloc[:,0], df2_plot.iloc[:,1], c=color_idx, cmap=cmap, alpha=0.5, s=50)
ax1.set_ylabel('Principal Component 2')
# Right plot
ax2.scatter(df2_plot.iloc[:,0], df2_plot.iloc[:,2], c=color_idx, cmap=cmap, alpha=0.5, s=50)
ax2.set_ylabel('Principal Component 3')
# Custom legend for the classes (y) since we do not create scatter plots per class (which could have their own labels).
handles = []
labels = pd.factorize(y.type.unique())
norm = mpl.colors.Normalize(vmin=0.0, vmax=14.0)
for i, v in zip(labels[0], labels[1]):
    handles.append(mpl.patches.Patch(color=cmap(norm(i)), label=v, alpha=0.5))
ax2.legend(handles=handles, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
# xlabel for both plots
for ax in fig.axes:
    ax.set_xlabel('Principal Component 1')
    plt.show()
pd.DataFrame([df2_plot.iloc[:,:5].std(axis=0, ddof=0).to_numpy(),
pca2.explained_variance_ratio_[:5],
np.cumsum(pca2.explained_variance_ratio_[:5])],
index=['Standard Deviation', 'Proportion of Variance', 'Cumulative Proportion'],
columns=['PC1', 'PC2', 'PC3', 'PC4', 'PC5'])
df2_plot.iloc[:,:10].var(axis=0, ddof=0).plot(kind='bar', rot=0)
plt.ylabel('Variances')
plt.show()
fig , (ax1,ax2) = plt.subplots(1,2, figsize=(15,5))
# Left plot
ax1.plot(pca2.explained_variance_ratio_, '-o')
ax1.set_ylabel('Proportion of Variance Explained')
ax1.set_ylim(ymin=-0.01)
# Right plot
ax2.plot(np.cumsum(pca2.explained_variance_ratio_), '-ro')
ax2.set_ylabel('Cumulative Proportion of Variance Explained')
ax2.set_ylim(ymax=1.05)
for ax in fig.axes:
    ax.set_xlabel('Principal Component')
ax.set_xlim(-1,65)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16442293147783996, 'fraction_numerical': 0.1453637876266828, 'mean_word_length': 5.572564612326044, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '665302', 'license': 'Unlicense', 'n_tokens_mistral': 3309, 'n_tokens_neox': 2834, 'n_words': 516, 'path': 'PCA.ipynb', 'repo_name': 'researcher2312/machine-learning', 'size': '6611'}