Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "#les librairies utilisées\n",
  10. "import numpy as np\n",
  11. "from sklearn.neighbors import KNeighborsClassifier\n",
  12. "from sklearn.model_selection import train_test_split\n",
  13. "from sklearn.datasets import load_iris\n",
  14. "from IPython.display import display\n",
  15. "iris_dataset = load_iris()\n",
  16. "knn = KNeighborsClassifier(n_neighbors=1)\n"
  17. ]
  18. },
  19. {
  20. "cell_type": "code",
  21. "execution_count": 54,
  22. "metadata": {},
  23. "outputs": [],
  24. "source": [
  25. "#On lance la phase d'apprentissage qui est préalable.\n",
  26. "X_train,X_test,y_train,y_test= train_test_split(iris_dataset['data'],iris_dataset['target'],random_state=0)\n",
  27. "fit = knn.fit(X_train,y_train)"
  28. ]
  29. },
  30. {
  31. "cell_type": "code",
  32. "execution_count": 81,
  33. "metadata": {},
  34. "outputs": [
  35. {
  36. "name": "stdout",
  37. "output_type": "stream",
  38. "text": [
  39. "Forme du tableau X_new: (1, 4)\n",
  40. "Forme du tableau X_new2: (2, 4)\n",
  41. "Forme du tableau X_new_false: (2,)\n",
  42. "Forme du tableau X_train: (112, 4)\n"
  43. ]
  44. }
  45. ],
  46. "source": [
  47. "#Les mesures de notre amie botaniste\n",
  48. "X_new = np.array([[5,2.9,1,0.2]])\n",
  49. "X_new2 = np.array([[5,2.9,1,0.2],[2,9,2,1.3]])\n",
  50. "X_new_false= np.array([[5,2,1,0.3],[5,2,4]])\n",
  51. "print(\"Forme du tableau {}: {}\".format('X_new',X_new.shape))\n",
  52. "print(\"Forme du tableau {}: {}\".format('X_new2',X_new2.shape))\n",
  53. "print(\"Forme du tableau {}: {}\".format('X_new_false',X_new_false.shape))\n",
  54. "print(\"Forme du tableau {}: {}\".format('X_train',X_train.shape))"
  55. ]
  56. },
  57. {
  58. "cell_type": "markdown",
  59. "metadata": {},
  60. "source": [
  61. "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",
  62. "Nous voyons aussi avec X_new_false que le tableau np.array doit être homogêne pour afficher le nombre de colonne.\n",
  63. "(sinon quelle nombre de colonnes séléctioner ? 3? 4?)\n",
  64. "Amusons-nous à tester tout ça."
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 82,
  70. "metadata": {},
  71. "outputs": [
  72. {
  73. "name": "stdout",
  74. "output_type": "stream",
  75. "text": [
  76. "Prediction de X_new fait partie de l'espèce ['setosa']\n",
  77. "Prediction de X_new2 fait partie de l'espèce ['setosa' 'setosa']\n",
  78. "X_new_false affiche une erreur\n"
  79. ]
  80. }
  81. ],
  82. "source": [
  83. "tout = {'X_new':X_new,'X_new2':X_new2,'X_new_false':X_new_false}\n",
  84. "try:\n",
  85. " for key,value in tout.items():\n",
  86. " prediction = knn.predict(value)\n",
  87. " print(\"Prediction de {} fait partie de l'espèce {}\".format(key,iris_dataset['target_names'][prediction]),end=\"\\n\")\n",
  88. "except:\n",
  89. " print(\"{} affiche une erreur\".format(key))"
  90. ]
  91. },
  92. {
  93. "cell_type": "markdown",
  94. "metadata": {},
  95. "source": [
  96. "un message d'erreur s'affiche \n",
  97. "Comme on peut le voir X_new_false nous cause problème.\n",
  98. "Je vous laisse y réfléchir la solution est plutôt simple.\n",
  99. "La sortie de X_new2 est intéressante."
  100. ]
  101. }
  102. ],
  103. "metadata": {
  104. "kernelspec": {
  105. "display_name": "Python 3",
  106. "language": "python",
  107. "name": "python3"
  108. },
  109. "language_info": {
  110. "codemirror_mode": {
  111. "name": "ipython",
  112. "version": 3
  113. },
  114. "file_extension": ".py",
  115. "mimetype": "text/x-python",
  116. "name": "python",
  117. "nbconvert_exporter": "python",
  118. "pygments_lexer": "ipython3",
  119. "version": "3.7.1"
  120. }
  121. },
  122. "nbformat": 4,
  123. "nbformat_minor": 2
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement