{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30527,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport joblib","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:55.307355Z","iopub.execute_input":"2023-11-17T23:46:55.307753Z","iopub.status.idle":"2023-11-17T23:46:55.415711Z","shell.execute_reply.started":"2023-11-17T23:46:55.307723Z","shell.execute_reply":"2023-11-17T23:46:55.414783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1-Data","metadata":{}},{"cell_type":"code","source":"data_train_path='../input/digit-recognizer/train.csv'\ndata=pd.read_csv(data_train_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:55.417885Z","iopub.execute_input":"2023-11-17T23:46:55.418571Z","iopub.status.idle":"2023-11-17T23:46:59.748316Z","shell.execute_reply.started":"2023-11-17T23:46:55.418521Z","shell.execute_reply":"2023-11-17T23:46:59.747141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:59.749553Z","iopub.execute_input":"2023-11-17T23:46:59.749955Z","iopub.status.idle":"2023-11-17T23:46:59.785162Z","shell.execute_reply.started":"2023-11-17T23:46:59.749916Z","shell.execute_reply":"2023-11-17T23:46:59.784305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:59.787444Z","iopub.execute_input":"2023-11-17T23:46:59.788557Z","iopub.status.idle":"2023-11-17T23:46:59.795883Z","shell.execute_reply.started":"2023-11-17T23:46:59.788515Z","shell.execute_reply":"2023-11-17T23:46:59.794801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset=data.copy()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:59.797576Z","iopub.execute_input":"2023-11-17T23:46:59.798362Z","iopub.status.idle":"2023-11-17T23:46:59.928396Z","shell.execute_reply.started":"2023-11-17T23:46:59.798322Z","shell.execute_reply":"2023-11-17T23:46:59.927429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:59.930225Z","iopub.execute_input":"2023-11-17T23:46:59.931113Z","iopub.status.idle":"2023-11-17T23:46:59.951909Z","shell.execute_reply.started":"2023-11-17T23:46:59.931073Z","shell.execute_reply":"2023-11-17T23:46:59.950552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=dataset.pop('label')","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:59.954403Z","iopub.execute_input":"2023-11-17T23:46:59.954935Z","iopub.status.idle":"2023-11-17T23:46:59.967203Z","shell.execute_reply.started":"2023-11-17T23:46:59.954896Z","shell.execute_reply":"2023-11-17T23:46:59.966076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:59.968904Z","iopub.execute_input":"2023-11-17T23:46:59.969334Z","iopub.status.idle":"2023-11-17T23:46:59.979579Z","shell.execute_reply.started":"2023-11-17T23:46:59.969294Z","shell.execute_reply":"2023-11-17T23:46:59.978418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:46:59.981114Z","iopub.execute_input":"2023-11-17T23:46:59.981611Z","iopub.status.idle":"2023-11-17T23:47:01.285910Z","shell.execute_reply.started":"2023-11-17T23:46:59.981575Z","shell.execute_reply":"2023-11-17T23:47:01.284711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(dataset,y,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:01.291702Z","iopub.execute_input":"2023-11-17T23:47:01.292045Z","iopub.status.idle":"2023-11-17T23:47:01.773814Z","shell.execute_reply.started":"2023-11-17T23:47:01.292015Z","shell.execute_reply":"2023-11-17T23:47:01.772531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:01.775584Z","iopub.execute_input":"2023-11-17T23:47:01.776211Z","iopub.status.idle":"2023-11-17T23:47:01.785293Z","shell.execute_reply.started":"2023-11-17T23:47:01.776169Z","shell.execute_reply":"2023-11-17T23:47:01.784112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:01.786628Z","iopub.execute_input":"2023-11-17T23:47:01.787683Z","iopub.status.idle":"2023-11-17T23:47:01.793512Z","shell.execute_reply.started":"2023-11-17T23:47:01.787639Z","shell.execute_reply":"2023-11-17T23:47:01.792488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler=MinMaxScaler()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:01.795085Z","iopub.execute_input":"2023-11-17T23:47:01.795405Z","iopub.status.idle":"2023-11-17T23:47:01.805272Z","shell.execute_reply.started":"2023-11-17T23:47:01.795378Z","shell.execute_reply":"2023-11-17T23:47:01.804138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_scaled=scaler.fit_transform(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:01.807003Z","iopub.execute_input":"2023-11-17T23:47:01.807398Z","iopub.status.idle":"2023-11-17T23:47:02.100675Z","shell.execute_reply.started":"2023-11-17T23:47:01.807368Z","shell.execute_reply":"2023-11-17T23:47:02.099213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_scaled=scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:02.102540Z","iopub.execute_input":"2023-11-17T23:47:02.102980Z","iopub.status.idle":"2023-11-17T23:47:02.154332Z","shell.execute_reply.started":"2023-11-17T23:47:02.102937Z","shell.execute_reply":"2023-11-17T23:47:02.153090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-Model ","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:02.156213Z","iopub.execute_input":"2023-11-17T23:47:02.156688Z","iopub.status.idle":"2023-11-17T23:47:02.554689Z","shell.execute_reply.started":"2023-11-17T23:47:02.156647Z","shell.execute_reply":"2023-11-17T23:47:02.553685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models=[KNeighborsClassifier(n_neighbors=6),RandomForestClassifier(),SVC()]","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:02.556049Z","iopub.execute_input":"2023-11-17T23:47:02.556498Z","iopub.status.idle":"2023-11-17T23:47:02.562476Z","shell.execute_reply.started":"2023-11-17T23:47:02.556466Z","shell.execute_reply":"2023-11-17T23:47:02.560628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for model in models:\n    model.fit(X_scaled,y_train)\n    y_pred=model.predict(X_test_scaled)\n    print(accuracy_score(y_pred,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:47:02.563994Z","iopub.execute_input":"2023-11-17T23:47:02.564425Z","iopub.status.idle":"2023-11-17T23:50:24.669442Z","shell.execute_reply.started":"2023-11-17T23:47:02.564395Z","shell.execute_reply":"2023-11-17T23:50:24.668135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_pred,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:24.671219Z","iopub.execute_input":"2023-11-17T23:50:24.672054Z","iopub.status.idle":"2023-11-17T23:50:24.682664Z","shell.execute_reply.started":"2023-11-17T23:50:24.672001Z","shell.execute_reply":"2023-11-17T23:50:24.681510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3-Test ","metadata":{}},{"cell_type":"code","source":"data_test_path='../input/digit-recognizer/test.csv'\ntestset=pd.read_csv(data_test_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:24.683890Z","iopub.execute_input":"2023-11-17T23:50:24.684679Z","iopub.status.idle":"2023-11-17T23:50:27.224659Z","shell.execute_reply.started":"2023-11-17T23:50:24.684647Z","shell.execute_reply":"2023-11-17T23:50:27.223559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset=scaler.transform(testset)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.228957Z","iopub.execute_input":"2023-11-17T23:50:27.229306Z","iopub.status.idle":"2023-11-17T23:50:27.381473Z","shell.execute_reply.started":"2023-11-17T23:50:27.229274Z","shell.execute_reply":"2023-11-17T23:50:27.380183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=np.array(testset)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.382979Z","iopub.execute_input":"2023-11-17T23:50:27.383435Z","iopub.status.idle":"2023-11-17T23:50:27.455589Z","shell.execute_reply.started":"2023-11-17T23:50:27.383393Z","shell.execute_reply":"2023-11-17T23:50:27.454442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=test[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.457095Z","iopub.execute_input":"2023-11-17T23:50:27.457443Z","iopub.status.idle":"2023-11-17T23:50:27.462893Z","shell.execute_reply.started":"2023-11-17T23:50:27.457414Z","shell.execute_reply":"2023-11-17T23:50:27.461593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c=a.reshape((1,len(a)))","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.464693Z","iopub.execute_input":"2023-11-17T23:50:27.465019Z","iopub.status.idle":"2023-11-17T23:50:27.472606Z","shell.execute_reply.started":"2023-11-17T23:50:27.464971Z","shell.execute_reply":"2023-11-17T23:50:27.471515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.473700Z","iopub.execute_input":"2023-11-17T23:50:27.473992Z","iopub.status.idle":"2023-11-17T23:50:27.485557Z","shell.execute_reply.started":"2023-11-17T23:50:27.473966Z","shell.execute_reply":"2023-11-17T23:50:27.484422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c=scaler.transform(c)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.487585Z","iopub.execute_input":"2023-11-17T23:50:27.487957Z","iopub.status.idle":"2023-11-17T23:50:27.497941Z","shell.execute_reply.started":"2023-11-17T23:50:27.487927Z","shell.execute_reply":"2023-11-17T23:50:27.496333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"b=c.reshape((28,-1))","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.499228Z","iopub.execute_input":"2023-11-17T23:50:27.499608Z","iopub.status.idle":"2023-11-17T23:50:27.505207Z","shell.execute_reply.started":"2023-11-17T23:50:27.499559Z","shell.execute_reply":"2023-11-17T23:50:27.504114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.512664Z","iopub.execute_input":"2023-11-17T23:50:27.513215Z","iopub.status.idle":"2023-11-17T23:50:27.520875Z","shell.execute_reply.started":"2023-11-17T23:50:27.513182Z","shell.execute_reply":"2023-11-17T23:50:27.519615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(b)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.522424Z","iopub.execute_input":"2023-11-17T23:50:27.522756Z","iopub.status.idle":"2023-11-17T23:50:27.832616Z","shell.execute_reply.started":"2023-11-17T23:50:27.522726Z","shell.execute_reply":"2023-11-17T23:50:27.831608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(c)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.833805Z","iopub.execute_input":"2023-11-17T23:50:27.834140Z","iopub.status.idle":"2023-11-17T23:50:27.848361Z","shell.execute_reply.started":"2023-11-17T23:50:27.834111Z","shell.execute_reply":"2023-11-17T23:50:27.847518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output=model.predict(testset)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:50:27.849557Z","iopub.execute_input":"2023-11-17T23:50:27.850135Z","iopub.status.idle":"2023-11-17T23:53:46.210605Z","shell.execute_reply.started":"2023-11-17T23:50:27.850102Z","shell.execute_reply":"2023-11-17T23:53:46.209400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-Submission","metadata":{}},{"cell_type":"code","source":"ImageId=np.array([i for i in range(1,len(output)+1)])","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:53:46.211852Z","iopub.execute_input":"2023-11-17T23:53:46.212184Z","iopub.status.idle":"2023-11-17T23:53:46.223439Z","shell.execute_reply.started":"2023-11-17T23:53:46.212155Z","shell.execute_reply":"2023-11-17T23:53:46.222144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame({'ImageId':ImageId,'Label':output})","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:53:46.225122Z","iopub.execute_input":"2023-11-17T23:53:46.225579Z","iopub.status.idle":"2023-11-17T23:53:46.237582Z","shell.execute_reply.started":"2023-11-17T23:53:46.225536Z","shell.execute_reply":"2023-11-17T23:53:46.236628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:53:46.239153Z","iopub.execute_input":"2023-11-17T23:53:46.239681Z","iopub.status.idle":"2023-11-17T23:53:46.316118Z","shell.execute_reply.started":"2023-11-17T23:53:46.239640Z","shell.execute_reply":"2023-11-17T23:53:46.314931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_path='/kaggle/working/submission.csv'\npd.read_csv(submission_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:53:46.317874Z","iopub.execute_input":"2023-11-17T23:53:46.318321Z","iopub.status.idle":"2023-11-17T23:53:46.338320Z","shell.execute_reply.started":"2023-11-17T23:53:46.318278Z","shell.execute_reply":"2023-11-17T23:53:46.337523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"y_pred=model.predict(X_test_scaled)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T11:19:21.311961Z","iopub.execute_input":"2023-11-17T11:19:21.312373Z"}}},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:53:46.339540Z","iopub.execute_input":"2023-11-17T23:53:46.339843Z","iopub.status.idle":"2023-11-17T23:53:46.354012Z","shell.execute_reply.started":"2023-11-17T23:53:46.339817Z","shell.execute_reply":"2023-11-17T23:53:46.352569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"joblib.dump(model, 'svm_digit_recognizer_model.pkl')\njoblib.dump(scaler, 'scaler.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-11-17T23:53:46.355583Z","iopub.execute_input":"2023-11-17T23:53:46.355989Z","iopub.status.idle":"2023-11-17T23:53:46.424734Z","shell.execute_reply.started":"2023-11-17T23:53:46.355950Z","shell.execute_reply":"2023-11-17T23:53:46.423715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}