{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"x_train=pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c91a0333ea1dde1f40dfe207551ed645a7158bef"},"cell_type":"code","source":"x_test_data=pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3aada07b0b0d5245c2e9cef0f4f94e80d03317e2"},"cell_type":"code","source":"x_train.shape, x_test_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fdd453bd4d99f461c6941e78fd56e06f5ead0ee2"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"425e10513e827eb608eeb223e4c8b0756d685a29"},"cell_type":"code","source":"x=x_train.iloc[:,1:785]\ny=x_train.iloc[:,0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92bc906a3822e521222d7377e59ef705ab49666b"},"cell_type":"code","source":"x.shape,y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29c9c5d2388846a05b0d133f47fc4b665946e493"},"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.10, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16dfeb6ba8b958c62f7c415773fe1fdcbada0bd4"},"cell_type":"code","source":"x_train.shape,x_test.shape,y_train.shape,y_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d982e2f796607818b6796048274d3b51dcefb4b9"},"cell_type":"code","source":"x_train=x_train.values.reshape(37800,28,28,1)\nx_test=x_test.values.reshape(4200,28,28,1)\n#x_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c516c45aef947715b56b6ce2246ff7637215a47c"},"cell_type":"code","source":"x_train=x_train/255\nx_test=x_test/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c625b8560f9548dc498dd054ddb099674ea6d69"},"cell_type":"code","source":"#from tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten\nfrom keras.utils import to_categorical \nfrom keras.constraints import max_norm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"158fe1cd99fb24d989d92be5039913b0c1bae972"},"cell_type":"code","source":"classes=10\ny_train=to_categorical(y_train,num_classes = classes)\ny_test=to_categorical(y_test,num_classes = classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7bd973a75dd0f55e0529194bb7e2ffda6180dff7","scrolled":true},"cell_type":"code","source":"y_test.shape, y_train.shape,x_train.shape,x_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a20046c6f28f010f66c05be21d61a65b4c9316f5"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1),padding='Same'))\nmodel.add(Conv2D(32, kernel_size=(5, 5), activation='relu',padding='Same'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(64, kernel_size=(3, 3), activation='relu',padding='Same'))\nmodel.add(Conv2D(64, kernel_size=(3, 3), activation='relu',padding='Same'))\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\n#model.add(Dense(512, activation='relu',kernel_constraint=max_norm(3)))\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(classes, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f11cdc6b79dd4590728357ecfb2f780e970fd068"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b797462d70045e21e9ad3e8e34a404f90b94f782"},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f33c45dd4a10195c0c67bef9daa3e6c0dba3a017"},"cell_type":"code","source":"model.fit(x_train,y_train,batch_size=64,epochs=30,verbose=1,validation_data=(x_test,y_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"02f3fc94dc51d290a8032c22aafd61ea8f07af00"},"cell_type":"code","source":"x_test_data=x_test_data.values.reshape(28000,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08801c01be675427a21b24e33b9b858e95573c5a"},"cell_type":"code","source":"results = model.predict(x_test_data,batch_size=128,verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3098410bc3d9fee9ac33746f9424187d7cde4ce2"},"cell_type":"code","source":"results.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"881a9a7f244c2a2f49195c4be519de9684ab3acf"},"cell_type":"code","source":"results","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47231f32c76899006c8043c3ecbc43508cadc3c0"},"cell_type":"code","source":"results = np.argmax(results,axis = 1)\n\n#results = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b260400c06972d8d79796c55d59eafb8cdda560"},"cell_type":"code","source":"results","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e2e9b7ed8c821de1c85f8e6b0390f1c572d2313"},"cell_type":"code","source":"results.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"542354724878940124168f254805a2ea18f29af0"},"cell_type":"code","source":"results = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a1c2dc2eb3f6c909335411eb902a0da57bddd0e"},"cell_type":"code","source":"results","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"969c17c683bc63cfa160084f32bed64eb0eb51ff"},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e22907f2cfc0996dd9b92182439d3f2412b5f075"},"cell_type":"code","source":"submission.to_csv(\"Avinash.csv\", index=False, header=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cace683a51de7500a5bd2d8900c381468030e16"},"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.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}