{"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":"# 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)\nimport matplotlib.pyplot as plt\n%matplotlib inline\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%matplotlib inline\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":"2021-06-26T11:52:27.016544Z","iopub.execute_input":"2021-06-26T11:52:27.016963Z","iopub.status.idle":"2021-06-26T11:52:27.035815Z","shell.execute_reply.started":"2021-06-26T11:52:27.016872Z","shell.execute_reply":"2021-06-26T11:52:27.034859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:27.040563Z","iopub.execute_input":"2021-06-26T11:52:27.041078Z","iopub.status.idle":"2021-06-26T11:52:30.871924Z","shell.execute_reply.started":"2021-06-26T11:52:27.041043Z","shell.execute_reply":"2021-06-26T11:52:30.870999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:30.873325Z","iopub.execute_input":"2021-06-26T11:52:30.873568Z","iopub.status.idle":"2021-06-26T11:52:30.915447Z","shell.execute_reply.started":"2021-06-26T11:52:30.873542Z","shell.execute_reply":"2021-06-26T11:52:30.914201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:30.917239Z","iopub.execute_input":"2021-06-26T11:52:30.917498Z","iopub.status.idle":"2021-06-26T11:52:33.112782Z","shell.execute_reply.started":"2021-06-26T11:52:30.917472Z","shell.execute_reply":"2021-06-26T11:52:33.112000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data representation****","metadata":{}},{"cell_type":"code","source":"\n##required for image reprresention else usually you get a Nonetype error\nX = (train.iloc[:,1:].values).astype('float32')\ny = train.iloc[:,0].values.astype('int32')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.114282Z","iopub.execute_input":"2021-06-26T11:52:33.114973Z","iopub.status.idle":"2021-06-26T11:52:33.193305Z","shell.execute_reply.started":"2021-06-26T11:52:33.114925Z","shell.execute_reply":"2021-06-26T11:52:33.192577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1=test.copy()\ntest1 = test1.values.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.194782Z","iopub.execute_input":"2021-06-26T11:52:33.195440Z","iopub.status.idle":"2021-06-26T11:52:33.332216Z","shell.execute_reply.started":"2021-06-26T11:52:33.195394Z","shell.execute_reply":"2021-06-26T11:52:33.331303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#to make the values to be represented neatly and reuired to work with keras API\nX = X.reshape(X.shape[0], 28, 28,1)\ntest1 = test1.reshape(test1.shape[0], 28, 28,1)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.333816Z","iopub.execute_input":"2021-06-26T11:52:33.334524Z","iopub.status.idle":"2021-06-26T11:52:33.339954Z","shell.execute_reply.started":"2021-06-26T11:52:33.334474Z","shell.execute_reply":"2021-06-26T11:52:33.339104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10, 13):\n    plt.subplot(330 + (i+1))#gives the dimensions of the plot area\n    plt.imshow(X[i], cmap=plt.get_cmap('gray'))#considering gray area\n    plt.title(y[i])","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.343821Z","iopub.execute_input":"2021-06-26T11:52:33.344160Z","iopub.status.idle":"2021-06-26T11:52:33.622577Z","shell.execute_reply.started":"2021-06-26T11:52:33.344128Z","shell.execute_reply":"2021-06-26T11:52:33.621929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[10], cmap=plt.get_cmap('gray'))","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.624159Z","iopub.execute_input":"2021-06-26T11:52:33.624614Z","iopub.status.idle":"2021-06-26T11:52:33.750401Z","shell.execute_reply.started":"2021-06-26T11:52:33.624569Z","shell.execute_reply":"2021-06-26T11:52:33.749709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1=train.drop(['label'],axis=1)\ny1=train['label']","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.751447Z","iopub.execute_input":"2021-06-26T11:52:33.751744Z","iopub.status.idle":"2021-06-26T11:52:33.846238Z","shell.execute_reply.started":"2021-06-26T11:52:33.751716Z","shell.execute_reply":"2021-06-26T11:52:33.845128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.847491Z","iopub.execute_input":"2021-06-26T11:52:33.847810Z","iopub.status.idle":"2021-06-26T11:52:33.883484Z","shell.execute_reply.started":"2021-06-26T11:52:33.847781Z","shell.execute_reply":"2021-06-26T11:52:33.882514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.886336Z","iopub.execute_input":"2021-06-26T11:52:33.886805Z","iopub.status.idle":"2021-06-26T11:52:33.893455Z","shell.execute_reply.started":"2021-06-26T11:52:33.886758Z","shell.execute_reply":"2021-06-26T11:52:33.892695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##to apply keras we normalize the data by diving by 255 as rgb values range from 0-255\nX1=X1/255\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:33.894787Z","iopub.execute_input":"2021-06-26T11:52:33.895390Z","iopub.status.idle":"2021-06-26T11:52:33.988828Z","shell.execute_reply.started":"2021-06-26T11:52:33.895343Z","shell.execute_reply":"2021-06-26T11:52:33.987885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1.head","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-06-26T11:52:33.990216Z","iopub.execute_input":"2021-06-26T11:52:33.990504Z","iopub.status.idle":"2021-06-26T11:52:34.018915Z","shell.execute_reply.started":"2021-06-26T11:52:33.990476Z","shell.execute_reply":"2021-06-26T11:52:34.018256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:34.019935Z","iopub.execute_input":"2021-06-26T11:52:34.020347Z","iopub.status.idle":"2021-06-26T11:52:34.026377Z","shell.execute_reply.started":"2021-06-26T11:52:34.020304Z","shell.execute_reply":"2021-06-26T11:52:34.025519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\ny1=to_categorical(y1,10)#considering it to be categorised into 10 diff as numbers from 0-9\ny1\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:34.027758Z","iopub.execute_input":"2021-06-26T11:52:34.028337Z","iopub.status.idle":"2021-06-26T11:52:39.705146Z","shell.execute_reply.started":"2021-06-26T11:52:34.028296Z","shell.execute_reply":"2021-06-26T11:52:39.704386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#modelling:\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers.experimental import preprocessing\nX_train,X_test,y_train,y_test=train_test_split(X1,y1,test_size=0.3,random_state=50)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:39.706148Z","iopub.execute_input":"2021-06-26T11:52:39.706513Z","iopub.status.idle":"2021-06-26T11:52:40.775181Z","shell.execute_reply.started":"2021-06-26T11:52:39.706484Z","shell.execute_reply":"2021-06-26T11:52:40.774107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:40.776766Z","iopub.execute_input":"2021-06-26T11:52:40.777179Z","iopub.status.idle":"2021-06-26T11:52:40.783976Z","shell.execute_reply.started":"2021-06-26T11:52:40.777138Z","shell.execute_reply":"2021-06-26T11:52:40.782801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = keras.callbacks.EarlyStopping(\n    patience=5,\n    min_delta=0.001,\n    restore_best_weights=True,\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:40.785838Z","iopub.execute_input":"2021-06-26T11:52:40.786247Z","iopub.status.idle":"2021-06-26T11:52:40.794862Z","shell.execute_reply.started":"2021-06-26T11:52:40.786206Z","shell.execute_reply":"2021-06-26T11:52:40.794017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=keras.Sequential([\n    layers.Dense(64,activation='relu'),\n    layers.BatchNormalization(),\n    layers.Dropout(0.3),\n    layers.Dense(256,activation='relu'),\n    layers.BatchNormalization(),\n    layers.Dropout(0.3),\n    layers.Dense(512,activation='relu'),\n    layers.Dense(10,activation='softmax')\n    \n    \n])","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:40.796200Z","iopub.execute_input":"2021-06-26T11:52:40.796499Z","iopub.status.idle":"2021-06-26T11:52:40.849956Z","shell.execute_reply.started":"2021-06-26T11:52:40.796473Z","shell.execute_reply":"2021-06-26T11:52:40.848931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## we use sparse as multiclass classification and metrics is taken by the model-\n##the one which is best suited\nmodel.compile(\noptimizer='adam',\nloss='categorical_crossentropy',\nmetrics=['accuracy'],\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:40.851327Z","iopub.execute_input":"2021-06-26T11:52:40.851771Z","iopub.status.idle":"2021-06-26T11:52:40.872325Z","shell.execute_reply.started":"2021-06-26T11:52:40.851728Z","shell.execute_reply":"2021-06-26T11:52:40.871357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    X_train, y_train,\n    validation_data=(X_test, y_test),\n    batch_size=128,\n    epochs=200,\n    callbacks=[early_stopping],\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:52:40.873551Z","iopub.execute_input":"2021-06-26T11:52:40.873854Z","iopub.status.idle":"2021-06-26T11:53:15.727129Z","shell.execute_reply.started":"2021-06-26T11:52:40.873823Z","shell.execute_reply":"2021-06-26T11:53:15.726380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score=model.evaluate(X_test,y_test)\nscore[1]","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:15.730875Z","iopub.execute_input":"2021-06-26T11:53:15.731434Z","iopub.status.idle":"2021-06-26T11:53:16.574794Z","shell.execute_reply.started":"2021-06-26T11:53:15.731389Z","shell.execute_reply":"2021-06-26T11:53:16.573899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:16.576168Z","iopub.execute_input":"2021-06-26T11:53:16.576459Z","iopub.status.idle":"2021-06-26T11:53:18.366519Z","shell.execute_reply.started":"2021-06-26T11:53:16.576431Z","shell.execute_reply":"2021-06-26T11:53:18.365517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:18.368052Z","iopub.execute_input":"2021-06-26T11:53:18.368470Z","iopub.status.idle":"2021-06-26T11:53:18.374822Z","shell.execute_reply.started":"2021-06-26T11:53:18.368426Z","shell.execute_reply":"2021-06-26T11:53:18.373910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)\n\nprint(predictions)\n\n#submissions=pd.DataFrame({\"ImageId\": list(range(1,len(predictions_final)+1)),\n                         #\"Label\": predictions_final})\n#submissions.to_csv(\"submission.csv\", index=False, header=True)\n#print(\"Finished uploading submission file to csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:18.375904Z","iopub.execute_input":"2021-06-26T11:53:18.376195Z","iopub.status.idle":"2021-06-26T11:53:19.821287Z","shell.execute_reply.started":"2021-06-26T11:53:18.376167Z","shell.execute_reply":"2021-06-26T11:53:19.820638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Label=np.argmax(predictions,axis=1)\nImageId=np.arange(1,28001)\nsubs=pd.DataFrame(data={\"ImageId\":ImageId,\"Label\":Label})","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:19.822344Z","iopub.execute_input":"2021-06-26T11:53:19.822805Z","iopub.status.idle":"2021-06-26T11:53:19.829181Z","shell.execute_reply.started":"2021-06-26T11:53:19.822772Z","shell.execute_reply":"2021-06-26T11:53:19.828115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subs","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:19.830260Z","iopub.execute_input":"2021-06-26T11:53:19.830526Z","iopub.status.idle":"2021-06-26T11:53:19.847149Z","shell.execute_reply.started":"2021-06-26T11:53:19.830500Z","shell.execute_reply":"2021-06-26T11:53:19.846309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subs.to_csv('sub1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:19.848298Z","iopub.execute_input":"2021-06-26T11:53:19.848566Z","iopub.status.idle":"2021-06-26T11:53:19.912068Z","shell.execute_reply.started":"2021-06-26T11:53:19.848539Z","shell.execute_reply":"2021-06-26T11:53:19.911111Z"},"trusted":true},"execution_count":null,"outputs":[]}]}