{"cells":[{"metadata":{"_uuid":"99ac200d-d21f-44a9-befe-719b9af788c1","_cell_guid":"eac540d5-a875-43a5-9052-fda79f358e22","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.\n\nimport tensorflow as tf\nfrom tensorflow import keras\n\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b02bb35a-e4f9-4496-9d27-b370b8c4bba1","_cell_guid":"2adfe662-09c9-491a-bdb6-6e284e7121ff","trusted":true},"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b6c3364d-726d-484a-84cc-d65396b93edc","_cell_guid":"81aac0bc-24e7-409a-8a6f-31ef63acc40f","trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8944fbe-e2d7-4fae-9ca6-3a14ce30d60f","_cell_guid":"4637662e-8f28-41b8-b8c5-c0e412ae6146","trusted":true},"cell_type":"code","source":"X = df.drop('label', axis=1).to_numpy()\ny = df['label'].to_numpy()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f29fc149-7875-4ef5-9d2c-28179e1ecb3f","_cell_guid":"4b3f4062-7f49-4a84-9078-7a1459a6ff21","trusted":true},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d39a1dc2-b3aa-4cfc-8930-e776a91845c2","_cell_guid":"daff6dc2-b867-4874-bdf3-33f594e43f7c","trusted":true},"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2fb09cf9-941b-42c2-a78c-a3cb6f034e2d","_cell_guid":"32159b8a-b2c1-4a6e-8313-caeb02b33813","trusted":true},"cell_type":"code","source":"X_train = X_train / 255\nX_test = X_test / 255","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"88fed518-ac7f-4fc1-a6a7-221ddcbfae8c","_cell_guid":"fd510a58-e1aa-4d9b-a976-ee59544cae47","trusted":true},"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Dense(256, activation='relu'),\n    keras.layers.Dropout(0.3),\n    keras.layers.Dense(256, activation='relu'),\n    keras.layers.Dropout(0.3),\n    keras.layers.Dense(512, activation='relu'),\n    keras.layers.Dense(512, activation='relu'),\n    keras.layers.Dense(10)\n])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d6c44dba-77ad-41e7-acaa-4218c97dd572","_cell_guid":"cbc93502-5255-4eff-83d1-be7d4f50c538","trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4770bbc5-91fa-4d6c-972a-522d832fa5e9","_cell_guid":"0281046b-9113-48a9-9aae-4ac63217a7f1","trusted":true},"cell_type":"code","source":"history = model.fit(X_train, y_train,\n                   epochs=30,\n                   batch_size=128, \n                   validation_data=(X_test, y_test))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ed68af6b-3dfb-48a5-a67a-44aa338b7a99","_cell_guid":"b79144bc-213d-42ef-ba69-9897eafcbe83","trusted":true},"cell_type":"code","source":"# summarize history for accuracy\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b527f22b-1ecf-4b04-bab0-2df23bf9d0de","_cell_guid":"29b6c068-88b6-450e-9c7c-1bf66aa672aa","trusted":true},"cell_type":"code","source":"# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = np.genfromtxt('../input/digit-recognizer/test.csv', delimiter=',', skip_header=1)\ntest","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cd61facd-9012-42d2-ba5e-279d95b4d267","_cell_guid":"f5b11e91-7ba1-4d15-907f-e1c2944b6e89","trusted":true},"cell_type":"code","source":"probability_model = tf.keras.Sequential([model, \n                                         tf.keras.layers.Softmax()])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c192b4fc-395c-4f3b-a751-8fe3ac83e053","_cell_guid":"5f53a75e-87de-4910-b9e0-7712411ccf95","trusted":true},"cell_type":"code","source":"predictions = probability_model.predict(test)\npredictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_prediction = probability_model.predict_classes(test)\nclass_prediction","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c420c429-9aa7-48ec-8025-258292df4722","_cell_guid":"7231afd5-838d-4bce-bb28-798e53a58d11","trusted":true},"cell_type":"code","source":"submission = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"69e3a36b-ef93-41b2-a174-fad452bf6937","_cell_guid":"d43ffb0b-2918-4d26-8423-5e0f8b21afaf","trusted":true},"cell_type":"code","source":"submission['Label'] = class_prediction\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.set_index('ImageId').to_csv('submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":4}