<jupyter_start><jupyter_text>Clustering Crypto<jupyter_code># Initial imports
import requests
import pandas as pd
import matplotlib.pyplot as plt
import hvplot.pandas
import plotly.express as px
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from pathlib import Path
import altair as alt
# Load the cryptocurrencies data
file_path = Path("Resources/crypto_data.csv")
crypto_df = pd.read_csv(file_path, index_col=0)
crypto_df.head(10)<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code># Keep only necessary columns:
# 'CoinName','Algorithm','IsTrading','ProofType','TotalCoinsMined','TotalCoinSupply'
crypto_df=crypto_df[['CoinName','Algorithm','IsTrading','ProofType','TotalCoinsMined','TotalCoinSupply']]
crypto_df.head(10)
# Keep only cryptocurrencies that are trading
crypto_df = crypto_df[crypto_df["IsTrading"] == True]
# Keep only cryptocurrencies with a working algorithm
crypto_df = crypto_df[crypto_df["Algorithm"] != "N/A"]
# Remove the "IsTrading" column
crypto_df.drop("IsTrading", axis=1, inplace=True)
# Remove rows with at least 1 null value
crypto_df = crypto_df.dropna(axis=0, how="any")
# Remove rows with cryptocurrencies having no coins mined
crypto_df = crypto_df[crypto_df["TotalCoinsMined"] > 0]
# Drop rows where there are 'N/A' text values
crypto_df = crypto_df[crypto_df.iloc[:] != 'N/A'].dropna()
crypto_df.head(10)
# Store the 'CoinName'column in its own DataFrame prior to dropping it from crypto_df
coins_name = pd.DataFrame(crypto_df["CoinName"], index=crypto_df.index)
print(coins_name.shape)
coins_name.head()
# Drop the 'CoinName' column since it's not going to be used on the clustering algorithm
crypto_df = crypto_df.drop("CoinName", axis=1)
print(crypto_df.shape)
crypto_df.head(10)
# Create dummies variables for text features
X = pd.get_dummies(data=crypto_df, columns=["Algorithm", "ProofType"])
print(X.shape)
X.head(10)
# Standardize data
X = StandardScaler().fit_transform(X)
X[:5]<jupyter_output><empty_output><jupyter_text>Reducing Dimensions Using PCA<jupyter_code># Use PCA to reduce dimension to 3 principal components
pca = PCA(n_components=3)
df_pca = pca.fit_transform(X)
df_pca
# Create a DataFrame with the principal components data
pcs_df = pd.DataFrame(
    data = df_pca, columns = ['PC1','PC2','PC3'], index = crypto_df.index
)
pcs_df.head(10)<jupyter_output><empty_output><jupyter_text>Clustering Crytocurrencies Using K-Means Find the Best Value for `k` Using the Elbow Curve<jupyter_code>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(pcs_df)
    inertia.append(km.inertia_)

# Create the Elbow Curve using hvPlot
elbow_data = {"k": k, "inertia": inertia}
df_elbow = pd.DataFrame(elbow_data)
alt.Chart(df_elbow).mark_line().encode(x="k", y="inertia")<jupyter_output><empty_output><jupyter_text>Running K-Means with `k=`<jupyter_code># Initialize the K-Means model
model = KMeans(n_clusters=4, random_state=0)

# Fit the model
model.fit(pcs_df)

# Predict clusters
predictions = model.predict(pcs_df)

# Create a new DataFrame including predicted clusters and cryptocurrencies features
clustered_df = pd.concat([crypto_df, pcs_df], axis=1, sort=False)
clustered_df["CoinName"] = coins_name["CoinName"]
clustered_df["Class"] = model.labels_
print(clustered_df.shape)
clustered_df.head(10)<jupyter_output>(532, 9)<jupyter_text>Visualizing Results 3D-Scatter with Clusters<jupyter_code># Creating a 3D-Scatter with the PCA data and the clusters
fig = px.scatter_3d(
    clustered_df,
    hover_name="CoinName",
    hover_data=["Algorithm"],
    x="PC1",
    y="PC2",
    z="PC3",
    color="Class",
    symbol="Class",
    width=800,
)
fig.update_layout(legend=dict(x=0, y=1))
fig.show()<jupyter_output><empty_output><jupyter_text>Table of Tradable Cryptocurrencies<jupyter_code># Create a table with tradable cryptocurrencies.
clustered_df.hvplot.table(sortable=True, selectable=True)
print(clustered_df.count)<jupyter_output><bound method DataFrame.count of         Algorithm ProofType  TotalCoinsMined TotalCoinSupply       PC1  \
42         Scrypt   PoW/PoS     4.199995e+01              42 -0.340776   
404        Scrypt   PoW/PoS     1.055185e+09       532000000 -0.324095   
1337          X13   PoW/PoS     2.927942e+10    314159265359  2.306822   
BTC       SHA-256       PoW     1.792718e+07        21000000 -0.144050   
ETH        Ethash       PoW     1.076842e+08               0 -0.154448   
...           ...       ...              ...             ...       ...   
ZEPH      SHA-256      DPoS     2.000000e+09      2000000000  2.460587   
GAP        Scrypt   PoW/PoS     1.493105e+07       250000000 -0.338820   
BDX   CryptoNight       PoW     9.802226e+08      1400222610  0.324575   
ZEN      Equihash       PoW     7.296538e+06        21000000 -0.126936   
XBC        Scrypt       PoS     1.283270e+05         1000000 -0.300169   

           PC2       PC3     CoinName  Class  
42    1.014156 -0.572088      4[...]<jupyter_text>Scatter Plot with Tradable Cryptocurrencies<jupyter_code># Scale data to create the scatter plot
mm_scaler = MinMaxScaler()
plot_data = mm_scaler.fit_transform(
    clustered_df[["TotalCoinSupply", "TotalCoinsMined"]]
)
plot_df = pd.DataFrame(
    plot_data, columns=["TotalCoinSupply", "TotalCoinsMined"], index=clustered_df.index
)
plot_df["CoinName"] = clustered_df["CoinName"]
plot_df["Class"] = clustered_df["Class"]
plot_df.head()
# Create a hvplot.scatter plot using x="TotalCoinsMined" and y="TotalCoinSupply".
plot_df.hvplot.scatter(x="TotalCoinsMined", y="TotalCoinSupply", by="Class",hover_cols = ["CoinName"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11164964514453869, 'fraction_numerical': 0.0555651722347239, 'mean_word_length': 4.425352112676056, 'pattern_counts': {'":': 2, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '276413', 'license': 'Unlicense', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2054, 'n_words': 453, 'path': 'Starter_Files/crypto_clustering.ipynb', 'repo_name': 'Crawnicles/13-AWS', 'size': '5777'}