{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":10737,"databundleVersionId":290346,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-24T20:07:29.659896Z","iopub.execute_input":"2024-04-24T20:07:29.660265Z","iopub.status.idle":"2024-04-24T20:07:30.020822Z","shell.execute_reply.started":"2024-04-24T20:07:29.660235Z","shell.execute_reply":"2024-04-24T20:07:30.019948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import librairies","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport re \nimport nltk\nfrom nltk.corpus import stopwords\nfrom nltk.stem import SnowballStemmer\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Embedding, GlobalMaxPooling1D, Bidirectional, LSTM\nfrom tensorflow.keras.optimizers import SGD\n\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\n\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\n\nfrom imblearn.under_sampling import RandomUnderSampler\n\nfrom zipfile import ZipFile","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:07:33.854540Z","iopub.execute_input":"2024-04-24T20:07:33.855339Z","iopub.status.idle":"2024-04-24T20:07:46.683258Z","shell.execute_reply.started":"2024-04-24T20:07:33.855302Z","shell.execute_reply":"2024-04-24T20:07:46.682272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')\ntest_data = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:07:48.703142Z","iopub.execute_input":"2024-04-24T20:07:48.703727Z","iopub.status.idle":"2024-04-24T20:07:54.635059Z","shell.execute_reply.started":"2024-04-24T20:07:48.703701Z","shell.execute_reply":"2024-04-24T20:07:54.634048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:07:57.492306Z","iopub.execute_input":"2024-04-24T20:07:57.492985Z","iopub.status.idle":"2024-04-24T20:07:57.511468Z","shell.execute_reply.started":"2024-04-24T20:07:57.492955Z","shell.execute_reply":"2024-04-24T20:07:57.510620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:00.121597Z","iopub.execute_input":"2024-04-24T20:08:00.122158Z","iopub.status.idle":"2024-04-24T20:08:00.390784Z","shell.execute_reply.started":"2024-04-24T20:08:00.122127Z","shell.execute_reply":"2024-04-24T20:08:00.389867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:02.967594Z","iopub.execute_input":"2024-04-24T20:08:02.967985Z","iopub.status.idle":"2024-04-24T20:08:02.973975Z","shell.execute_reply.started":"2024-04-24T20:08:02.967957Z","shell.execute_reply":"2024-04-24T20:08:02.973070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.target.value_counts().plot(kind='bar', title=\"Count of target label\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:05.653202Z","iopub.execute_input":"2024-04-24T20:08:05.653560Z","iopub.status.idle":"2024-04-24T20:08:05.973603Z","shell.execute_reply.started":"2024-04-24T20:08:05.653534Z","shell.execute_reply":"2024-04-24T20:08:05.972672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"# Get the length of sentence\ndef longueurText(text):\n    return len(text.split(\" \"))\n\ntrain_data['longueur'] = train_data.question_text.apply(longueurText)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:09.839526Z","iopub.execute_input":"2024-04-24T20:08:09.840219Z","iopub.status.idle":"2024-04-24T20:08:11.678333Z","shell.execute_reply.started":"2024-04-24T20:08:09.840186Z","shell.execute_reply":"2024-04-24T20:08:11.677558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['longueur'].min(), train_data['longueur'].max(), train_data['longueur'].mean()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:16.012694Z","iopub.execute_input":"2024-04-24T20:08:16.013167Z","iopub.status.idle":"2024-04-24T20:08:16.026833Z","shell.execute_reply.started":"2024-04-24T20:08:16.013130Z","shell.execute_reply":"2024-04-24T20:08:16.025568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(train_data['longueur'])","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:19.137733Z","iopub.execute_input":"2024-04-24T20:08:19.138535Z","iopub.status.idle":"2024-04-24T20:08:22.260529Z","shell.execute_reply.started":"2024-04-24T20:08:19.138504Z","shell.execute_reply":"2024-04-24T20:08:22.259625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_data.question_text.apply(len).plot(kind='hist', bins=50, title=\"Histogram of question length\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_len_sequence = 35 # max len based on right edge of 'text' histogram\nvocabulaire_size = 50000 # random choice","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:42.095971Z","iopub.execute_input":"2024-04-24T20:08:42.096335Z","iopub.status.idle":"2024-04-24T20:08:42.101126Z","shell.execute_reply.started":"2024-04-24T20:08:42.096304Z","shell.execute_reply":"2024-04-24T20:08:42.100075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## _Clean sentences","metadata":{}},{"cell_type":"code","source":"reg_text_cleaning = \"@\\S+|https?:\\S+|http?:\\S+|[^A-Za-z0-9]+\"","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:45.911574Z","iopub.execute_input":"2024-04-24T20:08:45.911952Z","iopub.status.idle":"2024-04-24T20:08:45.918410Z","shell.execute_reply.started":"2024-04-24T20:08:45.911897Z","shell.execute_reply":"2024-04-24T20:08:45.917636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nltk.download(\"stopwords\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:08:48.132193Z","iopub.execute_input":"2024-04-24T20:08:48.132558Z","iopub.status.idle":"2024-04-24T20:09:08.177295Z","shell.execute_reply.started":"2024-04-24T20:08:48.132533Z","shell.execute_reply":"2024-04-24T20:09:08.176361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stop_words = set(stopwords.words(\"english\"))","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:09:10.776713Z","iopub.execute_input":"2024-04-24T20:09:10.777556Z","iopub.status.idle":"2024-04-24T20:09:10.786242Z","shell.execute_reply.started":"2024-04-24T20:09:10.777522Z","shell.execute_reply":"2024-04-24T20:09:10.785315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#stemmer = SnowballStemmer(\"english\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_text(texte):\n    # retirer les liens et les caractères non alpha-numériques\n    texte = re.sub(reg_text_cleaning, \" \", str(texte).lower().strip())\n    \n    # retirer les stopwords\n    liste = []    \n    for mot in texte.split(\" \"):\n        if mot not in stop_words:\n            #mot_stem = stemmer.stem(mot)\n            #liste.append(mot_stem)\n            liste.append(mot)\n    \n    cleaned_texte = \" \".join(liste).strip()\n    return cleaned_texte","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:09:18.097351Z","iopub.execute_input":"2024-04-24T20:09:18.098218Z","iopub.status.idle":"2024-04-24T20:09:18.104084Z","shell.execute_reply.started":"2024-04-24T20:09:18.098185Z","shell.execute_reply":"2024-04-24T20:09:18.103060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"clean_text\"] = train_data[\"question_text\"].apply(clean_text)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:09:21.202272Z","iopub.execute_input":"2024-04-24T20:09:21.202621Z","iopub.status.idle":"2024-04-24T20:09:40.370873Z","shell.execute_reply.started":"2024-04-24T20:09:21.202596Z","shell.execute_reply":"2024-04-24T20:09:40.370078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:10:28.088845Z","iopub.execute_input":"2024-04-24T20:10:28.089760Z","iopub.status.idle":"2024-04-24T20:10:28.100809Z","shell.execute_reply.started":"2024-04-24T20:10:28.089725Z","shell.execute_reply":"2024-04-24T20:10:28.099844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## _Tokenizer","metadata":{}},{"cell_type":"code","source":"#tokenizer = Tokenizer(num_words=vocabulaire_size, oov_token=\"<OOV>\", filters='\"!\"#$%&()*+,-./:;<=>?@[\\\\]^_`{|}~\\t\\n\"')\ntokenizer = Tokenizer(num_words=vocabulaire_size, oov_token=\"<OOV>\")\ntokenizer.fit_on_texts(train_data['clean_text'])","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:10:31.429136Z","iopub.execute_input":"2024-04-24T20:10:31.429877Z","iopub.status.idle":"2024-04-24T20:10:51.428669Z","shell.execute_reply.started":"2024-04-24T20:10:31.429847Z","shell.execute_reply":"2024-04-24T20:10:51.427844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## _Padding","metadata":{}},{"cell_type":"code","source":"train_df = tokenizer.texts_to_sequences(train_data['clean_text'])\ntrain_df = pad_sequences(train_df, padding=\"post\", maxlen=max_len_sequence, truncating=\"post\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:04.776477Z","iopub.execute_input":"2024-04-24T20:11:04.777201Z","iopub.status.idle":"2024-04-24T20:11:28.090544Z","shell.execute_reply.started":"2024-04-24T20:11:04.777171Z","shell.execute_reply":"2024-04-24T20:11:28.089551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:32.803518Z","iopub.execute_input":"2024-04-24T20:11:32.804253Z","iopub.status.idle":"2024-04-24T20:11:32.809649Z","shell.execute_reply.started":"2024-04-24T20:11:32.804220Z","shell.execute_reply":"2024-04-24T20:11:32.808816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## _Randon under sampling","metadata":{}},{"cell_type":"code","source":"rus = RandomUnderSampler(random_state=42)\nX, y = rus.fit_resample(train_df, train_data[\"target\"].values)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:36.561757Z","iopub.execute_input":"2024-04-24T20:11:36.562628Z","iopub.status.idle":"2024-04-24T20:11:37.020953Z","shell.execute_reply.started":"2024-04-24T20:11:36.562596Z","shell.execute_reply":"2024-04-24T20:11:37.020161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:39.735946Z","iopub.execute_input":"2024-04-24T20:11:39.736286Z","iopub.status.idle":"2024-04-24T20:11:39.741888Z","shell.execute_reply.started":"2024-04-24T20:11:39.736261Z","shell.execute_reply":"2024-04-24T20:11:39.740924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yy = pd.DataFrame(y, columns=[\"target\"])\nyy.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:41.771497Z","iopub.execute_input":"2024-04-24T20:11:41.771880Z","iopub.status.idle":"2024-04-24T20:11:41.781095Z","shell.execute_reply.started":"2024-04-24T20:11:41.771849Z","shell.execute_reply":"2024-04-24T20:11:41.780132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yy.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:44.386571Z","iopub.execute_input":"2024-04-24T20:11:44.387227Z","iopub.status.idle":"2024-04-24T20:11:44.397963Z","shell.execute_reply.started":"2024-04-24T20:11:44.387195Z","shell.execute_reply":"2024-04-24T20:11:44.397029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yy.target.value_counts().plot(kind='bar', title=\"Count of target label\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:46.871321Z","iopub.execute_input":"2024-04-24T20:11:46.871672Z","iopub.status.idle":"2024-04-24T20:11:47.152203Z","shell.execute_reply.started":"2024-04-24T20:11:46.871646Z","shell.execute_reply":"2024-04-24T20:11:47.151283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## _Embedding","metadata":{}},{"cell_type":"code","source":"word_index = tokenizer.word_index\nlen(word_index)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:50.510747Z","iopub.execute_input":"2024-04-24T20:11:50.511396Z","iopub.status.idle":"2024-04-24T20:11:50.516817Z","shell.execute_reply.started":"2024-04-24T20:11:50.511364Z","shell.execute_reply":"2024-04-24T20:11:50.516110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_embedding = '/kaggle/input/quora-insincere-questions-classification/embeddings.zip'\n\nglove_file_name = \"glove.840B.300d/glove.840B.300d.txt\"\n\nwith ZipFile(pretrained_embedding, 'r') as z_file:\n    z_file.extract(glove_file_name, path=\"/kaggle/working/embeddings\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:11:53.674643Z","iopub.execute_input":"2024-04-24T20:11:53.675494Z","iopub.status.idle":"2024-04-24T20:12:49.534444Z","shell.execute_reply.started":"2024-04-24T20:11:53.675465Z","shell.execute_reply":"2024-04-24T20:12:49.533621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_file=\"/kaggle/working/embeddings/glove.840B.300d/glove.840B.300d.txt\"","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:12:58.447096Z","iopub.execute_input":"2024-04-24T20:12:58.448001Z","iopub.status.idle":"2024-04-24T20:12:58.452520Z","shell.execute_reply.started":"2024-04-24T20:12:58.447962Z","shell.execute_reply":"2024-04-24T20:12:58.451438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"word_index = tokenizer.word_index","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:13:00.701659Z","iopub.execute_input":"2024-04-24T20:13:00.702043Z","iopub.status.idle":"2024-04-24T20:13:00.706371Z","shell.execute_reply.started":"2024-04-24T20:13:00.702013Z","shell.execute_reply":"2024-04-24T20:13:00.705334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings_dict = dict()\n    \nf = open(embedding_file)\n\n# generate the embedding dictionnary\nfor line in f:\n    values = line.split(\" \")\n    word = values[0]\n    coefs = np.asarray(values[1:], dtype='float32')\n    embeddings_dict[word] = coefs\nf.close() ","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:13:04.426696Z","iopub.execute_input":"2024-04-24T20:13:04.427077Z","iopub.status.idle":"2024-04-24T20:15:53.643967Z","shell.execute_reply.started":"2024-04-24T20:13:04.427047Z","shell.execute_reply":"2024-04-24T20:15:53.643141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(embeddings_dict.keys())","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:20.466348Z","iopub.execute_input":"2024-04-24T20:16:20.466689Z","iopub.status.idle":"2024-04-24T20:16:20.474714Z","shell.execute_reply.started":"2024-04-24T20:16:20.466663Z","shell.execute_reply":"2024-04-24T20:16:20.473885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the mean and standard deviation of the embeddings weights\nall_embs = np.stack(list(embeddings_dict.values()))\nembed_mean = all_embs.mean()\nembed_std = all_embs.std()\nembed_size = all_embs.shape[1]","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:22.723576Z","iopub.execute_input":"2024-04-24T20:16:22.724184Z","iopub.status.idle":"2024-04-24T20:16:28.332895Z","shell.execute_reply.started":"2024-04-24T20:16:22.724137Z","shell.execute_reply":"2024-04-24T20:16:28.332103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embed_size","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:37.940429Z","iopub.execute_input":"2024-04-24T20:16:37.940778Z","iopub.status.idle":"2024-04-24T20:16:37.946381Z","shell.execute_reply.started":"2024-04-24T20:16:37.940753Z","shell.execute_reply":"2024-04-24T20:16:37.945415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create embedding matrix\n#embedding_matrix = np.random.normal(embed_mean,embed_std,(len(word_index)+1,embed_size))\nembedding_matrix = np.zeros((vocabulaire_size,embed_size))\n\nfor word, i in word_index.items():\n    if i>vocabulaire_size-1:\n        break\n    embedding_vector = embeddings_dict.get(word)\n    if embedding_vector is not None: \n        embedding_matrix[i] = embedding_vector","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:40.193858Z","iopub.execute_input":"2024-04-24T20:16:40.194248Z","iopub.status.idle":"2024-04-24T20:16:40.331490Z","shell.execute_reply.started":"2024-04-24T20:16:40.194221Z","shell.execute_reply":"2024-04-24T20:16:40.330702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_matrix.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:42.573618Z","iopub.execute_input":"2024-04-24T20:16:42.574302Z","iopub.status.idle":"2024-04-24T20:16:42.580071Z","shell.execute_reply.started":"2024-04-24T20:16:42.574271Z","shell.execute_reply":"2024-04-24T20:16:42.579065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Maximum sequence length: {max_len_sequence}\")\nprint(f\"Number of words in the embedding: {vocabulaire_size}\")\nprint(f\"Number of words in the vocabulary: {len(word_index)}\")\nprint(f\"Number of features per embedding: {embed_size}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:44.536408Z","iopub.execute_input":"2024-04-24T20:16:44.536766Z","iopub.status.idle":"2024-04-24T20:16:44.542090Z","shell.execute_reply.started":"2024-04-24T20:16:44.536738Z","shell.execute_reply":"2024-04-24T20:16:44.541061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"embedding_dim = np.power(vocabulaire_size, 1/4)\nembedding_dim = round(embedding_dim)\nembedding_dim\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train test split","metadata":{}},{"cell_type":"code","source":"print(X.shape, y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:48.178556Z","iopub.execute_input":"2024-04-24T20:16:48.179492Z","iopub.status.idle":"2024-04-24T20:16:48.185072Z","shell.execute_reply.started":"2024-04-24T20:16:48.179455Z","shell.execute_reply":"2024-04-24T20:16:48.183957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X,y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:50.309091Z","iopub.execute_input":"2024-04-24T20:16:50.309439Z","iopub.status.idle":"2024-04-24T20:16:50.344502Z","shell.execute_reply.started":"2024-04-24T20:16:50.309414Z","shell.execute_reply":"2024-04-24T20:16:50.343612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, y_train.shape, X_val.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:53.320579Z","iopub.execute_input":"2024-04-24T20:16:53.320955Z","iopub.status.idle":"2024-04-24T20:16:53.327200Z","shell.execute_reply.started":"2024-04-24T20:16:53.320926Z","shell.execute_reply":"2024-04-24T20:16:53.325798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build model","metadata":{}},{"cell_type":"code","source":"print(embedding_matrix.shape, embed_size, max_len_sequence)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:56.150369Z","iopub.execute_input":"2024-04-24T20:16:56.150726Z","iopub.status.idle":"2024-04-24T20:16:56.155939Z","shell.execute_reply.started":"2024-04-24T20:16:56.150701Z","shell.execute_reply":"2024-04-24T20:16:56.154946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Embedding(vocabulaire_size, embed_size, \n                    embeddings_initializer=tf.keras.initializers.Constant(embedding_matrix),\n                   trainable=True))\nmodel.add(Bidirectional(LSTM(32, activation=\"relu\", return_sequences=True)))\nmodel.add(Bidirectional(LSTM(10, activation=\"relu\", return_sequences=True, dropout=0.2)))\nmodel.add(GlobalMaxPooling1D())\nmodel.add(Dense(16, activation=\"relu\"))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(4, activation=\"relu\"))\nmodel.add(Dense(1, activation=\"sigmoid\"))","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:16:59.191254Z","iopub.execute_input":"2024-04-24T20:16:59.191853Z","iopub.status.idle":"2024-04-24T20:16:59.879704Z","shell.execute_reply.started":"2024-04-24T20:16:59.191820Z","shell.execute_reply":"2024-04-24T20:16:59.878952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:17:03.833331Z","iopub.execute_input":"2024-04-24T20:17:03.834089Z","iopub.status.idle":"2024-04-24T20:17:03.857794Z","shell.execute_reply.started":"2024-04-24T20:17:03.834057Z","shell.execute_reply":"2024-04-24T20:17:03.857092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:17:05.746268Z","iopub.execute_input":"2024-04-24T20:17:05.747097Z","iopub.status.idle":"2024-04-24T20:17:05.768530Z","shell.execute_reply.started":"2024-04-24T20:17:05.747068Z","shell.execute_reply":"2024-04-24T20:17:05.767665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=5, validation_data=(X_val, y_val), batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:17:16.171813Z","iopub.execute_input":"2024-04-24T20:17:16.172734Z","iopub.status.idle":"2024-04-24T20:20:56.666632Z","shell.execute_reply.started":"2024-04-24T20:17:16.172699Z","shell.execute_reply":"2024-04-24T20:20:56.665648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_graphes(history, *metrics):\n    \n    n_plots = len(metrics)\n    \n    fig, axs = plt.subplots(1,n_plots, figsize=(18, 5))\n\n    hist = history.history\n\n    for ax, metric in zip(axs, metrics):\n        ax.plot(np.clip(hist[metric], 0, 1))\n        ax.plot(np.clip(hist[\"val_\"+metric], 0, 1))\n        ax.legend([metric, \"val_\" + metric])\n        ax.set_title(metric)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:22:24.246189Z","iopub.execute_input":"2024-04-24T20:22:24.247090Z","iopub.status.idle":"2024-04-24T20:22:24.253551Z","shell.execute_reply.started":"2024-04-24T20:22:24.247057Z","shell.execute_reply":"2024-04-24T20:22:24.252621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_graphes(history, 'loss','accuracy')","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:22:29.832572Z","iopub.execute_input":"2024-04-24T20:22:29.833311Z","iopub.status.idle":"2024-04-24T20:22:30.427734Z","shell.execute_reply.started":"2024-04-24T20:22:29.833277Z","shell.execute_reply":"2024-04-24T20:22:30.426935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"test_data[\"clean_text\"] = test_data[\"question_text\"].apply(clean_text)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:22:36.266129Z","iopub.execute_input":"2024-04-24T20:22:36.266748Z","iopub.status.idle":"2024-04-24T20:22:41.952703Z","shell.execute_reply.started":"2024-04-24T20:22:36.266716Z","shell.execute_reply":"2024-04-24T20:22:41.951830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:22:46.151739Z","iopub.execute_input":"2024-04-24T20:22:46.152106Z","iopub.status.idle":"2024-04-24T20:22:46.162673Z","shell.execute_reply.started":"2024-04-24T20:22:46.152077Z","shell.execute_reply":"2024-04-24T20:22:46.161606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = tokenizer.texts_to_sequences(test_data['clean_text'])\ntest_df = pad_sequences(test_df, padding=\"post\", maxlen=max_len_sequence, truncating=\"post\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:22:52.223055Z","iopub.execute_input":"2024-04-24T20:22:52.223780Z","iopub.status.idle":"2024-04-24T20:22:59.179972Z","shell.execute_reply.started":"2024-04-24T20:22:52.223750Z","shell.execute_reply":"2024-04-24T20:22:59.179157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:23:02.531100Z","iopub.execute_input":"2024-04-24T20:23:02.531935Z","iopub.status.idle":"2024-04-24T20:23:02.538295Z","shell.execute_reply.started":"2024-04-24T20:23:02.531887Z","shell.execute_reply":"2024-04-24T20:23:02.537342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = model.predict(test_df)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:23:06.405471Z","iopub.execute_input":"2024-04-24T20:23:06.406226Z","iopub.status.idle":"2024-04-24T20:23:52.067716Z","shell.execute_reply.started":"2024-04-24T20:23:06.406194Z","shell.execute_reply":"2024-04-24T20:23:52.066663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:25.475812Z","iopub.execute_input":"2024-04-24T20:24:25.476166Z","iopub.status.idle":"2024-04-24T20:24:25.482938Z","shell.execute_reply.started":"2024-04-24T20:24:25.476140Z","shell.execute_reply":"2024-04-24T20:24:25.481952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds2 = ((test_preds > 0.5)+0).ravel()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:28.715587Z","iopub.execute_input":"2024-04-24T20:24:28.716312Z","iopub.status.idle":"2024-04-24T20:24:28.721732Z","shell.execute_reply.started":"2024-04-24T20:24:28.716276Z","shell.execute_reply":"2024-04-24T20:24:28.720568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds2","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:32.315686Z","iopub.execute_input":"2024-04-24T20:24:32.316377Z","iopub.status.idle":"2024-04-24T20:24:32.323096Z","shell.execute_reply.started":"2024-04-24T20:24:32.316344Z","shell.execute_reply":"2024-04-24T20:24:32.321891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission file","metadata":{}},{"cell_type":"code","source":"sample_data = pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:42.043185Z","iopub.execute_input":"2024-04-24T20:24:42.043932Z","iopub.status.idle":"2024-04-24T20:24:42.405389Z","shell.execute_reply.started":"2024-04-24T20:24:42.043881Z","shell.execute_reply":"2024-04-24T20:24:42.404580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data['prediction'] = test_preds2","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:44.322498Z","iopub.execute_input":"2024-04-24T20:24:44.323199Z","iopub.status.idle":"2024-04-24T20:24:44.327664Z","shell.execute_reply.started":"2024-04-24T20:24:44.323169Z","shell.execute_reply":"2024-04-24T20:24:44.326778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:46.610578Z","iopub.execute_input":"2024-04-24T20:24:46.611284Z","iopub.status.idle":"2024-04-24T20:24:46.620123Z","shell.execute_reply.started":"2024-04-24T20:24:46.611254Z","shell.execute_reply":"2024-04-24T20:24:46.619216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:52.291293Z","iopub.execute_input":"2024-04-24T20:24:52.292157Z","iopub.status.idle":"2024-04-24T20:24:52.297776Z","shell.execute_reply.started":"2024-04-24T20:24:52.292123Z","shell.execute_reply":"2024-04-24T20:24:52.296896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data.prediction.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:24:54.642735Z","iopub.execute_input":"2024-04-24T20:24:54.643396Z","iopub.status.idle":"2024-04-24T20:24:54.653928Z","shell.execute_reply.started":"2024-04-24T20:24:54.643365Z","shell.execute_reply":"2024-04-24T20:24:54.652975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:25:01.105399Z","iopub.execute_input":"2024-04-24T20:25:01.105754Z","iopub.status.idle":"2024-04-24T20:25:01.725813Z","shell.execute_reply.started":"2024-04-24T20:25:01.105727Z","shell.execute_reply":"2024-04-24T20:25:01.725027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}