123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "id": "40152b50",
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "id": "be8d8613",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "training + evaluating time : 1.9439990520477295, score = 0.9504444444444444\n",
- "[[470 0 1 0 0 0 1 0 1 0]\n",
- " [ 0 502 2 1 0 0 0 0 0 0]\n",
- " [ 4 9 388 2 0 0 2 13 2 0]\n",
- " [ 2 0 2 454 1 12 0 2 3 3]\n",
- " [ 0 8 0 0 388 0 2 2 0 20]\n",
- " [ 1 3 0 11 0 368 6 0 0 2]\n",
- " [ 6 2 0 0 1 2 454 0 0 0]\n",
- " [ 1 5 2 0 1 0 0 440 0 14]\n",
- " [ 2 12 4 12 2 7 2 2 398 4]\n",
- " [ 1 2 0 8 8 0 0 5 0 415]]\n",
- "training + evaluating time : 7.785594463348389, score = 0.9304444444444444\n",
- "[[465 0 3 0 0 0 3 1 0 1]\n",
- " [ 0 494 4 1 3 0 2 0 0 1]\n",
- " [ 3 13 378 8 4 0 4 6 3 1]\n",
- " [ 3 1 9 446 0 2 1 3 8 6]\n",
- " [ 1 3 1 0 392 2 5 4 3 9]\n",
- " [ 5 2 0 9 4 353 8 1 7 2]\n",
- " [ 5 2 4 0 7 1 444 0 1 1]\n",
- " [ 2 3 3 3 4 1 0 431 0 16]\n",
- " [ 1 15 10 8 1 6 4 3 394 3]\n",
- " [ 4 2 1 10 11 2 0 11 8 390]]\n",
- "training + evaluating time : 27.00832462310791, score = 0.9651111111111111\n",
- "[[467 0 1 0 0 1 3 0 1 0]\n",
- " [ 1 495 4 2 1 1 0 0 0 1]\n",
- " [ 1 1 401 3 4 2 2 4 2 0]\n",
- " [ 1 0 3 460 0 8 0 2 3 2]\n",
- " [ 0 1 0 0 407 0 3 1 0 8]\n",
- " [ 0 0 0 7 0 378 4 0 1 1]\n",
- " [ 3 0 0 0 2 4 455 0 1 0]\n",
- " [ 1 3 2 1 2 1 0 443 0 10]\n",
- " [ 1 6 3 5 2 5 1 0 421 1]\n",
- " [ 1 2 1 8 4 1 0 4 2 416]]\n"
- ]
- }
- ],
- "source": [
- "import matplotlib.pyplot as plt\n",
- "from sklearn.neural_network import MLPClassifier\n",
- "from sklearn.neighbors import KNeighborsClassifier\n",
- "from sklearn.svm import SVC\n",
- "from sklearn.model_selection import train_test_split\n",
- "from sklearn.model_selection import KFold\n",
- "from sklearn.metrics import precision_score\n",
- "from sklearn.metrics import confusion_matrix\n",
- "import random\n",
- "import time\n",
- "from sklearn.datasets import fetch_openml \n",
- "\n",
- "#mnist = fetch_openml('mnist_784') \n",
- "\n",
- "indices = [i for i in range(len(mnist.data))]\n",
- "random.shuffle(indices)\n",
- "indices = indices[:15000]\n",
- "\n",
- "data = [mnist.data.values[i] for i in indices]\n",
- "target = [mnist.target[i] for i in indices]\n",
- "\n",
- "classifiers = [KNeighborsClassifier(3), MLPClassifier(hidden_layer_sizes = (100,)), SVC()]\n",
- "xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)\n",
- "\n",
- "\n",
- "for clf in classifiers:\n",
- " start = time.time()\n",
- " clf.fit(xtrain, ytrain)\n",
- " score = clf.score(xtest, ytest)\n",
- " end = time.time()\n",
- " print(f\"training + evaluating time : {end - start}, score = {score}\")\n",
- " print(confusion_matrix(ytest, clf.predict(xtest)))\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1aff265d",
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3 (ipykernel)",
- "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.9.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 5
- }
|