{"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 pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-12T21:22:52.185987Z","iopub.execute_input":"2023-04-12T21:22:52.186340Z","iopub.status.idle":"2023-04-12T21:22:52.191564Z","shell.execute_reply.started":"2023-04-12T21:22:52.186309Z","shell.execute_reply":"2023-04-12T21:22:52.190494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:52.194195Z","iopub.execute_input":"2023-04-12T21:22:52.194542Z","iopub.status.idle":"2023-04-12T21:22:53.804215Z","shell.execute_reply.started":"2023-04-12T21:22:52.194500Z","shell.execute_reply":"2023-04-12T21:22:53.802256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()\n#Data Seems clear no preprocessing required","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:53.806272Z","iopub.execute_input":"2023-04-12T21:22:53.806685Z","iopub.status.idle":"2023-04-12T21:22:53.826311Z","shell.execute_reply.started":"2023-04-12T21:22:53.806646Z","shell.execute_reply":"2023-04-12T21:22:53.825018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:53.828195Z","iopub.execute_input":"2023-04-12T21:22:53.828551Z","iopub.status.idle":"2023-04-12T21:22:53.839617Z","shell.execute_reply.started":"2023-04-12T21:22:53.828519Z","shell.execute_reply":"2023-04-12T21:22:53.838124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# here in this data we see we have got activation value of each pixel.\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:53.843582Z","iopub.execute_input":"2023-04-12T21:22:53.844481Z","iopub.status.idle":"2023-04-12T21:22:53.851170Z","shell.execute_reply.started":"2023-04-12T21:22:53.844424Z","shell.execute_reply":"2023-04-12T21:22:53.849854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  seeing how many of them are with each digit\nsns.countplot(y=df['label'] ) , \ndf['label'].value_counts()\n\n# here we see each digits are more than 4000 but still varries\n","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:53.853153Z","iopub.execute_input":"2023-04-12T21:22:53.853662Z","iopub.status.idle":"2023-04-12T21:22:54.049063Z","shell.execute_reply.started":"2023-04-12T21:22:53.853573Z","shell.execute_reply":"2023-04-12T21:22:54.047249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting The lable and Data\n","metadata":{}},{"cell_type":"code","source":"X = df.drop(['label'] , axis=1)\ny = df['label']","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:54.051098Z","iopub.execute_input":"2023-04-12T21:22:54.051437Z","iopub.status.idle":"2023-04-12T21:22:54.106543Z","shell.execute_reply.started":"2023-04-12T21:22:54.051404Z","shell.execute_reply":"2023-04-12T21:22:54.105508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:54.108037Z","iopub.execute_input":"2023-04-12T21:22:54.108417Z","iopub.status.idle":"2023-04-12T21:22:54.134204Z","shell.execute_reply.started":"2023-04-12T21:22:54.108376Z","shell.execute_reply":"2023-04-12T21:22:54.132740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:54.135963Z","iopub.execute_input":"2023-04-12T21:22:54.136342Z","iopub.status.idle":"2023-04-12T21:22:54.145834Z","shell.execute_reply.started":"2023-04-12T21:22:54.136305Z","shell.execute_reply":"2023-04-12T21:22:54.144092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting Some Digit","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# lets try to plotting some figure\n","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:54.146690Z","iopub.execute_input":"2023-04-12T21:22:54.146956Z","iopub.status.idle":"2023-04-12T21:22:54.159459Z","shell.execute_reply.started":"2023-04-12T21:22:54.146920Z","shell.execute_reply":"2023-04-12T21:22:54.158282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(5,5,figsize = (15,15))\nax = ax.ravel()\nfor i in range(25):\n    image = X.iloc[i]\n    ax[i].imshow(image.to_numpy().reshape(28,28),cmap = 'gray')\n    ax[i].set_title(y[i])\n    ax[i].axis(\"off\")\n  \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:54.160380Z","iopub.execute_input":"2023-04-12T21:22:54.160636Z","iopub.status.idle":"2023-04-12T21:22:54.868723Z","shell.execute_reply.started":"2023-04-12T21:22:54.160611Z","shell.execute_reply":"2023-04-12T21:22:54.866878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now normalizing the data as whole of data is either in form of zero or one \n\nX = X/225","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:54.870556Z","iopub.execute_input":"2023-04-12T21:22:54.871745Z","iopub.status.idle":"2023-04-12T21:22:54.922276Z","shell.execute_reply.started":"2023-04-12T21:22:54.871698Z","shell.execute_reply":"2023-04-12T21:22:54.921454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting into train_test_split","metadata":{}},{"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 = 42)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:54.923143Z","iopub.execute_input":"2023-04-12T21:22:54.923452Z","iopub.status.idle":"2023-04-12T21:22:55.107143Z","shell.execute_reply.started":"2023-04-12T21:22:54.923416Z","shell.execute_reply":"2023-04-12T21:22:55.106025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## One hot encoding the target:\nWe have the labels for each input data. They are numbers between 0 and 9 that indicate which digit represents the image, that is, to which class they are associated. We will represent this label with a vector of 10 positions, where the position corresponding to the digit that represents the image contains a 1 and the remaining positions of the vector contain the value 0.\n\nSo, we will use the one-hot encoding procedure. It consists of transforming the labels into a vector of as many zeros as the number of different labels, and containing the value of 1 in the index that corresponds to the value of the label. Keras offers many support functions, including to_categorical to perform this transformation, which we can import from keras.utils:","metadata":{}},{"cell_type":"code","source":"from keras.utils import np_utils","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.112911Z","iopub.execute_input":"2023-04-12T21:22:55.113250Z","iopub.status.idle":"2023-04-12T21:22:55.118727Z","shell.execute_reply.started":"2023-04-12T21:22:55.113222Z","shell.execute_reply":"2023-04-12T21:22:55.117605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np_utils.to_categorical(y_train, 10)\ny_test = np_utils.to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.120370Z","iopub.execute_input":"2023-04-12T21:22:55.120674Z","iopub.status.idle":"2023-04-12T21:22:55.131007Z","shell.execute_reply.started":"2023-04-12T21:22:55.120646Z","shell.execute_reply":"2023-04-12T21:22:55.129250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Modeling:\nWe are going to use Keras. Keras is a high-level neural network API that is built on top of TensorFlow, Theano, or CNTK. There are several reasons why Keras is a good choice for building neural networks:\n\nUser-friendly interface: Keras provides a simple, intuitive, and easy-to-use interface for building neural networks. It offers a wide range of pre-built layers and models, which makes it easier to get started with deep learning.\n\nModular design: Keras is designed in a modular way, which means that you can build and modify neural networks by adding or removing layers. This makes it easier to experiment with different architectures and hyperparameters","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.132163Z","iopub.execute_input":"2023-04-12T21:22:55.132449Z","iopub.status.idle":"2023-04-12T21:22:55.142665Z","shell.execute_reply.started":"2023-04-12T21:22:55.132420Z","shell.execute_reply":"2023-04-12T21:22:55.140955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(128, activation = \"relu\", input_shape =(784,)))\nmodel.add(Dense(90, activation = \"relu\"))\nmodel.add(Dense(10, activation =\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.144559Z","iopub.execute_input":"2023-04-12T21:22:55.145094Z","iopub.status.idle":"2023-04-12T21:22:55.197940Z","shell.execute_reply.started":"2023-04-12T21:22:55.145042Z","shell.execute_reply":"2023-04-12T21:22:55.196667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.199119Z","iopub.execute_input":"2023-04-12T21:22:55.199368Z","iopub.status.idle":"2023-04-12T21:22:55.219732Z","shell.execute_reply.started":"2023-04-12T21:22:55.199341Z","shell.execute_reply":"2023-04-12T21:22:55.218286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",\n              optimizer=\"Adam\",\n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.221148Z","iopub.execute_input":"2023-04-12T21:22:55.221478Z","iopub.status.idle":"2023-04-12T21:22:55.234432Z","shell.execute_reply.started":"2023-04-12T21:22:55.221446Z","shell.execute_reply":"2023-04-12T21:22:55.232969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntf.config.run_functions_eagerly(True)\nimport warnings\nwarnings.filterwarnings(\"ignore\") #this is to ignore warnings generated by code","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.235378Z","iopub.execute_input":"2023-04-12T21:22:55.235642Z","iopub.status.idle":"2023-04-12T21:22:55.242809Z","shell.execute_reply.started":"2023-04-12T21:22:55.235617Z","shell.execute_reply":"2023-04-12T21:22:55.241588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train, batch_size = 100, epochs=10,validation_data=(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:22:55.244178Z","iopub.execute_input":"2023-04-12T21:22:55.244533Z","iopub.status.idle":"2023-04-12T21:24:40.192256Z","shell.execute_reply.started":"2023-04-12T21:22:55.244500Z","shell.execute_reply":"2023-04-12T21:24:40.191007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:40.193689Z","iopub.execute_input":"2023-04-12T21:24:40.194090Z","iopub.status.idle":"2023-04-12T21:24:42.800160Z","shell.execute_reply.started":"2023-04-12T21:24:40.194051Z","shell.execute_reply":"2023-04-12T21:24:42.798656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_acc)\n# printing Accuracy","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:42.801385Z","iopub.execute_input":"2023-04-12T21:24:42.801692Z","iopub.status.idle":"2023-04-12T21:24:42.807537Z","shell.execute_reply.started":"2023-04-12T21:24:42.801665Z","shell.execute_reply":"2023-04-12T21:24:42.806054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\n# Convert predictions classes back to the digit \ny_pred_classes = np.argmax(y_pred, axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:42.809333Z","iopub.execute_input":"2023-04-12T21:24:42.809993Z","iopub.status.idle":"2023-04-12T21:24:43.755958Z","shell.execute_reply.started":"2023-04-12T21:24:42.809737Z","shell.execute_reply":"2023-04-12T21:24:43.754792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.round(y_pred[0]))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:43.758007Z","iopub.execute_input":"2023-04-12T21:24:43.758300Z","iopub.status.idle":"2023-04-12T21:24:43.765337Z","shell.execute_reply.started":"2023-04-12T21:24:43.758271Z","shell.execute_reply":"2023-04-12T21:24:43.764036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = np.argmax(y_test, axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:43.766548Z","iopub.execute_input":"2023-04-12T21:24:43.766814Z","iopub.status.idle":"2023-04-12T21:24:43.776371Z","shell.execute_reply.started":"2023-04-12T21:24:43.766785Z","shell.execute_reply":"2023-04-12T21:24:43.775007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nconfusion = confusion_matrix(y_true, y_pred_classes)\nsns.heatmap(confusion, annot=True, fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:43.778059Z","iopub.execute_input":"2023-04-12T21:24:43.778485Z","iopub.status.idle":"2023-04-12T21:24:44.231591Z","shell.execute_reply.started":"2023-04-12T21:24:43.778452Z","shell.execute_reply":"2023-04-12T21:24:44.230638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_true,y_pred_classes))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:44.232920Z","iopub.execute_input":"2023-04-12T21:24:44.233441Z","iopub.status.idle":"2023-04-12T21:24:44.255146Z","shell.execute_reply.started":"2023-04-12T21:24:44.233412Z","shell.execute_reply":"2023-04-12T21:24:44.254052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:44.257480Z","iopub.execute_input":"2023-04-12T21:24:44.258046Z","iopub.status.idle":"2023-04-12T21:24:45.287608Z","shell.execute_reply.started":"2023-04-12T21:24:44.258011Z","shell.execute_reply":"2023-04-12T21:24:45.285978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test/255\n# just normalizing the data like training set","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:24:45.289464Z","iopub.execute_input":"2023-04-12T21:24:45.289957Z","iopub.status.idle":"2023-04-12T21:24:45.326802Z","shell.execute_reply.started":"2023-04-12T21:24:45.289880Z","shell.execute_reply":"2023-04-12T21:24:45.325504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:28:33.468576Z","iopub.execute_input":"2023-04-12T21:28:33.468982Z","iopub.status.idle":"2023-04-12T21:28:36.531645Z","shell.execute_reply.started":"2023-04-12T21:28:33.468946Z","shell.execute_reply":"2023-04-12T21:28:36.529986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:28:37.617740Z","iopub.execute_input":"2023-04-12T21:28:37.618149Z","iopub.status.idle":"2023-04-12T21:28:37.625635Z","shell.execute_reply.started":"2023-04-12T21:28:37.618112Z","shell.execute_reply":"2023-04-12T21:28:37.624572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_classes = np.argmax(result, axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:28:38.828295Z","iopub.execute_input":"2023-04-12T21:28:38.828690Z","iopub.status.idle":"2023-04-12T21:28:38.833564Z","shell.execute_reply.started":"2023-04-12T21:28:38.828653Z","shell.execute_reply":"2023-04-12T21:28:38.832468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reslt = pd.DataFrame(result_classes)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:28:39.923382Z","iopub.execute_input":"2023-04-12T21:28:39.924460Z","iopub.status.idle":"2023-04-12T21:28:39.930947Z","shell.execute_reply.started":"2023-04-12T21:28:39.924420Z","shell.execute_reply":"2023-04-12T21:28:39.929134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = [*range(1,28001)]","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:28:40.951001Z","iopub.execute_input":"2023-04-12T21:28:40.951975Z","iopub.status.idle":"2023-04-12T21:28:40.957830Z","shell.execute_reply.started":"2023-04-12T21:28:40.951909Z","shell.execute_reply":"2023-04-12T21:28:40.956563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ID = pd.DataFrame(arr)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:28:41.966293Z","iopub.execute_input":"2023-04-12T21:28:41.966645Z","iopub.status.idle":"2023-04-12T21:28:41.979485Z","shell.execute_reply.started":"2023-04-12T21:28:41.966618Z","shell.execute_reply":"2023-04-12T21:28:41.977794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.concat([ID, reslt],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:28:43.108719Z","iopub.execute_input":"2023-04-12T21:28:43.109956Z","iopub.status.idle":"2023-04-12T21:28:43.116497Z","shell.execute_reply.started":"2023-04-12T21:28:43.109867Z","shell.execute_reply":"2023-04-12T21:28:43.114565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.columns = ['ImageID','Label']","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:35:19.233575Z","iopub.execute_input":"2023-04-12T21:35:19.234631Z","iopub.status.idle":"2023-04-12T21:35:19.239334Z","shell.execute_reply.started":"2023-04-12T21:35:19.234589Z","shell.execute_reply":"2023-04-12T21:35:19.238311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('/kaggle/working/result.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:35:20.373976Z","iopub.execute_input":"2023-04-12T21:35:20.374323Z","iopub.status.idle":"2023-04-12T21:35:20.406483Z","shell.execute_reply.started":"2023-04-12T21:35:20.374288Z","shell.execute_reply":"2023-04-12T21:35:20.405411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.read_csv('/kaggle/working/result.csv')\ndf2","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:35:22.289548Z","iopub.execute_input":"2023-04-12T21:35:22.289943Z","iopub.status.idle":"2023-04-12T21:35:22.306314Z","shell.execute_reply.started":"2023-04-12T21:35:22.289906Z","shell.execute_reply":"2023-04-12T21:35:22.305238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}