No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Untitled.ipynb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "id": "40152b50",
  7. "metadata": {},
  8. "outputs": [],
  9. "source": []
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": 18,
  14. "id": "be8d8613",
  15. "metadata": {},
  16. "outputs": [
  17. {
  18. "name": "stdout",
  19. "output_type": "stream",
  20. "text": [
  21. "training + evaluating time : 1.9439990520477295, score = 0.9504444444444444\n",
  22. "[[470 0 1 0 0 0 1 0 1 0]\n",
  23. " [ 0 502 2 1 0 0 0 0 0 0]\n",
  24. " [ 4 9 388 2 0 0 2 13 2 0]\n",
  25. " [ 2 0 2 454 1 12 0 2 3 3]\n",
  26. " [ 0 8 0 0 388 0 2 2 0 20]\n",
  27. " [ 1 3 0 11 0 368 6 0 0 2]\n",
  28. " [ 6 2 0 0 1 2 454 0 0 0]\n",
  29. " [ 1 5 2 0 1 0 0 440 0 14]\n",
  30. " [ 2 12 4 12 2 7 2 2 398 4]\n",
  31. " [ 1 2 0 8 8 0 0 5 0 415]]\n",
  32. "training + evaluating time : 7.785594463348389, score = 0.9304444444444444\n",
  33. "[[465 0 3 0 0 0 3 1 0 1]\n",
  34. " [ 0 494 4 1 3 0 2 0 0 1]\n",
  35. " [ 3 13 378 8 4 0 4 6 3 1]\n",
  36. " [ 3 1 9 446 0 2 1 3 8 6]\n",
  37. " [ 1 3 1 0 392 2 5 4 3 9]\n",
  38. " [ 5 2 0 9 4 353 8 1 7 2]\n",
  39. " [ 5 2 4 0 7 1 444 0 1 1]\n",
  40. " [ 2 3 3 3 4 1 0 431 0 16]\n",
  41. " [ 1 15 10 8 1 6 4 3 394 3]\n",
  42. " [ 4 2 1 10 11 2 0 11 8 390]]\n",
  43. "training + evaluating time : 27.00832462310791, score = 0.9651111111111111\n",
  44. "[[467 0 1 0 0 1 3 0 1 0]\n",
  45. " [ 1 495 4 2 1 1 0 0 0 1]\n",
  46. " [ 1 1 401 3 4 2 2 4 2 0]\n",
  47. " [ 1 0 3 460 0 8 0 2 3 2]\n",
  48. " [ 0 1 0 0 407 0 3 1 0 8]\n",
  49. " [ 0 0 0 7 0 378 4 0 1 1]\n",
  50. " [ 3 0 0 0 2 4 455 0 1 0]\n",
  51. " [ 1 3 2 1 2 1 0 443 0 10]\n",
  52. " [ 1 6 3 5 2 5 1 0 421 1]\n",
  53. " [ 1 2 1 8 4 1 0 4 2 416]]\n"
  54. ]
  55. }
  56. ],
  57. "source": [
  58. "import matplotlib.pyplot as plt\n",
  59. "from sklearn.neural_network import MLPClassifier\n",
  60. "from sklearn.neighbors import KNeighborsClassifier\n",
  61. "from sklearn.svm import SVC\n",
  62. "from sklearn.model_selection import train_test_split\n",
  63. "from sklearn.model_selection import KFold\n",
  64. "from sklearn.metrics import precision_score\n",
  65. "from sklearn.metrics import confusion_matrix\n",
  66. "import random\n",
  67. "import time\n",
  68. "from sklearn.datasets import fetch_openml \n",
  69. "\n",
  70. "#mnist = fetch_openml('mnist_784') \n",
  71. "\n",
  72. "indices = [i for i in range(len(mnist.data))]\n",
  73. "random.shuffle(indices)\n",
  74. "indices = indices[:15000]\n",
  75. "\n",
  76. "data = [mnist.data.values[i] for i in indices]\n",
  77. "target = [mnist.target[i] for i in indices]\n",
  78. "\n",
  79. "classifiers = [KNeighborsClassifier(3), MLPClassifier(hidden_layer_sizes = (100,)), SVC()]\n",
  80. "xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)\n",
  81. "\n",
  82. "\n",
  83. "for clf in classifiers:\n",
  84. " start = time.time()\n",
  85. " clf.fit(xtrain, ytrain)\n",
  86. " score = clf.score(xtest, ytest)\n",
  87. " end = time.time()\n",
  88. " print(f\"training + evaluating time : {end - start}, score = {score}\")\n",
  89. " print(confusion_matrix(ytest, clf.predict(xtest)))\n"
  90. ]
  91. },
  92. {
  93. "cell_type": "code",
  94. "execution_count": null,
  95. "id": "1aff265d",
  96. "metadata": {},
  97. "outputs": [],
  98. "source": []
  99. }
  100. ],
  101. "metadata": {
  102. "kernelspec": {
  103. "display_name": "Python 3 (ipykernel)",
  104. "language": "python",
  105. "name": "python3"
  106. },
  107. "language_info": {
  108. "codemirror_mode": {
  109. "name": "ipython",
  110. "version": 3
  111. },
  112. "file_extension": ".py",
  113. "mimetype": "text/x-python",
  114. "name": "python",
  115. "nbconvert_exporter": "python",
  116. "pygments_lexer": "ipython3",
  117. "version": "3.9.9"
  118. }
  119. },
  120. "nbformat": 4,
  121. "nbformat_minor": 5
  122. }