Crypto Clustering Application¶

The CSV file provided for this challenge contains price change data of cryptocurrencies in different periods.

The steps for this challenge are broken out into the following sections:

  • Import the Data (provided in the starter code)
  • Prepare the Data (provided in the starter code)
  • Find the Best Value for k Using the Original Data
  • Cluster Cryptocurrencies with K-means Using the Original Data
  • Optimize Clusters with Principal Component Analysis
  • Find the Best Value for k Using the PCA Data
  • Cluster the Cryptocurrencies with K-means Using the PCA Data
  • Visualize and Compare the Results

Import the Data¶

This section imports the data into a new DataFrame. It follows these steps:

  1. Read the “crypto_market_data.csv” file from the Resources folder into a DataFrame, and use index_col="coin_id" to set the cryptocurrency name as the index. Review the DataFrame.

  2. Generate the summary statistics, and use HvPlot to visualize your data to observe what your DataFrame contains.

price_change_percentage_24h price_change_percentage_7d price_change_percentage_14d price_change_percentage_30d price_change_percentage_60d price_change_percentage_200d price_change_percentage_1y
coin_id
bitcoin 1.08388 7.60278 6.57509 7.67258 -3.25185 83.51840 37.51761
ethereum 0.22392 10.38134 4.80849 0.13169 -12.88890 186.77418 101.96023
tether -0.21173 0.04935 0.00640 -0.04237 0.28037 -0.00542 0.01954
ripple -0.37819 -0.60926 2.24984 0.23455 -17.55245 39.53888 -16.60193
bitcoin-cash 2.90585 17.09717 14.75334 15.74903 -13.71793 21.66042 14.49384
binancecoin 2.10423 12.85511 6.80688 0.05865 36.33486 155.61937 69.69195
chainlink -0.23935 20.69459 9.30098 -11.21747 -43.69522 403.22917 325.13186
cardano 0.00322 13.99302 5.55476 10.10553 -22.84776 264.51418 156.09756
litecoin -0.06341 6.60221 7.28931 1.21662 -17.23960 27.49919 -12.66408
bitcoin-cash-sv 0.92530 3.29641 -1.86656 2.88926 -24.87434 7.42562 93.73082
crypto-com-chain 0.61209 -5.67151 -8.53948 -17.44782 -16.47600 226.70782 305.05908
usd-coin -0.17825 -0.11871 -0.00568 -0.16584 0.04271 -0.15691 -0.19205
eos 0.14477 -1.31177 1.13751 -4.63398 -30.16898 18.06111 -17.56753
monero 0.42961 15.78515 18.41097 38.95974 41.72500 169.52147 141.04116
tron 0.07647 4.23886 1.40337 -12.60389 5.52545 132.88436 59.23821
tezos -0.67316 8.95665 2.32062 -14.12663 -44.82248 43.42842 140.01279
okb -2.72700 -4.55389 -5.02662 -10.43847 -2.83120 39.95853 141.95791
stellar -1.00843 2.07149 -1.08217 -8.12933 -30.80369 84.62157 13.80715
cosmos -0.95103 16.08534 5.51074 4.57813 -7.20130 185.99786 82.43833
cdai 0.21169 0.05820 0.17076 -2.18147 0.65726 -0.39210 -0.28783
neo 0.49302 2.44243 -9.84803 -21.95472 13.51879 158.64773 131.29655
wrapped-bitcoin 1.10231 7.40537 6.55668 7.37557 -3.58772 83.90520 37.53424
leo-token -0.13192 -1.34886 -7.02859 3.07525 -7.54455 16.40588 21.00263
huobi-token -0.40818 1.61798 0.45488 -3.25488 -3.40689 42.23704 24.57164
nem -0.84990 -0.85140 2.64844 -3.65382 82.86094 216.17761 200.71797
binance-usd -0.10642 0.04726 0.05902 0.01843 0.09383 0.09959 0.13928
iota 0.41996 6.06830 -3.77714 -4.15281 -34.51894 95.02821 -3.98533
vechain 1.28766 -1.76352 -18.15890 -14.16831 -43.62359 269.70264 202.86827
zcash -0.60897 10.34780 5.79179 7.37007 -20.54216 122.54767 82.88499
theta-token -4.56089 -6.09456 -6.57354 31.43355 80.03112 882.65105 701.37599
dash -1.06006 5.09387 0.51708 -7.79140 -26.22460 6.23435 -2.45897
ethereum-classic -0.45950 3.05209 -1.26669 2.89572 -25.90799 10.39203 11.11094
ethlend -13.52786 4.21266 -9.80075 -29.99499 2.13917 2227.92782 7852.08970
maker -0.60285 8.15400 -1.48854 16.71360 -18.61722 82.53544 10.65279
havven -4.07216 4.33651 -13.29164 -20.84154 -39.42657 622.92465 678.78427
omisego 4.84033 6.82985 -13.21636 -9.11552 79.27505 585.26307 320.69054
celsius-degree-token 2.51323 0.60354 24.23919 140.79570 223.06437 1590.19149 2009.72217
ontology -1.35845 -1.21399 -10.56222 -34.70548 -32.70004 54.76717 -12.65493
ftx-token 0.83416 7.10438 -0.20629 -10.56394 5.90295 57.48950 168.37251
true-usd -0.06197 0.16642 0.10974 0.03090 0.25154 -0.08874 0.40617
digibyte 2.97084 0.67038 -7.31941 13.25354 -31.09030 482.58918 184.38014
price_change_percentage_24h price_change_percentage_7d price_change_percentage_14d price_change_percentage_30d price_change_percentage_60d price_change_percentage_200d price_change_percentage_1y
count 41.000000 41.000000 41.000000 41.000000 41.000000 41.000000 41.000000
mean -0.269686 4.497147 0.185787 1.545693 -0.094119 236.537432 347.667956
std 2.694793 6.375218 8.376939 26.344218 47.365803 435.225304 1247.842884
min -13.527860 -6.094560 -18.158900 -34.705480 -44.822480 -0.392100 -17.567530
25% -0.608970 0.047260 -5.026620 -10.438470 -25.907990 21.660420 0.406170
50% -0.063410 3.296410 0.109740 -0.042370 -7.544550 83.905200 69.691950
75% 0.612090 7.602780 5.510740 4.578130 0.657260 216.177610 168.372510
max 4.840330 20.694590 24.239190 140.795700 223.064370 2227.927820 7852.089700
Variable:
price_change_percentage_1y
coin_id:
ethlend
value:
7.852e+3

Prepare the Data¶

This section prepares the data before running the K-Means algorithm. It follows these steps:

  1. Use the StandardScaler module from scikit-learn to normalize the CSV file data. This will require you to utilize the fit_transform function.

  2. Create a DataFrame that contains the scaled data. Be sure to set the coin_id index from the original DataFrame as the index for the new DataFrame. Review the resulting DataFrame.

price_change_percentage_24h price_change_percentage_7d price_change_percentage_14d price_change_percentage_30d price_change_percentage_60d price_change_percentage_200d price_change_percentage_1y
coin_id
bitcoin 0.508529 0.493193 0.772200 0.235460 -0.067495 -0.355953 -0.251637
ethereum 0.185446 0.934445 0.558692 -0.054341 -0.273483 -0.115759 -0.199352
tether 0.021774 -0.706337 -0.021680 -0.061030 0.008005 -0.550247 -0.282061
ripple -0.040764 -0.810928 0.249458 -0.050388 -0.373164 -0.458259 -0.295546
bitcoin-cash 1.193036 2.000959 1.760610 0.545842 -0.291203 -0.499848 -0.270317

Find the Best Value for k Using the Original Data¶

In this section, you will use the elbow method to find the best value for k.

  1. Code the elbow method algorithm to find the best value for k. Use a range from 1 to 11.

  2. Plot a line chart with all the inertia values computed with the different values of k to visually identify the optimal value for k.

  3. Answer the following question: What is the best value for k?

Answer the following question: What is the best value for k?¶

Question: What is the best value for k?

Answer: Observing the graph shows minimal change in inertia for values of k > 4. Thus, the best number of clusters is k=4.


Cluster Cryptocurrencies with K-means Using the Original Data¶

In this section, you will use the K-Means algorithm with the best value for k found in the previous section to cluster the cryptocurrencies according to the price changes of cryptocurrencies provided.

  1. Initialize the K-Means model with four clusters using the best value for k.

  2. Fit the K-Means model using the original data.

  3. Predict the clusters to group the cryptocurrencies using the original data. View the resulting array of cluster values.

  4. Add a new column to the DataFrame with the original data to store the predicted clusters.

  5. Create a scatter plot using hvPlot by setting x="price_change_percentage_24h" and y="price_change_percentage_7d". Color the graph points with the labels found using K-Means and add the crypto name in the hover_cols parameter to identify the cryptocurrency represented by each data point.

KMeans(n_clusters=4, random_state=0)
array([0, 0, 3, 3, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 3, 0, 3, 3, 0, 3, 3, 0,
       3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 1, 0, 3, 3, 2, 3, 3, 3, 3])
price_change_percentage_24h price_change_percentage_7d price_change_percentage_14d price_change_percentage_30d price_change_percentage_60d price_change_percentage_200d price_change_percentage_1y mkt_cluster
coin_id
bitcoin 0.508529 0.493193 0.772200 0.235460 -0.067495 -0.355953 -0.251637 0
ethereum 0.185446 0.934445 0.558692 -0.054341 -0.273483 -0.115759 -0.199352 0
tether 0.021774 -0.706337 -0.021680 -0.061030 0.008005 -0.550247 -0.282061 3
ripple -0.040764 -0.810928 0.249458 -0.050388 -0.373164 -0.458259 -0.295546 3
bitcoin-cash 1.193036 2.000959 1.760610 0.545842 -0.291203 -0.499848 -0.270317 0

Optimize Clusters with Principal Component Analysis¶

In this section, you will perform a principal component analysis (PCA) and reduce the features to three principal components.

  1. Create a PCA model instance and set n_components=3.

  2. Use the PCA model to reduce to three principal components. View the first five rows of the DataFrame.

  3. Retrieve the explained variance to determine how much information can be attributed to each principal component.

  4. Answer the following question: What is the total explained variance of the three principal components?

  5. Create a new DataFrame with the PCA data. Be sure to set the coin_id index from the original DataFrame as the index for the new DataFrame. Review the resulting DataFrame.

[0.36941241 0.29169375 0.22893878]

Answer the following question: What is the total explained variance of the three principal components?¶

Question: What is the total explained variance of the three principal components?

Answer: Total explained variance of 3 principal components is:

Total explained variance: 0.89%
PC1 PC2 PC3
coinid
bitcoin 2.034093 -0.641571 -0.357629
ethereum 1.994502 -0.490549 -0.912250
tether -1.068544 -0.417205 0.596311
ripple -1.040919 -0.455234 0.467801
bitcoin-cash 3.302764 -1.242458 -0.217276

Find the Best Value for k Using the PCA Data¶

In this section, you will use the elbow method to find the best value for k using the PCA data.

  1. Code the elbow method algorithm and use the PCA data to find the best value for k. Use a range from 1 to 11.

  2. Plot a line chart with all the inertia values computed with the different values of k to visually identify the optimal value for k.

  3. Answer the following questions: What is the best value for k when using the PCA data? Does it differ from the best k value found using the original data?

Answer the following questions: What is the best value for k when using the PCA data? Does it differ from the best k value found using the original data?¶

  • Question: What is the best value for k when using the PCA data?

    • Answer: Observing the graph shows minimal change in inertia for values of k > 4. Thus, the best number of clusters is k=4.
  • Question: Does it differ from the best k value found using the original data?

    • Answer: The best k for the original data set was also k=4, no difference between the original data and the PCA data.

Cluster Cryptocurrencies with K-means Using the PCA Data¶

In this section, you will use the PCA data and the K-Means algorithm with the best value for k found in the previous section to cluster the cryptocurrencies according to the principal components.

  1. Initialize the K-Means model with four clusters using the best value for k.

  2. Fit the K-Means model using the PCA data.

  3. Predict the clusters to group the cryptocurrencies using the PCA data. View the resulting array of cluster values.

  4. Add a new column to the DataFrame with the PCA data to store the predicted clusters.

  5. Create a scatter plot using hvPlot by setting x="price_change_percentage_24h" and y="price_change_percentage_7d". Color the graph points with the labels found using K-Means and add the crypto name in the hover_cols parameter to identify the cryptocurrency represented by each data point.

KMeans(n_clusters=4, random_state=0)
array([0, 0, 2, 2, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 2, 0,
       2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 3, 0, 2, 2, 1, 2, 2, 2, 2])
PC1 PC2 PC3 pca_cluster
coinid
bitcoin 2.034093 -0.641571 -0.357629 0
ethereum 1.994502 -0.490549 -0.912250 0
tether -1.068544 -0.417205 0.596311 2
ripple -1.040919 -0.455234 0.467801 2
bitcoin-cash 3.302764 -1.242458 -0.217276 0

Visualize and Compare the Results¶

In this section, you will visually analyze the cluster analysis results by contrasting the outcome with and without using the optimization techniques.

  1. Create a composite plot using hvPlot and the plus (+) operator to contrast the Elbow Curve that you created to find the best value for k with the original and the PCA data.

  2. Create a composite plot using hvPlot and the plus (+) operator to contrast the cryptocurrencies clusters using the original and the PCA data.

  3. Answer the following question: After visually analyzing the cluster analysis results, what is the impact of using fewer features to cluster the data using K-Means?

Rewind: Back in Lesson 3 of Module 6, you learned how to create composite plots. You can look at that lesson to review how to make these plots; also, you can check the hvPlot documentation.

Answer the following question: After visually analyzing the cluster analysis results, what is the impact of using fewer features to cluster the data using K-Means?

  • Question: After visually analyzing the cluster analysis results, what is the impact of using fewer features to cluster the data using K-Means?

  • Answer: Using principal component analysis to reduce the number of features in the dataset results in a scatter plot showing the datapoints more tightly grouped by cluster than the graph including all the features in the original dataset.

PC1 PC2 PC3 pca_cluster
coinid
bitcoin 2.034093 -0.641571 -0.357629 0
ethereum 1.994502 -0.490549 -0.912250 0
tether -1.068544 -0.417205 0.596311 2
ripple -1.040919 -0.455234 0.467801 2
bitcoin-cash 3.302764 -1.242458 -0.217276 0
binancecoin 2.505692 -0.575832 -0.129389 0
chainlink 2.770042 -0.495691 -1.890371 0
cardano 2.372449 -0.389146 -0.992513 0
litecoin 1.832344 -0.678797 -0.741581 0
bitcoin-cash-sv -0.928101 -0.744485 0.333337 2
crypto-com-chain -2.126715 -0.191442 0.129535 2
usd-coin -1.081649 -0.422198 0.602544 2
eos -1.220389 -0.668893 0.300882 2
monero 3.727784 -0.048745 0.653647 0
tron -0.786538 -0.382913 0.303559 2
tezos 1.448663 -0.800424 -1.706247 0
okb -1.912573 0.115405 0.020176 2
stellar -1.190303 -0.459525 -0.144873 2
cosmos 2.449141 -0.350412 -1.138638 0
cdai -1.061363 -0.498291 0.622566 2
neo -1.600606 -0.321262 0.022433 2
wrapped-bitcoin 2.015727 -0.644576 -0.357140 0
leo-token -1.543865 -0.379812 0.444688 2
huobi-token -0.981967 -0.391970 0.406427 2
nem -0.793836 0.605338 1.444187 2
binance-usd -1.060689 -0.434818 0.612013 2
iota -0.956149 -0.743088 -0.136118 2
vechain -2.391973 -0.483822 -0.417661 2
zcash 2.058316 -0.459102 -0.942991 0
theta-token -1.402246 2.694093 1.472609 2
dash -0.880733 -0.594293 -0.172796 2
ethereum-classic -0.979139 -0.561415 0.164016 2
ethlend -0.634091 8.182308 -4.405176 3
maker 1.598712 -0.409341 -0.828938 0
havven -1.956560 0.938434 -1.466931 2
omisego -0.897144 0.112481 1.350364 2
celsius-degree-token 3.499309 4.598443 6.269908 1
ontology -2.270212 -0.558114 -0.867624 2
ftx-token -0.622694 -0.587809 0.275713 2
true-usd -1.046745 -0.443262 0.616862 2
digibyte -1.173793 -0.280006 0.716562 2