{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"mount_file_id":"12GVGXoH5580yBK-GYU-LBGHhw3q2RWnt","authorship_tag":"ABX9TyMTtFQIX31NmVg0b5t3x8FU"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"id":"KjD28oyNaAM9","executionInfo":{"status":"ok","timestamp":1678870486154,"user_tz":-330,"elapsed":335,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}}},"outputs":[],"source":["import numpy as np\n","import matplotlib.pyplot as plt\n","from keras.datasets import cifar10\n","\n","plt.rcParams['figure.figsize'] = (10.0,8.0)\n","plt.rcParams['image.interpolation'] = 'nearest'\n","plt.rcParams['image.cmap'] = 'gray'"]},{"cell_type":"code","source":["(X_train,y_train),(X_test,y_test) = cifar10.load_data()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"GjtwJVfNd1dv","executionInfo":{"status":"ok","timestamp":1678870546337,"user_tz":-330,"elapsed":7855,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}},"outputId":"fbc5be46-ace5-48de-cbbd-dbbe2de685de"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\n","170498071/170498071 [==============================] - 4s 0us/step\n"]}]},{"cell_type":"code","source":["print(\"Number of training images: \", len(X_train))\n","print(\"Number of testing images : \", len(X_test))\n","print(\"Size of each image  : \", len(X_train[0]),\"x\",len(X_train[0][0]),\"x\",len(X_train[0][0][0]))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"sGVm8u3Ye2hq","executionInfo":{"status":"ok","timestamp":1678870920922,"user_tz":-330,"elapsed":347,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}},"outputId":"29598367-094b-4ad8-daa0-dea628d659e5"},"execution_count":3,"outputs":[{"output_type":"stream","name":"stdout","text":["Number of training images:  50000\n","Number of testing images :  10000\n","Size of each image  :  32 x 32 x 3\n"]}]},{"cell_type":"code","source":["X_train = np.reshape(X_train ,(X_train.shape[0],-1))\n","X_test = np.reshape(X_test,(X_test.shape[0],-1))\n","print(X_train.shape[0])"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"L805xWySgUtW","executionInfo":{"status":"ok","timestamp":1678871826447,"user_tz":-330,"elapsed":6,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}},"outputId":"2544adae-a36c-42e3-dd75-a345526f5ea2"},"execution_count":9,"outputs":[{"output_type":"stream","name":"stdout","text":["50000\n"]}]},{"cell_type":"code","source":["from sklearn.neighbors import KNeighborsClassifier\n","from sklearn import metrics\n","#Train Model and predict\n","k = 4\n","neigh = KNeighborsClassifier(n_neighbors = k).fit(X_train,y_train.ravel())\n"],"metadata":{"id":"WxwYC-68hQY5","executionInfo":{"status":"ok","timestamp":1678872036721,"user_tz":-330,"elapsed":5,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}}},"execution_count":11,"outputs":[]},{"cell_type":"code","source":["y_test_pred = neigh.predict(X_test)\n","num_correct = np.sum(y_test_pred == y_test.ravel())\n","accuracy = float(num_correct)/ len(X_test)\n","print('Got %d / %d correct => accuracy : %f' %(num_correct,len(X_test),accuracy))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"3BAbmNPOjxuq","executionInfo":{"status":"ok","timestamp":1678872710355,"user_tz":-330,"elapsed":112196,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}},"outputId":"73c3d5cf-ff8c-4391-ecee-36f96ab6a89b"},"execution_count":13,"outputs":[{"output_type":"stream","name":"stdout","text":["Got 3398 / 10000 correct => accuracy : 0.339800\n"]}]},{"cell_type":"code","source":["from sklearn.metrics import confusion_matrix\n","cm = confusion_matrix(y_test_pred,y_test.ravel())\n","print(cm)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"dLO5ZnOjm1cj","executionInfo":{"status":"ok","timestamp":1678875902803,"user_tz":-330,"elapsed":6,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}},"outputId":"ca079585-14c4-4181-de7e-0abf8fce2690"},"execution_count":16,"outputs":[{"output_type":"stream","name":"stdout","text":["[[563 165 125  92  90  81  31 112 168 191]\n"," [ 10 229   6   9   3   4   4  10  18  73]\n"," [106 119 453 235 270 225 290 187  49 121]\n"," [ 15  42  50 220  39 163  67  55  33  66]\n"," [ 50 135 211 167 462 159 283 259  51  96]\n"," [  5  31  35 107  22 221  32  48  16  21]\n"," [ 30  52  59 102  51  82 260  54  10  45]\n"," [  5   9   7  21  12  14   3 216   5  19]\n"," [210 199  51  41  50  47  28  55 643 237]\n"," [  6  19   3   6   1   4   2   4   7 131]]\n"]}]},{"cell_type":"code","source":["from sklearn.metrics import classification_report\n","print(classification_report(y_test_pred,y_test.ravel()))\n","\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"IpWANG3azFIk","executionInfo":{"status":"ok","timestamp":1678876003753,"user_tz":-330,"elapsed":5,"user":{"displayName":"Malleshwar Reddy Lingala","userId":"12557761592982653339"}},"outputId":"27ab4074-3184-4834-ac8d-b0d38885e3c8"},"execution_count":17,"outputs":[{"output_type":"stream","name":"stdout","text":["              precision    recall  f1-score   support\n","\n","           0       0.56      0.35      0.43      1618\n","           1       0.23      0.63      0.34       366\n","           2       0.45      0.22      0.30      2055\n","           3       0.22      0.29      0.25       750\n","           4       0.46      0.25      0.32      1873\n","           5       0.22      0.41      0.29       538\n","           6       0.26      0.35      0.30       745\n","           7       0.22      0.69      0.33       311\n","           8       0.64      0.41      0.50      1561\n","           9       0.13      0.72      0.22       183\n","\n","    accuracy                           0.34     10000\n","   macro avg       0.34      0.43      0.33     10000\n","weighted avg       0.44      0.34      0.35     10000\n","\n"]}]}]}