{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1048,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Neurone artificiel\n",
    "#importation\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import *\n",
    "from sklearn.datasets import make_blobs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1049,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dimension de X: (100, 2)\n",
      "dimension de y: (100, 1)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 33348 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Génération du dataset\n",
    "X, y=make_blobs(n_samples= 100, n_features=2, centers=2, random_state=0)\n",
    "y = y.reshape((y.shape[0], 1))\n",
    "\n",
    "#Affichage des dimension du dataset\n",
    "print(\"dimension de X:\", X.shape)\n",
    "print(\"dimension de y:\", y.shape)\n",
    "\n",
    "#Mise en graphique du dataset\n",
    "plt.scatter(X[:,0], X[:,1], c=y, cmap=\"summer\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1050,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Création de la fonction d'initialisation de la matrice \"W\" et de l'entier \"b\"\n",
    "def Initialisation(X):\n",
    "    W = np.random.randn(X.shape[1], 1)\n",
    "    b = np.random.randn(1)\n",
    "    return (W, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1051,
   "metadata": {},
   "outputs": [],
   "source": [
    "#initialisation de la fonction de création du model linéaire\n",
    "def Model(X, W, b):\n",
    "    Z = X.dot(W) + b\n",
    "    A = 1 / (1 + np.exp(-Z))\n",
    "    return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1052,
   "metadata": {},
   "outputs": [],
   "source": [
    "#initialisation de la fonction cout (logloss) afin de determiner la marge d'erreur de notre model linéaire\n",
    "def Log_loss(A, y):\n",
    "    return 1 / len(y) * np.sum(-y * np.log(A) - (1 - y) * np.log(1 - A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1053,
   "metadata": {},
   "outputs": [],
   "source": [
    "#initialisation des valeurs des gradient de W et b\n",
    "def Gradients(A, X, y):\n",
    "    dW = 1 / len(y) * np.dot(X.T, A - y)\n",
    "    db = 1 /len(y) * np.sum(A - y)\n",
    "    return (dW, db)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1054,
   "metadata": {},
   "outputs": [],
   "source": [
    "#initialisation de la fonction d'update servant a mettre a jour les valeur de W en fonction de dW et de b en fonction de db afin de miniùmiser la marge d'erreur\n",
    "def Update(W, b, dW, db, LearningRate):\n",
    "    W = W - LearningRate * dW\n",
    "    b = b - LearningRate * db\n",
    "    return (W, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1055,
   "metadata": {},
   "outputs": [],
   "source": [
    "#initialisation de la fonction permettant de prédire à quelle classe appartiendrat un nouveaux cas selon \"x\" variable (2)\n",
    "def Predict(X, W, b):\n",
    "    A = Model(X, W, b)\n",
    "    print(A)\n",
    "    return A >= 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1056,
   "metadata": {},
   "outputs": [],
   "source": [
    "#initialisation de la fonction rassemblant toutes les autre pour former un neurone artificiel\n",
    "def ArtificialNeuron(X, y, LearningRate = 0.1, NbIter = 100):\n",
    "    #initialisation W, b\n",
    "    W, b = Initialisation(X)\n",
    "\n",
    "    #création d'une liste de suivit de la marge d'erreur du neurone en fonction du nombre d'itération\n",
    "    Loss = []\n",
    "\n",
    "    #Aprentissage: Répétition de la descente de grdient pour minimiser la marge d'erreure \n",
    "    for i in range(NbIter):\n",
    "        A=Model(X, W, b)\n",
    "        Loss.append(Log_loss(A, y))\n",
    "        dW, db = Gradients(A, X, y)\n",
    "        W, b = Update(W, b, dW, db, LearningRate)\n",
    "\n",
    "    #prédiction du dataset selon notre neurone après aprentissage\n",
    "    y_pred = Predict(X, W, b)\n",
    "\n",
    "    #comparaison avec les vrai réponse\n",
    "    print(accuracy_score(y, y_pred))\n",
    "    \n",
    "    \n",
    "    \n",
    "    #affichage de la liste de suivit de la marge d'erreur\n",
    "    plt.plot(Loss)\n",
    "    plt.show()\n",
    "\n",
    "    #renvoi des paramètre finaux après aprentissage\n",
    "    return (W, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1057,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9.79688138e-01]\n",
      " [6.22791607e-01]\n",
      " [5.18082049e-03]\n",
      " [8.88739676e-02]\n",
      " [9.64849669e-01]\n",
      " [3.30771238e-01]\n",
      " [7.61475671e-02]\n",
      " [9.67544074e-01]\n",
      " [4.57105461e-02]\n",
      " [7.73783334e-01]\n",
      " [3.30105495e-02]\n",
      " [8.15190806e-01]\n",
      " [3.68744341e-02]\n",
      " [1.60359382e-02]\n",
      " [6.75235745e-01]\n",
      " [9.89457701e-01]\n",
      " [9.88236894e-01]\n",
      " [3.19214556e-02]\n",
      " [5.67615062e-01]\n",
      " [5.53629690e-01]\n",
      " [5.60677414e-02]\n",
      " [3.78090404e-02]\n",
      " [3.52313676e-01]\n",
      " [5.30181359e-03]\n",
      " [9.39871220e-01]\n",
      " [3.78858770e-02]\n",
      " [8.07734208e-01]\n",
      " [1.39690586e-02]\n",
      " [8.65599153e-02]\n",
      " [7.53658384e-01]\n",
      " [9.65607891e-01]\n",
      " [5.26937372e-02]\n",
      " [5.35192277e-01]\n",
      " [9.66381696e-01]\n",
      " [3.95912523e-01]\n",
      " [3.05464666e-01]\n",
      " [6.89238507e-01]\n",
      " [5.59582898e-01]\n",
      " [5.08428441e-01]\n",
      " [3.16860896e-01]\n",
      " [4.51492407e-02]\n",
      " [1.51285886e-01]\n",
      " [9.61208671e-04]\n",
      " [1.77121842e-01]\n",
      " [1.68126301e-01]\n",
      " [8.20798139e-01]\n",
      " [9.41461262e-01]\n",
      " [9.23440007e-01]\n",
      " [3.89208057e-03]\n",
      " [1.23389526e-02]\n",
      " [9.04906833e-01]\n",
      " [4.19536712e-01]\n",
      " [1.03402487e-01]\n",
      " [5.68340945e-02]\n",
      " [8.69963723e-01]\n",
      " [5.28641961e-02]\n",
      " [7.74660620e-01]\n",
      " [7.56496114e-01]\n",
      " [9.48556791e-01]\n",
      " [9.84316161e-01]\n",
      " [5.73324815e-01]\n",
      " [2.34083446e-01]\n",
      " [5.33881791e-03]\n",
      " [9.21932085e-01]\n",
      " [2.24979024e-02]\n",
      " [3.57710691e-01]\n",
      " [2.88507806e-02]\n",
      " [7.09065742e-01]\n",
      " [9.47263130e-01]\n",
      " [3.09850898e-01]\n",
      " [1.79196482e-01]\n",
      " [8.65083643e-01]\n",
      " [9.78336202e-01]\n",
      " [1.47291503e-01]\n",
      " [8.03834871e-02]\n",
      " [3.38069095e-02]\n",
      " [4.25549261e-01]\n",
      " [8.73996450e-01]\n",
      " [4.30033780e-02]\n",
      " [4.15428194e-02]\n",
      " [2.48388164e-01]\n",
      " [1.07674416e-01]\n",
      " [4.27780057e-03]\n",
      " [1.54167179e-01]\n",
      " [7.86249419e-02]\n",
      " [1.22575801e-02]\n",
      " [9.66556144e-01]\n",
      " [1.25420806e-01]\n",
      " [7.15793882e-01]\n",
      " [9.45841986e-01]\n",
      " [9.05183630e-01]\n",
      " [9.74895565e-01]\n",
      " [3.78248543e-01]\n",
      " [8.42701951e-01]\n",
      " [4.17794856e-01]\n",
      " [8.41845869e-04]\n",
      " [9.42413229e-01]\n",
      " [9.59184874e-01]\n",
      " [7.13395316e-03]\n",
      " [3.26378301e-01]]\n",
      "0.86\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 21252 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#execution de la fonction d'entrainement du neurone artificiel\n",
    "W, b = ArtificialNeuron(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1058,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/q1/84vf8ynd79g502ds_gw52x580000gp/T/ipykernel_80509/3911570722.py:12: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored\n",
      "  plt.scatter(new_plant[0], new_plant[1], c='red', cmap=\"summer\")\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 41152 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.83940979]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ True])"
      ]
     },
     "execution_count": 1058,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#création d'une nouvelle plante\n",
    "new_plant = np.array([2,1])\n",
    "\n",
    "#calcul de la frontière de déscision\n",
    "x0 = np.linspace(-2, 5, 100)\n",
    "x1 = ( -W[0] * x0 -b) / W[1]\n",
    "\n",
    "#affichage du dataset de base\n",
    "plt.scatter(X[:,0], X[:,1], c=y, cmap=\"summer\")\n",
    "\n",
    "#ajout de la nouvelle plante en rouge dans le dataset\n",
    "plt.scatter(new_plant[0], new_plant[1], c='red', cmap=\"summer\")\n",
    "\n",
    "\n",
    "#affichage de la frontière de déscision\n",
    "plt.plot(x0, x1, 'b', lw=2)\n",
    "plt.show()\n",
    "\n",
    "#prédiction sur le type de la nouvelle plante\n",
    "Predict(new_plant, W, b)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
