{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \nimport warnings\nwarnings.filterwarnings('ignore')\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-29T10:38:11.342113Z","iopub.execute_input":"2023-01-29T10:38:11.342550Z","iopub.status.idle":"2023-01-29T10:38:11.352655Z","shell.execute_reply.started":"2023-01-29T10:38:11.342515Z","shell.execute_reply":"2023-01-29T10:38:11.351339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataframe_train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndataframe_train","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:38:11.354587Z","iopub.execute_input":"2023-01-29T10:38:11.354900Z","iopub.status.idle":"2023-01-29T10:38:13.239562Z","shell.execute_reply.started":"2023-01-29T10:38:11.354872Z","shell.execute_reply":"2023-01-29T10:38:13.238025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X,y=dataframe_train.iloc[:,1:],dataframe_train[\"label\"]\nX","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:38:13.241919Z","iopub.execute_input":"2023-01-29T10:38:13.242383Z","iopub.status.idle":"2023-01-29T10:38:13.266029Z","shell.execute_reply.started":"2023-01-29T10:38:13.242344Z","shell.execute_reply":"2023-01-29T10:38:13.265313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y\nX_train,X_test,y_train,y_test=X[:60000],X[60000:],y[:60000],y[60000:]\n","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:38:13.267041Z","iopub.execute_input":"2023-01-29T10:38:13.267682Z","iopub.status.idle":"2023-01-29T10:38:13.273763Z","shell.execute_reply.started":"2023-01-29T10:38:13.267653Z","shell.execute_reply":"2023-01-29T10:38:13.272491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_5=(y_train==5)\ny_test_5=(y_test==5)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:38:13.276503Z","iopub.execute_input":"2023-01-29T10:38:13.276965Z","iopub.status.idle":"2023-01-29T10:38:13.286580Z","shell.execute_reply.started":"2023-01-29T10:38:13.276918Z","shell.execute_reply":"2023-01-29T10:38:13.285372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import SGDClassifier\nsgd_clf=SGDClassifier(random_state=42)\nsgd_clf.fit(X_train,y_train_5)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:38:13.288599Z","iopub.execute_input":"2023-01-29T10:38:13.289007Z","iopub.status.idle":"2023-01-29T10:38:21.927371Z","shell.execute_reply.started":"2023-01-29T10:38:13.288971Z","shell.execute_reply":"2023-01-29T10:38:21.925449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"some_digit=X.iloc[10].values.reshape((1,-1))\nsgd_clf.predict(some_digit)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:38:21.929360Z","iopub.execute_input":"2023-01-29T10:38:21.929940Z","iopub.status.idle":"2023-01-29T10:38:21.938981Z","shell.execute_reply.started":"2023-01-29T10:38:21.929889Z","shell.execute_reply":"2023-01-29T10:38:21.938132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd_clf.fit(X_train,y_train)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:38:21.940009Z","iopub.execute_input":"2023-01-29T10:38:21.940348Z","iopub.status.idle":"2023-01-29T10:39:55.697961Z","shell.execute_reply.started":"2023-01-29T10:38:21.940314Z","shell.execute_reply":"2023-01-29T10:39:55.696443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd_clf.predict(some_digit)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:39:55.700062Z","iopub.execute_input":"2023-01-29T10:39:55.700501Z","iopub.status.idle":"2023-01-29T10:39:55.708014Z","shell.execute_reply.started":"2023-01-29T10:39:55.700456Z","shell.execute_reply":"2023-01-29T10:39:55.706711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()\nX_train_scaled=scaler.fit_transform(X_train.astype(np.float64))\n","metadata":{"execution":{"iopub.status.busy":"2023-01-29T10:39:55.710276Z","iopub.execute_input":"2023-01-29T10:39:55.710642Z","iopub.status.idle":"2023-01-29T10:39:56.089554Z","shell.execute_reply.started":"2023-01-29T10:39:55.710609Z","shell.execute_reply":"2023-01-29T10:39:56.087638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_predict\nfrom sklearn.metrics import confusion_matrix\ny_train_pred=cross_val_predict(sgd_clf,X_train_scaled,y_train,cv=3)\nconf_mx=confusion_matrix(y_train,y_train_pred)\nconf_mx","metadata":{"execution":{"iopub.status.busy":"2023-01-29T11:02:02.895945Z","iopub.execute_input":"2023-01-29T11:02:02.896389Z","iopub.status.idle":"2023-01-29T11:09:40.325424Z","shell.execute_reply.started":"2023-01-29T11:02:02.896355Z","shell.execute_reply":"2023-01-29T11:09:40.324470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.matshow(conf_mx,cmap=plt.cm.gray)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T11:10:18.610834Z","iopub.execute_input":"2023-01-29T11:10:18.611312Z","iopub.status.idle":"2023-01-29T11:10:18.803226Z","shell.execute_reply.started":"2023-01-29T11:10:18.611271Z","shell.execute_reply":"2023-01-29T11:10:18.802324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row_sums=conf_mx.sum(axis=1,keepdims=True)\nnorm_conf_mx=conf_mx/row_sums\nnp.fill_diagonal(norm_conf_mx,0)\nplt.matshow(norm_conf_mx,cmap=plt.cm.gray)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T11:10:21.561030Z","iopub.execute_input":"2023-01-29T11:10:21.561810Z","iopub.status.idle":"2023-01-29T11:10:21.706046Z","shell.execute_reply.started":"2023-01-29T11:10:21.561766Z","shell.execute_reply":"2023-01-29T11:10:21.705195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn .neighbors import KNeighborsClassifier\nknn_clf=KNeighborsClassifier()\nknn_clf.fit(X_train,y_train)\nclean_digit=knn_clf.predict(some_digit)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-29T11:13:31.188801Z","iopub.execute_input":"2023-01-29T11:13:31.189338Z","iopub.status.idle":"2023-01-29T11:13:31.307815Z","shell.execute_reply.started":"2023-01-29T11:13:31.189282Z","shell.execute_reply":"2023-01-29T11:13:31.306861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean_digit\n","metadata":{"execution":{"iopub.status.busy":"2023-01-29T11:13:45.406583Z","iopub.execute_input":"2023-01-29T11:13:45.407491Z","iopub.status.idle":"2023-01-29T11:13:45.414008Z","shell.execute_reply.started":"2023-01-29T11:13:45.407453Z","shell.execute_reply":"2023-01-29T11:13:45.412442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-01-29T11:16:26.034010Z","iopub.execute_input":"2023-01-29T11:16:26.034622Z","iopub.status.idle":"2023-01-29T11:16:26.041764Z","shell.execute_reply.started":"2023-01-29T11:16:26.034586Z","shell.execute_reply":"2023-01-29T11:16:26.040494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(some_digit.reshape(28,28),cmap=\"binary\")","metadata":{"execution":{"iopub.status.busy":"2023-01-29T11:16:39.639301Z","iopub.execute_input":"2023-01-29T11:16:39.639704Z","iopub.status.idle":"2023-01-29T11:16:39.822509Z","shell.execute_reply.started":"2023-01-29T11:16:39.639663Z","shell.execute_reply":"2023-01-29T11:16:39.821333Z"},"trusted":true},"execution_count":null,"outputs":[]}]}