{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd \ntrain_file = \"../input/digit-recognizer/train.csv\"\nraw_data = pd.read_csv(train_file)\nraw_data","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:48:26.377190Z","iopub.execute_input":"2024-07-30T19:48:26.377913Z","iopub.status.idle":"2024-07-30T19:48:28.813501Z","shell.execute_reply.started":"2024-07-30T19:48:26.377879Z","shell.execute_reply":"2024-07-30T19:48:28.812607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"raw_data['label'].value_counts().sort_values() ","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:48:28.815078Z","iopub.execute_input":"2024-07-30T19:48:28.815364Z","iopub.status.idle":"2024-07-30T19:48:28.830666Z","shell.execute_reply.started":"2024-07-30T19:48:28.815340Z","shell.execute_reply":"2024-07-30T19:48:28.829764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_loss_curves(history):\n    \"\"\"\n    Plots the training and validation loss and accuracy curves from the Keras model history.\n\n    Args:\n    - history: Keras History object from model.fit()\n\n    Returns:\n    - None\n    \"\"\"\n    # Retrieve loss and validation loss from history\n    loss = history.history.get('loss')\n    val_loss = history.history.get('val_loss')\n    \n    # Retrieve accuracy and validation accuracy from history\n    accuracy = history.history.get('accuracy')\n    val_accuracy = history.history.get('val_accuracy')\n\n    # Create figure and axis objects\n    plt.figure(figsize=(14, 6))\n\n    # Plot training and validation loss\n    plt.subplot(1, 2, 1)\n    plt.plot(loss, label='Training Loss', color='blue')\n    if val_loss:\n        plt.plot(val_loss, label='Validation Loss', color='red')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.title('Loss Curves')\n    plt.legend()\n\n    # Plot training and validation accuracy\n    plt.subplot(1, 2, 2)\n    plt.plot(accuracy, label='Training Accuracy', color='green')\n    if val_accuracy:\n        plt.plot(val_accuracy, label='Validation Accuracy', color='orange')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.title('Accuracy Curves')\n    plt.legend()\n\n    # Show plots\n    plt.tight_layout()\n    plt.show()\n\n# Example usage:\n# history = model.fit(x, y, ...)\n# plot_loss_curves(history)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:48:28.831838Z","iopub.execute_input":"2024-07-30T19:48:28.832133Z","iopub.status.idle":"2024-07-30T19:48:28.841448Z","shell.execute_reply.started":"2024-07-30T19:48:28.832109Z","shell.execute_reply":"2024-07-30T19:48:28.840481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout\nfrom tensorflow.keras.utils import to_categorical \nimport warnings \nwarnings.filterwarnings('ignore')\nimg_rows, img_cols = 28, 28\nnum_classes = 10\n\n\ndef data_prep(raw):\n    out_y =to_categorical(raw.label, num_classes)\n\n    num_images = raw.shape[0]\n    x_as_array = raw.values[:,1:]\n    x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)\n    out_x = x_shaped_array / 255\n    return out_x, out_y\n\ntrain_size = 30000\n\nx, y = data_prep(raw_data)\n\nmodel = Sequential()\nmodel.add(Conv2D(30, kernel_size=(3, 3),\n                 strides=2,\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\nmodel.add(Dropout(0.5))\nmodel.add(Conv2D(30, kernel_size=(3, 3), strides=2, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-30T19:48:28.844168Z","iopub.execute_input":"2024-07-30T19:48:28.844786Z","iopub.status.idle":"2024-07-30T19:48:41.396329Z","shell.execute_reply.started":"2024-07-30T19:48:28.844756Z","shell.execute_reply":"2024-07-30T19:48:41.395449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:57:22.429703Z","iopub.execute_input":"2024-07-30T19:57:22.430079Z","iopub.status.idle":"2024-07-30T19:57:22.436010Z","shell.execute_reply.started":"2024-07-30T19:57:22.430051Z","shell.execute_reply":"2024-07-30T19:57:22.435076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience=8, restore_best_weights=True,verbose=1)\n\nhistory=model.fit(x, y,\n          batch_size=128,\n          epochs=50,\n          validation_split = 0.2,callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:57:22.597449Z","iopub.execute_input":"2024-07-30T19:57:22.597697Z","iopub.status.idle":"2024-07-30T19:58:00.373840Z","shell.execute_reply.started":"2024-07-30T19:57:22.597676Z","shell.execute_reply":"2024-07-30T19:58:00.373030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nplot_loss_curves(history)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:58:00.375440Z","iopub.execute_input":"2024-07-30T19:58:00.375741Z","iopub.status.idle":"2024-07-30T19:58:01.063346Z","shell.execute_reply.started":"2024-07-30T19:58:00.375697Z","shell.execute_reply":"2024-07-30T19:58:01.062435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split \nX=raw_data.drop('label',axis=1)\ny_1=raw_data['label']\nX_train,X_test,y_train,y_test=train_test_split(X,y_1,test_size=.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:58:01.064551Z","iopub.execute_input":"2024-07-30T19:58:01.064869Z","iopub.status.idle":"2024-07-30T19:58:01.357278Z","shell.execute_reply.started":"2024-07-30T19:58:01.064841Z","shell.execute_reply":"2024-07-30T19:58:01.356251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import ExtraTreesClassifier \nfrom sklearn.metrics import classification_report \n\nextra_classifier=ExtraTreesClassifier() \nextra_classifier.fit(X_train,y_train)\ny_pred_1=extra_classifier.predict(X_test)\nprint(classification_report(y_test,y_pred_1))","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:58:01.359988Z","iopub.execute_input":"2024-07-30T19:58:01.360319Z","iopub.status.idle":"2024-07-30T19:58:19.580095Z","shell.execute_reply.started":"2024-07-30T19:58:01.360292Z","shell.execute_reply":"2024-07-30T19:58:19.579138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_all_classifiers(X_train, X_test, y_train, y_test):\n    from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier\n    from sklearn.linear_model import LogisticRegression\n    from sklearn.svm import SVC\n    from sklearn.neighbors import KNeighborsClassifier\n    from sklearn.tree import DecisionTreeClassifier\n    from sklearn.naive_bayes import GaussianNB\n    from sklearn.metrics import classification_report\n    \n    classifiers = {\n        \"Extra Trees Classifier\": ExtraTreesClassifier(),\n        \"Random Forest Classifier\": RandomForestClassifier(),\n        \"Logistic Regression\": LogisticRegression(max_iter=1000),\n        \"Support Vector Classifier\": SVC(),\n        \"K-Nearest Neighbors\": KNeighborsClassifier(),\n        \"Decision Tree Classifier\": DecisionTreeClassifier(),\n        \"Gaussian Naive Bayes\": GaussianNB()\n    }\n    \n    for name, clf in classifiers.items():\n        clf.fit(X_train, y_train)\n        y_pred = clf.predict(X_test)\n        print(f\"\\n{name} Classification Report:\\n\")\n        print(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:58:19.581579Z","iopub.execute_input":"2024-07-30T19:58:19.582410Z","iopub.status.idle":"2024-07-30T19:58:19.589740Z","shell.execute_reply.started":"2024-07-30T19:58:19.582371Z","shell.execute_reply":"2024-07-30T19:58:19.588774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_all_classifiers(X_train,X_test,y_train,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T19:58:19.591267Z","iopub.execute_input":"2024-07-30T19:58:19.591587Z","iopub.status.idle":"2024-07-30T20:02:43.899208Z","shell.execute_reply.started":"2024-07-30T19:58:19.591556Z","shell.execute_reply":"2024-07-30T20:02:43.898174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1 = Sequential()\nmodel_1.add(Conv2D(30, kernel_size=(3, 3),\n                 strides=2,\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\nmodel_1.add(Dropout(0.5))\nmodel_1.add(Conv2D(30, kernel_size=(3, 3), strides=2, activation='relu'))\nmodel_1.add(Dropout(0.5))\nmodel_1.add(Flatten())\nmodel_1.add(Dense(128, activation='relu'))\nmodel_1.add(Dense(num_classes, activation='softmax'))\n\nmodel_1.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n\n\nearly_stopping = EarlyStopping(monitor='loss', patience=8, restore_best_weights=True,verbose=1)\n\nhistory_1=model_1.fit(x, y,\n          batch_size=128,\n          epochs=50,\n          callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:02:43.900843Z","iopub.execute_input":"2024-07-30T20:02:43.901224Z","iopub.status.idle":"2024-07-30T20:03:30.938842Z","shell.execute_reply.started":"2024-07-30T20:02:43.901189Z","shell.execute_reply":"2024-07-30T20:03:30.938050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_loss_curves(history_1)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:03:30.940117Z","iopub.execute_input":"2024-07-30T20:03:30.940395Z","iopub.status.idle":"2024-07-30T20:03:31.736888Z","shell.execute_reply.started":"2024-07-30T20:03:30.940371Z","shell.execute_reply":"2024-07-30T20:03:31.735968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\ntest","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:03:31.738041Z","iopub.execute_input":"2024-07-30T20:03:31.738343Z","iopub.status.idle":"2024-07-30T20:03:33.774568Z","shell.execute_reply.started":"2024-07-30T20:03:31.738316Z","shell.execute_reply":"2024-07-30T20:03:33.773580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg_rows,img_cols=(28,28)\nnum_images = test.shape[0]\nx_as_array = test.values[:,:]\nx_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)\nX = x_shaped_array / 255\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:08:33.191644Z","iopub.execute_input":"2024-07-30T20:08:33.192678Z","iopub.status.idle":"2024-07-30T20:08:33.264483Z","shell.execute_reply.started":"2024-07-30T20:08:33.192643Z","shell.execute_reply":"2024-07-30T20:08:33.263636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_submission=model_1.predict(X)\ny_submission","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:09:12.955443Z","iopub.execute_input":"2024-07-30T20:09:12.956093Z","iopub.status.idle":"2024-07-30T20:09:15.365550Z","shell.execute_reply.started":"2024-07-30T20:09:12.956058Z","shell.execute_reply":"2024-07-30T20:09:15.364584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_submission.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:09:32.971477Z","iopub.execute_input":"2024-07-30T20:09:32.972110Z","iopub.status.idle":"2024-07-30T20:09:32.977931Z","shell.execute_reply.started":"2024-07-30T20:09:32.972075Z","shell.execute_reply":"2024-07-30T20:09:32.976999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(y_submission,axis=1).shape","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:10:58.509579Z","iopub.execute_input":"2024-07-30T20:10:58.510571Z","iopub.status.idle":"2024-07-30T20:10:58.518387Z","shell.execute_reply.started":"2024-07-30T20:10:58.510531Z","shell.execute_reply":"2024-07-30T20:10:58.517423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes=np.argmax(y_submission,axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:13:57.716097Z","iopub.execute_input":"2024-07-30T20:13:57.716737Z","iopub.status.idle":"2024-07-30T20:13:57.721773Z","shell.execute_reply.started":"2024-07-30T20:13:57.716692Z","shell.execute_reply":"2024-07-30T20:13:57.720757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ImageId  Label","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission={'ImageId':list(range(1,28001)),'Label':predicted_classes}","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:15:22.667753Z","iopub.execute_input":"2024-07-30T20:15:22.668603Z","iopub.status.idle":"2024-07-30T20:15:22.673061Z","shell.execute_reply.started":"2024-07-30T20:15:22.668571Z","shell.execute_reply":"2024-07-30T20:15:22.672097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df=pd.DataFrame(submission)\nsubmission_df","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:15:53.275188Z","iopub.execute_input":"2024-07-30T20:15:53.275887Z","iopub.status.idle":"2024-07-30T20:15:53.299399Z","shell.execute_reply.started":"2024-07-30T20:15:53.275854Z","shell.execute_reply":"2024-07-30T20:15:53.298294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T20:17:56.068395Z","iopub.execute_input":"2024-07-30T20:17:56.069117Z","iopub.status.idle":"2024-07-30T20:17:56.106548Z","shell.execute_reply.started":"2024-07-30T20:17:56.069085Z","shell.execute_reply":"2024-07-30T20:17:56.105580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}