<jupyter_start><jupyter_text>PCA reduction<jupyter_code># PCA analysis

# initialize PCA
pca = PCA(n_components=0.90)

crypto_pca = pca.fit_transform(X_scaled)

df_crypto_pca = pd.DataFrame(crypto_pca)

df_crypto_pca.head()<jupyter_output><empty_output><jupyter_text>PCA reduction decreased the number of features from 98 to 74 to preserve 90% explained variance<jupyter_code># t-SNE reduction to visualize the clustering

# initialize t-sne
tsne = TSNE(learning_rate=250)

tsne_features = tsne.fit_transform(df_crypto_pca)
# checking features are reduced to 2
tsne_features.shape
# plotting tsne reduction features
plt.scatter(tsne_features[:,0], tsne_features[:,1])
plt.show()<jupyter_output><empty_output><jupyter_text>The previous t-SNE reduction has created approximately two clusters with two outliers that consistently show up.  K-Means Clustering Inertia plot indicates that between k = 2 and k = 3 the elbow occurs.<jupyter_code>def get_clusters(k, data):
    # Initialize the K-Means model
    model = KMeans(n_clusters=k, random_state=0)

    # Train the model
    model.fit(data)

    # Predict clusters
    predictions = model.predict(data)

    # Create return DataFrame with predicted clusters
    data["class"] = model.labels_

    return data
inertia = []
k = list(range(1,10))

# loop through k values and save inertia for each one
for i in k:
    km = KMeans(n_clusters=i, random_state=0)
    km.fit(tsne_features)
    inertia.append(km.inertia_)
    
elbow_data = {'k': k, 'inertia': inertia}
df_elbow = pd.DataFrame(elbow_data)
df_elbow.head()
# plotting elbow curve data
plt.plot(df_elbow['k'], df_elbow['inertia'])
plt.xticks(range(1,11))
plt.xlabel('k value')
plt.ylabel('inertia')
plt.title('Inertia vs. k-value, Crypto K-Means Clustering, t-SNE reduced data')
plt.show()
df_crypto_tsne = pd.DataFrame(tsne_features, columns=['Feature 1', 'Feature 2'])


clusters_tsne = get_clusters(4, df_crypto_tsne)

clusters_tsne
fig, ax = plt.subplots(1,1, figsize=(7,5))

scatter = ax.scatter(x=clusters_tsne['Feature 1'], y=clusters_tsne['Feature 2'], c=clusters_tsne['class'])

legend = ax.legend(*scatter.legend_elements(), loc='upper left')

ax.add_artist(legend)

plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('K-means clustering on t-SNE reduced dataset')

plt.show()
df_crypto_tsne_clusted = pd.merge(df, clusters_tsne['class'], left_index=True, right_index=True)

df_crypto_tsne_clusted.head()
df_crypto_tsne_clusted[df_crypto_tsne_clusted['class'] == 2]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11949186184994046, 'fraction_numerical': 0.014688368400158794, 'mean_word_length': 6.0194986072423395, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '501768', 'license': 'ADSL', 'n_tokens_mistral': 972, 'n_tokens_neox': 910, 'n_words': 205, 'path': 'crypto_currencies_clustering.ipynb', 'repo_name': 'JacobTrevithick/Unsupervised-Learning-Homework', 'size': '2519'}