{"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 PIL import Image\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\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\nsubm = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\ntrain_sample = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_sample = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Images are 28x28 black-or-white pixels","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":false,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2021-10-11T14:53:45.009366Z","iopub.execute_input":"2021-10-11T14:53:45.009820Z","iopub.status.idle":"2021-10-11T14:53:51.637839Z","shell.execute_reply.started":"2021-10-11T14:53:45.009702Z","shell.execute_reply":"2021-10-11T14:53:51.636972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sample.label.hist()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:53:51.639506Z","iopub.execute_input":"2021-10-11T14:53:51.639784Z","iopub.status.idle":"2021-10-11T14:53:51.937792Z","shell.execute_reply.started":"2021-10-11T14:53:51.639749Z","shell.execute_reply":"2021-10-11T14:53:51.936847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sample.describe()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:53:51.939317Z","iopub.execute_input":"2021-10-11T14:53:51.939579Z","iopub.status.idle":"2021-10-11T14:53:54.457751Z","shell.execute_reply.started":"2021-10-11T14:53:51.939550Z","shell.execute_reply":"2021-10-11T14:53:54.456723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\npixels = train_sample[train_sample.columns[1:]].iloc[0:10]\npixels = np.array(pixels)\npixels = np.reshape(pixels, (10, 28, 28))\npixels = pixels.astype(np.uint8)\npix = pixels[0]\nImage.fromarray(pix, 'L')\n\n\"\"\"\n_, axes_list = plt.subplots(h, w, figsize=(2*w, 2*h)) # define a grid of (w, h)\n\nfor axes in axes_list:\n    for ax in axes:\n        ax.axis('off')\n        img = np.random.choice(train) # take a random train filename (like 000c34352.jpg)\n        ax.imshow(load_img(img)) # load and show\n        ax.set_title(img)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:53:54.459795Z","iopub.execute_input":"2021-10-11T14:53:54.460098Z","iopub.status.idle":"2021-10-11T14:53:54.611494Z","shell.execute_reply.started":"2021-10-11T14:53:54.460064Z","shell.execute_reply":"2021-10-11T14:53:54.610643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(zip(train_sample.label, np.histogram(train_sample.label)[0]))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:53:54.613038Z","iopub.execute_input":"2021-10-11T14:53:54.613515Z","iopub.status.idle":"2021-10-11T14:53:54.629238Z","shell.execute_reply.started":"2021-10-11T14:53:54.613475Z","shell.execute_reply":"2021-10-11T14:53:54.628267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nimport matplotlib.pyplot as plot\n\n# On sélectionne seulement les pixels\npixels = train_sample[train_sample.columns[1:]]\n\n# On normalise les valeurs des pixels z = (x - moyenne)/ecart-type\npixels = StandardScaler().fit_transform(pixels)\n\npca = PCA(n_components=784)  # 28x28 pixels\npca.fit(pixels)\n\npercentage_var_explained = pca.explained_variance_ / np.sum(pca.explained_variance_);\ncum_var_explained = np.cumsum(percentage_var_explained)\n\nplot.figure(1, figsize=(6, 4))\nplot.clf()\nplot.plot(cum_var_explained, linewidth=2)\nplot.axis('tight')\nplot.grid()\nplot.xlabel('n_components')\nplot.ylabel('Cumulative explained variance')\nplot.show()\n\nplot.plot(pca.explained_variance_ratio_)\nplot.ylabel('Explained Variance')\nplot.xlabel('Components')\nplot.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:53:54.630867Z","iopub.execute_input":"2021-10-11T14:53:54.631357Z","iopub.status.idle":"2021-10-11T14:54:03.709489Z","shell.execute_reply.started":"2021-10-11T14:53:54.631306Z","shell.execute_reply":"2021-10-11T14:54:03.708795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot.imshow(train_sample.iloc[4,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:54:03.711409Z","iopub.execute_input":"2021-10-11T14:54:03.712047Z","iopub.status.idle":"2021-10-11T14:54:03.949059Z","shell.execute_reply.started":"2021-10-11T14:54:03.711976Z","shell.execute_reply":"2021-10-11T14:54:03.948024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Séparation de l'échantillon en ensemble d'entraînement, de test, et de validation","metadata":{}},{"cell_type":"code","source":"#df = train_sample\n#N = len(df)\n#s_train, s_validate, s_test =  np.split(df.sample(frac=1), [int(.75 * N), int(.9 * N)])\n#print('Taille des ensembles:', 'train =', s_train.shape[0], 'validate =', s_validate.shape[0], 'test =', s_test.shape[0])","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:54:35.560679Z","iopub.execute_input":"2021-10-11T14:54:35.561186Z","iopub.status.idle":"2021-10-11T14:54:35.565507Z","shell.execute_reply.started":"2021-10-11T14:54:35.561147Z","shell.execute_reply":"2021-10-11T14:54:35.564510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndf = train_sample\nX, y = df[df.columns[1:]], df.label\n\nX = X / 255.0 # normalisation des valeurs des pixels\n\nX_train, X_test, y_train, y_test = train_test_split(X.values, y.values, train_size=0.7, random_state=42)\n# X_test, X_test, y_test, y_test = train_test_split(X, y, train_size=0.7, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:54:37.326934Z","iopub.execute_input":"2021-10-11T14:54:37.327915Z","iopub.status.idle":"2021-10-11T14:54:38.208379Z","shell.execute_reply.started":"2021-10-11T14:54:37.327853Z","shell.execute_reply":"2021-10-11T14:54:38.207530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Taille des ensembles:', 'train =', len(X_train), ' | test = ', len(X_test))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:57:44.770219Z","iopub.execute_input":"2021-10-11T14:57:44.770897Z","iopub.status.idle":"2021-10-11T14:57:44.776681Z","shell.execute_reply.started":"2021-10-11T14:57:44.770848Z","shell.execute_reply":"2021-10-11T14:57:44.775968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Entraînement du modèle (SVM)","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVC\n\nsvc = SVC(kernel='poly') # linear, poly, rbf (≈ gaussienne), ...\nsvc.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T15:09:21.201591Z","iopub.execute_input":"2021-10-11T15:09:21.201970Z","iopub.status.idle":"2021-10-11T15:13:01.244827Z","shell.execute_reply.started":"2021-10-11T15:09:21.201931Z","shell.execute_reply":"2021-10-11T15:13:01.243682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Évaluation du modèle","metadata":{}},{"cell_type":"code","source":"from sklearn import metrics\ny_pred = svc.predict(X_test)\nprecision = metrics.accuracy_score(y_true=y_test, y_pred=y_pred)\nprint(f\"Précision = {100*precision:f} %\")","metadata":{"execution":{"iopub.status.busy":"2021-10-11T15:13:23.391959Z","iopub.execute_input":"2021-10-11T15:13:23.393007Z","iopub.status.idle":"2021-10-11T15:15:05.072055Z","shell.execute_reply.started":"2021-10-11T15:13:23.392946Z","shell.execute_reply":"2021-10-11T15:15:05.070848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = X_test[0]\nprint(\"Prédiction:\", svc.predict([x])[0])\nplot.imshow(x.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T15:15:23.567246Z","iopub.execute_input":"2021-10-11T15:15:23.567584Z","iopub.status.idle":"2021-10-11T15:15:23.813164Z","shell.execute_reply.started":"2021-10-11T15:15:23.567552Z","shell.execute_reply":"2021-10-11T15:15:23.811879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}