{"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\nimport keras\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras import layers\nfrom keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, MaxPool2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D\nfrom keras.models import Model\nfrom keras.models import Sequential\nfrom keras.preprocessing import image\nfrom keras.utils import layer_utils\nfrom keras import regularizers\nfrom keras.utils.data_utils import get_file\nfrom keras.applications.imagenet_utils import preprocess_input\nfrom keras.utils.vis_utils import model_to_dot\nfrom keras.utils import plot_model\nfrom keras.utils import to_categorical\nfrom keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop\nfrom keras.callbacks import ReduceLROnPlateau, LearningRateScheduler\nimport keras.backend as K\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing.image import ImageDataGenerator\nK.set_image_data_format('channels_last')\nfrom matplotlib.pyplot import imshow\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\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":{"trusted":true},"cell_type":"code","source":"#Cargamos los Datos y verificamos\ntrain = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')\ndf = train.copy()\ndf_test = test.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Train: \", df.shape)\nprint(\"Test: \", df_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Verificamos que no existan valores nulos en el train\ndf.isnull().any().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Verificamos que no existan valores nulos en el test\ndf_test.isnull().any().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Conversión de el Data Frame a una matriz numpy\nx = train.drop(labels = [\"label\"],axis = 1)\ny = train[\"label\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Hacems One Hot Encoding\n# y=to_categorical(y,10) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Verificamos que se lean los numeros, podemos cambiar el numero de muestra alterando el numero dentro de \nx = np.array(x)\ne =x[10005]\nimage = e.reshape(28,28)\nplt.imshow(image, cmap = plt.cm.binary,\n           interpolation = 'nearest')\nplt.axis('off')\nplt.show()\nprint(y[10005])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xtrain, xtest, ytrain, ytest = train_test_split(x,y, test_size = 0.2, random_state = 123)\n\nxtrain.shape, xtest.shape, ytrain.shape, ytest.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xtrain = xtrain.reshape(-1, 28, 28, 1)\nxtest = xtest.reshape(-1, 28, 28, 1)\ntest = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Normalizamos los datos\nxtrain = xtrain.astype(\"float32\")/255\nxtest = xtest.astype(\"float32\")/255\ntest = test.astype(\"float32\")/255\nytrain = to_categorical(ytrain, num_classes=10)\nytest = to_categorical(ytest, num_classes=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Modelo Convolutional Neural Network usando Keras\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32,\n    kernel_size = (3,3),padding = 'Same', activation ='relu', \n    input_shape = (28,28,1)))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), padding = 'Same',  activation ='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(10, activation = \"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Optimizamos\noptimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999 )\n# Compilamos el modelo\nmodel.compile(optimizer=optimizer, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n    # establecer la media de entrada en 0 sobre el conjunto de datos\n        featurewise_center=False, \n        samplewise_center=False,  \n        featurewise_std_normalization=False, \n        samplewise_std_normalization=False, \n        zca_whitening=False, \n    # rotar aleatoriamente las imágenes en el rango de 0 a 180 grados\n        rotation_range=10, \n    # Ampliar imagen al azar\n        zoom_range = 0.1,\n    # desplazar aleatoriamente las imágenes verticalmente y horizontalmente\n        width_shift_range=0.1,  \n        height_shift_range=0.1, \n    # voltear imágenes al azar\n        horizontal_flip=False,  \n        vertical_flip=False) \ndatagen.fit(xtrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 128\nepochs = 50\nreduce_lr = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)\n\nhistory = model.fit_generator(datagen.flow(xtrain, ytrain, batch_size = batch_size), epochs = epochs, \n                              validation_data = (xtest, ytest), verbose=2, \n                              steps_per_epoch=xtrain.shape[0] // batch_size,\n                              callbacks = [reduce_lr])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate(xtest, ytest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate(xtrain, ytrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Graficamos\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('Model Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend(['Train','Test'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Graficamos\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epochs')\nplt.legend(['train', 'test'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Cargamos los datos\ntestprediction=np.argmax(model.predict(test),axis=1)\ntestimage=[]\nfor i in range (len(testprediction)):\n    testimage.append(i+1)\nfinal={'ImageId':testimage,'Label':testprediction}\nsubmission=pd.DataFrame(final)\nsubmission.to_csv('submssion.csv',index=False)","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}