{
"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": 9,
"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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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.1 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"
]
}
],
"source": [
"pip install seaborn"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" popularity loudness danceability\n",
"0 48 -6.699 0.666\n",
"1 30 -5.640 0.710\n",
"2 40 -7.127 0.836\n",
"3 14 -4.961 0.894\n",
"4 25 -6.044 0.702"
],
"text/html": "
\n\n
\n \n \n | \n popularity | \n loudness | \n danceability | \n
\n \n \n \n 0 | \n 48 | \n -6.699 | \n 0.666 | \n
\n \n 1 | \n 30 | \n -5.640 | \n 0.710 | \n
\n \n 2 | \n 40 | \n -7.127 | \n 0.836 | \n
\n \n 3 | \n 14 | \n -4.961 | \n 0.894 | \n
\n \n 4 | \n 25 | \n -6.044 | \n 0.702 | \n
\n \n
\n
"
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"from sklearn.cluster import KMeans\n",
"\n",
"plt.style.use(\"seaborn-whitegrid\")\n",
"plt.rc(\"figure\", autolayout=True)\n",
"plt.rc(\n",
" \"axes\",\n",
" labelweight=\"bold\",\n",
" labelsize=\"large\",\n",
" titleweight=\"bold\",\n",
" titlesize=14,\n",
" titlepad=10,\n",
")\n",
"\n",
"df = pd.read_csv(\"../../data/nigerian-songs.csv\")\n",
"X = df.loc[:, [\"popularity\", \"loudness\", \"danceability\"]]\n",
"X.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" popularity loudness danceability Cluster\n",
"0 48 -6.699 0.666 5\n",
"1 30 -5.640 0.710 0\n",
"2 40 -7.127 0.836 0\n",
"3 14 -4.961 0.894 4\n",
"4 25 -6.044 0.702 2"
],
"text/html": "\n\n
\n \n \n | \n popularity | \n loudness | \n danceability | \n Cluster | \n
\n \n \n \n 0 | \n 48 | \n -6.699 | \n 0.666 | \n 5 | \n
\n \n 1 | \n 30 | \n -5.640 | \n 0.710 | \n 0 | \n
\n \n 2 | \n 40 | \n -7.127 | \n 0.836 | \n 0 | \n
\n \n 3 | \n 14 | \n -4.961 | \n 0.894 | \n 4 | \n
\n \n 4 | \n 25 | \n -6.044 | \n 0.702 | \n 2 | \n
\n \n
\n
"
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"kmeans = KMeans(n_clusters=6)\n",
"X[\"Cluster\"] = kmeans.fit_predict(X)\n",
"X[\"Cluster\"] = X[\"Cluster\"].astype(\"category\")\n",
"\n",
"X.head()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "