{"cells":[{"metadata":{"trusted":true,"_uuid":"194675b4ac5c7915a478ffa3a99e66b4e997ed5f"},"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1202991880d05caa4811961ee1f154faf04f89af"},"cell_type":"code","source":"mnist_train = pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fb729b40dab8a0ad1384740dd74072276df370b"},"cell_type":"code","source":"mnist_test = pd.read_csv('../input/test.csv')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10ba4ca6f923d97acb41ab8f1c66ebf3afbcbe82"},"cell_type":"code","source":"mnist_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ef9df624b80677f7228aaffadeb7d583cd29c02"},"cell_type":"code","source":"Xtrain = mnist_train.drop('label',axis=1) #X","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3b637a645ce00307d6e1a61664ae66a867e466f"},"cell_type":"code","source":"ytrain = mnist_train['label']             #y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f99b8f265777f65ce2f1a2376dfccf625b6a73d2"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74991e4fe6eb07e9f99db7496b4234d5c9392d51"},"cell_type":"code","source":"Xtrain=Xtrain/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"91c71bc3e69bcca0dad38c50fcd6cbf148d6452a"},"cell_type":"code","source":"Xtest = mnist_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63eef7dbd08fb94cf89c7ad3bd5cb57bd1b97c65"},"cell_type":"code","source":"Xtest = Xtest/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"386c78a4d8d7245713051e179687bd4f00a72122"},"cell_type":"code","source":"Xtrain = Xtrain.values.astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d2cd507eca644aa7a18f546f2367dec62b872c1"},"cell_type":"code","source":"Xtest = Xtest.values.astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01cd9c73ad37047138582b07e587452d8af90876"},"cell_type":"code","source":"plt.imshow(Xtest[4].reshape(28,28),cmap=plt.cm.binary)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d65875e2cb30b231a52d547d0beacb33d8da411e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c85d86abc4a8f4821c104e84ccad2cd3bc0d1fc"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aad576871dc54d3be33f131343a6d4d46da05598"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d05d20afa4a5db9df79aadd90eaef6158b6b8ffe"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"102dbeafffb21f264ebdf44053d6ea30a525623d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"192506e66ba913453d7b748b8f908b56c9fbe97d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a411ab84e698d4898aa6365d2dc577f33024882"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f87349da318b9ab70bb939f22a30dbcef1964d5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24b9e1416b6fda53b1cca7ee80f9110705eba0f1"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf57eb216f4fe0bb3729cbf003f59b8ba1868800"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9521a08e04e8fac6c24f1e5837e528f90b7b128f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95ed8c5e1a78551c1604dfcf0ed1e8e4d7b5a5bc"},"cell_type":"code","source":"Xtrain.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bfe2ba8cf8af9724154ed94725a7f8ee21973de3"},"cell_type":"code","source":"Xtrain = Xtrain.reshape(Xtrain.shape[0],28,28)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"45c0573ade9f9310b28e330699900b7e4590a817"},"cell_type":"code","source":"Xtrain.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58f26d3d43d4185fb2963eb58473e7f7a377c521"},"cell_type":"code","source":"Xtest = Xtest.reshape(Xtest.shape[0],28,28)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ad3d2f9a242911d3ea2787fd99d851dea3776e3"},"cell_type":"code","source":"Xtest.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9e97b3ffa267d27b5da1d849fefc9dc99cc9280"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"458ec4e5aa0dd8677494d231fdf2d02b93f3a3f0"},"cell_type":"code","source":"Xtrain[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7e42267152eb709ca92c02a6a6d392184431691"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"23382e9f16a99dae676dde12e30f8622447010af"},"cell_type":"code","source":"model = tf.keras.Sequential()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3fa66180bbe80e9252b15ed108e10bed04b777e9"},"cell_type":"code","source":"model.add(tf.keras.layers.Flatten())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42dfe2c5d33e2d2de1b1434aa06fea60307fba31"},"cell_type":"code","source":"model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f111d8564e3c2d1c5353571edfd2a2ea408cf065"},"cell_type":"code","source":"model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9c75a2f5ae4c8f3fd44d044fc021d56dfb8037b"},"cell_type":"code","source":"model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fcc5c52f48f9c7da6723b3a231af4cdc7355d896"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e137b4dc415508ab617c4d515ec91672afaa8fac"},"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb737c5ec7024145f417bdcb064ff0b7ce010468"},"cell_type":"code","source":"ytrain = ytrain.values.astype('uint8')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e41d6aebb982ec16c2cf2d1eb9d2db387978e87"},"cell_type":"code","source":"ytrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2cb0f06b559ab164c9b97cb1cffe4e709fdedc60"},"cell_type":"code","source":"model.fit(x=Xtrain,y=ytrain,batch_size=128,epochs=8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed39bdcf50678f85b5d769d2c0836f109edeff42"},"cell_type":"code","source":"plt.imshow(Xtest[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b1bae3289426ba68c3f5d7bdb6ae7bb32e4baa2"},"cell_type":"code","source":"Xtest","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"245ee613af6e397f9492f23e4a19017a7d98b509"},"cell_type":"code","source":"plt.imshow(Xtest[27])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"65b99878c47cdb8e2a686630f72330619c1be7ab"},"cell_type":"code","source":"predictions = model.predict([Xtest])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8267888e5d5a319ddf5b27703f261087ad1abf94"},"cell_type":"code","source":"import numpy as np\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bafb093f6df0b5b8f38cb24dc698aaa16b4f22fa"},"cell_type":"code","source":"np.argmax(predictions[27])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"745998dc0fcbf539756d90acd5a0c2afac060700"},"cell_type":"code","source":"len(predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11670e074cd76ce50f0ad8e4abfd71464ab52aa3"},"cell_type":"code","source":"Label=[]\nfor array in predictions:\n    Label.append(np.argmax(array))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67d73fc0206dda6462c4d269fbd9973d59080f90"},"cell_type":"code","source":"Label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c001a8fc2f5ee6e462a518ef561783a47f01c62"},"cell_type":"code","source":"pred=pd.DataFrame({'Label':Label})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9bec6cc78a34c67640bfa68f8947caab551c83c"},"cell_type":"code","source":"idx=pd.DataFrame({'ImageId':range(1,len(pred)+1)})\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00904a0adbec0fcbc123964f2147ee7b6e8e6970"},"cell_type":"code","source":"OUTPUT_RESULT=\"submission.csv\"\nsubmission=pd.concat([idx,pred],axis=1)\nsubmission.to_csv(OUTPUT_RESULT,index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd71188cbdb4b30aa34863e997168eba120f8856"},"cell_type":"code","source":"submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"a13a222b75587038d6c025e9b4616f66ea370c1a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.5"}},"nbformat":4,"nbformat_minor":1}