{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Clustering hands-on\n",
    "Goal of this excercise is to complete the hands-on experience task with similar task description as in the clustering project case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "x6XOUhvFa-wT"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sklearn.preprocessing, sklearn.cluster, sklearn.metrics\n",
    "import scipy.spatial\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data description\n",
    "\n",
    "Data source: https://www.kaggle.com/datasets/subhajitnayak/country-data?resource=download&select=Country-data.csv\n",
    "\n",
    "|Attribute|Description|\n",
    "|:-------|:-------|\n",
    "|country | Name of the country |\n",
    "|child_mort | Death of children under 5 years of age per 1000 live births |\n",
    "|exports | Exports of goods and services. Given as %age of the Total GDP |\n",
    "|health | Total health spending as %age of Total GDP |\n",
    "|imports | Imports of goods and services. Given as %age of the Total GDP |\n",
    "|Income | Net income per person |\n",
    "|Inflation | The measurement of the annual growth rate of the Total GDP |\n",
    "|life_expec | The average number of years a new born child would live if the current mortality patterns are to rem... |\n",
    "|total_fer | The number of children that would be born to each woman if the current age-fertility rates remain th... |\n",
    "|gdpp | The GDP per capita. Calculated as the Total GDP divided by the total population. |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('country-data.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_only_numeric = df.select_dtypes(np.number)\n",
    "\n",
    "# Create the subplots\n",
    "n_rows, n_cols = df_only_numeric.shape\n",
    "fig, axes = plt.subplots(nrows=1, ncols=n_cols, figsize=(40, 6))\n",
    "for i, column in enumerate(df_only_numeric):\n",
    "    sns.histplot(data=df_only_numeric, x=column, ax=axes[i]).set_title(column)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Is it wise to scale the data?\n",
    "- How would it help us?\n",
    "- What different options do we have?\n",
    "\n",
    "## Use k-means or agglomerative clustering algorithm to cluster the data\n",
    "- How many clusters did you choose? Why?\n",
    "\n",
    "## Perform an exploration analysis of the clustered data\n",
    "- Write down your conlusions about differences/similarities about the feature characteristics according to the individial clusters\n",
    "\n",
    "## Write down a conlustion about clusters as a whole, i.e. try to characterize the data in them in a few sentences\n",
    "- Cluster 1 - ...\n",
    "- Cluster 2 - ...\n",
    "- ...\n",
    "- Cluster N - ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task (2p)\n",
    "- Finished the proposed tasks\n",
    "\n",
    "**Write down conclusion to the Markdown cell**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data preprocessing\n",
    "- Why do we need to preprocess the data?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df_only_numeric.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scaler = sklearn.preprocessing.MinMaxScaler()\n",
    "X_min_max_scaled = scaler.fit_transform(X)\n",
    "X_min_max_scaled"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.select_dtypes(np.number).columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_scaled = pd.DataFrame(X_min_max_scaled, columns=df_only_numeric.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_scaled"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "cv4.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
