You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ML-For-Beginners/5-Clustering/2-K-Means/solution/notebook.ipynb

323 lines
210 KiB

4 years ago
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python37364bit8d3b438fb5fc4430a93ac2cb74d693a7",
"display_name": "Python 3.7.0 64-bit ('3.7')"
},
"metadata": {
"interpreter": {
"hash": "70b38d7a306a849643e446cd70466270a13445e5987dfa1344ef2b127438fa4d"
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"source": [
"# Nigerian Music scraped from Spotify - an analysis"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 81,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: seaborn in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (0.11.1)\n",
"Requirement already satisfied: scipy>=1.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from seaborn) (1.4.1)\n",
"Requirement already satisfied: pandas>=0.23 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from seaborn) (1.1.2)\n",
"Requirement already satisfied: numpy>=1.15 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from seaborn) (1.19.2)\n",
"Requirement already satisfied: matplotlib>=2.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from seaborn) (3.1.0)\n",
"Requirement already satisfied: pytz>=2017.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas>=0.23->seaborn) (2019.1)\n",
"Requirement already satisfied: python-dateutil>=2.7.3 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas>=0.23->seaborn) (2.8.0)\n",
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from matplotlib>=2.2->seaborn) (2.4.0)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from matplotlib>=2.2->seaborn) (1.1.0)\n",
"Requirement already satisfied: cycler>=0.10 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from matplotlib>=2.2->seaborn) (0.10.0)\n",
"Requirement already satisfied: six>=1.5 in /Users/jenlooper/Library/Python/3.7/lib/python/site-packages (from python-dateutil>=2.7.3->pandas>=0.23->seaborn) (1.12.0)\n",
"Requirement already satisfied: setuptools in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib>=2.2->seaborn) (45.1.0)\n",
"\u001b[33mWARNING: You are using pip version 20.2.3; however, version 21.1.2 is available.\n",
"You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -m pip install --upgrade pip' command.\u001b[0m\n",
"Note: you may need to restart the kernel to use updated packages.\n"
4 years ago
]
}
],
"source": [
"pip install seaborn"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" name album \\\n",
"0 Sparky Mandy & The Jungle \n",
"1 shuga rush EVERYTHING YOU HEARD IS TRUE \n",
"2 LITT! LITT! \n",
"3 Confident / Feeling Cool Enjoy Your Life \n",
"4 wanted you rare. \n",
"\n",
" artist artist_top_genre release_date length popularity \\\n",
"0 Cruel Santino alternative r&b 2019 144000 48 \n",
"1 Odunsi (The Engine) afropop 2020 89488 30 \n",
"2 AYLØ indie r&b 2018 207758 40 \n",
"3 Lady Donli nigerian pop 2019 175135 14 \n",
"4 Odunsi (The Engine) afropop 2018 152049 25 \n",
"\n",
" danceability acousticness energy instrumentalness liveness loudness \\\n",
"0 0.666 0.8510 0.420 0.534000 0.1100 -6.699 \n",
"1 0.710 0.0822 0.683 0.000169 0.1010 -5.640 \n",
"2 0.836 0.2720 0.564 0.000537 0.1100 -7.127 \n",
"3 0.894 0.7980 0.611 0.000187 0.0964 -4.961 \n",
"4 0.702 0.1160 0.833 0.910000 0.3480 -6.044 \n",
"\n",
" speechiness tempo time_signature \n",
"0 0.0829 133.015 5 \n",
"1 0.3600 129.993 3 \n",
"2 0.0424 130.005 4 \n",
"3 0.1130 111.087 4 \n",
"4 0.0447 105.115 4 "
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>name</th>\n <th>album</th>\n <th>artist</th>\n <th>artist_top_genre</th>\n <th>release_date</th>\n <th>length</th>\n <th>popularity</th>\n <th>danceability</th>\n <th>acousticness</th>\n <th>energy</th>\n <th>instrumentalness</th>\n <th>liveness</th>\n <th>loudness</th>\n <th>speechiness</th>\n <th>tempo</th>\n <th>time_signature</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Sparky</td>\n <td>Mandy &amp; The Jungle</td>\n <td>Cruel Santino</td>\n <td>alternative r&amp;b</td>\n <td>2019</td>\n <td>144000</td>\n <td>48</td>\n <td>0.666</td>\n <td>0.8510</td>\n <td>0.420</td>\n <td>0.534000</td>\n <td>0.1100</td>\n <td>-6.699</td>\n <td>0.0829</td>\n <td>133.015</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>shuga rush</td>\n <td>EVERYTHING YOU HEARD IS TRUE</td>\n <td>Odunsi (The Engine)</td>\n <td>afropop</td>\n <td>2020</td>\n <td>89488</td>\n <td>30</td>\n <td>0.710</td>\n <td>0.0822</td>\n <td>0.683</td>\n <td>0.000169</td>\n <td>0.1010</td>\n <td>-5.640</td>\n <td>0.3600</td>\n <td>129.993</td>\n <td>3</td>\n </tr>\n <tr>\n <th>2</th>\n <td>LITT!</td>\n <td>LITT!</td>\n <td>AYLØ</td>\n <td>indie r&amp;b</td>\n <td>2018</td>\n <td>207758</td>\n <td>40</td>\n <td>0.836</td>\n <td>0.2720</td>\n <td>0.564</td>\n <td>0.000537</td>\n <td>0.1100</td>\n <td>-7.127</td>\n <td>0.0424</td>\n <td>130.005</td>\n <td>4</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Confident / Feeling Cool</td>\n <td>Enjoy Your Life</td>\n <td>Lady Donli</td>\n <td>nigerian pop</td>\n <td>2019</td>\n <td>175135</td>\n <td>14</td>\n <td>0.894</td>\n <td>0.7980</td>\n <td>0.611</td>\n <td>0.000187</td>\n <td>0.0964</td>\n <td>-4.961</td>\n <td>0.1130</td>\n <td>111.087</td>\n <td>4</td>\n </tr>\n <tr>\n <th>4</th>\n <td>wanted you</td>\n <td>rare.</td>\n <td>Odunsi (The Engine)</td>\n <td>afropop</td>\n <td>2018</td>\n <td>152049</td>\n <td>25</td>\n <td>0.702</td>\n <td>0.1160</td>\n <td>0.833</td>\n <td>0.910000</td>\n <td>0.3480</td>\n <td>-6.044</td>\n <td>0.0447</td>\n <td>105.115</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 82
}
],
4 years ago
"source": [
"\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"\n",
"\n",
"df = pd.read_csv(\"../../data/nigerian-songs.csv\")\n",
"df.head()"
4 years ago
]
},
{
"cell_type": "code",
"execution_count": 83,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Text(0.5, 1.0, 'Top genres')"
]
4 years ago
},
"metadata": {},
"execution_count": 83
},
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 720x504 with 1 Axes>",
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"474.468454pt\" version=\"1.1\" viewBox=\"0 0 598.4875 474.468454\" width=\"598.4875pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;white-space:pre;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 474.468454 \nL 598.4875 474.468454 \nL 598.4875 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 33.2875 402.838125 \nL 591.2875 402.838125 \nL 591.2875 22.318125 \nL 33.2875 22.318125 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"patch_3\">\n <path clip-path=\"url(#pcfb5f5270b)\" d=\"M 51.8875 402.838125 \nL 200.6875 402.838125 \nL 200.6875 40.438125 \nL 51.8875 40.438125 \nz\n\" style=\"fill:#3274a1;\"/>\n </g>\n <g id=\"patch_4\">\n <path clip-path=\"url(#pcfb5f5270b)\" d=\"M 237.8875 402.838125 \nL 386.6875 402.838125 \nL 386.6875 295.525504 \nL 237.8875 295.525504 \nz\n\" style=\"fill:#e1812c;\"/>\n </g>\n <g id=\"patch_5\">\n <path clip-path=\"url(#pcfb5f5270b)\" d=\"M 423.8875 402.838125 \nL 572.6875 402.838125 \nL 572.6875 369.412882 \nL 423.8875 369.412882 \nz\n\" style=\"fill:#3a923a;\"/>\n </g>\n <g id=\"matplotlib.axis_1\">\n <g id=\"xtick_1\">\n <g id=\"line2d_1\">\n <defs>\n <path d=\"M 0 0 \nL 0 3.5 \n\" id=\"mce301f6c6e\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"126.2875\" xlink:href=\"#mce301f6c6e\" y=\"402.838125\"/>\n </g>\n </g>\n <g id=\"text_1\">\n <!-- afro dancehall -->\n <defs>\n <path d=\"M 34.28125 27.484375 \nQ 23.390625 27.484375 19.1875 25 \nQ 14.984375 22.515625 14.984375 16.5 \nQ 14.984375 11.71875 18.140625 8.90625 \nQ 21.296875 6.109375 26.703125 6.109375 \nQ 34.1875 6.109375 38.703125 11.40625 \nQ 43.21875 16.703125 43.21875 25.484375 \nL 43.21875 27.484375 \nz\nM 52.203125 31.203125 \nL 52.203125 0 \nL 43.21875 0 \nL 43.21875 8.296875 \nQ 40.140625 3.328125 35.546875 0.953125 \nQ 30.953125 -1.421875 24.3125 -1.421875 \nQ 15.921875 -1.421875 10.953125 3.296875 \nQ 6 8.015625 6 15.921875 \nQ 6 25.140625 12.171875 29.828125 \nQ 18.359375 34.515625 30.609375 34.515625 \nL 43.21875 34.515625 \nL 43.21875 35.40625 \nQ 43.21875 41.609375 39.140625 45 \nQ 35.0625 48.390625 27.6875 48.390625 \nQ 23 48.390625 18.546875 47.265625 \nQ 14.109375 46.140625 10.015625 43.890625 \nL 10.015625 52.203125 \nQ 14.9375 54.109375 19.578125 55.046875 \nQ 24.21875 56 28.609375 56 \nQ 40.484375 56 46.34375 49.84375 \nQ 52.203125 43.703125 52.203125 31.203125 \nz\n\" id=\"DejaVuSans-97\"/>\n <path d=\"M 37.109375 75.984375 \nL 37.109375 68.5 \nL 28.515625 68.5 \nQ 23.6875 68.5 21.796875 66.546875 \nQ 19.921875 64.59375 19.921875 59.515625 \nL 19.921875 54.6875 \nL 34.71875 54.6875 \nL 34.71875 47.703125 \nL 19.921875 47.703125 \nL 19.921875 0 \nL 10.890625 0 \nL 10.890625 47.703125 \nL 2.296875 47.703125 \nL 2.296875 54.6875 \nL 10.890625 54.6875 \nL 10.890625 58.5 \nQ 10.890625 67.625 15.140625 71.796875 \nQ 19.390625 75.984375 28.609375 75.984375 \nz\n\" id=\"DejaVuSans-102\"/>\n <path d=\"M 41.109375 46.296875 \nQ 39.59375 47.171875 37.8125 47.578125 \nQ 36.03125 48 33.890625 48 \nQ 26.265625 48 22.1875 43.046875 \nQ 18.109375 38.09375 18.109375 28.8125 \nL 18.109375 0 \nL 9.078125 0 \nL 9.078125 54.6875 \nL 18.109375 54.6875 \nL 18.109375 46.1875 \nQ 20.953125 51.171875 25.484375 53.578125 \nQ 30.03125 56 36.53125 56 \nQ 37.453125 56 38.578125 55.875 \nQ 39.703125 55.765625 41.0625 55.515625 \nz\n\" id=\"DejaVuSans-114\"/>\n <path d=\"M 30.609375 48.390625 \nQ 23.390625 48.390625 19.1875 42.75 \nQ 14.984375 37.109375 14.984375 27.296875 \nQ 14.984375 17.4
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAHbCAYAAAAJY9SEAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3de7ymc73/8dfbjNROhUwINR0msjvInk07hZLILofaiSJKTQfS+biT2NXu3O6oKL+0f6WURG0dpIOdnTJkO5UMEdNgoaQIw2f/cV1Td2ONGbO+y32vNa/n47Ee676/13Vf9yetWet9f09XqgpJkiRN3GrDLkCSJGm6MFhJkiQ1YrCSJElqxGAlSZLUiMFKkiSpEYOVJElSIwYrSZKkRgxWkiZFwh8Hvm5PuGng+fOHXZ8kTYa4QaikyZZwKfDiKr437FomImFmFYuHXYek0WWPlaShSLhXwicSFiVckfD+hNX7YzslLEg4NOG6hF8nPOdOrjUn4X8Sbkj4dsKnEz4zcPxJCT9N+H3CWQlbDxw7PeGQ/vsfEk5KWL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IkNWKwkiRJasRgJUmS1IjBSpIkqRGDlSRJUiMGK0mSpEYMVpIkSY0YrCRJkhoxWEmSJDVisJIkSWrEYCVJktSIwUqSJKkRg5UkSVIjBitJkqRGDFaSJEmNLDdYJTkqydVJzhtoe3+SXyY5J8nxSdbq22cnuSnJ2f3XpyazeEmSpFGyIj1WnwN2WqrtZOBRVfUY4FfAWwaOXVxVm/dfL2tTpiRJ0uhbbrCqqlOB65Zq+25VLe6fng5sNAm1SZIkTSkt5li9CPjWwPOHJPl5kh8ledKyXpRkXpL5Sea
},
"metadata": {
"needs_background": "light"
}
4 years ago
}
],
"source": [
"df = df[(df['artist_top_genre'] == 'afro dancehall') | (df['artist_top_genre'] == 'afropop') | (df['artist_top_genre'] == 'nigerian pop')]\n",
"df = df[(df['popularity'] > 0)]\n",
"top = df['artist_top_genre'].value_counts()\n",
"plt.figure(figsize=(10,7))\n",
"sns.barplot(x=top.index,y=top.values)\n",
"plt.xticks(rotation=45)\n",
"plt.title('Top genres',color = 'blue')"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"ipykernel_launcher:12: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" artist_top_genre popularity danceability acousticness loudness \\\n",
"1 1 30 0.710 0.0822 -5.640 \n",
"3 2 14 0.894 0.7980 -4.961 \n",
"4 1 25 0.702 0.1160 -6.044 \n",
"5 2 26 0.803 0.1270 -10.034 \n",
"6 2 29 0.818 0.4520 -9.840 \n",
".. ... ... ... ... ... \n",
"514 0 20 0.838 0.0358 -3.723 \n",
"515 0 14 0.786 0.1950 -4.232 \n",
"519 1 2 0.879 0.2240 -4.602 \n",
"522 0 26 0.863 0.0366 -3.130 \n",
"525 0 10 0.735 0.6320 -2.582 \n",
"\n",
" energy \n",
"1 0.683 \n",
"3 0.611 \n",
"4 0.833 \n",
"5 0.525 \n",
"6 0.587 \n",
".. ... \n",
"514 0.931 \n",
"515 0.806 \n",
"519 0.916 \n",
"522 0.896 \n",
"525 0.918 \n",
"\n",
"[286 rows x 6 columns]\n"
]
}
],
"source": [
"from sklearn.cluster import KMeans\n",
"wcss = []\n",
"\n",
"X = df[['artist_top_genre','popularity','danceability','acousticness','loudness','energy']]\n",
"\n",
"y = df['artist_top_genre']\n",
"\n",
"from sklearn.preprocessing import LabelEncoder\n",
"\n",
"le = LabelEncoder()\n",
"\n",
"X['artist_top_genre'] = le.fit_transform(X['artist_top_genre'])\n",
4 years ago
"\n",
"# X = le.transform(X)\n",
"\n",
"y = le.transform(y)\n",
"\n",
"print(X)\n",
"\n",
"for i in range(1, 11):\n",
" kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)\n",
" kmeans.fit(X)\n",
" # inertia method returns wcss for that model\n",
" wcss.append(kmeans.inertia_)"
4 years ago
]
},
{
"cell_type": "code",
"execution_count": 85,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.\n FutureWarning\n"
]
},
4 years ago
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 720x360 with 1 Axes>",
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"331.674375pt\" version=\"1.1\" viewBox=\"0 0 624.890625 331.674375\" width=\"624.890625pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;white-space:pre;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 331.674375 \nL 624.890625 331.674375 \nL 624.890625 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 59.690625 294.118125 \nL 617.690625 294.118125 \nL 617.690625 22.318125 \nL 59.690625 22.318125 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"matplotlib.axis_1\">\n <g id=\"xtick_1\">\n <g id=\"line2d_1\">\n <defs>\n <path d=\"M 0 0 \nL 0 3.5 \n\" id=\"m7811a6ee09\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"141.417898\" xlink:href=\"#m7811a6ee09\" y=\"294.118125\"/>\n </g>\n </g>\n <g id=\"text_1\">\n <!-- 2 -->\n <defs>\n <path d=\"M 19.1875 8.296875 \nL 53.609375 8.296875 \nL 53.609375 0 \nL 7.328125 0 \nL 7.328125 8.296875 \nQ 12.9375 14.109375 22.625 23.890625 \nQ 32.328125 33.6875 34.8125 36.53125 \nQ 39.546875 41.84375 41.421875 45.53125 \nQ 43.3125 49.21875 43.3125 52.78125 \nQ 43.3125 58.59375 39.234375 62.25 \nQ 35.15625 65.921875 28.609375 65.921875 \nQ 23.96875 65.921875 18.8125 64.3125 \nQ 13.671875 62.703125 7.8125 59.421875 \nL 7.8125 69.390625 \nQ 13.765625 71.78125 18.9375 73 \nQ 24.125 74.21875 28.421875 74.21875 \nQ 39.75 74.21875 46.484375 68.546875 \nQ 53.21875 62.890625 53.21875 53.421875 \nQ 53.21875 48.921875 51.53125 44.890625 \nQ 49.859375 40.875 45.40625 35.40625 \nQ 44.1875 33.984375 37.640625 27.21875 \nQ 31.109375 20.453125 19.1875 8.296875 \nz\n\" id=\"DejaVuSans-50\"/>\n </defs>\n <g transform=\"translate(138.236648 308.716563)scale(0.1 -0.1)\">\n <use xlink:href=\"#DejaVuSans-50\"/>\n </g>\n </g>\n </g>\n <g id=\"xtick_2\">\n <g id=\"line2d_2\">\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"254.14517\" xlink:href=\"#m7811a6ee09\" y=\"294.118125\"/>\n </g>\n </g>\n <g id=\"text_2\">\n <!-- 4 -->\n <defs>\n <path d=\"M 37.796875 64.3125 \nL 12.890625 25.390625 \nL 37.796875 25.390625 \nz\nM 35.203125 72.90625 \nL 47.609375 72.90625 \nL 47.609375 25.390625 \nL 58.015625 25.390625 \nL 58.015625 17.1875 \nL 47.609375 17.1875 \nL 47.609375 0 \nL 37.796875 0 \nL 37.796875 17.1875 \nL 4.890625 17.1875 \nL 4.890625 26.703125 \nz\n\" id=\"DejaVuSans-52\"/>\n </defs>\n <g transform=\"translate(250.96392 308.716563)scale(0.1 -0.1)\">\n <use xlink:href=\"#DejaVuSans-52\"/>\n </g>\n </g>\n </g>\n <g id=\"xtick_3\">\n <g id=\"line2d_3\">\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"366.872443\" xlink:href=\"#m7811a6ee09\" y=\"294.118125\"/>\n </g>\n </g>\n <g id=\"text_3\">\n <!-- 6 -->\n <defs>\n <path d=\"M 33.015625 40.375 \nQ 26.375 40.375 22.484375 35.828125 \nQ 18.609375 31.296875 18.609375 23.390625 \nQ 18.609375 15.53125 22.484375 10.953125 \nQ 26.375 6.390625 33.015625 6.390625 \nQ 39.65625 6.390625 43.53125 10.953125 \nQ 47.40625 15.53125 47.40625 23.390625 \nQ 47.40625 31.296875 43.53125 35.828125 \nQ 39.65625 40.375 33.015625 40.375 \nz\nM 52.59375 71.296875 \nL 52.59375 62.3125 \nQ 48.875 64.0625 45.09375 64.984375 \nQ 41.3125 65.921875 37.59375 65.921875 \nQ 27.828125 65.921875 22.671875 59.328125 \nQ 17.53125 52.734375 16.796875 39.40625 \nQ 19.671875 43.65625 24.015625 45.921875 \nQ 28.375 48.1875 33.59375 48.1875 \nQ 44.578125 48.1875 50.953125 41.515625 \nQ 57.328125 34.859375 57.328125 23.3906
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnAAAAFNCAYAAACAH1JNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3de7hcZXn///e9c4IQOQQSUgImICcjVMANBkUp4SyHwGypeER/VmqLoijfVnvQVv212tqq1NaWaqsoX5ByCic5yLkokABKQCqkhJAgkEggIIeEJPf3j7W2mezs7EOyZ6+Z2e/Xdc01M89aM+temYvkw/Os9TyRmUiSJKl1dFRdgCRJkgbHACdJktRiDHCSJEktxgAnSZLUYgxwkiRJLcYAJ0mS1GIMcJKaQkT8VUT8YBiOMz0iMiJGl+9viYg/aPRxh8NQnktEfDcivjQU3yVp6BngJA2LiPhN3WNtRLxc9/69Q3ys70bEqh7H/PlQHmNT1QXI+3q071DW/NgAv2dYAq+k5mSAkzQsMnNC9wN4HDihru38Bhzy7+qPmZlvbMAxNsf4iNin7v17gIVVFSOptRjgJDWTsRFxXkS8EBEPRkRn94aI2CkiLomIZRGxMCLOHMLjvi4i7o6I5yNiTkRMrDvuiWUtz5VDlK8v2z8UEVfW7fdIRPxX3fvFEbFfH8f8PnBa3fsPAOfV77Cxc46IY4A/A97VS+/itIi4o/wzvD4idujvXMpt+0fEveXnfghsMbA/OklVMMBJaiYnAhcC2wJXAN8EiIgO4Erg58BU4HDgkxFx9BAd9wPA/wf8DrAaOKc87p7ABcAngUnANcCVETEWuBV4W0R0RMROwFjg4PJzuwETgPv7OOYPgFMjYlREzCj3v6t7Y1/nnJnXAn8D/LCX3sX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gCHXYRcXpEzIuIecuWLauihA11dcHtt8PTT1ddiSRJajGNHEI9AliYmcsy81XgUuCtwLblkCrAzsAT5esngF0Ayu3bAM/Ut/f4zMbaN5CZ52ZmZ2Z2Tpo0aSjObfPVasWSWnPmVF2JJElqMY0McI8DMyNifHkt2+HAL4CbgXeW+5wGdCeYK8r3lNtvysws208t71LdFdgDuBuYC+xR3tU6luJGhysaeD5Da999YffdnU5EkiQNWiOvgbuL4maEe4H55bHOBf4U+FRELKC4xu075Ue+A2xftn8K+Ez5PQ8CF1GEv2uBMzJzTXmd3MeA64CHgIvKfVtDRNELd+ON8OyzVVcjSZJaSBSdXCNHZ2dnzps3r+oyCnffDW9+M5x3Hrz//VVXI0mSmkxE3JOZnT3bXYmhSp2dsPPODqNKkqRBMcBVqaOjmBPu2mvhN7+puhpJktQiDHBV6+qCV14pQpwkSdIAGOCqdsghMGmSk/pKkqQBM8BVbdQoOOkkuOqqoidOkiSpHwa4ZlCrFdfA/fjHVVciSZJagAGuGcyaBdts492okiRpQAxwzWDsWDjhhGJZrdWrq65GkiQ1OQNcs6jVYPlyuPXWqiuRJElNzgDXLI4+GsaPdxhVkiT1ywDXLMaPh2OPhcsug7Vrq65GkiQ1MQNcM+nqgiefhDvvrLoSSZLUxAxwzeS444obGpzUV5Ik9cEA10y23hqOPLK4Di6z6mokSVKTMsA1m1oNHnsMfvazqiuRJElNygDXbE48sVhey2FUSZK0EQa4ZrPDDnDooU4nIkmSNsoA14xqNXjooeIhSZLUgwGuGZ18cvFsL5wkSeqFAa4Z7bQTHHyw18FJkqReGeCaVVcX3HcfLFxYdSWSJKnJGOCaVfcw6mWXVVuHJElqOga4ZrXbbrDffg6jSpKkDRjgmllXF/zkJ8X6qJIkSSUDXDOr1Ypnh1ElSVIdA1wzmzE
4 years ago
},
"metadata": {
"needs_background": "light"
}
4 years ago
}
],
"source": [
"plt.figure(figsize=(10,5))\n",
"sns.lineplot(range(1, 11), wcss,marker='o',color='red')\n",
"plt.title('The Elbow Method')\n",
"plt.xlabel('Number of clusters')\n",
"plt.ylabel('WCSS')\n",
"plt.show()"
4 years ago
]
},
{
"cell_type": "code",
"execution_count": 88,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 432x288 with 1 Axes>",
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"265.728679pt\" version=\"1.1\" viewBox=\"0 0 372.103125 265.728679\" width=\"372.103125pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;white-space:pre;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 265.728679 \nL 372.103125 265.728679 \nL 372.103125 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 30.103125 228.172429 \nL 364.903125 228.172429 \nL 364.903125 10.732429 \nL 30.103125 10.732429 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"PathCollection_1\">\n <defs>\n <path d=\"M 0 3 \nC 0.795609 3 1.55874 2.683901 2.12132 2.12132 \nC 2.683901 1.55874 3 0.795609 3 0 \nC 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \nC 1.55874 -2.683901 0.795609 -3 0 -3 \nC -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \nC -2.683901 -1.55874 -3 -0.795609 -3 0 \nC -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \nC -1.55874 2.683901 -0.795609 3 0 3 \nz\n\" id=\"C0_0_e0592f08c6\"/>\n </defs>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"167.919579\" xlink:href=\"#C0_0_e0592f08c6\" y=\"90.221967\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#440154;stroke:#440154;\" x=\"100.300047\" xlink:href=\"#C0_0_e0592f08c6\" y=\"39.956499\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#440154;stroke:#440154;\" x=\"146.788475\" xlink:href=\"#C0_0_e0592f08c6\" y=\"92.407422\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#440154;stroke:#440154;\" x=\"151.014696\" xlink:href=\"#C0_0_e0592f08c6\" y=\"64.816051\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"163.693359\" xlink:href=\"#C0_0_e0592f08c6\" y=\"60.718323\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#440154;stroke:#440154;\" x=\"155.240917\" xlink:href=\"#C0_0_e0592f08c6\" y=\"63.450142\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"193.276904\" xlink:href=\"#C0_0_e0592f08c6\" y=\"53.06923\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"189.050683\" xlink:href=\"#C0_0_e0592f08c6\" y=\"103.881061\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#440154;stroke:#440154;\" x=\"138.336034\" xlink:href=\"#C0_0_e0592f08c6\" y=\"124.916067\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#440154;stroke:#440154;\" x=\"100.300047\" xlink:href=\"#C0_0_e0592f08c6\" y=\"95.685605\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"163.693359\" xlink:href=\"#C0_0_e0592f08c6\" y=\"113.715609\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"184.824463\" xlink:href=\"#C0_0_e0592f08c6\" y=\"89.402421\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#440154;stroke:#440154;\" x=\"112.978709\" xlink:href=\"#C0_0_e0592f08c6\" y=\"103.061516\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"193.276904\" xlink:href=\"#C0_0_e0592f08c6\" y=\"41.049227\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"231.312891\" xlink:href=\"#C0_0_e0592f08c6\" y=\"87.216966\"/>\n </g>\n <g clip-path=\"url(#pe1c08e0356)\">\n <use style=\"fill:#fde725;stroke:#fde725;\" x=\"193.276904\" xlink:href=\"#C0_0_e0592f08c6\" y=\"76.562872\"/>\n </g>\n <
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEKCAYAAAACS67iAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nOydd3xTVRvHv+dmpwNayhJZIgoKDlygghMngrj3QMWtuNeruAduRQQF3AMFBwKKiiiIgCKiyHIhyobuptn3ef84aTpy06alFcF8P5/389rk5tyTkDznnGf8HiUipEmTJk2abR9ja08gTZo0adI0DmmDniZNmjTbCWmDniZNmjTbCWmDniZNmjTbCWmDniZNmjTbCWmDniZNmjTbCXUadKXUeKXURqXUz0meV0qpZ5RSvymlflJK9Wr8aaZJkyZNmrpIZYf+MnBMLc8f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aKutGHWgDzLKRUPZi5X2ib1FNIVBCUPYUojJQ3tNQRga0mISU3AfBWaBs4B6Ayrp1m6/GRdnRJyGr07od7O2T1AIY4Nh66bppEknF5XIKcIyIXBz7+1zgABG5qso1OwAjgc7ALOBkoIeIJM3pagyXS2lhGR+P+4Ll83+lU4/2HD+0Py3a5jR4vN9+WMmwvndWS81zeV0ce9HhXPn0EHzFPk7fYWi1Ap4KlKEQU3C47HgyPTy/cASt2ufVec9l83/ltmPux1ecuEPaaY+OjFn0WEpzj0aivPv4R3z0/HT8pX72PWYvLn7obFp1aJnS6wFmTZzLI+ePxFAK09Q
},
"metadata": {
"needs_background": "light"
}
}
],
"source": [
"\n",
"from sklearn.cluster import KMeans\n",
"kmeans = KMeans(n_clusters = 2)\n",
"kmeans.fit(X)\n",
"labels = kmeans.predict(X)\n",
"plt.scatter(df['popularity'],df['danceability'],c = labels)\n",
"plt.xlabel('danceability')\n",
"plt.xlabel('popularity')\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Result: 143 out of 286 samples were correctly labeled.\nAccuracy score: 0.50\n"
]
}
],
"source": [
"\n",
"labels = kmeans.labels_\n",
"\n",
"correct_labels = sum(y == labels)\n",
"\n",
"print(\"Result: %d out of %d samples were correctly labeled.\" % (correct_labels, y.size))\n",
"\n",
"print('Accuracy score: {0:0.2f}'. format(correct_labels/float(y.size)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
4 years ago
}
]
}