<jupyter_start><jupyter_text>Demo of `pythontools` library and Principal Component Analysis Load librariesImport `pythontools` libraries.<jupyter_code>import numpy as np
import arraytools as arry
import statstools as stats
import plottools as pt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Generate DataGenerate toy example data matrix `X` with 2 features.<jupyter_code>tempA = arry.concate([2*np.random.randn(100,1)-3, 1*np.random.randn(100,1)-3], axis=1)
tempB = arry.concate([2*np.random.randn(100,1)+13, 1*np.random.randn(100,1)+3], axis=1)
X = arry.concate([tempA, tempB])

pcolor = 'k'
alpha = 0.5
marker_size=4
fig, ax = pt.makefig(figsize=(3,3))
ax.plot(X[:,0], X[:,1], 'o', color=pcolor, markeredgecolor=pcolor, ms=marker_size, alpha=alpha)
pt.set_axis(ax, x_ticks=[-10,-5,0,5,10,15,20], y_ticks=[-8,-4,0,4,8])
pt.set_axis(ax, xlim=[-12,None], ylim=[-8,None])
pt.set_label(ax, x_label='x', y_label='y',)<jupyter_output><empty_output><jupyter_text>PCARun `statstools.pca` to perform Principal Component Analysis using covariance method.<jupyter_code>C = stats.cov(X)
print(C)
eigvec, eigval, proj = stats.pca(X)<jupyter_output><empty_output><jupyter_text>Eigenvalues<jupyter_code>fig_size=(2,1)
fig, ax = pt.makefig(figsize=fig_size)
ax.plot(eigval, 'o', color='b', markeredgecolor='b')
pt.set_axis(ax, xlim=[-0.5,1.5], ylim=[-0.1,2.2],)
pt.set_axis(ax, x_ticks=range(2), x_ticks_label=np.arange(1,3), y_ticks=[0,1,2])
pt.set_label(ax, x_label='Eigenvalue number', y_label='Variance', title='Eigenvalues')
ax.grid()<jupyter_output><empty_output><jupyter_text>Projections to PC1 and PC2<jupyter_code>fig_size=(3,2)
fig, ax = pt.makefig(figsize=fig_size)
ax.plot(proj[:,0], proj[:,1], 'o', color='b', markeredgecolor='b', ms=marker_size, alpha=alpha)
pt.set_axis(ax, x_ticks=[-2,-1,0,1,2], y_ticks=[-1,0,1], xlim=[-2.5,2.5], ylim=[-1.1,1.1])
pt.set_label(ax, x_label='pc1', y_label='pc2',)<jupyter_output><empty_output><jupyter_text>Principal components on normalized data points<jupyter_code>def pcLine(x, eigvec, mu):
    '''
    y = ax+b
    '''
    a = eigvec[1]/eigvec[0]
    b = mu[1] - a*mu[0]
    return a*x+b

B = stats.normalizeX(X)
mu_A = np.mean(B[range(tempA.shape[0]),:],0)
mu_B = np.mean(B[range(tempB.shape[0],B.shape[0]),:],0)
mid = np.mean(np.vstack((mu_A, mu_B)),0)
x = np.linspace(-2.2,2,100)
y1 = pcLine(x, eigvec[:,0], mu_A)
y2 = pcLine(x, eigvec[:,1], mid)

fig_size=(2,2)
fig, ax = pt.makefig(figsize=fig_size)
ax.plot(B[:,0], B[:,-1], 'o', color='k', markeredgecolor='k', ms=marker_size, alpha=alpha)
ax.plot(x, y1, 'r')
ax.plot(x, y2, 'b')
pt.set_axis(ax, xlim=[-3,3], ylim=[-3,3])
pt.set_label(ax, x_label='x', y_label='y',)
pt.text(ax, 'PC1', [0.7,0.9], 'r', ftsize=12)
pt.text(ax, 'PC2', [0.7,0.3], 'b', ftsize=12)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21380050053628888, 'fraction_numerical': 0.0493385770468359, 'mean_word_length': 7.377245508982036, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '329588', 'license': 'MIT', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1289, 'n_words': 198, 'path': 'examples/Principal Component Analysis.ipynb', 'repo_name': 'bongsoos/pythontools', 'size': '2797'}