{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Load the data\ntrain_data = pd.read_csv(\"../input/train.csv\")\ntest_data = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_data = train_data['label']\ny_data = pd.get_dummies(y_data)\nX_data = train_data.drop('label',axis=1)\ndel train_data\nplt.imshow(X_data.iloc[3].values.reshape(28,28), cmap='gist_gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Normalize\nX_data = X_data / 255.0\ntest_data = test_data / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = tf.placeholder(tf.float32,shape=[None,784])\nW = tf.Variable(tf.zeros([784,10]))\nb = tf.Variable(tf.zeros([10]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create the Graph\ny = tf.matmul(x,W) + b ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_true = tf.placeholder(tf.float32,[None,10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true, logits=y))\noptimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)\ntrain = optimizer.minimize(cross_entropy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"init = tf.global_variables_initializer()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create training and validation data\nX_train, X_valid, y_train, y_valid = train_test_split(X_data, y_data, test_size=0.25, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epoch_accuracies = []\noutput = pd.read_csv(\"../input/sample_submission.csv\")\n\n# Model\nwith tf.Session() as sess:\n    sess.run(init)\n    start, end = 0, 0\n    for epochs in range(100):\n        for step in range(63):\n            start = step*500\n            end = (1+step)*500\n            batch_x , batch_y = X_train.iloc[start:end,], y_train.iloc[start:end,]\n            \n            sess.run(train,feed_dict={x:batch_x,y_true:batch_y})\n\n        # Test the Train Model\n        matches = tf.equal(tf.argmax(y,1),tf.argmax(y_true,1))\n        print('.',end=\" \")\n        acc = tf.reduce_mean(tf.cast(matches,tf.float32))\n        epoch_accuracy = sess.run(acc,feed_dict={x:X_valid,y_true:y_valid})\n        epoch_accuracies.append(epoch_accuracy) \n    print (\"Train Accuracy:\", acc.eval({x: X_train, y_true: y_train}))\n\n    \n    # Plot the accuracy\n    plt.plot(np.squeeze(epoch_accuracies))\n    plt.ylabel('accuracy')\n    plt.xlabel('iterations')\n    plt.show()\n    \n    # Make predictions on test data\n    prediction = sess.run(y, feed_dict={x: test_data})\n    output['Label'] = tf.argmax(prediction,1).eval()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Adapted from the course \"Python for Data Science\" on Udemy."},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}