# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

import pandas as pd
from path import Path
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

# ### Data Preparation

file = Path('crypto_data.csv')

df = pd.read_csv(file, index_col=0)
df.head(10)

# List all algorithms in use
algos = set(df.Algorithm.values)
print(algos)

# print the counts of stocks being currently traded vs. not being currently traded 
df["IsTrading"].value_counts()

# Select only cryptocurrencies that are being traded
df= df.loc[df["IsTrading"]==True]
df["IsTrading"].value_counts()

# Delete the IsTrading column; it is no longer necessary
df= df.drop("IsTrading", axis=1)
df.head()

# Delete all rows with null values
df = df.dropna(axis=0, how='any')
df.shape

# Sort values in TotalCoinsMined to identify negative or zero values
df.TotalCoinsMined.sort_values()

# Select only rows with positive TotalCoinsMined values
df = df[df["TotalCoinsMined"]>0]
len(df)

# Delete the CoinName column from the original dataframe
df = df.drop("CoinName", axis=1)
df.head()

# Create dummy variables for columns with string values
X = pd.get_dummies(data=df, columns=["Algorithm", "ProofType"])
print(X.shape)
X.head()

# Standardize the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# print the scaled data
X_scaled[0]

# Identify the numbers of rows and columns in the scaled data
X_scaled.shape

# ### Reduce dataset dimensions with PCA

#99%
pca = PCA(n_components=.99)
prin_components = pca.fit_transform(X_scaled)
prin_components.shape

pca.explained_variance_.sum()

# Reduce dimensions with PCA 90%
pca = PCA(n_components=.90)
prin_components = pca.fit_transform(X_scaled)
prin_components.shape

# The sum of the explained variance of the principal components
pca.explained_variance_.sum()

# Reduce dimensions with t-SNE
tSNE1 = TSNE(perplexity=50)
t1Features= tSNE1.fit_transform(prin_components)
t1Features.shape

# Plot t-SNE output
X = t1Features[:,0]
Y = t1Features[:,1]
plt.scatter(X, Y)
plt.show()

# +
# Identify clusters with k-means
inertia = []
k = [1,2,3,4,5,6,7,8,9,10]

for v in k:
    kmeans = KMeans(n_clusters=v, random_state=0)
    kmeans.fit(prin_components)
    inertia.append(kmeans.inertia_)
# -

elbow = pd.DataFrame({'K': k, "Inertia": inertia})
elbow.plot.line(x='K', y="Inertia")
plt.ylabel("Inertia")
plt.xlabel("K Value")
plt.title("Elbow Plot of Scaled Data")
plt.show()

# My recommendation would be that there is no clustering in this data set. Using the t-SNE model I was able to only creat one cluster and with the KMeans and the elbow plot, there was no bend which shows there is no clustering in the data.

StarcoderdataJupyterScriptsDedupFiltered