{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\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\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":"2024-06-14T20:38:03.891674Z","iopub.execute_input":"2024-06-14T20:38:03.892069Z","iopub.status.idle":"2024-06-14T20:38:03.902119Z","shell.execute_reply.started":"2024-06-14T20:38:03.892039Z","shell.execute_reply":"2024-06-14T20:38:03.900878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:03.908888Z","iopub.execute_input":"2024-06-14T20:38:03.909295Z","iopub.status.idle":"2024-06-14T20:38:06.740685Z","shell.execute_reply.started":"2024-06-14T20:38:03.909265Z","shell.execute_reply":"2024-06-14T20:38:06.739430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:06.743057Z","iopub.execute_input":"2024-06-14T20:38:06.743504Z","iopub.status.idle":"2024-06-14T20:38:06.751599Z","shell.execute_reply.started":"2024-06-14T20:38:06.743464Z","shell.execute_reply":"2024-06-14T20:38:06.750407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df[['label']]","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:06.753332Z","iopub.execute_input":"2024-06-14T20:38:06.753782Z","iopub.status.idle":"2024-06-14T20:38:06.764338Z","shell.execute_reply.started":"2024-06-14T20:38:06.753740Z","shell.execute_reply":"2024-06-14T20:38:06.762766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train , X_test , y_train , y_test = train_test_split(X,y,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:06.766793Z","iopub.execute_input":"2024-06-14T20:38:06.767163Z","iopub.status.idle":"2024-06-14T20:38:07.003195Z","shell.execute_reply.started":"2024-06-14T20:38:06.767127Z","shell.execute_reply":"2024-06-14T20:38:07.002260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nsvm_clf = SVC(random_state=42)\n\nsvm_clf.fit(X_train[:2000], y_train[:2000])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:07.004481Z","iopub.execute_input":"2024-06-14T20:38:07.004865Z","iopub.status.idle":"2024-06-14T20:38:07.760523Z","shell.execute_reply.started":"2024-06-14T20:38:07.004833Z","shell.execute_reply":"2024-06-14T20:38:07.759406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = df.iloc[:,1:]\n\nsome_digit = images.iloc[[10]]\nsvm_clf.predict(some_digit)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:07.761851Z","iopub.execute_input":"2024-06-14T20:38:07.762205Z","iopub.status.idle":"2024-06-14T20:38:07.783445Z","shell.execute_reply.started":"2024-06-14T20:38:07.762175Z","shell.execute_reply":"2024-06-14T20:38:07.782273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"some_digit_scores = svm_clf.decision_function(some_digit)\nsome_digit_scores.round(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:07.785239Z","iopub.execute_input":"2024-06-14T20:38:07.786392Z","iopub.status.idle":"2024-06-14T20:38:07.809925Z","shell.execute_reply.started":"2024-06-14T20:38:07.786348Z","shell.execute_reply":"2024-06-14T20:38:07.808787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_id = some_digit_scores.argmax()\nclass_id","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:07.811311Z","iopub.execute_input":"2024-06-14T20:38:07.811737Z","iopub.status.idle":"2024-06-14T20:38:07.819788Z","shell.execute_reply.started":"2024-06-14T20:38:07.811698Z","shell.execute_reply":"2024-06-14T20:38:07.818605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.multiclass import OneVsRestClassifier\novr_clf = OneVsRestClassifier(SVC(random_state=42))\novr_clf.fit(X_train[:3000], y_train[:3000])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:07.821185Z","iopub.execute_input":"2024-06-14T20:38:07.821656Z","iopub.status.idle":"2024-06-14T20:38:15.060788Z","shell.execute_reply.started":"2024-06-14T20:38:07.821618Z","shell.execute_reply":"2024-06-14T20:38:15.059123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.model_selection import cross_val_score \n\n# cross_val_score(svm_clf, X_train, y_train, cv=3,\n# scoring=\"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:15.064117Z","iopub.execute_input":"2024-06-14T20:38:15.064573Z","iopub.status.idle":"2024-06-14T20:38:15.069792Z","shell.execute_reply.started":"2024-06-14T20:38:15.064524Z","shell.execute_reply":"2024-06-14T20:38:15.068645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.preprocessing import StandardScaler\n    \n# scaler = StandardScaler()\n# X_train_scaled = scaler.fit_transform(X_train.astype(\"float64\"))\n# cross_val_score(svm_clf, X_train_scaled, y_train, cv=3,\n# scoring=\"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:15.071031Z","iopub.execute_input":"2024-06-14T20:38:15.071715Z","iopub.status.idle":"2024-06-14T20:38:15.084366Z","shell.execute_reply.started":"2024-06-14T20:38:15.071675Z","shell.execute_reply":"2024-06-14T20:38:15.083009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.metrics import ConfusionMatrixDisplay\n# from sklearn.model_selection import cross_val_predict \n\n# y_pred = cross_val_predict(svm_clf, X_test,y_test, cv=3)\n# ConfusionMatrixDisplay.from_predictions(y_test, y_pred)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:15.085734Z","iopub.execute_input":"2024-06-14T20:38:15.086204Z","iopub.status.idle":"2024-06-14T20:38:15.102398Z","shell.execute_reply.started":"2024-06-14T20:38:15.086167Z","shell.execute_reply":"2024-06-14T20:38:15.101145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ConfusionMatrixDisplay.from_predictions(y_test, y_pred,\n#  normalize=\"true\",\n# values_format=\".0%\")\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:15.103879Z","iopub.execute_input":"2024-06-14T20:38:15.104392Z","iopub.status.idle":"2024-06-14T20:38:15.114483Z","shell.execute_reply.started":"2024-06-14T20:38:15.104349Z","shell.execute_reply":"2024-06-14T20:38:15.113550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ovr_clf.predict(some_digit)\n# len(ovr_clf.estimators_)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:15.116061Z","iopub.execute_input":"2024-06-14T20:38:15.116481Z","iopub.status.idle":"2024-06-14T20:38:15.126254Z","shell.execute_reply.started":"2024-06-14T20:38:15.116443Z","shell.execute_reply":"2024-06-14T20:38:15.125172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n\n# def plot_digit(image_data):\n#  image = image_data.values.reshape(28, 28)\n#  plt.imshow(image, cmap=\"binary\")\n#  plt.axis(\"off\")\n# some_digit = images.iloc[[10]]\n# plot_digit(some_digit)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:15.127665Z","iopub.execute_input":"2024-06-14T20:38:15.128367Z","iopub.status.idle":"2024-06-14T20:38:15.138507Z","shell.execute_reply.started":"2024-06-14T20:38:15.128335Z","shell.execute_reply":"2024-06-14T20:38:15.137409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:15.140187Z","iopub.execute_input":"2024-06-14T20:38:15.140589Z","iopub.status.idle":"2024-06-14T20:38:16.619994Z","shell.execute_reply.started":"2024-06-14T20:38:15.140549Z","shell.execute_reply":"2024-06-14T20:38:16.618788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_sub = svm_clf.predict(test)\ny_sub","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:16.621435Z","iopub.execute_input":"2024-06-14T20:38:16.621870Z","iopub.status.idle":"2024-06-14T20:38:44.975883Z","shell.execute_reply.started":"2024-06-14T20:38:16.621832Z","shell.execute_reply":"2024-06-14T20:38:44.974773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_sub)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:42:04.414016Z","iopub.execute_input":"2024-06-14T20:42:04.414429Z","iopub.status.idle":"2024-06-14T20:42:04.423558Z","shell.execute_reply.started":"2024-06-14T20:42:04.414397Z","shell.execute_reply":"2024-06-14T20:42:04.422079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'imageId':range(1, len(y_sub) + 1),'label':y_sub})\n# submission.index = range(1, len(submission) + 1)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:42:11.505432Z","iopub.execute_input":"2024-06-14T20:42:11.505861Z","iopub.status.idle":"2024-06-14T20:42:11.520038Z","shell.execute_reply.started":"2024-06-14T20:42:11.505827Z","shell.execute_reply":"2024-06-14T20:42:11.518206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:38:44.992250Z","iopub.execute_input":"2024-06-14T20:38:44.992601Z","iopub.status.idle":"2024-06-14T20:38:45.026458Z","shell.execute_reply.started":"2024-06-14T20:38:44.992570Z","shell.execute_reply":"2024-06-14T20:38:45.025319Z"},"trusted":true},"execution_count":null,"outputs":[]}]}