<jupyter_start><jupyter_text>Cryptocurrency clusters<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
from matplotlib import pyplot
from sklearn.decomposition import PCA
import plotly.express as px
import numpy as np
# Read crypto_data.csv into Pandas. 
df = pd.read_csv('crypto_data.csv', index_col =0)
df.head()
# Keep only cryptocurrencies that are trading
df = df.loc[df['IsTrading'] == True]
# drop the IsTrading column from the dataframe.
df.drop('IsTrading', axis=1, inplace=True)
df.head()
# Remove all rows that have at least one null value.
df = df.dropna(how='any',axis=0)
df.head()
# Filter for cryptocurrencies that have been mined. That is, the total coins mined should be greater than zero.
# mined_df = df.query('TotalCoinsMined > 0')
df = df.loc[df.TotalCoinsMined > 0]
df.head()
# Store the 'CoinName'column in its own DataFrame prior to dropping it from df 
coinname_df = pd.DataFrame(df.CoinName)
coinname_df.head()
# Drop the 'CoinName' column since it's not going to be used on the clustering algorithm
df = df.drop(columns = 'CoinName')
df.head()
# convert the remaining features with text values, Algorithm and ProofType, into numerical data. 
category_columns = ['Algorithm','ProofType']
get_dummies_df = pd.get_dummies(df, columns = category_columns)
# df = df.reindex(labels=df.columns,axis=1)
get_dummies_df.head()
# Standardize your dataset so that columns that contain larger values do not unduly influence the outcome.
# Scale the data

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaled_data = scaler.fit_transform(get_dummies_df)
scaled_data<jupyter_output><empty_output><jupyter_text>Dimensionality Reduction<jupyter_code># Perform dimensionality reduction with PCA
from sklearn.decomposition import PCA
pca = PCA(n_components=0.9)
pca_data = pca.fit_transform(scaled_data)

# Create a DataFrame with the principal components data
# pca_df = pd.DataFrame(
#     data=pca_data, columns=["PC 1", "PC 2", "PC 3"]
# )
# pca_df.head()
# pca_data
# Fetch the explained variance
pca.explained_variance_ratio_
print(pca_data.shape)
# reduce the dataset dimensions with t-SNE and visually inspect the results.
from sklearn.manifold import TSNE

# Initialize t-SNE model
tsne = TSNE(perplexity=50, learning_rate=100)    

# Reduce dimensions
tsne_features = tsne.fit_transform(pca_data)
tsne_features.shape
plt.scatter(tsne_features[:,0],tsne_features[:,1])
plt.colorbar()
plt.show()<jupyter_output><empty_output><jupyter_text>Cluster Analysis with k-Means  Create an elbow plot to identify the best number of clusters. Use a for-loop to determine the inertia for each k between 1 through 10. Determine, if possible, where the elbow of the plot is, and at which value of k it appears.<jupyter_code>from sklearn.cluster import KMeans
inertia = []
k = list(range(1, 11))

# Calculate the inertia for the range of k values
for i in k:
    km = KMeans(n_clusters=i, random_state=0)
    km.fit(tsne_features)
    inertia.append(km.inertia_)

# Create the Elbow Curve 
elbow_data = {"k": k, "inertia": inertia}
df_elbow = pd.DataFrame(elbow_data)

plt.plot(df_elbow['k'], df_elbow['inertia'], marker = "o")
plt.xticks(range(1,11))
plt.xlabel('Number of clusters')
plt.ylabel('Inertia')
plt.show()
tsne_features = pd.DataFrame(tsne_features)
tsne_features.head()
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
clusters = get_clusters(4, tsne_features)
clusters

plot_df = pd.DataFrame(data=clusters)

plot_df.head()
plt.scatter(plot_df[0],plot_df[1],c=plot_df['class'])
plt.xlabel('X')
plt.ylabel('Y')
plt.colorbar()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10336048879837068, 'fraction_numerical': 0.008146639511201629, 'mean_word_length': 5.559265442404007, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 15, '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': '282402', 'license': 'ADSL', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1329, 'n_words': 383, 'path': 'cryptocurrency_clusters.ipynb', 'repo_name': 'wycislo/cryptocurrency', 'size': '3928'}