{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"508c8a6e297e077012e275687100b3d5e45992b8"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Lambda, Flatten\nfrom keras.optimizers import Adam, RMSprop\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d412b371213c4f7487a6902fa6ab14a7aaf2640"},"cell_type":"code","source":"np.random.seed(2)\nfrom random import randint\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d266632e18f40db107f3d7521ed8e2731f57098"},"cell_type":"code","source":"import matplotlib.pyplot as plt\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 = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01323e08a75ed63e2a7ad1e90420d8665bdfd5fc"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8911fa1e44f70088fe489f6af2745a0f6a342e76"},"cell_type":"code","source":"Y_train = train[\"label\"]\nX_train = train.drop(\"label\",axis = 1)\nX_test = test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f816f2f1d656a40430089a637af3d3710721037"},"cell_type":"code","source":"Y_train.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d42f0fcab8e5084afe15d59406bbcc3078212f11"},"cell_type":"code","source":"X_train.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"660294532fd042d6de80f82ccb9fea57f3eb21cc"},"cell_type":"code","source":"X_test.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97b47498f7e46f910d44c57592d19d29f485d614"},"cell_type":"code","source":"#Normalize the data\n\n# Reshape image in 3 dimensions (height = 28px, width = 28px)\n\nX_train = X_train.values.reshape(-1,28*28)\nX_train = (X_train.astype('float32') - 128)/128\n\nX_test = X_test.values.reshape(-1,28*28)\nX_test = (X_test.astype('float32') - 128)/128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"596ec050c7685a9b621b9402ef559de0efb616b8"},"cell_type":"code","source":"X_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ff8e95b469811b44a9da697b35252d9caabccbb"},"cell_type":"code","source":"# Encode labels to one hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])\nY_train = to_categorical(Y_train, num_classes = 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43fbda3e170083f93b71921d2e485c7162a8ce97"},"cell_type":"code","source":"# Set the random seed\nrandom_seed = 2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b1458a3aa457a4d56286e61e10d7fcc168d496c"},"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X_train,Y_train, test_size = 0.1, random_state = random_seed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2519cd5c3e0e7bb21540dcddcfe0b4613d2aac96"},"cell_type":"code","source":"#plt.imshow(X_train[randint(0, X_train.shape[0]-1)][:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b532a7e546b040a2cf2270367335053851b32478"},"cell_type":"code","source":"#del network","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d1a02ad521629e67a13ad04e0180c68e7c969c4"},"cell_type":"code","source":"network = Sequential()\nnetwork.add(Dense(512, activation = 'relu', input_shape = (28*28,)))\nnetwork.add(Dense(10, activation = 'relu'))\nnetwork.add(Dense(10, activation = 'relu'))\nnetwork.add(Dense(10, activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85488e1e5d8e5d7de8012d3b4484df42632de317"},"cell_type":"code","source":"network.compile(optimizer = 'rmsprop',\n               loss = 'categorical_crossentropy',\n               metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"142fee7ab807c72b7bd94432567689757bc26e1c"},"cell_type":"code","source":"network.fit(X_train,Y_train, epochs = 10, batch_size = 128 )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6fff2aae604674ab84d68bcb9cd62957d849a3a6"},"cell_type":"code","source":"val_loss, val_acc = network.evaluate(X_val,Y_val)\nprint(\"val_acc:\", val_acc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"541419539bcf225f07fc1e17411137a6940c47f8"},"cell_type":"code","source":"predictions =  network.predict_classes(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1aeeeb4a7062928eab2b097c3411ef87edbde0af"},"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':range(1,predictions.shape[0]+1),'Label':predictions})\n\n#Visualize the first 5 rows\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbbc0ba5319154c1374ca5be81ffbfcc5154242b"},"cell_type":"code","source":"#Convert DataFrame to a csv file that can be uploaded\n#This is saved in the same directory as your notebook\nfilename = 'MNIST Predictions 1.csv'\n\nsubmission.to_csv(filename,index=False)\n\nprint('Saved file: ' + filename)","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}