Commit 0b6a62bb authored by EXT Monge Nicolas's avatar EXT Monge Nicolas
Browse files

repos initianlization

parent 9ecba006
.ipynb_checkpoints/*
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"id": "funny-february",
"metadata": {},
"source": [
"This notebook is for testing dependancies with xailib"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "identical-attraction",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.chdir('/home/nicolas/Documents/xai/xai')\n",
"\n",
"import xailib.modeling as mod"
]
}
],
"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.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Perceptron"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nous allons nous intéresser à l'algorithme du perceptron (Rosenblatt, 58) dont le pseudo-code est le suivant:\n",
"\n",
"`input: Train, eta, m, MaxEp\n",
"init : w\n",
"epoque=0\n",
"err=1\n",
"while epoque<=MaxEp and err!=0\n",
" err=0\n",
" for i in 1:m\n",
" prendre le i-ième exemple (x,y) de Train\n",
" dp <- produit_scalaire(x,w)\n",
" if (dp <= 0)\n",
" w <- w + eta*y*x\n",
" err <- err+1\n",
" epoque <- epoque+1\n",
"output: w`\n",
"\n",
"Le but étant de l'appliquer à quelques bases de la collection UCI (http://archive.ics.uci.edu/ml/index.php). En commençant par la base sonar qui constient 208 exemples en dimension 60 séparés par `,` et la dernière élément correspond à la classe de l'exemple. \n",
"\n",
"Pour cela nous allons nous appuyer sur les fonctions suivantes:\n",
"\n",
"* La fonction `ConstTrainTest` qui va télécharger la collection avec la fonction read_table de la librairie pandas (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_table.html), et crée une liste de listes qui contient chacune le vecteur représentatif d'un exemple en ajoutant sa classe en fin de la liste. En sortie, cette fonction retourne deux bases d'apprentissage et de test avec des proportions données:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import random\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"def ConstTrainTest(filename,perTest):\n",
" tag_df=pd.read_table(filename,sep=',',header=None)\n",
" print(len(tag_df))\n",
" Dic={'R': -1, 'M': +1}\n",
" X=[]\n",
" for e in range(len(tag_df)):\n",
" x=list(tag_df.loc[e,:])\n",
" cls=x.pop()\n",
" x.insert(len(x),Dic[cls])\n",
" X.append(x)\n",
"\n",
" random.shuffle(X)\n",
"\n",
" x_train ,x_test = train_test_split(X,test_size=perTest)\n",
" return x_train,x_test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* La fonction `dotproduct` qui calcule le produit scalaire entre un exemple $\\mathbf{x}=(x_1,\\ldots,x_d)$ et le vecteur poids $\\mathbf{w}=(w_0,w_1,\\ldots,w_d)$: $\\langle \\mathbf{w},\\mathbf{x} \\rangle=w_0+\\sum_{j=1}^d w_j x_j$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def dotproduct(x,w):\n",
" res=w[0]\n",
" for i in range(len(x)-1):\n",
" res+=w[i+1]*x[i]\n",
" return res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Appelez la fonction la fonction `ConstTrainTest` pour créer les bases d'apprentissage (Train) et de test (Test) avec une proportion 75% et 25%. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* L'algorithtme du perceptron prend en entrée la base Train, le nombre maximum d'époques et un pas d'apprentissage eta et donne en sortie le vecteur poids et l'erreur commise à la dernière époque:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def perceptron(Train,MaxEpoch,eta):\n",
" W=[0.0 for i in range(len(Train[0]))] \n",
" epoch=0\n",
" err=1\n",
" d=len(Train[0])-1\n",
" while (epoch<MaxEpoch and err!=0):\n",
" err=0\n",
" for x in Train:\n",
" dp=dotproduct(x,W) \n",
" if(x[d]*dp<=0):\n",
" err+=1\n",
" W[0]+=eta*x[d]\n",
" for i in range(d):\n",
" W[i+1]+=eta*x[d]*x[i]\n",
" epoch+=1\n",
" return W,err"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Tournez l'algorithme sur la base Train avec 1000 époques et un pas de eta=0.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Modifiez le code pour avoir la liste des erreurs commises à chaque époque. On appelera cette liste E. Le but étant d'afficher les erreurs en s'aidant de la fonction suivante: "
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"def plotting(X,Y,strtitle,strxlabel,strylabel):\n",
" plt.plot(X, Y)\n",
" plt.yscale('linear')\n",
" plt.title(strtitle)\n",
" plt.xlabel(strxlabel)\n",
" plt.ylabel(strylabel)\n",
" plt.grid(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Téléchargez les bases https://archive.ics.uci.edu/ml/datasets/spambase, https://archive.ics.uci.edu/ml/datasets/heart+Disease, https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Original%29, https://archive.ics.uci.edu/ml/datasets/Ionosphere et reportez dans le tableau ci-dessous les erreurs du modèles sur les bases tests (on gardera les mêmes paramètres qu'avant)\n",
" \n",
" \n",
" | Collection | Perceptron |\n",
" |------------|------------|\n",
" | Breast | |\n",
" | Ionosphere | |\n",
" | Heart | |\n",
" | Sonar | |\n",
" | Spam | |"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
,nicolas,srv-xai,31.03.2021 11:58,file:///home/nicolas/.config/libreoffice/4;
\ No newline at end of file
print("Hello World. \n Welcome to Python!")
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment