{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "wicked-opportunity",
   "metadata": {},
   "source": [
    "# Hands-on Session 1: Statistics 101"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "substantial-bradley",
   "metadata": {},
   "source": [
    "## Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "charming-motor",
   "metadata": {},
   "source": [
    "Language: Python https://www.python.org/\n",
    "\n",
    "Package manager: Pip \n",
    "\n",
    "Alternative: Anaconda with Conda https://www.anaconda.com/ "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lesser-albert",
   "metadata": {},
   "source": [
    "### Clean environment"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fifteen-gauge",
   "metadata": {},
   "source": [
    "Create a folder that will contain the ML hands-on's.\n",
    "\n",
    "```mkdir ~/path_to_folder```\n",
    "\n",
    "Now move to the new folder.\n",
    "\n",
    "```cd ~/path_to_folder```\n",
    "\n",
    "Create a local Python environment.\n",
    "\n",
    "```python3 -m venv env```\n",
    "\n",
    "You can activate it at any time with\n",
    "\n",
    "```source ~/path_to_folder/env/bin/activate```\n",
    "\n",
    "and deactivate it with\n",
    "\n",
    "```deactivate```.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cheap-startup",
   "metadata": {},
   "source": [
    "### Packages"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "coordinated-cheat",
   "metadata": {},
   "source": [
    "Numpy https://numpy.org/\n",
    "\n",
    "Scipy https://www.scipy.org/\n",
    "\n",
    "Matplotlib https://matplotlib.org/\n",
    "\n",
    "Pandas https://pandas.pydata.org/\n",
    "\n",
    "Scikit-learn https://scikit-learn.org/stable/index.html\n",
    "\n",
    "Jupyter https://jupyter.org/\n",
    "\n",
    "Once in your local env you can install them with \n",
    "\n",
    "```pip install numpy scipy matplotlib pandas scikit-learn jupyter```."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "molecular-currency",
   "metadata": {},
   "source": [
    "### Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "working-budapest",
   "metadata": {},
   "source": [
    "Jupyter notebooks allow to present code and run it as you want.\n",
    "In your local env, use\n",
    "\n",
    "```jupyter notebook```\n",
    "\n",
    "to start it. It will automatically start a server that can be opened in your browser\n",
    "(works with Firefox and probably all the major ones).\n",
    "\n",
    "The two major cell types that you are going to use are code cells and markdown cells.\n",
    "A cell can be executed with CAPS + ENTER."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "great-broadcast",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "furnished-arbitration",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline \n",
    "#Allows matplotlib to work in a notebook\n",
    "#No need to import scipy or sklearn for now"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deluxe-sample",
   "metadata": {},
   "source": [
    "## Hands on 1 Mean or Median?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "controversial-arcade",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "broad-arrest",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Gaussian samples\n",
    "mc_runs = 100\n",
    "mu = 1\n",
    "\n",
    "ns = np.power(10, np.arange(7))\n",
    "mean_mses = []\n",
    "median_mses = []\n",
    "\n",
    "for n in ns:\n",
    "    mean_mse = 0\n",
    "    median_mse = 0\n",
    "    \n",
    "    for _ in range(mc_runs):\n",
    "        X = np.random.normal(mu, 1, n)\n",
    "        mean_mse += (mu - np.mean(X))**2\n",
    "        median_mse += (mu - np.median(X))**2\n",
    "    \n",
    "    mean_mse /= mc_runs\n",
    "    median_mse /= mc_runs \n",
    "    \n",
    "    mean_mses.append(mean_mse)\n",
    "    median_mses.append(median_mse)\n",
    "    \n",
    "plt.plot(ns, mean_mses, label='Mean')\n",
    "plt.plot(ns, median_mses, label='Median')\n",
    "plt.xscale('log')\n",
    "plt.yscale('log')\n",
    "plt.legend()\n",
    "plt.title('L2 Risk for gaussian samples')\n",
    "plt.xlabel('n')\n",
    "plt.ylabel('L2 Risk')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "discrete-pioneer",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Laplace samples\n",
    "mc_runs = 100\n",
    "mu = 1\n",
    "\n",
    "ns = np.power(10, np.arange(7))\n",
    "mean_mses = []\n",
    "median_mses = []\n",
    "\n",
    "for n in ns:\n",
    "    mean_mse = 0\n",
    "    median_mse = 0\n",
    "    \n",
    "    for _ in range(mc_runs):\n",
    "        X = np.random.laplace(mu, 1, n)\n",
    "        mean_mse += (mu - np.mean(X))**2\n",
    "        median_mse += (mu - np.median(X))**2\n",
    "    \n",
    "    mean_mse /= mc_runs\n",
    "    median_mse /= mc_runs \n",
    "    \n",
    "    mean_mses.append(mean_mse)\n",
    "    median_mses.append(median_mse)\n",
    "    \n",
    "plt.plot(ns, mean_mses, label='Mean')\n",
    "plt.plot(ns, median_mses, label='Median')\n",
    "plt.xscale('log')\n",
    "plt.yscale('log')\n",
    "plt.legend()\n",
    "plt.title('L2 Risk for Laplace samples')\n",
    "plt.xlabel('n')\n",
    "plt.ylabel('L2 Risk')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dutch-perfume",
   "metadata": {},
   "source": [
    "## Hands on 2 Linear Regression with scikitlearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "appreciated-update",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn import datasets, linear_model\n",
    "from sklearn.metrics import mean_squared_error, r2_score\n",
    "\n",
    "# Load the diabetes dataset\n",
    "diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)\n",
    "\n",
    "print(\"Example of input data:\")\n",
    "print(diabetes_X[0])\n",
    "\n",
    "print(\"Example of output data:\")\n",
    "print(diabetes_y[0])\n",
    "\n",
    "# Use only one feature\n",
    "diabetes_X = diabetes_X[:, [1, 2, 4, 8]]\n",
    "\n",
    "# Split the data into training/testing sets\n",
    "diabetes_X_train = diabetes_X[:-20]\n",
    "diabetes_X_test = diabetes_X[-20:]\n",
    "\n",
    "# Split the targets into training/testing sets\n",
    "diabetes_y_train = diabetes_y[:-20]\n",
    "diabetes_y_test = diabetes_y[-20:]\n",
    "\n",
    "# Create linear regression object\n",
    "regr = linear_model.LinearRegression()\n",
    "\n",
    "# Train the model using the training sets\n",
    "regr.fit(diabetes_X_train, diabetes_y_train)\n",
    "\n",
    "# Make predictions using the testing set\n",
    "diabetes_y_pred = regr.predict(diabetes_X_test)\n",
    "\n",
    "# The coefficients\n",
    "print('Coefficients: \\n', regr.coef_)\n",
    "# The mean squared error\n",
    "print('Mean squared error: %.2f'\n",
    "      % mean_squared_error(diabetes_y_test, diabetes_y_pred))\n",
    "# The coefficient of determination: 1 is perfect prediction\n",
    "print('Coefficient of determination: %.2f'\n",
    "      % r2_score(diabetes_y_test, diabetes_y_pred))\n",
    "\n",
    "# Plot outputs\n",
    "#plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')\n",
    "#plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=3)\n",
    "\n",
    "#plt.xticks(())\n",
    "#plt.yticks(())\n",
    "\n",
    "#plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "japanese-adaptation",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
