<jupyter_start><jupyter_text>No Optimal cluster. Variables are similar to each other.  Data Preparation<jupyter_code>#1.Discard all cryptocurrencies that are not being traded. filter for currencies that are currently being traded. 
#2. drop the IsTrading column from the dataframe
#3. Remove all rows that have at least one null value.
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import pandas as pd
from pathlib import Path
from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder
import numpy as np
from sklearn.cluster import KMeans

file_path = 'crypto_data.csv'
df = pd.read_csv(file_path)
df_filter = df.loc[df['IsTrading'] == True]
drop_Is = df_filter.drop(['IsTrading'], axis=1)
drop_Is.dropna(how='any', axis =0)
#4. Filter for cryptocurrencies that have been mined. That is, the total coins mined should be greater than zero.
Mined = drop_Is.loc[df['TotalCoinsMined'] > 0]
Mined


#5. In order for your dataset to be comprehensible to a machine learning algorithm, its data should be numeric. 
#Since the coin names do not contribute to the analysis of the data, delete the CoinName from the original dataframe.

Drop_Mined = Mined.drop(['CoinName', 'Unnamed: 0'], axis=1)
#6. Your next step in data preparation is to convert the remaining features with text values, Algorithm and ProofType, into numerical data. To accomplish this task, use Pandas to create dummy variables. Examine the number of rows and columns of your dataset now. 
#How did they change?

# Create Dummies
X_dummies = pd.get_dummies(data=Drop_Mined, columns=['Algorithm', 'ProofType'])
print(X_dummies.columns)
X_dummies
#7. Standardize your dataset so that columns that contain larger values do not unduly influence the outcome.

scaler = StandardScaler().fit(X_dummies)
X_dummies_scaled = scaler.transform(X_dummies)
X_dummies_scaled<jupyter_output><empty_output><jupyter_text>Dimensionality Reduction<jupyter_code>#PCA

test_pca = PCA(n_components=98)
test_pca.fit(X_dummies_scaled)
plt.plot(np.cumsum(test_pca.explained_variance_ratio_))
plt.xlabel("Number of componets")
plt.ylabel("Cum Variance")

PCA_Componets = 90
pca = PCA(n_components= PCA_Componets)
X_dummies_pca = pca.fit_transform(X_dummies_scaled)

pca.explained_variance_ratio_
#How did the number of the features change?
# Not much of a change, 90% of the componets contain the information.
#run t-SNE on the principal components: the output of the PCA transformation. 
#Then create a scatter plot of the t-SNE output.
tsne = TSNE(learning_rate=35)
tsne_features = tsne.fit_transform(X_dummies_pca)
tsne_features.shape
# Prepare to plot the dataset

# The first column of transformed features
x = tsne_features[:,0]
# The second column of transformed features
y = tsne_features[:,1]<jupyter_output><empty_output><jupyter_text>Cluster Analysis with k-Means<jupyter_code>#Create an elbow plot to identify the best number of clusters. Use a for-loop to determine the inertia for each k between 1 through 10. 
#Determine, if possible, where the elbow of the plot is, and at which value of k it appears.

inertia = []
# Same as k = list(range(1, 11))
k = [1,2,3,4,5,6,7,8,9,10,11]


# Looking for the best k
for i in k:
    km = KMeans(n_clusters=i, random_state=0)
    km.fit(X_dummies_pca)
    inertia.append(km.inertia_)

# Define a DataFrame to plot the Elbow Curve using hvPlot
elbow_data = {"k": k, "inertia": inertia}
df_elbow = pd.DataFrame(elbow_data)

plt.plot(df_elbow['k'], df_elbow['inertia'])
plt.xticks(range(1,11))
plt.xlabel('Number of clusters')
plt.ylabel('Inertia')
plt.show()

# Initializing model with K = 3 (since we already know there are three classes of iris plants)
model = KMeans(n_clusters=3, random_state=5)
# Fitting model
model.fit(X_dummies_scaled)
# Get predictions
predictions = model.predict(X_dummies_scaled)
print(predictions)
# Add a new class column to df_iris
X_dummies["class"] = model.labels_
X_dummies.head()
plt.scatter(x, y, c=X_dummies['class'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08922392486406328, 'fraction_numerical': 0.011863568956994563, 'mean_word_length': 5.756260434056761, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 14, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '337586', 'license': 'ADSL', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1357, 'n_words': 453, 'path': 'Cryptocurrency Clusters.ipynb', 'repo_name': 'zhuolima/Unsupervised_Machine_Learning', 'size': '4046'}