{"cells":[{"metadata":{"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\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 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","execution_count":null,"outputs":[]},{"metadata":{"id":"5xgl7dGWBvbB","trusted":false},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{"id":"Lk1XOJjgB9JS","trusted":false},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"SBelMdxLCHQl","outputId":"6828b344-ab6a-48b1-bf0e-fccd602a7ce2","trusted":false},"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","execution_count":null,"outputs":[]},{"metadata":{"id":"1IQAeBhbCMXX","trusted":false},"cell_type":"code","source":"Y_train = train.label\nX_train = train.drop(labels = [\"label\"],axis = 1) ","execution_count":null,"outputs":[]},{"metadata":{"id":"PeoO_6OoCjhc","outputId":"f45ea800-1b67-41cd-b522-7522b742800f","trusted":false},"cell_type":"code","source":"Y_train.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"hrDT_rBrCZ0K","outputId":"88244fa8-40b7-43c3-936e-56577d58a327","trusted":false},"cell_type":"code","source":"# afficher quelques exemples\nimg = X_train.iloc[0].to_numpy() \nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[0,0])\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"DGJx2PBVCoit","outputId":"7a99116f-3283-40bb-ef1a-ac9635257b9c","trusted":false},"cell_type":"code","source":"img = X_train.iloc[10].to_numpy() \nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[0,0])\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"_VEdg-pdDM9g","trusted":false},"cell_type":"code","source":"# Normaliser les données\nX_train = X_train / 255.0\ntest = test / 255.0","execution_count":null,"outputs":[]},{"metadata":{"id":"iVOtrAYwDToI","trusted":false},"cell_type":"code","source":"#redimensionner les données sous forme des images 28 * 28\nX_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"id":"c7HrMYGXDdvt","trusted":false},"cell_type":"code","source":"#encoder Y_train (10 catégories)\nfrom keras.utils.np_utils import to_categorical \nY_train = to_categorical(Y_train, num_classes = 10)","execution_count":null,"outputs":[]},{"metadata":{"id":"99Z5D28uDpvk","trusted":false},"cell_type":"code","source":"#splitter les données :\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.15, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"id":"D1t81IVHD4Cc","trusted":false},"cell_type":"code","source":"#préparer le CNN","execution_count":null,"outputs":[]},{"metadata":{"id":"4Xr_fq8sD9_S","trusted":false},"cell_type":"code","source":"#les importations nécessaires\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau","execution_count":null,"outputs":[]},{"metadata":{"id":"cw5jIoMbEGxn","trusted":false},"cell_type":"code","source":"#CNN :\n\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 8, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 16, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"id":"yXBplOteESH4","trusted":false},"cell_type":"code","source":"# Défénir un \"optimizer\"\noptimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)","execution_count":null,"outputs":[]},{"metadata":{"id":"7jeSplT3East","trusted":false},"cell_type":"code","source":"# Compiler le modéle\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"id":"HJLtWy-WEiJJ","trusted":false},"cell_type":"code","source":"epochs = 10  \n#on peut prendre un nombre d'epochs plus grand pour une meilleure résultat\nbatch_size = 256\n#on peut l'agmenter pour diminuer le nombre des étapes par \"epoch\" et rendre l'entrainement de CNN plus vite","execution_count":null,"outputs":[]},{"metadata":{"id":"2g9jVpuiE-dq","trusted":false},"cell_type":"code","source":"#pour plus de précision et augmenter la taille des données\n\ndatagen = ImageDataGenerator(  \n        rotation_range=5,  \n        zoom_range = 0.1, \n        width_shift_range=0.1,  \n        height_shift_range=0.1)\n#on peut choisir d'autres paramétres ou utiliser d'autres pour optimiser le résultat\ndatagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"id":"6s3jaMSpFli4","outputId":"3ba52aae-6c15-45d1-e156-8607ecbefd87","trusted":false},"cell_type":"code","source":"#Fit\nhistory = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (X_val,Y_val), steps_per_epoch=X_train.shape[0] // batch_size)","execution_count":null,"outputs":[]},{"metadata":{"id":"Y-LAVAmpGBzR","trusted":false},"cell_type":"code","source":"Y_pred = model.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"id":"BqF7up8vIlyH","trusted":false},"cell_type":"code","source":"Y_pred_classes = np.argmax(Y_pred,axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"id":"NfwLD_1kHceu","trusted":false},"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"PLOPeP6aHlmr","outputId":"9460a242-0c17-4466-94ff-d0c6d84b94c8","trusted":false},"cell_type":"code","source":"sub.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"8_gMXYoLHo9M","trusted":false},"cell_type":"code","source":"submission = pd.DataFrame()\nsubmission['ImageId'] = sub.ImageId\nsubmission['Label'] = Y_pred_classes\n\nsubmission.to_csv('model.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"id":"u7TJ1xpZITRz","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}