Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "#les librairies utilisées\n",
- "import numpy as np\n",
- "from sklearn.neighbors import KNeighborsClassifier\n",
- "from sklearn.model_selection import train_test_split\n",
- "from sklearn.datasets import load_iris\n",
- "from IPython.display import display\n",
- "iris_dataset = load_iris()\n",
- "knn = KNeighborsClassifier(n_neighbors=1)\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "metadata": {},
- "outputs": [],
- "source": [
- "#On lance la phase d'apprentissage qui est préalable.\n",
- "X_train,X_test,y_train,y_test= train_test_split(iris_dataset['data'],iris_dataset['target'],random_state=0)\n",
- "fit = knn.fit(X_train,y_train)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 81,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Forme du tableau X_new: (1, 4)\n",
- "Forme du tableau X_new2: (2, 4)\n",
- "Forme du tableau X_new_false: (2,)\n",
- "Forme du tableau X_train: (112, 4)\n"
- ]
- }
- ],
- "source": [
- "#Les mesures de notre amie botaniste\n",
- "X_new = np.array([[5,2.9,1,0.2]])\n",
- "X_new2 = np.array([[5,2.9,1,0.2],[2,9,2,1.3]])\n",
- "X_new_false= np.array([[5,2,1,0.3],[5,2,4]])\n",
- "print(\"Forme du tableau {}: {}\".format('X_new',X_new.shape))\n",
- "print(\"Forme du tableau {}: {}\".format('X_new2',X_new2.shape))\n",
- "print(\"Forme du tableau {}: {}\".format('X_new_false',X_new_false.shape))\n",
- "print(\"Forme du tableau {}: {}\".format('X_train',X_train.shape))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Nous voyons ici que la forme s'affiche en format 'tuple', le premier nombre correspond aux lignes de ce tableau le deuxième aux colonnes.\n",
- "Nous voyons aussi avec X_new_false que le tableau np.array doit être homogêne pour afficher le nombre de colonne.\n",
- "(sinon quelle nombre de colonnes séléctioner ? 3? 4?)\n",
- "Amusons-nous à tester tout ça."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 82,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Prediction de X_new fait partie de l'espèce ['setosa']\n",
- "Prediction de X_new2 fait partie de l'espèce ['setosa' 'setosa']\n",
- "X_new_false affiche une erreur\n"
- ]
- }
- ],
- "source": [
- "tout = {'X_new':X_new,'X_new2':X_new2,'X_new_false':X_new_false}\n",
- "try:\n",
- " for key,value in tout.items():\n",
- " prediction = knn.predict(value)\n",
- " print(\"Prediction de {} fait partie de l'espèce {}\".format(key,iris_dataset['target_names'][prediction]),end=\"\\n\")\n",
- "except:\n",
- " print(\"{} affiche une erreur\".format(key))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "un message d'erreur s'affiche \n",
- "Comme on peut le voir X_new_false nous cause problème.\n",
- "Je vous laisse y réfléchir la solution est plutôt simple.\n",
- "La sortie de X_new2 est intéressante."
- ]
- }
- ],
- "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.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement