<jupyter_start><jupyter_text>Bimodal distribution  generate random data<jupyter_code>np.random.seed(1) # random seed for consistency
N = 100
cov = np.eye(3)/2.0
class1_sample = np.random.multivariate_normal(np.array([0,0,0]), cov, N).T
class2_sample = np.random.multivariate_normal(np.array([2,2,2]), cov, N).T
class3_sample = np.random.multivariate_normal(np.array([0,4,0]), cov, N).T
all_samples = np.concatenate((class1_sample, class2_sample, class3_sample), axis=1)
cols = np.array([(1.0, 0.2, 0.2)]*N + [(0.5, 0.5, 1)]*N  + [(1.0, 0.2, 1.0)]*N)<jupyter_output><empty_output><jupyter_text>plot data in 3D<jupyter_code>fig = plt.figure(figsize=(20,5))
ax = fig.add_subplot(1, 3, 1, projection='3d')
ax.scatter(all_samples[0,:], all_samples[1,:], all_samples[2,:], s=10, c=cols, alpha=0.7, linewidth=0)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.axis('equal')
ax.view_init(elev=1., azim=240)

ax = fig.add_subplot(1, 3, 2, projection='3d')
ax.scatter(all_samples[0,:], all_samples[1,:], all_samples[2,:], s=10, c=cols, alpha=0.7, linewidth=0)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.axis('equal')
ax.view_init(elev=25, azim=200)

ax = fig.add_subplot(1, 3, 3, projection='3d')
ax.scatter(all_samples[0,:], all_samples[1,:], all_samples[2,:], s=10, c=cols, alpha=0.7, linewidth=0)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.axis('equal')
ax.view_init(elev=50, azim=200)

plt.show()<jupyter_output><empty_output><jupyter_text>performing eigenvalue decomposition on covariance matrix<jupyter_code># eigenvectors and eigenvalues for the from the covariance matrix
X_centered = all_samples - np.matrix(np.mean(all_samples, 1)).T * np.ones((1, all_samples.shape[1]))
eig_vec, eig_val, _ = svd(X_centered / np.sqrt(all_samples.shape[1] - 1))


fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection='3d')
mu = np.mean(all_samples, 1)
ax.scatter(all_samples[0,:], all_samples[1,:], all_samples[2,:], s=40, c=cols, alpha=0.2, linewidth=0)
for i in xrange(3):
    end = mu + eig_vec[:, i] * eig_val[i]
    end_plus = mu + eig_vec[:, i] * eig_val[i]
    a = Arrow3D([mu[0], end[0]], [mu[1], end[1]], [mu[2], end[2]], mutation_scale=20, lw=1, arrowstyle="-|>", color="g")
    ax.add_artist(a)
    ax.text(end_plus[0], end_plus[1], end_plus[2], 'PC %d' % (i+1), color='g')
ax.axis('equal')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.view_init(elev=20, azim=200)
ax.set_title('Eigenvectors')
plt.show()<jupyter_output><empty_output><jupyter_text>Projecting to 2D using PCA<jupyter_code>transformed = eig_vec[:, 0:2].T.dot(all_samples)

fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)
ax.scatter(transformed[0,:], transformed[1,:], s=40, c=cols, alpha=0.7, linewidth=0)
plt.xlabel('PC 1')
plt.ylabel('PC 2')
plt.title('Transformed samples')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2087260034904014, 'fraction_numerical': 0.05340314136125655, 'mean_word_length': 7.073239436619718, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '351027', 'license': 'MIT', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1264, 'n_words': 203, 'path': 'jupyter_notebooks/PCA example - bimodal 1.ipynb', 'repo_name': 'eladnoor/sysbio2016', 'size': '2865'}