{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport matplotlib as mpl\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-10T11:19:18.668513Z","iopub.execute_input":"2021-07-10T11:19:18.668925Z","iopub.status.idle":"2021-07-10T11:19:18.676961Z","shell.execute_reply.started":"2021-07-10T11:19:18.668891Z","shell.execute_reply":"2021-07-10T11:19:18.675449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labeled_img = pd.read_csv('../input/digit-recognizer/train.csv')\nimgs = labeled_img.iloc[0:5000, 1:]\nlabels = labeled_img.iloc[0:5000, :1]\nX_train, X_test, y_train, y_test= train_test_split(imgs, labels, train_size=0.8, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:19.417005Z","iopub.execute_input":"2021-07-10T11:19:19.417381Z","iopub.status.idle":"2021-07-10T11:19:23.314817Z","shell.execute_reply.started":"2021-07-10T11:19:19.417350Z","shell.execute_reply":"2021-07-10T11:19:23.314065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[X_train.shape, X_test.shape, y_train.shape, y_test.shape]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:23.316017Z","iopub.execute_input":"2021-07-10T11:19:23.316432Z","iopub.status.idle":"2021-07-10T11:19:23.324573Z","shell.execute_reply.started":"2021-07-10T11:19:23.316395Z","shell.execute_reply":"2021-07-10T11:19:23.323674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=4\nimg = X_train.iloc[i].to_numpy().reshape(28,28)\nplt.imshow(img, cmap =\"gray\")\nplt.title(y_train.iloc[i, 0])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:24.590052Z","iopub.execute_input":"2021-07-10T11:19:24.590569Z","iopub.status.idle":"2021-07-10T11:19:24.810362Z","shell.execute_reply.started":"2021-07-10T11:19:24.590527Z","shell.execute_reply":"2021-07-10T11:19:24.808903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(X_train.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:28.917206Z","iopub.execute_input":"2021-07-10T11:19:28.917870Z","iopub.status.idle":"2021-07-10T11:19:29.128154Z","shell.execute_reply.started":"2021-07-10T11:19:28.917821Z","shell.execute_reply":"2021-07-10T11:19:29.126816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[X_train>0]=1\nX_test[X_test>0]=1","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:32.669581Z","iopub.execute_input":"2021-07-10T11:19:32.669985Z","iopub.status.idle":"2021-07-10T11:19:32.770763Z","shell.execute_reply.started":"2021-07-10T11:19:32.669951Z","shell.execute_reply":"2021-07-10T11:19:32.769697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=4\nimg = X_train.iloc[i].to_numpy().reshape(28,28)\nplt.imshow(img, cmap =\"gray\")\nplt.title(y_train.iloc[i, 0])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:36.412615Z","iopub.execute_input":"2021-07-10T11:19:36.413335Z","iopub.status.idle":"2021-07-10T11:19:36.568602Z","shell.execute_reply.started":"2021-07-10T11:19:36.413252Z","shell.execute_reply":"2021-07-10T11:19:36.567890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#clf = svm.SVC()\n# clf.fit(X_train, y_train.values.ravel())\n# clf.score(X_test, y_test) ","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:11:07.434005Z","iopub.execute_input":"2021-07-10T11:11:07.434393Z","iopub.status.idle":"2021-07-10T11:11:17.753267Z","shell.execute_reply.started":"2021-07-10T11:11:07.434351Z","shell.execute_reply":"2021-07-10T11:11:17.752569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = labeled_img.iloc[:, 1:]\nlabels = labeled_img.iloc[:, :1]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:41.160247Z","iopub.execute_input":"2021-07-10T11:19:41.160696Z","iopub.status.idle":"2021-07-10T11:19:41.167200Z","shell.execute_reply.started":"2021-07-10T11:19:41.160661Z","shell.execute_reply":"2021-07-10T11:19:41.165618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs[imgs>0]=1\nimgs.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:19:44.519327Z","iopub.execute_input":"2021-07-10T11:19:44.519864Z","iopub.status.idle":"2021-07-10T11:19:45.340297Z","shell.execute_reply.started":"2021-07-10T11:19:44.519814Z","shell.execute_reply":"2021-07-10T11:19:45.339181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = svm.SVC()\nclf.fit(imgs, labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:20:30.882492Z","iopub.execute_input":"2021-07-10T11:20:30.882901Z","iopub.status.idle":"2021-07-10T11:26:06.495172Z","shell.execute_reply.started":"2021-07-10T11:20:30.882869Z","shell.execute_reply":"2021-07-10T11:26:06.494259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=pd.read_csv('../input/digit-recognizer/test.csv')\ntest_data[test_data>0]=1\nresults=clf.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:27:40.287415Z","iopub.execute_input":"2021-07-10T11:27:40.287752Z","iopub.status.idle":"2021-07-10T11:34:10.656482Z","shell.execute_reply.started":"2021-07-10T11:27:40.287723Z","shell.execute_reply":"2021-07-10T11:34:10.655578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(results)\ndf.index.name='ImageId'\ndf.index+=1\ndf.columns=['Label']\ndf.to_csv('./didit_results.csv', header=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T11:34:16.747702Z","iopub.execute_input":"2021-07-10T11:34:16.748040Z","iopub.status.idle":"2021-07-10T11:34:16.820717Z","shell.execute_reply.started":"2021-07-10T11:34:16.748011Z","shell.execute_reply":"2021-07-10T11:34:16.819459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}