{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# import liberys needed\n\nfrom sklearn.metrics import classification_report\nimport tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential \nfrom tensorflow.keras.layers import Dense,Dropout\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:53:55.428788Z","iopub.execute_input":"2024-12-20T19:53:55.429151Z","iopub.status.idle":"2024-12-20T19:53:55.434240Z","shell.execute_reply.started":"2024-12-20T19:53:55.429119Z","shell.execute_reply":"2024-12-20T19:53:55.433435Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# DATA LOADING","metadata":{}},{"cell_type":"code","source":"# reading csv file in pandas dataframe\ndf=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:53:55.435220Z","iopub.execute_input":"2024-12-20T19:53:55.435448Z","iopub.status.idle":"2024-12-20T19:53:57.810556Z","shell.execute_reply.started":"2024-12-20T19:53:55.435428Z","shell.execute_reply":"2024-12-20T19:53:57.809769Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:53:57.811537Z","iopub.execute_input":"2024-12-20T19:53:57.811792Z","iopub.status.idle":"2024-12-20T19:53:59.616193Z","shell.execute_reply.started":"2024-12-20T19:53:57.811770Z","shell.execute_reply":"2024-12-20T19:53:59.615360Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check Class imbalance\ndf.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:53:59.618627Z","iopub.execute_input":"2024-12-20T19:53:59.618926Z","iopub.status.idle":"2024-12-20T19:53:59.631242Z","shell.execute_reply.started":"2024-12-20T19:53:59.618895Z","shell.execute_reply":"2024-12-20T19:53:59.630414Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# drop nan values\ndf.dropna(axis = 0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:53:59.632180Z","iopub.execute_input":"2024-12-20T19:53:59.632430Z","iopub.status.idle":"2024-12-20T19:53:59.742957Z","shell.execute_reply.started":"2024-12-20T19:53:59.632411Z","shell.execute_reply":"2024-12-20T19:53:59.742008Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# select features and target\nX = df.drop('label',axis=1).values\ny = df['label'].values","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:53:59.744154Z","iopub.execute_input":"2024-12-20T19:53:59.744467Z","iopub.status.idle":"2024-12-20T19:53:59.829641Z","shell.execute_reply.started":"2024-12-20T19:53:59.744439Z","shell.execute_reply":"2024-12-20T19:53:59.828686Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# split data traing and testing\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-12-20T19:53:59.830839Z","iopub.execute_input":"2024-12-20T19:53:59.831150Z","iopub.status.idle":"2024-12-20T19:54:00.206604Z","shell.execute_reply.started":"2024-12-20T19:53:59.831125Z","shell.execute_reply":"2024-12-20T19:54:00.205925Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,11):\n    plt.subplot(2,5,i)\n    \n    img = X_train[i].reshape((28,28))\n    plt.imshow(img,cmap='Greys')\n    plt.title(y_train[i])\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:54:00.207631Z","iopub.execute_input":"2024-12-20T19:54:00.207921Z","iopub.status.idle":"2024-12-20T19:54:00.785185Z","shell.execute_reply.started":"2024-12-20T19:54:00.207887Z","shell.execute_reply":"2024-12-20T19:54:00.784400Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape,y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:54:00.786132Z","iopub.execute_input":"2024-12-20T19:54:00.786399Z","iopub.status.idle":"2024-12-20T19:54:00.791735Z","shell.execute_reply.started":"2024-12-20T19:54:00.786378Z","shell.execute_reply":"2024-12-20T19:54:00.790779Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X_train/255\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:54:00.792838Z","iopub.execute_input":"2024-12-20T19:54:00.793137Z","iopub.status.idle":"2024-12-20T19:54:00.902620Z","shell.execute_reply.started":"2024-12-20T19:54:00.793115Z","shell.execute_reply":"2024-12-20T19:54:00.901930Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Dense(784,activation='relu',input_dim=784))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(400,activation='relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(400,activation='relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(200,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:54:00.903678Z","iopub.execute_input":"2024-12-20T19:54:00.904010Z","iopub.status.idle":"2024-12-20T19:54:02.187243Z","shell.execute_reply.started":"2024-12-20T19:54:00.903975Z","shell.execute_reply":"2024-12-20T19:54:02.186401Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='SparseCategoricalCrossentropy',metrics=['accuracy'])\nhistory = model.fit(X_train,y_train,batch_size=32,epochs=25,verbose=1,validation_split= 0.2)","metadata":{"_kg_hide-input":false,"_kg_hide-output":false,"scrolled":true,"execution":{"iopub.status.busy":"2024-12-20T19:54:02.188525Z","iopub.execute_input":"2024-12-20T19:54:02.188780Z","iopub.status.idle":"2024-12-20T19:55:11.961485Z","shell.execute_reply.started":"2024-12-20T19:54:02.188758Z","shell.execute_reply":"2024-12-20T19:55:11.960783Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss']);","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:55:11.962604Z","iopub.execute_input":"2024-12-20T19:55:11.962889Z","iopub.status.idle":"2024-12-20T19:55:12.195084Z","shell.execute_reply.started":"2024-12-20T19:55:11.962848Z","shell.execute_reply":"2024-12-20T19:55:12.194267Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy']);","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:55:12.196048Z","iopub.execute_input":"2024-12-20T19:55:12.196275Z","iopub.status.idle":"2024-12-20T19:55:12.349183Z","shell.execute_reply.started":"2024-12-20T19:55:12.196256Z","shell.execute_reply":"2024-12-20T19:55:12.348311Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)\ny_pred_classes = np.argmax(y_pred, axis=1)\n\n# Generate a classification report\nreport = classification_report(y_test, y_pred_classes)\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:55:12.350320Z","iopub.execute_input":"2024-12-20T19:55:12.350624Z","iopub.status.idle":"2024-12-20T19:55:13.076295Z","shell.execute_reply.started":"2024-12-20T19:55:12.350596Z","shell.execute_reply":"2024-12-20T19:55:13.075552Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training full Data","metadata":{}},{"cell_type":"code","source":"X = X/255\nprint(X.shape,y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:55:13.079206Z","iopub.execute_input":"2024-12-20T19:55:13.079461Z","iopub.status.idle":"2024-12-20T19:55:13.185024Z","shell.execute_reply.started":"2024-12-20T19:55:13.079439Z","shell.execute_reply":"2024-12-20T19:55:13.184065Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"New_model=Sequential()\nNew_model.add(Dense(784,activation='LeakyReLU',input_dim=784))\nNew_model.add(Dropout(0.5))\nNew_model.add(Dense(400,activation='LeakyReLU'))\nNew_model.add(Dropout(0.3))\nNew_model.add(Dense(240,activation='relu'))\nNew_model.add(Dropout(0.2))\nNew_model.add(Dense(200,activation='relu'))\nNew_model.add(Dropout(0.2))\nNew_model.add(Dense(120,activation='relu'))\nNew_model.add(Dropout(0.2))\nNew_model.add(Dense(32,activation='relu'))\nNew_model.add(Dense(10,activation='softmax'))\nNew_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:55:13.186039Z","iopub.execute_input":"2024-12-20T19:55:13.186298Z","iopub.status.idle":"2024-12-20T19:55:13.309719Z","shell.execute_reply.started":"2024-12-20T19:55:13.186275Z","shell.execute_reply":"2024-12-20T19:55:13.308925Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"New_model.compile(optimizer='Adam',loss='SparseCategoricalCrossentropy',metrics=['accuracy'])\nhistory = New_model.fit(X,y,batch_size=32,epochs=25,verbose=1,validation_split= 0.2)","metadata":{"execution":{"iopub.status.busy":"2024-12-20T19:55:13.310683Z","iopub.execute_input":"2024-12-20T19:55:13.310946Z","iopub.status.idle":"2024-12-20T19:56:52.780403Z","shell.execute_reply.started":"2024-12-20T19:55:13.310924Z","shell.execute_reply":"2024-12-20T19:56:52.779541Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss']);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:52.781753Z","iopub.execute_input":"2024-12-20T19:56:52.782134Z","iopub.status.idle":"2024-12-20T19:56:52.976667Z","shell.execute_reply.started":"2024-12-20T19:56:52.782101Z","shell.execute_reply":"2024-12-20T19:56:52.975806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy']);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:52.977767Z","iopub.execute_input":"2024-12-20T19:56:52.978092Z","iopub.status.idle":"2024-12-20T19:56:53.185665Z","shell.execute_reply.started":"2024-12-20T19:56:52.978062Z","shell.execute_reply":"2024-12-20T19:56:53.184494Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"test_df=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:53.187249Z","iopub.execute_input":"2024-12-20T19:56:53.187492Z","iopub.status.idle":"2024-12-20T19:56:54.788548Z","shell.execute_reply.started":"2024-12-20T19:56:53.187470Z","shell.execute_reply":"2024-12-20T19:56:54.787613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = New_model.predict(test_df.values)\ny_pred_classes = np.argmax(y_pred, axis=1)\ny_pred_classes[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:54.789635Z","iopub.execute_input":"2024-12-20T19:56:54.789939Z","iopub.status.idle":"2024-12-20T19:56:56.986025Z","shell.execute_reply.started":"2024-12-20T19:56:54.789910Z","shell.execute_reply":"2024-12-20T19:56:56.985184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:56.986958Z","iopub.execute_input":"2024-12-20T19:56:56.987213Z","iopub.status.idle":"2024-12-20T19:56:56.999980Z","shell.execute_reply.started":"2024-12-20T19:56:56.987191Z","shell.execute_reply":"2024-12-20T19:56:56.999034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsample.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:57.001104Z","iopub.execute_input":"2024-12-20T19:56:57.001418Z","iopub.status.idle":"2024-12-20T19:56:57.022934Z","shell.execute_reply.started":"2024-12-20T19:56:57.001390Z","shell.execute_reply":"2024-12-20T19:56:57.022322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"subdata = pd.concat([sample['ImageId'],pd.Series(y_pred_classes, name='Label')], axis=1)\nsubdata.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:57.023722Z","iopub.execute_input":"2024-12-20T19:56:57.023968Z","iopub.status.idle":"2024-12-20T19:56:57.031447Z","shell.execute_reply.started":"2024-12-20T19:56:57.023947Z","shell.execute_reply":"2024-12-20T19:56:57.030741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"subdata.shape,y_pred_classes.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:57.032536Z","iopub.execute_input":"2024-12-20T19:56:57.032860Z","iopub.status.idle":"2024-12-20T19:56:57.042678Z","shell.execute_reply.started":"2024-12-20T19:56:57.032831Z","shell.execute_reply":"2024-12-20T19:56:57.041917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"subdata.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T19:56:57.043687Z","iopub.execute_input":"2024-12-20T19:56:57.044145Z","iopub.status.idle":"2024-12-20T19:56:57.086082Z","shell.execute_reply.started":"2024-12-20T19:56:57.044116Z","shell.execute_reply":"2024-12-20T19:56:57.085441Z"}},"outputs":[],"execution_count":null}]}