{"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)\n\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\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 sessional","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-01T14:46:48.766556Z","iopub.execute_input":"2022-01-01T14:46:48.767050Z","iopub.status.idle":"2022-01-01T14:46:48.776384Z","shell.execute_reply.started":"2022-01-01T14:46:48.767016Z","shell.execute_reply":"2022-01-01T14:46:48.775700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:46:49.646575Z","iopub.execute_input":"2022-01-01T14:46:49.646942Z","iopub.status.idle":"2022-01-01T14:46:52.804568Z","shell.execute_reply.started":"2022-01-01T14:46:49.646913Z","shell.execute_reply":"2022-01-01T14:46:52.803517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:46:52.805951Z","iopub.execute_input":"2022-01-01T14:46:52.806208Z","iopub.status.idle":"2022-01-01T14:46:52.844520Z","shell.execute_reply.started":"2022-01-01T14:46:52.806183Z","shell.execute_reply":"2022-01-01T14:46:52.843395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nsns.countplot(train['label'])","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:46:53.841802Z","iopub.execute_input":"2022-01-01T14:46:53.842130Z","iopub.status.idle":"2022-01-01T14:46:54.780156Z","shell.execute_reply.started":"2022-01-01T14:46:53.842102Z","shell.execute_reply":"2022-01-01T14:46:54.779201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Figure size\nplt.figure(figsize=(6,6))\n\n# Subplot \nfor i in range(9):\n    img = np.asarray(train.iloc[i,1:].values.reshape((28,28)));\n    plt.subplot(3, 3, i+1)\n    plt.imshow(img, cmap='gray')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:46:54.781922Z","iopub.execute_input":"2022-01-01T14:46:54.782303Z","iopub.status.idle":"2022-01-01T14:46:55.532186Z","shell.execute_reply.started":"2022-01-01T14:46:54.782264Z","shell.execute_reply":"2022-01-01T14:46:55.530888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Labels\ny=train.label","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:46:58.316724Z","iopub.execute_input":"2022-01-01T14:46:58.317061Z","iopub.status.idle":"2022-01-01T14:46:58.321445Z","shell.execute_reply.started":"2022-01-01T14:46:58.317031Z","shell.execute_reply":"2022-01-01T14:46:58.320418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# One-hot encoding of labels\ny=pd.get_dummies(y)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:46:58.977225Z","iopub.execute_input":"2022-01-01T14:46:58.977539Z","iopub.status.idle":"2022-01-01T14:46:58.985155Z","shell.execute_reply.started":"2022-01-01T14:46:58.977511Z","shell.execute_reply":"2022-01-01T14:46:58.984086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Features scaled to lie in [0,1]\nX=train.drop('label', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:46:59.216711Z","iopub.execute_input":"2022-01-01T14:46:59.217021Z","iopub.status.idle":"2022-01-01T14:46:59.327597Z","shell.execute_reply.started":"2022-01-01T14:46:59.216995Z","shell.execute_reply":"2022-01-01T14:46:59.326158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_valid, y_train, y_valid = train_test_split(X,y,train_size=0.9,test_size=0.1,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:02.451151Z","iopub.execute_input":"2022-01-01T14:47:02.451502Z","iopub.status.idle":"2022-01-01T14:47:02.889315Z","shell.execute_reply.started":"2022-01-01T14:47:02.451475Z","shell.execute_reply":"2022-01-01T14:47:02.888243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:03.016251Z","iopub.execute_input":"2022-01-01T14:47:03.016603Z","iopub.status.idle":"2022-01-01T14:47:08.169072Z","shell.execute_reply.started":"2022-01-01T14:47:03.016572Z","shell.execute_reply":"2022-01-01T14:47:08.168001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:08.170477Z","iopub.execute_input":"2022-01-01T14:47:08.170787Z","iopub.status.idle":"2022-01-01T14:47:08.174818Z","shell.execute_reply.started":"2022-01-01T14:47:08.170755Z","shell.execute_reply":"2022-01-01T14:47:08.173906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    \n    # hidden layer 1\n    layers.Dense(units=256, activation='relu', input_shape=[784]),\n    \n    \n    # output layer (softmax returns a probability distribution)\n    layers.Dense(units=10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:08.176204Z","iopub.execute_input":"2022-01-01T14:47:08.176616Z","iopub.status.idle":"2022-01-01T14:47:08.272937Z","shell.execute_reply.started":"2022-01-01T14:47:08.176588Z","shell.execute_reply":"2022-01-01T14:47:08.272222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['categorical_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:35.298007Z","iopub.execute_input":"2022-01-01T14:47:35.298495Z","iopub.status.idle":"2022-01-01T14:47:35.316805Z","shell.execute_reply.started":"2022-01-01T14:47:35.298466Z","shell.execute_reply":"2022-01-01T14:47:35.315691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    validation_data=(X_valid, y_valid),\n    batch_size=500,\n    epochs=10,\n    verbose=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:35.616656Z","iopub.execute_input":"2022-01-01T14:47:35.617254Z","iopub.status.idle":"2022-01-01T14:47:43.726517Z","shell.execute_reply.started":"2022-01-01T14:47:35.617222Z","shell.execute_reply":"2022-01-01T14:47:43.725777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:55.516832Z","iopub.execute_input":"2022-01-01T14:47:55.517400Z","iopub.status.idle":"2022-01-01T14:47:57.492836Z","shell.execute_reply.started":"2022-01-01T14:47:55.517365Z","shell.execute_reply":"2022-01-01T14:47:57.491757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:57.494217Z","iopub.execute_input":"2022-01-01T14:47:57.494534Z","iopub.status.idle":"2022-01-01T14:47:57.516841Z","shell.execute_reply.started":"2022-01-01T14:47:57.494505Z","shell.execute_reply":"2022-01-01T14:47:57.515756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predictions\npreds=model.predict(test)\n\n# Retrieve most likely classes\npred_classes = np.argmax(preds,axis=1)\n\n# Save predictions to file\noutput = pd.DataFrame({'ImageId': test.index+1,\n                       'Label': pred_classes})\n\n# Check format\noutput","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:47:57.518746Z","iopub.execute_input":"2022-01-01T14:47:57.519131Z","iopub.status.idle":"2022-01-01T14:47:58.799855Z","shell.execute_reply.started":"2022-01-01T14:47:57.519093Z","shell.execute_reply":"2022-01-01T14:47:58.798735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T14:48:03.242541Z","iopub.execute_input":"2022-01-01T14:48:03.242984Z","iopub.status.idle":"2022-01-01T14:48:03.285632Z","shell.execute_reply.started":"2022-01-01T14:48:03.242944Z","shell.execute_reply":"2022-01-01T14:48:03.284689Z"},"trusted":true},"execution_count":null,"outputs":[]}]}