{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0555a39",
   "metadata": {},
   "source": [
    "# Introduction to Machine Learning\n",
    "\n",
    "Mathurin Massias, mathurin.massias@inria.fr\n",
    "Rémi Gribonval, remi.gribonval@inria.fr\n",
    "\n",
    "The first part of this notebook is based upon https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html and other examples from the scikit-learn documentation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c8161d68",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sklearn\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "051c843e",
   "metadata": {},
   "source": [
    "## Examples of ML data\n",
    "\n",
    "Lets start with the Digits imaging dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a0c1182",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_digits\n",
    "digits = load_digits()\n",
    "data = digits.data\n",
    "labels = digits.target\n",
    "n_samples, n_features = data.shape\n",
    "n_digits = np.unique(labels).size\n",
    "print(f\"# labels: {n_digits}; # samples = n =  {n_samples}; # features = p = {n_features}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60fab955",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axarr = plt.subplots(1, 10, figsize=(12, 4))\n",
    "for i in range(len(axarr)):\n",
    "    axarr[i].imshow(digits.images[i], cmap='gray');\n",
    "    axarr[i].axis(\"off\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "711649e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(labels[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17798258",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(digits.images[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c32bfda3",
   "metadata": {},
   "source": [
    "What's the ML setup here?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18225309",
   "metadata": {},
   "source": [
    "## Iris dataset\n",
    "\n",
    "Iris is a dataset containing measures of petals and sepals sizes (width, length) of various sample of three iris varieties (Setosa, Versicolour, Virginica) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e2e2a43",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "iris = load_iris()\n",
    "X = iris.data[:, :2]  # we keep only two features our of four for visualization purposes\n",
    "y = iris.target "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09b182f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5\n",
    "y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5\n",
    "\n",
    "plt.figure(2, figsize=(8, 6))\n",
    "plt.clf()\n",
    "# Plot the training points\n",
    "plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Set1, edgecolor=\"k\")\n",
    "plt.xlabel(\"Sepal length\")\n",
    "plt.ylabel(\"Sepal width\")\n",
    "\n",
    "plt.xlim(x_min, x_max)\n",
    "plt.ylim(y_min, y_max)\n",
    "plt.xticks(())\n",
    "plt.yticks(())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a92be6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.scatter(*X.T, c=y, cmap=plt.cm.Set1, edgecolor=\"k\")\n",
    "plt.xlabel(\"Sepal length\")\n",
    "plt.ylabel(\"Sepal width\");"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19c48161",
   "metadata": {},
   "source": [
    "What type of problem are we dealing with here?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95cfbe06",
   "metadata": {},
   "source": [
    "## Diabetes dataset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "289f5b55",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_diabetes\n",
    "\n",
    "diabetes_X, diabetes_y = load_diabetes(return_X_y=True)\n",
    "# keep a single feature\n",
    "diabetes_X = diabetes_X[:, 2:3]\n",
    "# train/test split \n",
    "diabetes_X_train = diabetes_X[:-20]\n",
    "diabetes_X_test = diabetes_X[-20:]\n",
    "diabetes_y_train = diabetes_y[:-20]\n",
    "diabetes_y_test = diabetes_y[-20:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbb93ca5",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(diabetes_X.shape)\n",
    "print(diabetes_X[:10])\n",
    "print(diabetes_y[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1080cf77",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import linear_model\n",
    "from sklearn.metrics import mean_squared_error, r2_score\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\" % mean_squared_error(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": "f9eba61f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
