<jupyter_start><jupyter_text>Clustering Crypto<jupyter_code># Initial imports
import pandas as pd
import hvplot.pandas
from path import Path
import plotly.express as px
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Deliverable 1: Preprocessing the Data for PCA<jupyter_code># Load the crypto_data.csv dataset.
# YOUR CODE HERE
crypto_data="Resources/crypto_data.csv"
crypto_data_df=pd.read_csv(crypto_data)
crypto_data_df
crypto_data_df.dtypes
# Keep all the cryptocurrencies that are being traded.
# YOUR CODE HERE
trading= crypto_data_df['IsTrading'] != False
crypto_trading_df = crypto_data_df.loc[trading]

crypto_trading_df

crypto_trading_df=crypto_trading_df.rename(columns={"Unnamed: 0":"test"})
crypto_trading_df
crypto_trading_df=crypto_trading_df.set_index('test')
crypto_trading_df.index.name=None
crypto_trading_df
# Keep all the cryptocurrencies that have a working algorithm.
# YOUR CODE HERE
# Remove the "IsTrading" column. 
# YOUR CODE HERE
crypto_trading_df=crypto_trading_df.drop(columns=['IsTrading'],axis=1)
# Remove rows that have at least 1 null value.
# YOUR CODE HERE
crypto_trading_df=crypto_trading_df.dropna(axis=0,how="any")
crypto_trading_df
# Keep the rows where coins are mined.
# YOUR CODE HERE
crypto_trading_df=crypto_trading_df[crypto_trading_df.TotalCoinsMined > 0]
crypto_trading_df
# Create a new DataFrame that holds only the cryptocurrencies names.
# YOUR CODE HERE
crypto_name_df=\
crypto_trading_df[['CoinName']]
crypto_name_df
crypto_trading_df
crypto_trading_df.dtypes
# Drop the 'CoinName' column since it's not going to be used on the clustering algorithm.
# YOUR CODE HERE
crypto_trading_df=\
crypto_trading_df.drop(columns=['CoinName'],axis=1)
crypto_trading_df
crypto_trading_df['TotalCoinSupply']=crypto_trading_df['TotalCoinSupply'].astype('float64',errors='raise')
crypto_trading_df.dtypes
X = crypto_trading_df
print(X.shape)
X.dtypes
# Use get_dummies() to create variables for text features.
X=pd.get_dummies(X)
X
# Use get_dummies() to create variables for text features.
# YOUR CODE HERE
scaler=StandardScaler
coin_scaled=scaler().fit_transform(X)
print(coin_scaled[0:5])
# Standardize the data with StandardScaler().
# YOUR CODE HERE<jupyter_output><empty_output><jupyter_text>Deliverable 2: Reducing Data Dimensions Using PCA<jupyter_code># Initialize PCA model
pca = PCA(n_components=3)

# Get three principal components for the coin data.
coin_pca = pca.fit_transform(coin_scaled)

df_coin_pca = pd.DataFrame(
    data=coin_pca, columns=["pc1", "pc2","pc3"],index=crypto_trading_df.index
)

df_coin_pca
# Using PCA to reduce dimension to three principal components.
# YOUR CODE HERE
# Create a DataFrame with the three principal components.
# YOUR CODE HERE<jupyter_output><empty_output><jupyter_text>Deliverable 3: Clustering Crytocurrencies Using K-Means Finding the Best Value for `k` Using the Elbow Curve<jupyter_code>inertia = []
k=list(range(1,11))

for i in k:
    km=KMeans(n_clusters=i,random_state=0)
    km.fit(df_coin_pca)
    inertia.append(km.inertia_)
    
# Creating the Elbow Curve
elbow_data = {"k": k, "inertia": inertia}
df_elbow = pd.DataFrame(elbow_data)
df_elbow.head()
df_elbow.hvplot.line(x='k',y="inertia",title='Cyrptocurrency Elbow Line',xlabel='K Value',ylabel='Inertia')<jupyter_output><empty_output><jupyter_text>Running K-Means with `k=4`<jupyter_code># Initialize the K-Means model.
# YOUR CODE HERE
model = KMeans(n_clusters=4, random_state=42).fit(df_coin_pca)
# Fit the model
# YOUR CODE HERE

# Predict clusters
# YOUR CODE HERE
coin_pred = model.predict(df_coin_pca)
coin_pred



# Create a new DataFrame including predicted clusters and cryptocurrencies features.
# Concatentate the crypto_df and pcs_df DataFrames on the same columns.


# YOUR CODE HERE
df_y = pd.DataFrame(coin_pred,columns=['Class'],index=crypto_trading_df.index)

clustered_df = crypto_trading_df.join(df_y, how="inner")
clustered_df.head()
clustered_df = clustered_df.join(df_coin_pca, how="inner")

#  Add a new column, "CoinName" to the clustered_df DataFrame that holds the names of the cryptocurrencies. 
# YOUR CODE HERE
clustered_df = clustered_df.join(crypto_name_df,how="inner")
cluster_col=clustered_df.columns.tolist()
cluster_reord=cluster_col[:4]+cluster_col[5:]+[cluster_col[4]]
clustered_df = clustered_df[cluster_reord]
#  Add a new column, "Class" to the clustered_df DataFrame that holds the predictions.
# YOUR CODE HERE

# Print the shape of the clustered_df
print(clustered_df.shape)
clustered_df.head(10)<jupyter_output>(532, 9)<jupyter_text>Deliverable 4: Visualizing Cryptocurrencies Results 3D-Scatter with Clusters<jupyter_code># Creating a 3D-Scatter with the PCA data and the clusters
# YOUR CODE HERE
fig=px.scatter_3d(clustered_df,x="pc1",y="pc2",z="pc3",color="Class",hover_name="CoinName",hover_data=["Algorithm"])
fig.update_layout(hoverlabel=dict(
        bgcolor="black",
        font_size=12,
        font_family="Helvetica"
    ))
fig.show()

# Create a table with tradable cryptocurrencies.
# YOUR CODE HERE
clustered_df.hvplot.table(columns=['CoinName', 'Algorithm', 'ProofType', 'TotalCoinSupply','TotalCoinsMined','Class'], sortable=True, selectable=True)

# Print the total number of tradable cryptocurrencies.
# YOUR CODE HERE
print(f'There are {len(clustered_df)} tradable cryptocurrencies.')
# Scaling data to create the scatter plot with tradable cryptocurrencies.
# YOUR CODE HERE
clustered_df.dtypes
# Create a new DataFrame that has the scaled data with the clustered_df DataFrame index.
# YOUR CODE HERE
scaler=MinMaxScaler()
scaled_plot=scaler.fit_transform(clustered_df[['TotalCoinSupply','TotalCoinsMined']])



scaled_plot_df=pd.DataFrame(scaled_plot,columns=['TotalCoinSupply','TotalCoinsMined'],index=crypto_name_df.index)
scaled_plot_df
# Add the "CoinName" column from the clustered_df DataFrame to the new DataFrame.
# Add the "Class" column from the clustered_df DataFrame to the new DataFrame. 

plot_df=scaled_plot_df.merge(clustered_df[['CoinName','Class']],left_index=True,right_index=True)

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','Class','index'],width=900,height=500)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10026182042199291, 'fraction_numerical': 0.007546588633913445, 'mean_word_length': 7.283163265306122, 'pattern_counts': {'":': 3, '<': 24, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '148484', 'license': 'MIT', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2238, 'n_words': 531, 'path': 'crypto_clustering.ipynb', 'repo_name': 'ethiry99/HW18_CryptoCurrency', 'size': '6493'}