{"cells":[{"metadata":{},"cell_type":"markdown","source":"# MNIST Digit Recognition\n\nBased on @[Sentdex](https://www.youtube.com/channel/UCfzlCWGWYyIQ0aLC5w48gBQ) [tutorial](https://pythonprogramming.net/introduction-deep-learning-python-tensorflow-keras/)","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Importing libraries\n\n# Machine Learning\nimport tensorflow as tf\n\n# Plotting\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec\n\n# Data Preparation\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(r'../input/digit-recognizer/train.csv')\nkaggle_test = pd.read_csv(r'../input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split data into X and y\nX = train[[col for col in train.columns if 'pixel' in col]]\ny = train['label']\n\n# Split data into Train and Test\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\nX_train.shape, X_test.shape, y_train.shape, y_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Preview the data\n\nplt.figure(figsize = (4,4))\ngs1 = gridspec.GridSpec(4, 4)\ngs1.update(wspace=0.025, hspace=0.05) # set the spacing between axes. \n\nfor i in range(16):\n    a = X_train.sample(1).values.reshape(28, 28)\n    \n    ax1 = plt.subplot(gs1[i])\n    ax1.set_xticks([])\n    ax1.set_yticks([])\n    ax1.imshow(a, cmap=plt.cm.binary)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = tf.keras.utils.normalize(X_train, axis=1)\nX_test = tf.keras.utils.normalize(X_test, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"avg_values = X_train.mean().values.reshape(28, 28)\n\nplt.imshow(avg_values, cmap=plt.cm.binary)\nplt.title('Average Values of Training Data');","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Modeling","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.models.Sequential()\n#model.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) # relu is the go-to activation function for hidden layer\nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))\n\nmodel.compile(optimizer='adam', # adam is the go-to optimizer\n                loss='sparse_categorical_crossentropy',\n                metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X_train, y_train, epochs=30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_loss, val_acc = model.evaluate(X_test, y_test)\nprint(val_loss, val_acc)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Predicting","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"output = model.predict(X_test)\npredictions = np.argmax(output, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Predicted {predictions[0]}\\nActual Value {y_test.head(1).values[0]}')\nplt.imshow(X_test.head(1).values.reshape(28,28), cmap=plt.cm.binary);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Kaggle Submission","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"kaggle_test = tf.keras.utils.normalize(kaggle_test, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kaggle_output = model.predict(kaggle_test)\nkaggle_predictions = np.argmax(kaggle_output, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kaggle_predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kaggle_submission = pd.DataFrame({'ImageId': range(1,kaggle_test.shape[0]+1), 'Label': kaggle_predictions})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kaggle_submission.to_csv('Submission.csv', index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}