{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%%time\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-13T11:23:08.248868Z","iopub.execute_input":"2022-01-13T11:23:08.249610Z","iopub.status.idle":"2022-01-13T11:23:08.256814Z","shell.execute_reply.started":"2022-01-13T11:23:08.249557Z","shell.execute_reply":"2022-01-13T11:23:08.256098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\nlabels = train['label']\ntrain.drop(['label'],axis=1, inplace=True)\n\ntrain = train.to_numpy(dtype = np.float32)\ntest = test.to_numpy(dtype = np.float32)\nlabels = labels.to_numpy(dtype = np.float32)#.reshape(-1,1)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:05:29.128619Z","iopub.execute_input":"2022-01-13T11:05:29.128991Z","iopub.status.idle":"2022-01-13T11:05:35.107294Z","shell.execute_reply.started":"2022-01-13T11:05:29.128960Z","shell.execute_reply":"2022-01-13T11:05:35.106355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n#Since we only have labels for train. I am spliting the train data to test ability of KNN\nX = train.copy()\ny = labels.copy()\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1, stratify=y)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:05:35.108764Z","iopub.execute_input":"2022-01-13T11:05:35.109106Z","iopub.status.idle":"2022-01-13T11:05:36.407849Z","shell.execute_reply.started":"2022-01-13T11:05:35.109063Z","shell.execute_reply":"2022-01-13T11:05:36.406784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import cross_val_score\n\n#create KNN model\nknn = KNeighborsClassifier(n_neighbors=3)\n\n#train model with cv of 5 \ncv_scores = cross_val_score(knn, X_train, y_train, cv=3, n_jobs=-1, verbose=1)\n\n#print each cv score (accuracy) and average them\nprint(cv_scores)\nprint(\"cross val scores mean:{}\".format(np.mean(cv_scores)))","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:05:36.410723Z","iopub.execute_input":"2022-01-13T11:05:36.411082Z","iopub.status.idle":"2022-01-13T11:14:12.584591Z","shell.execute_reply.started":"2022-01-13T11:05:36.411033Z","shell.execute_reply":"2022-01-13T11:14:12.583588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fit the model\nclassifier = knn.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:19:03.218998Z","iopub.execute_input":"2022-01-13T11:19:03.219339Z","iopub.status.idle":"2022-01-13T11:19:09.680125Z","shell.execute_reply.started":"2022-01-13T11:19:03.219305Z","shell.execute_reply":"2022-01-13T11:19:09.679269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef draw_test(name, pred, input_im):\n    BLACK = [0,0,0]\n    expanded_image = cv2.copyMakeBorder(input_im, 0,0,0, imageL.shape[0], cv2.BORDER_CONSTANT, value=[100,100,100])\n    expanded_image = cv2.cvtColor(expanded_image, cv2.COLOR_GRAY2BGR)\n    cv2.putText(expanded_image, str(pred).strip('[]').strip('.'), (145,79), cv2.FONT_HERSHEY_COMPLEX_SMALL, 4, (0,255,0), 2)\n    plt.imshow(expanded_image)\n    plt.show()\n    \nfor i in range(0,10):\n    rand = np.random.randint(0,len(X_test))\n    input_im = X_test[rand].reshape(28,28)\n    \n    imageL = cv2.resize(input_im, None, fx=4, fy=4, interpolation = cv2.INTER_CUBIC)\n    \n    res = knn.predict(input_im.reshape(1,-1))\n    draw_test(\"Predictions\", res, imageL)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:38:24.978887Z","iopub.execute_input":"2022-01-13T11:38:24.979257Z","iopub.status.idle":"2022-01-13T11:38:27.948875Z","shell.execute_reply.started":"2022-01-13T11:38:24.979220Z","shell.execute_reply":"2022-01-13T11:38:27.947924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}