{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:33.093532Z","iopub.execute_input":"2023-02-03T17:43:33.093981Z","iopub.status.idle":"2023-02-03T17:43:34.041658Z","shell.execute_reply.started":"2023-02-03T17:43:33.093893Z","shell.execute_reply":"2023-02-03T17:43:34.040466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/digit-recognizer/train.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:34.043247Z","iopub.execute_input":"2023-02-03T17:43:34.043602Z","iopub.status.idle":"2023-02-03T17:43:37.936655Z","shell.execute_reply.started":"2023-02-03T17:43:34.043572Z","shell.execute_reply":"2023-02-03T17:43:37.935484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:37.938286Z","iopub.execute_input":"2023-02-03T17:43:37.939391Z","iopub.status.idle":"2023-02-03T17:43:37.993899Z","shell.execute_reply.started":"2023-02-03T17:43:37.939346Z","shell.execute_reply":"2023-02-03T17:43:37.992740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=df.drop('label',axis=1)\ny=df['label']","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:37.996977Z","iopub.execute_input":"2023-02-03T17:43:37.998055Z","iopub.status.idle":"2023-02-03T17:43:38.113920Z","shell.execute_reply.started":"2023-02-03T17:43:37.998008Z","shell.execute_reply":"2023-02-03T17:43:38.112444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X/255.0\n","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:38.116254Z","iopub.execute_input":"2023-02-03T17:43:38.116732Z","iopub.status.idle":"2023-02-03T17:43:38.199552Z","shell.execute_reply.started":"2023-02-03T17:43:38.116697Z","shell.execute_reply":"2023-02-03T17:43:38.198401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX = X.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:38.201139Z","iopub.execute_input":"2023-02-03T17:43:38.201551Z","iopub.status.idle":"2023-02-03T17:43:38.207431Z","shell.execute_reply.started":"2023-02-03T17:43:38.201513Z","shell.execute_reply":"2023-02-03T17:43:38.206314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_image=len(X)\nno_image","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:38.209176Z","iopub.execute_input":"2023-02-03T17:43:38.209969Z","iopub.status.idle":"2023-02-03T17:43:38.221973Z","shell.execute_reply.started":"2023-02-03T17:43:38.209926Z","shell.execute_reply":"2023-02-03T17:43:38.220910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[11,:].reshape(28,28),cmap=\"gray\")\nplt.show()\nplt.imshow(X[16,:].reshape(28,28),cmap=\"gray\")\nplt.show()\nplt.imshow(X[20,:].reshape(28,28),cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:38.223744Z","iopub.execute_input":"2023-02-03T17:43:38.224392Z","iopub.status.idle":"2023-02-03T17:43:38.821264Z","shell.execute_reply.started":"2023-02-03T17:43:38.224350Z","shell.execute_reply":"2023-02-03T17:43:38.820492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X,y ,test_size =0.2,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:38.822449Z","iopub.execute_input":"2023-02-03T17:43:38.822997Z","iopub.status.idle":"2023-02-03T17:43:39.343319Z","shell.execute_reply.started":"2023-02-03T17:43:38.822964Z","shell.execute_reply":"2023-02-03T17:43:39.342069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:39.344990Z","iopub.execute_input":"2023-02-03T17:43:39.345445Z","iopub.status.idle":"2023-02-03T17:43:44.524984Z","shell.execute_reply.started":"2023-02-03T17:43:39.345380Z","shell.execute_reply":"2023-02-03T17:43:44.523800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Label Encoading\n\ny_train = keras.utils.to_categorical(y_train, num_classes = 10)\ny_test = keras.utils.to_categorical(y_test, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:44.529221Z","iopub.execute_input":"2023-02-03T17:43:44.529867Z","iopub.status.idle":"2023-02-03T17:43:45.564736Z","shell.execute_reply.started":"2023-02-03T17:43:44.529830Z","shell.execute_reply":"2023-02-03T17:43:45.563706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"# Initialize the CNN\n\nfrom keras.models import Sequential\n\nclassfier=Sequential()","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.566094Z","iopub.execute_input":"2023-02-03T17:43:45.566538Z","iopub.status.idle":"2023-02-03T17:43:45.631279Z","shell.execute_reply.started":"2023-02-03T17:43:45.566489Z","shell.execute_reply":"2023-02-03T17:43:45.630005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1. Convolution","metadata":{}},{"cell_type":"code","source":"from keras.layers import Conv2D\nclassfier.add(Conv2D(filters=32,kernel_size=3,activation='relu',input_shape=[28,28,1]))","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.632848Z","iopub.execute_input":"2023-02-03T17:43:45.634027Z","iopub.status.idle":"2023-02-03T17:43:45.689817Z","shell.execute_reply.started":"2023-02-03T17:43:45.633979Z","shell.execute_reply":"2023-02-03T17:43:45.688652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Pooling","metadata":{}},{"cell_type":"code","source":"from keras.layers import MaxPooling2D\nclassfier.add(MaxPooling2D(pool_size=2,strides=2))","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.691051Z","iopub.execute_input":"2023-02-03T17:43:45.691379Z","iopub.status.idle":"2023-02-03T17:43:45.703063Z","shell.execute_reply.started":"2023-02-03T17:43:45.691349Z","shell.execute_reply":"2023-02-03T17:43:45.701748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Add 2nd Convolution layer\nclassfier.add(Conv2D(filters=32,kernel_size=3,activation='relu'))\nclassfier.add(MaxPooling2D(pool_size=2,strides=2))","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.704564Z","iopub.execute_input":"2023-02-03T17:43:45.705017Z","iopub.status.idle":"2023-02-03T17:43:45.725590Z","shell.execute_reply.started":"2023-02-03T17:43:45.704982Z","shell.execute_reply":"2023-02-03T17:43:45.724482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Flattening","metadata":{}},{"cell_type":"code","source":"from keras.layers import Flatten\nclassfier.add(Flatten())","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.726984Z","iopub.execute_input":"2023-02-03T17:43:45.727284Z","iopub.status.idle":"2023-02-03T17:43:45.736438Z","shell.execute_reply.started":"2023-02-03T17:43:45.727257Z","shell.execute_reply":"2023-02-03T17:43:45.735315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4. full Connection","metadata":{}},{"cell_type":"code","source":"from keras.layers import Dense\nclassfier.add(Dense(units=128,activation='relu'))\nclassfier.add(Dense(units=10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.737492Z","iopub.execute_input":"2023-02-03T17:43:45.737791Z","iopub.status.idle":"2023-02-03T17:43:45.761277Z","shell.execute_reply.started":"2023-02-03T17:43:45.737763Z","shell.execute_reply":"2023-02-03T17:43:45.760196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Compile The CNN","metadata":{}},{"cell_type":"code","source":"classfier.compile(optimizer = \"Adam\", loss = \"CategoricalCrossentropy\", metrics = \"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.762765Z","iopub.execute_input":"2023-02-03T17:43:45.763475Z","iopub.status.idle":"2023-02-03T17:43:45.776716Z","shell.execute_reply.started":"2023-02-03T17:43:45.763441Z","shell.execute_reply":"2023-02-03T17:43:45.775676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training The Model","metadata":{}},{"cell_type":"code","source":"classfier.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=25)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:43:45.778249Z","iopub.execute_input":"2023-02-03T17:43:45.779035Z","iopub.status.idle":"2023-02-03T17:48:08.548095Z","shell.execute_reply.started":"2023-02-03T17:43:45.778999Z","shell.execute_reply":"2023-02-03T17:48:08.546841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = classfier.predict(X_test).argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:08.549898Z","iopub.execute_input":"2023-02-03T17:48:08.550240Z","iopub.status.idle":"2023-02-03T17:48:09.359887Z","shell.execute_reply.started":"2023-02-03T17:48:08.550210Z","shell.execute_reply":"2023-02-03T17:48:09.358894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:09.361758Z","iopub.execute_input":"2023-02-03T17:48:09.362264Z","iopub.status.idle":"2023-02-03T17:48:09.369700Z","shell.execute_reply.started":"2023-02-03T17:48:09.362222Z","shell.execute_reply":"2023-02-03T17:48:09.368568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18, 24))\n\nfor i in range(100):\n    plt.subplot(10, 10, i+1)\n    plt.imshow(X_test[i], cmap='gray')\n    plt.title(f\"Pred: {predictions[i]}\")\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:09.371408Z","iopub.execute_input":"2023-02-03T17:48:09.372456Z","iopub.status.idle":"2023-02-03T17:48:13.490952Z","shell.execute_reply.started":"2023-02-03T17:48:09.372405Z","shell.execute_reply":"2023-02-03T17:48:13.488544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:13.492758Z","iopub.execute_input":"2023-02-03T17:48:13.493427Z","iopub.status.idle":"2023-02-03T17:48:13.498390Z","shell.execute_reply.started":"2023-02-03T17:48:13.493379Z","shell.execute_reply":"2023-02-03T17:48:13.497172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=pd.read_csv(\"../input/digit-recognizer/test.csv\")\ndf1","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:13.499666Z","iopub.execute_input":"2023-02-03T17:48:13.500014Z","iopub.status.idle":"2023-02-03T17:48:15.450208Z","shell.execute_reply.started":"2023-02-03T17:48:13.499983Z","shell.execute_reply":"2023-02-03T17:48:15.449135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1=df1/255.0","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:15.451571Z","iopub.execute_input":"2023-02-03T17:48:15.451880Z","iopub.status.idle":"2023-02-03T17:48:15.504871Z","shell.execute_reply.started":"2023-02-03T17:48:15.451853Z","shell.execute_reply":"2023-02-03T17:48:15.503363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1 = X1.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:15.506203Z","iopub.execute_input":"2023-02-03T17:48:15.506555Z","iopub.status.idle":"2023-02-03T17:48:15.512386Z","shell.execute_reply.started":"2023-02-03T17:48:15.506523Z","shell.execute_reply":"2023-02-03T17:48:15.511259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X1[11,:].reshape(28,28),cmap=\"gray\")\nplt.show()\nplt.imshow(X1[16,:].reshape(28,28),cmap=\"gray\")\nplt.show()\nplt.imshow(X1[20,:].reshape(28,28),cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:15.514320Z","iopub.execute_input":"2023-02-03T17:48:15.514705Z","iopub.status.idle":"2023-02-03T17:48:16.059400Z","shell.execute_reply.started":"2023-02-03T17:48:15.514658Z","shell.execute_reply":"2023-02-03T17:48:16.058498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = classfier.predict(X1).argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:16.060760Z","iopub.execute_input":"2023-02-03T17:48:16.061448Z","iopub.status.idle":"2023-02-03T17:48:18.889524Z","shell.execute_reply.started":"2023-02-03T17:48:16.061389Z","shell.execute_reply":"2023-02-03T17:48:18.888303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:18.891339Z","iopub.execute_input":"2023-02-03T17:48:18.891822Z","iopub.status.idle":"2023-02-03T17:48:18.900132Z","shell.execute_reply.started":"2023-02-03T17:48:18.891776Z","shell.execute_reply":"2023-02-03T17:48:18.898780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18, 24))\n\nfor i in range(100):\n    plt.subplot(10, 10, i+1)\n    plt.imshow(X1[i], cmap='gray')\n    plt.title(f\"Pred: {test_predictions[i]}\")\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:18.902388Z","iopub.execute_input":"2023-02-03T17:48:18.903168Z","iopub.status.idle":"2023-02-03T17:48:23.309137Z","shell.execute_reply.started":"2023-02-03T17:48:18.903124Z","shell.execute_reply":"2023-02-03T17:48:23.308033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame({'ImageId': list(range(1, len(test_predictions)+1)), 'Label': test_predictions})\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-03T17:48:23.310461Z","iopub.execute_input":"2023-02-03T17:48:23.310813Z","iopub.status.idle":"2023-02-03T17:48:23.356546Z","shell.execute_reply.started":"2023-02-03T17:48:23.310781Z","shell.execute_reply":"2023-02-03T17:48:23.355495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}