<jupyter_start><jupyter_text>Clustering Crypto<jupyter_code># imports
import requests
import pandas as pd
from pathlib import Path
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># Use the following endpoint to fetch json data
import json
import pprint
url = "https://min-api.cryptocompare.com/data/all/coinlist"
r = requests.get(url)
content = r.content
content_d = content.decode('utf-8')
data = json.loads(content_d)
# Create a DataFrame 
# HINT: You will need to use the 'Data' key from the json response, then transpose the DataFrame.
# dataframe creation
crypto_df = pd.DataFrame(data=data['Data'])

# Transpose Dataframe

crypto_df = crypto_df.transpose()
crypto_df.head(1)
# Alternatively, use the provided csv file:
# file_path = Path("Resources/crypto_data.csv")

# Create a DataFrame
# crypto_df = pd.read_csv(file_path)
# crypto_df.head()<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code># Keep only necessary columns:
# 'CoinName','Algorithm','IsTrading','ProofType','TotalCoinsMined','TotalCoinSupply'


# Reorder Columns that are still in the dataframe
crypto_df = crypto_df[['CoinName', 'Algorithm', 'IsTrading', 'ProofType', 'TotalCoinsMined', 'MaxSupply']]

crypto_df.head()
# Keep only cryptocurrencies that are trading

# determine the count of false values(1094)
# crypto_df['IsTrading'].value_counts() 

crypto_df.drop(crypto_df[crypto_df['IsTrading'] == False].index,
              inplace=True)
# Keep only cryptocurrencies with a working algorithm
crypto_df.drop(crypto_df[crypto_df['Algorithm'] == 'N/A'].index, inplace=True)
crypto_df.head()
# Remove the "IsTrading" column
crypto_df.drop(columns = ['IsTrading'], inplace=True)
# Remove rows with at least 1 null value
crypto_df.dropna(inplace=True)
# Remove rows with cryptocurrencies having no coins mined
crypto_df.drop(crypto_df[crypto_df['TotalCoinsMined'] == 0].index, inplace=True)
# Drop rows where there are 'N/A' text values
crypto_df = crypto_df[crypto_df != 'N/A'].dropna()
# Store the 'CoinName'column in its own DataFrame prior to dropping it from crypto_df
CoinName_df = crypto_df['CoinName'].to_frame()

# Drop the 'CoinName' column since it's not going to be used on the clustering algorithm
crypto_df.drop(columns = ['CoinName'], inplace=True)
# Create dummy variables for text features
X = pd.get_dummies(crypto_df, columns=['Algorithm', 'ProofType'], drop_first=True)
# Standardize data
X_scaled = StandardScaler().fit_transform(X)<jupyter_output><empty_output><jupyter_text>Reducing Dimensions Using PCA<jupyter_code># Use PCA to reduce dimensions to 3 principal components
pca = PCA(n_components=3)
crypto_pca = pca.fit_transform(X_scaled)
# Create a DataFrame with the principal components data
pca_df = pd.DataFrame(data=crypto_pca,
                            columns=['PC1', 'PC2', 'PC3'],
                            index=crypto_df.index
                            )
pca_df.head()<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
# set up a FOR loopy to run thru.. 
for i in k:
    km = KMeans(n_clusters=i, random_state=6)
    km.fit(pca_df)
    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 Plot")<jupyter_output><empty_output><jupyter_text>Running K-Means with `k=`<jupyter_code># Initialize the K-Means model
model = KMeans(n_clusters=5, random_state=0)

# Fit the model
model.fit(pca_df)

# Predict clusters
predictions = model.predict(pca_df)

# add the predicted class columns
pca_df['class'] = model.labels_
pca_df.head()
# Create a new DataFrame including predicted clusters and cryptocurrencies features
clustered_df = pd.concat([crypto_df, CoinName_df, pca_df], axis=1)
clustered_df.head()<jupyter_output><empty_output><jupyter_text>Visualizing Results 3D-Scatter with Clusters<jupyter_code>fig = px.scatter_3d(
    clustered_df,
    x="PC3",
    y="PC2",
    z="PC1",
    color="class",
    symbol="class",
    width=800,
    hover_name="CoinName",
    hover_data=["Algorithm"]
    
)
fig.update_layout(legend=dict(x=0, y=1))
fig.show()<jupyter_output><empty_output><jupyter_text>hvplot Table of Tradable Cryptocurrencies<jupyter_code># Table with tradable cryptos
clustered_df.hvplot.table(columns=['CoinName', 'Algorithm', 'ProofType', 'TotalCoinsMined', 'MaxSupply', 'class'], width=600)
# Print the total number of tradable cryptocurrencies
print(f'The total number of tradable cryptocurrenices is: {len(clustered_df_no_pc)}')<jupyter_output>The total number of tradable cryptocurrenices is: 102<jupyter_text>Scatter Plot with Tradable Cryptocurrencies<jupyter_code># Scale data to create the scatter plot
scaler = MinMaxScaler()

normalize_columns = ['TotalCoinsMined', 'MaxSupply']
x = crypto_df[normalize_columns].values
x_scaled = scaler.fit_transform(x)

df_tradable = pd.DataFrame(x_scaled, columns=normalize_columns, index=crypto_df.index).reset_index()

# Create a scatter plot using hvplot.scatter
df_tradable.hvplot.scatter(x='TotalCoinsMined', y='MaxSupply',hover_cols=["CoinName"])
## below is me just trying things 

# trying to get an animation for the scatter but i think the ranges are kind of messed up 
fig2 = px.scatter(df_tradable, x="TotalCoinsMined", y="MaxSupply", animation_frame="TotalCoinsMined", animation_group="MaxSupply",
                 size="MaxSupply", color="TotalCoinsMined", hover_name="TotalCoinsMined",
             log_x=True, log_y=True, size_max=45, range_x=[100,10000000], range_y=[25,140000])
fig2.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11217895608947805, 'fraction_numerical': 0.009776304888152444, 'mean_word_length': 6.002320185614849, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '236632', 'license': 'Xnet', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2049, 'n_words': 497, 'path': 'clusteringcrypto/crypto_clustering_solutions_file.ipynb', 'repo_name': 'VRkaavar/boot-camp-homework13', 'size': '6035'}