{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Machine learning\n", "### Méthode naïve de Bayes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On charge les données et on parcours les données sur lesquelles nous allons travailler" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".. _wine_dataset:\n", "\n", "Wine recognition dataset\n", "------------------------\n", "\n", "**Data Set Characteristics:**\n", "\n", " :Number of Instances: 178 (50 in each of three classes)\n", " :Number of Attributes: 13 numeric, predictive attributes and the class\n", " :Attribute Information:\n", " \t\t- Alcohol\n", " \t\t- Malic acid\n", " \t\t- Ash\n", "\t\t- Alcalinity of ash \n", " \t\t- Magnesium\n", "\t\t- Total phenols\n", " \t\t- Flavanoids\n", " \t\t- Nonflavanoid phenols\n", " \t\t- Proanthocyanins\n", "\t\t- Color intensity\n", " \t\t- Hue\n", " \t\t- OD280/OD315 of diluted wines\n", " \t\t- Proline\n", "\n", " - class:\n", " - class_0\n", " - class_1\n", " - class_2\n", "\t\t\n", " :Summary Statistics:\n", " \n", " ============================= ==== ===== ======= =====\n", " Min Max Mean SD\n", " ============================= ==== ===== ======= =====\n", " Alcohol: 11.0 14.8 13.0 0.8\n", " Malic Acid: 0.74 5.80 2.34 1.12\n", " Ash: 1.36 3.23 2.36 0.27\n", " Alcalinity of Ash: 10.6 30.0 19.5 3.3\n", " Magnesium: 70.0 162.0 99.7 14.3\n", " Total Phenols: 0.98 3.88 2.29 0.63\n", " Flavanoids: 0.34 5.08 2.03 1.00\n", " Nonflavanoid Phenols: 0.13 0.66 0.36 0.12\n", " Proanthocyanins: 0.41 3.58 1.59 0.57\n", " Colour Intensity: 1.3 13.0 5.1 2.3\n", " Hue: 0.48 1.71 0.96 0.23\n", " OD280/OD315 of diluted wines: 1.27 4.00 2.61 0.71\n", " Proline: 278 1680 746 315\n", " ============================= ==== ===== ======= =====\n", "\n", " :Missing Attribute Values: None\n", " :Class Distribution: class_0 (59), class_1 (71), class_2 (48)\n", " :Creator: R.A. Fisher\n", " :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n", " :Date: July, 1988\n", "\n", "This is a copy of UCI ML Wine recognition datasets.\n", "https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data\n", "\n", "The data is the results of a chemical analysis of wines grown in the same\n", "region in Italy by three different cultivators. There are thirteen different\n", "measurements taken for different constituents found in the three types of\n", "wine.\n", "\n", "Original Owners: \n", "\n", "Forina, M. et al, PARVUS - \n", "An Extendible Package for Data Exploration, Classification and Correlation. \n", "Institute of Pharmaceutical and Food Analysis and Technologies,\n", "Via Brigata Salerno, 16147 Genoa, Italy.\n", "\n", "Citation:\n", "\n", "Lichman, M. (2013). UCI Machine Learning Repository\n", "[https://archive.ics.uci.edu/ml]. Irvine, CA: University of California,\n", "School of Information and Computer Science. \n", "\n", ".. topic:: References\n", "\n", " (1) S. Aeberhard, D. Coomans and O. de Vel, \n", " Comparison of Classifiers in High Dimensional Settings, \n", " Tech. Rep. no. 92-02, (1992), Dept. of Computer Science and Dept. of \n", " Mathematics and Statistics, James Cook University of North Queensland. \n", " (Also submitted to Technometrics). \n", "\n", " The data was used with many others for comparing various \n", " classifiers. The classes are separable, though only RDA \n", " has achieved 100% correct classification. \n", " (RDA : 100%, QDA 99.4%, LDA 98.9%, 1NN 96.1% (z-transformed data)) \n", " (All results using the leave-one-out technique) \n", "\n", " (2) S. Aeberhard, D. Coomans and O. de Vel, \n", " \"THE CLASSIFICATION PERFORMANCE OF RDA\" \n", " Tech. Rep. no. 92-01, (1992), Dept. of Computer Science and Dept. of \n", " Mathematics and Statistics, James Cook University of North Queensland. \n", " (Also submitted to Journal of Chemometrics).\n", "\n" ] } ], "source": [ "from sklearn import datasets\n", "wine = datasets.load_wine()\n", "print(wine.DESCR)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Features: ['alcohol', 'malic_acid', 'ash', 'alcalinity_of_ash', 'magnesium', 'total_phenols', 'flavanoids', 'nonflavanoid_phenols', 'proanthocyanins', 'color_intensity', 'hue', 'od280/od315_of_diluted_wines', 'proline']\n", "Labels: ['class_0' 'class_1' 'class_2']\n" ] } ], "source": [ "print('Features: ', wine.feature_names)\n", "print('Labels: ', wine.target_names)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "data = wine.data\n", "target = wine.target" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1.423e+01 1.710e+00 2.430e+00 ... 1.040e+00 3.920e+00 1.065e+03]\n", " [1.320e+01 1.780e+00 2.140e+00 ... 1.050e+00 3.400e+00 1.050e+03]\n", " [1.316e+01 2.360e+00 2.670e+00 ... 1.030e+00 3.170e+00 1.185e+03]\n", " ...\n", " [1.327e+01 4.280e+00 2.260e+00 ... 5.900e-01 1.560e+00 8.350e+02]\n", " [1.317e+01 2.590e+00 2.370e+00 ... 6.000e-01 1.620e+00 8.400e+02]\n", " [1.413e+01 4.100e+00 2.740e+00 ... 6.100e-01 1.600e+00 5.600e+02]]\n", "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", " 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", " 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n", " 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]\n" ] } ], "source": [ "print(data)\n", "print(target)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On sépare les données en deux parties : une pour l'entrainement et une pour le test" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=.3, random_state=109)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Classifieur naïf de Bayes" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "GaussianNB(priors=None, var_smoothing=1e-09)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.naive_bayes import GaussianNB\n", "nb = GaussianNB()\n", "nb.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "y_pred = nb.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 90.74%\n" ] } ], "source": [ "from sklearn import metrics\n", "scores = metrics.accuracy_score(y_test, y_pred)\n", "print('Accuracy: ','{:2.2%}'.format(scores))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[20 1 0]\n", " [ 2 15 2]\n", " [ 0 0 14]]\n" ] } ], "source": [ "cm = metrics.confusion_matrix(y_test, y_pred)\n", "print(cm)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " 0 0.91 0.95 0.93 21\n", " 1 0.94 0.79 0.86 19\n", " 2 0.88 1.00 0.93 14\n", "\n", " accuracy 0.91 54\n", " macro avg 0.91 0.91 0.91 54\n", "weighted avg 0.91 0.91 0.91 54\n", "\n" ] } ], "source": [ "from sklearn.metrics import classification_report\n", "print(classification_report(y_test, y_pred))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9074074074074074\n" ] } ], "source": [ "import numpy as np\n", "print(np.sum(np.diag(cm)/np.sum(cm)))" ] }, { "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }