<jupyter_start><jupyter_text>Clustering Crypto<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Fetching Cryptocurrency Data<jupyter_code>url = "https://min-api.cryptocompare.com/data/all/coinlist"

from pathlib import Path
# Creating a DataFrame
file_path = ('./Resources/crypto_data.csv')
crypto_df = pd.read_csv(file_path)
crypto_df.head()
#df.dtypes<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code># Traspose the data
for column in crypto_df.columns:
  print(f"Column {column} has {crypto_df[column].isnull().sum()} null values")
# Remove unnecessary columns

crypto_df.head()
# Keep only cryptocurrencies that are on trading
#key_columns = ['Unnamed: 0', 'CoinName', 'Algorithm', 'IsTrading', 'TotalCoinsMined', 'TotalCoinSupply']
#for col in key_columns:
#    IsTrading[col] = IsTrading[col].replace('False', None)
for index, row in crypto_df.iterrows():
    if(index, row['IsTrading']) == "False":
        row['IsTrading'].replace('False', 'NaN', inplace=True)
 
#import numpy as np
#crypto_df = crypto_df.replace('False', np.NaN)

for column in crypto_df.columns:
  print(f"Column {column} has {crypto_df[column].isnull().sum()} null values")     
print(index, row['IsTrading'])
# Keep only cryptocurrencies with a working algorithm


# Remove the "IsTrading" column
crypto_df.drop(columns=['IsTrading'], inplace=True)
crypto_df.head()

# Removing rows with at least 1 null value
crypto_df.dropna(inplace=True)
crypto_df.isnull().sum()
# Removing rows with cryptocurrencies without coins mined
import numpy as np

crypto_df = crypto_df.replace(0, np.nan)
crypto_df = crypto_df.dropna(how='all', axis=0)
crypto_df.head()
# Dropping rows where there are 'N/A' text values
crypto_df.dropna(axis=0, how='any', thresh=None, subset=None, inplace=True)
crypto_df.head()
# Fetching the cryptocurrencies names prior to drop them from crypto_df
print(crypto_df['CoinName'])
# Removig the cryptocurrency name since it's not going to be used on the clustering algorithm
#crypto_df.drop(columns=['CoinName'],inplace=True)
#crypto_df.head()
crypto_df.rename(columns = {'Unnamed: 0':'Coin'}, inplace = True) 
crypto_df.set_index(('Coin'), inplace=True)
crypto_df.head()

# Creating dummies variables for text features
crypto_dummies = pd.get_dummies(crypto_df)
crypto_dummies.head()
# Standardizing data

crypto_scaled = StandardScaler().fit_transform(crypto_dummies)
print(crypto_scaled[0:5])<jupyter_output>[[-0.11416167 -0.04163054 -0.04163054 ... -0.04163054 -0.04163054
  -0.04163054]
 [-0.09006124 -0.04163054 -0.04163054 ... -0.04163054 -0.04163054
  -0.04163054]
 [ 0.55458069 -0.04163054 -0.04163054 ... -0.04163054 -0.04163054
  -0.04163054]
 [-0.11375222 -0.04163054 -0.04163054 ... -0.04163054 -0.04163054
  -0.04163054]
 [-0.11170216 -0.04163054 -0.04163054 ... -0.04163054 -0.04163054
  -0.04163054]]<jupyter_text>Reducing Dimensions Using PCA<jupyter_code># Using PCA to reduce dimension to 3 principal components
pca = PCA(n_components=3)
crypto_pca = pca.fit_transform(crypto_scaled)
# Creating a DataFrame with the principal components data
crypto_pca = pd.DataFrame(
    data=crypto_pca,
    columns=["PC1", "PC2", "PC3"],
).set_index(crypto_dummies.index)
crypto_pca.head()<jupyter_output><empty_output><jupyter_text>Clustering Crytocurrencies Using K-Means Finding the Best Value for `k` Using the Elbow Curve<jupyter_code># Import library KMeans
from sklearn.cluster import KMeans
inertia = []
k = list(range(1, 11))

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

# Create the Elbow Curve using hvPlot
elbow_data = {'k':k, 'inertia':inertia}
elbow_df = pd.DataFrame(elbow_data)
elbow_df.hvplot.line(x='k', y='inertia', xticks=k, title="Elbow Curve Crypto")<jupyter_output><empty_output><jupyter_text>Running K-Means with `k=`<jupyter_code># Initialize the K-Means model
model = KMeans(n_clusters = 3, random_state = 0)
# Fit the model
model.fit(crypto_pca)
# Predict clusters
predictions = model.predict(crypto_pca)
# Creating a new DataFrame including predicted clusters and cryptocurrencies features
crypto_pca['class'] = model.labels_
crypto_pca.head()

#Creat new dataFrame including more colum
#crypto_pca.shape
#crypto_df.shape
cluster_df = pd.concat([crypto_df, crypto_pca], axis=1)
cluster_df.head()<jupyter_output><empty_output><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(
    cluster_df,
    x="PC3",
    y="PC2",
    z="PC1",
    hover_name = "CoinName",
    hover_data = ["Algorithm"],
    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>import hvplot.pandas
# Table with tradable cryptos
cluster_df.hvplot.table(columns=['CoinName', 'Algorithm', 'ProofType', 'TotalCoinSupply', 
                                 'TotalCoinsMined','class'], width=800)<jupyter_output><empty_output><jupyter_text>Scatter Plot with Tradable Cryptocurrencies<jupyter_code># Scaling data to create the scatter plot

cluster_df["TotalCoinSupply"] = cluster_df["TotalCoinSupply"].astype("float")
cluster_df.dtypes
# Plot the scatter with x="TotalCoinsMined" and y="TotalCoinSupply"
cluster_df.plot(kind='scatter', x = 'TotalCoinsMined', y = 'TotalCoinSupply')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12471576001399336, 'fraction_numerical': 0.053174741997551164, 'mean_word_length': 6.015950920245399, 'pattern_counts': {'":': 1, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '337220', 'license': 'Unlicense', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2074, 'n_words': 456, 'path': 'Starter_Files/crypto_clustering2.ipynb', 'repo_name': 'tdraizen/Unsupervised-Learning-using-K-Means', 'size': '5717'}