{"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":30698,"isInternetEnabled":false,"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-05-30T16:14:12.080909Z","iopub.execute_input":"2024-05-30T16:14:12.081400Z","iopub.status.idle":"2024-05-30T16:14:13.560039Z","shell.execute_reply.started":"2024-05-30T16:14:12.081364Z","shell.execute_reply":"2024-05-30T16:14:13.558888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score, confusion_matrix\nfrom sklearn.multiclass import OneVsOneClassifier\nfrom sklearn.linear_model import Perceptron\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:24:58.755552Z","iopub.execute_input":"2024-05-30T16:24:58.756044Z","iopub.status.idle":"2024-05-30T16:24:58.765575Z","shell.execute_reply.started":"2024-05-30T16:24:58.756008Z","shell.execute_reply":"2024-05-30T16:24:58.763951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:14:33.970312Z","iopub.execute_input":"2024-05-30T16:14:33.970765Z","iopub.status.idle":"2024-05-30T16:14:40.633874Z","shell.execute_reply.started":"2024-05-30T16:14:33.970722Z","shell.execute_reply":"2024-05-30T16:14:40.632415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:14:51.701362Z","iopub.execute_input":"2024-05-30T16:14:51.701799Z","iopub.status.idle":"2024-05-30T16:14:51.745132Z","shell.execute_reply.started":"2024-05-30T16:14:51.701765Z","shell.execute_reply":"2024-05-30T16:14:51.743891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Visualization","metadata":{}},{"cell_type":"code","source":"def visualize_data(data, labels):\n    plt.figure(figsize=(10, 10))\n    for i in range(16):\n        plt.subplot(4, 4, i + 1)\n        plt.imshow(data.iloc[i].values.reshape(28, 28), cmap='gray')\n        plt.title(f\"Label: {labels.iloc[i]}\")\n        plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:15:39.962061Z","iopub.execute_input":"2024-05-30T16:15:39.962539Z","iopub.status.idle":"2024-05-30T16:15:39.972429Z","shell.execute_reply.started":"2024-05-30T16:15:39.962504Z","shell.execute_reply":"2024-05-30T16:15:39.971045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_data(train_df.drop(columns=['label']), train_df['label'])","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:15:49.744508Z","iopub.execute_input":"2024-05-30T16:15:49.744984Z","iopub.status.idle":"2024-05-30T16:15:51.405187Z","shell.execute_reply.started":"2024-05-30T16:15:49.744949Z","shell.execute_reply":"2024-05-30T16:15:51.403889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(columns=['label'])\ny = train_df['label']","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:16:02.606124Z","iopub.execute_input":"2024-05-30T16:16:02.606525Z","iopub.status.idle":"2024-05-30T16:16:02.702737Z","shell.execute_reply.started":"2024-05-30T16:16:02.606494Z","shell.execute_reply":"2024-05-30T16:16:02.701073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:16:09.598015Z","iopub.execute_input":"2024-05-30T16:16:09.598495Z","iopub.status.idle":"2024-05-30T16:16:10.016862Z","shell.execute_reply.started":"2024-05-30T16:16:09.598454Z","shell.execute_reply":"2024-05-30T16:16:10.015514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Pairwise Linear Classifier","metadata":{}},{"cell_type":"code","source":"X_train = train_df.loc[:, train_df.columns != 'label'].values\ny_train = train_df['label'].values\nX_test = test_df.values","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:23:53.895436Z","iopub.execute_input":"2024-05-30T16:23:53.895950Z","iopub.status.idle":"2024-05-30T16:23:54.047584Z","shell.execute_reply.started":"2024-05-30T16:23:53.895903Z","shell.execute_reply":"2024-05-30T16:23:54.046191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_df.loc[:, train_df.columns != 'label'].values[:35000]\ny_train = train_df['label'].values[:35000]\nX_test = train_df.loc[:, train_df.columns != 'label'].values[35000:]\ny_test = train_df['label'].values[35000:]","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:24:00.856107Z","iopub.execute_input":"2024-05-30T16:24:00.856600Z","iopub.status.idle":"2024-05-30T16:24:01.052294Z","shell.execute_reply.started":"2024-05-30T16:24:00.856562Z","shell.execute_reply":"2024-05-30T16:24:01.051129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = OneVsOneClassifier(\n    Perceptron()).fit(X_train, y_train)#Perceptron(tol=1e-3, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:25:03.561781Z","iopub.execute_input":"2024-05-30T16:25:03.563155Z","iopub.status.idle":"2024-05-30T16:25:18.712537Z","shell.execute_reply.started":"2024-05-30T16:25:03.563096Z","shell.execute_reply":"2024-05-30T16:25:18.711038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.score(X_test,y_test)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:25:21.285056Z","iopub.execute_input":"2024-05-30T16:25:21.285557Z","iopub.status.idle":"2024-05-30T16:25:25.260068Z","shell.execute_reply.started":"2024-05-30T16:25:21.285519Z","shell.execute_reply":"2024-05-30T16:25:25.258518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = clf.predict(test_df.values)\n\n# Prepare submission DataFrame\nsubmission_df = pd.DataFrame({\n    'ImageId': np.arange(1, len(predictions) + 1),\n    'Label': predictions\n})\n\n# Save the submission file\nsubmission_df.to_csv('/kaggle/working/results.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:25:37.990771Z","iopub.execute_input":"2024-05-30T16:25:37.991299Z","iopub.status.idle":"2024-05-30T16:25:51.365858Z","shell.execute_reply.started":"2024-05-30T16:25:37.991256Z","shell.execute_reply":"2024-05-30T16:25:51.364618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Random Forest Classifier","metadata":{}},{"cell_type":"code","source":"rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)\nrf_classifier.fit(X_train, y_train)  # Note: Keep X_train as DataFrame\ny_val_pred_rf = rf_classifier.predict(X_val)\nval_accuracy_rf = accuracy_score(y_val, y_val_pred_rf)\nprint(f\"Validation Accuracy with Random Forest: {val_accuracy_rf}\")","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:30:05.177796Z","iopub.execute_input":"2024-05-30T16:30:05.178401Z","iopub.status.idle":"2024-05-30T16:30:34.123713Z","shell.execute_reply.started":"2024-05-30T16:30:05.178360Z","shell.execute_reply":"2024-05-30T16:30:34.122496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_matrix_rf = confusion_matrix(y_val, y_val_pred_rf)\nplt.figure(figsize=(10, 8))\nsns.heatmap(conf_matrix_rf, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix - Random Forest\")\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"Actual\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:31:08.181653Z","iopub.execute_input":"2024-05-30T16:31:08.182866Z","iopub.status.idle":"2024-05-30T16:31:08.856693Z","shell.execute_reply.started":"2024-05-30T16:31:08.182800Z","shell.execute_reply":"2024-05-30T16:31:08.855383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_df.values\npredictions = rf_classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:31:19.092633Z","iopub.execute_input":"2024-05-30T16:31:19.093123Z","iopub.status.idle":"2024-05-30T16:31:20.390842Z","shell.execute_reply.started":"2024-05-30T16:31:19.093088Z","shell.execute_reply":"2024-05-30T16:31:20.389447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'ImageId': np.arange(1, len(predictions) + 1),\n    'Label': predictions\n})","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:31:29.641422Z","iopub.execute_input":"2024-05-30T16:31:29.641908Z","iopub.status.idle":"2024-05-30T16:31:29.650107Z","shell.execute_reply.started":"2024-05-30T16:31:29.641872Z","shell.execute_reply":"2024-05-30T16:31:29.648677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('/kaggle/working/results.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T16:31:38.376593Z","iopub.execute_input":"2024-05-30T16:31:38.377100Z","iopub.status.idle":"2024-05-30T16:31:38.424417Z","shell.execute_reply.started":"2024-05-30T16:31:38.377066Z","shell.execute_reply":"2024-05-30T16:31:38.423183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}