{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom keras.datasets import mnist\nfrom keras.utils import np_utils\nfrom tensorflow.keras import layers\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-23T22:16:26.739243Z","iopub.execute_input":"2022-04-23T22:16:26.739578Z","iopub.status.idle":"2022-04-23T22:16:33.265866Z","shell.execute_reply.started":"2022-04-23T22:16:26.739497Z","shell.execute_reply":"2022-04-23T22:16:33.264866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_classes = 10\ndata_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndata_train = np.array(data_train)\ndata_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ndata_test = np.array(data_test)\n\ny_train, x_train = data_train[:,0], data_train[:, 1:]\nx_test = data_test","metadata":{"execution":{"iopub.status.busy":"2022-04-23T21:52:57.513241Z","iopub.execute_input":"2022-04-23T21:52:57.513801Z","iopub.status.idle":"2022-04-23T21:53:03.202002Z","shell.execute_reply.started":"2022-04-23T21:52:57.513748Z","shell.execute_reply":"2022-04-23T21:53:03.201172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalisation des entrées et reshape (Pixels entre 0 et 255 seront entre 0 et 1 maintenant)\nfrom tensorflow.keras.utils import to_categorical\n\ndef split(X, y, test_size):\n     idx = np.arange(X.shape[0])\n     np.random.shuffle(idx)\n     nb_test = int(test_size * X.shape[0])\n     return X[nb_test:,:, :, :], y[nb_test:], X[:nb_test, :, :, :], y[:nb_test]\n\nx_train = x_train.astype('float32') / 255.\nx_test = x_test.astype('float32') / 255.\n\n# Reshape to original format\nx_train = x_train.reshape((x_train.shape[0], 28, 28, 1))\nx_test = x_test.reshape((x_test.shape[0], 28, 28, 1))\n\nx_train, y_train, x_val, y_val = split(x_train, y_train,test_size=0.1)\n\nx_val = x_val.reshape((x_val.shape[0], 28, 28, 1))\n\ny_train = to_categorical(y_train, nb_classes)\ny_val = to_categorical(y_val, nb_classes)\n\nprint (x_train.shape)\nprint (x_val.shape)\nprint (y_train.shape)\nprint (y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-23T21:53:05.924142Z","iopub.execute_input":"2022-04-23T21:53:05.924689Z","iopub.status.idle":"2022-04-23T21:53:06.07032Z","shell.execute_reply.started":"2022-04-23T21:53:05.92463Z","shell.execute_reply":"2022-04-23T21:53:06.069364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(shear_range=0.1,zoom_range=0.2,rotation_range=10, width_shift_range=0.1, height_shift_range=0.1)\n#datagen = ImageDataGenerator(shear_range=0.1,zoom_range=0.2,rotation_range=20, width_shift_range=0.1, height_shift_range=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-04-23T21:53:14.895053Z","iopub.execute_input":"2022-04-23T21:53:14.895467Z","iopub.status.idle":"2022-04-23T21:53:14.901905Z","shell.execute_reply.started":"2022-04-23T21:53:14.895414Z","shell.execute_reply":"2022-04-23T21:53:14.901027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import from Keras\n\nfrom keras.layers import Input, Dense, Dropout, Flatten\nfrom keras.models import Model, Sequential\nfrom keras import metrics","metadata":{"execution":{"iopub.status.busy":"2022-04-23T21:53:17.526232Z","iopub.execute_input":"2022-04-23T21:53:17.526566Z","iopub.status.idle":"2022-04-23T21:53:17.531465Z","shell.execute_reply.started":"2022-04-23T21:53:17.526528Z","shell.execute_reply":"2022-04-23T21:53:17.530536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Flatten(input_shape=(x_train.shape[1:])))\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.2))\n# Dernière couche avec un noeud pour chaque classe finale\nmodel.add(Dense(10, activation='softmax'))\n\n\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['acc'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-23T21:53:20.095003Z","iopub.execute_input":"2022-04-23T21:53:20.095662Z","iopub.status.idle":"2022-04-23T21:53:20.181312Z","shell.execute_reply.started":"2022-04-23T21:53:20.095619Z","shell.execute_reply":"2022-04-23T21:53:20.18028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Entrainement du modele\n\ntrain_flow = datagen.flow(x_train, y_train, batch_size=128)\nmodel.fit(train_flow, steps_per_epoch=250, validation_data=(x_val, y_val), epochs=15)","metadata":{"execution":{"iopub.status.busy":"2022-04-23T21:53:24.575091Z","iopub.execute_input":"2022-04-23T21:53:24.575401Z","iopub.status.idle":"2022-04-23T21:57:20.692538Z","shell.execute_reply.started":"2022-04-23T21:53:24.575368Z","shell.execute_reply":"2022-04-23T21:57:20.691365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Label_pre = model.predict(x_test, batch_size=128)\nLabel_pre = np.argmax(Label_pre, axis = -1)\nList_x = range(1,28001)\nsub = pd.DataFrame({ 'ImageId': List_x, 'Label': Label_pre })\nsub.to_csv(\"Aluizio-Pires.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-23T21:57:52.37128Z","iopub.execute_input":"2022-04-23T21:57:52.37191Z","iopub.status.idle":"2022-04-23T21:57:53.196419Z","shell.execute_reply.started":"2022-04-23T21:57:52.371861Z","shell.execute_reply":"2022-04-23T21:57:53.195692Z"},"trusted":true},"execution_count":null,"outputs":[]}]}