{"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)\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-07-09T16:50:03.895153Z","iopub.execute_input":"2022-07-09T16:50:03.895630Z","iopub.status.idle":"2022-07-09T16:50:03.927466Z","shell.execute_reply.started":"2022-07-09T16:50:03.895529Z","shell.execute_reply":"2022-07-09T16:50:03.926465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/nlp-getting-started/train.csv\")\ntest_df = pd .read_csv(\"/kaggle/input/nlp-getting-started/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:50:36.888099Z","iopub.execute_input":"2022-07-09T16:50:36.888472Z","iopub.status.idle":"2022-07-09T16:50:36.952839Z","shell.execute_reply.started":"2022-07-09T16:50:36.888442Z","shell.execute_reply":"2022-07-09T16:50:36.951944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sample(8)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:50:48.743045Z","iopub.execute_input":"2022-07-09T16:50:48.743426Z","iopub.status.idle":"2022-07-09T16:50:48.772239Z","shell.execute_reply.started":"2022-07-09T16:50:48.743396Z","shell.execute_reply":"2022-07-09T16:50:48.771377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:51:05.094374Z","iopub.execute_input":"2022-07-09T16:51:05.094783Z","iopub.status.idle":"2022-07-09T16:51:05.113907Z","shell.execute_reply.started":"2022-07-09T16:51:05.094750Z","shell.execute_reply":"2022-07-09T16:51:05.112621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:51:16.185467Z","iopub.execute_input":"2022-07-09T16:51:16.185852Z","iopub.status.idle":"2022-07-09T16:51:16.192715Z","shell.execute_reply.started":"2022-07-09T16:51:16.185821Z","shell.execute_reply":"2022-07-09T16:51:16.191576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.count()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:51:29.753799Z","iopub.execute_input":"2022-07-09T16:51:29.754196Z","iopub.status.idle":"2022-07-09T16:51:29.770545Z","shell.execute_reply.started":"2022-07-09T16:51:29.754164Z","shell.execute_reply":"2022-07-09T16:51:29.769503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:51:40.394512Z","iopub.execute_input":"2022-07-09T16:51:40.395013Z","iopub.status.idle":"2022-07-09T16:51:40.414198Z","shell.execute_reply.started":"2022-07-09T16:51:40.394938Z","shell.execute_reply":"2022-07-09T16:51:40.413090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:51:52.249388Z","iopub.execute_input":"2022-07-09T16:51:52.249751Z","iopub.status.idle":"2022-07-09T16:51:52.261689Z","shell.execute_reply.started":"2022-07-09T16:51:52.249723Z","shell.execute_reply":"2022-07-09T16:51:52.260939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(columns=['keyword','location'],inplace=True)\ntest_df.drop(columns=['keyword','location'],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:52:04.464965Z","iopub.execute_input":"2022-07-09T16:52:04.465357Z","iopub.status.idle":"2022-07-09T16:52:04.477283Z","shell.execute_reply.started":"2022-07-09T16:52:04.465327Z","shell.execute_reply":"2022-07-09T16:52:04.476089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:52:15.256638Z","iopub.execute_input":"2022-07-09T16:52:15.256993Z","iopub.status.idle":"2022-07-09T16:52:15.278260Z","shell.execute_reply.started":"2022-07-09T16:52:15.256964Z","shell.execute_reply":"2022-07-09T16:52:15.277455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:52:28.024185Z","iopub.execute_input":"2022-07-09T16:52:28.024555Z","iopub.status.idle":"2022-07-09T16:52:28.038864Z","shell.execute_reply.started":"2022-07-09T16:52:28.024525Z","shell.execute_reply":"2022-07-09T16:52:28.038096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"target\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:52:40.087767Z","iopub.execute_input":"2022-07-09T16:52:40.088150Z","iopub.status.idle":"2022-07-09T16:52:40.096160Z","shell.execute_reply.started":"2022-07-09T16:52:40.088114Z","shell.execute_reply":"2022-07-09T16:52:40.095356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.pie(train_df['target'].value_counts(), labels=[0,1],autopct=\"%0.2f\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:52:53.401182Z","iopub.execute_input":"2022-07-09T16:52:53.401917Z","iopub.status.idle":"2022-07-09T16:52:53.544589Z","shell.execute_reply.started":"2022-07-09T16:52:53.401877Z","shell.execute_reply":"2022-07-09T16:52:53.543059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nltk","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:53:04.392348Z","iopub.execute_input":"2022-07-09T16:53:04.392755Z","iopub.status.idle":"2022-07-09T16:53:05.487637Z","shell.execute_reply.started":"2022-07-09T16:53:04.392725Z","shell.execute_reply":"2022-07-09T16:53:05.486481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nltk.download('punkt')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:53:14.441394Z","iopub.execute_input":"2022-07-09T16:53:14.441776Z","iopub.status.idle":"2022-07-09T16:53:14.670640Z","shell.execute_reply.started":"2022-07-09T16:53:14.441744Z","shell.execute_reply":"2022-07-09T16:53:14.669438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['num_characters'] = train_df['text'].apply(len)\ntest_df['num_characters'] = test_df['text'].apply(len)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:53:24.921727Z","iopub.execute_input":"2022-07-09T16:53:24.922099Z","iopub.status.idle":"2022-07-09T16:53:24.936080Z","shell.execute_reply.started":"2022-07-09T16:53:24.922068Z","shell.execute_reply":"2022-07-09T16:53:24.935198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:53:34.119055Z","iopub.execute_input":"2022-07-09T16:53:34.119452Z","iopub.status.idle":"2022-07-09T16:53:34.131653Z","shell.execute_reply.started":"2022-07-09T16:53:34.119418Z","shell.execute_reply":"2022-07-09T16:53:34.130327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['num_words'] = train_df['text'].apply(lambda x:len(nltk.word_tokenize(x)))\ntest_df['num_words'] = test_df['text'].apply(lambda x:len(nltk.word_tokenize(x)))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:53:44.312471Z","iopub.execute_input":"2022-07-09T16:53:44.312846Z","iopub.status.idle":"2022-07-09T16:53:47.766595Z","shell.execute_reply.started":"2022-07-09T16:53:44.312815Z","shell.execute_reply":"2022-07-09T16:53:47.765447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:54:00.777643Z","iopub.execute_input":"2022-07-09T16:54:00.778491Z","iopub.status.idle":"2022-07-09T16:54:00.790894Z","shell.execute_reply.started":"2022-07-09T16:54:00.778433Z","shell.execute_reply":"2022-07-09T16:54:00.790033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['num_sentences'] = train_df['text'].apply(lambda x:len(nltk.sent_tokenize(x)))\ntest_df['num_sentences'] = test_df['text'].apply(lambda x:len(nltk.sent_tokenize(x)))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:54:09.192822Z","iopub.execute_input":"2022-07-09T16:54:09.193533Z","iopub.status.idle":"2022-07-09T16:54:10.026976Z","shell.execute_reply.started":"2022-07-09T16:54:09.193487Z","shell.execute_reply":"2022-07-09T16:54:10.025901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:54:18.296911Z","iopub.execute_input":"2022-07-09T16:54:18.297305Z","iopub.status.idle":"2022-07-09T16:54:18.309449Z","shell.execute_reply.started":"2022-07-09T16:54:18.297274Z","shell.execute_reply":"2022-07-09T16:54:18.308536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[[\"num_characters\",\"num_words\",\"num_sentences\"]].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:54:30.312134Z","iopub.execute_input":"2022-07-09T16:54:30.312571Z","iopub.status.idle":"2022-07-09T16:54:30.339305Z","shell.execute_reply.started":"2022-07-09T16:54:30.312538Z","shell.execute_reply":"2022-07-09T16:54:30.338070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:54:44.008611Z","iopub.execute_input":"2022-07-09T16:54:44.009042Z","iopub.status.idle":"2022-07-09T16:54:44.185163Z","shell.execute_reply.started":"2022-07-09T16:54:44.008990Z","shell.execute_reply":"2022-07-09T16:54:44.183921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsns.histplot(train_df[train_df['target'] == 0]['num_characters'])\nsns.histplot(train_df[train_df['target'] == 1]['num_characters'],color='red')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:54:54.327944Z","iopub.execute_input":"2022-07-09T16:54:54.328364Z","iopub.status.idle":"2022-07-09T16:54:54.647047Z","shell.execute_reply.started":"2022-07-09T16:54:54.328324Z","shell.execute_reply":"2022-07-09T16:54:54.645892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsns.histplot(train_df[train_df['target'] == 0]['num_words'])\nsns.histplot(train_df[train_df['target'] == 1]['num_words'],color='red')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:55:05.750308Z","iopub.execute_input":"2022-07-09T16:55:05.751049Z","iopub.status.idle":"2022-07-09T16:55:06.119724Z","shell.execute_reply.started":"2022-07-09T16:55:05.750989Z","shell.execute_reply":"2022-07-09T16:55:06.118917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(train_df,hue=\"target\")","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:55:17.512477Z","iopub.execute_input":"2022-07-09T16:55:17.512893Z","iopub.status.idle":"2022-07-09T16:55:26.584830Z","shell.execute_reply.started":"2022-07-09T16:55:17.512856Z","shell.execute_reply":"2022-07-09T16:55:26.583809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_df.corr(),annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:56:55.044798Z","iopub.execute_input":"2022-07-09T16:56:55.045168Z","iopub.status.idle":"2022-07-09T16:56:55.319488Z","shell.execute_reply.started":"2022-07-09T16:56:55.045137Z","shell.execute_reply":"2022-07-09T16:56:55.318379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# data preprocessing","metadata":{}},{"cell_type":"code","source":"from nltk.corpus import stopwords\nimport string\nfrom nltk.stem.porter import PorterStemmer\nps = PorterStemmer()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:58:08.729039Z","iopub.execute_input":"2022-07-09T16:58:08.730105Z","iopub.status.idle":"2022-07-09T16:58:08.736842Z","shell.execute_reply.started":"2022-07-09T16:58:08.730050Z","shell.execute_reply":"2022-07-09T16:58:08.735550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transform_text(text):\n    text = text.lower()\n    text = nltk.word_tokenize(text)\n    \n    y = []\n    for i in text:\n        if i.isalnum():\n            y.append(i)\n    \n    text = y[:]\n    y.clear()\n    \n    for i in text:\n        if i not in stopwords.words('english') and i not in string.punctuation:\n            y.append(i)\n            \n    text = y[:]\n    y.clear()\n    \n    for i in text:\n        y.append(ps.stem(i))\n    \n            \n    return \" \".join(y)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:58:20.488970Z","iopub.execute_input":"2022-07-09T16:58:20.489396Z","iopub.status.idle":"2022-07-09T16:58:20.497721Z","shell.execute_reply.started":"2022-07-09T16:58:20.489362Z","shell.execute_reply":"2022-07-09T16:58:20.496869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nltk\nnltk.download('stopwords')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:58:39.013990Z","iopub.execute_input":"2022-07-09T16:58:39.014387Z","iopub.status.idle":"2022-07-09T16:58:39.141441Z","shell.execute_reply.started":"2022-07-09T16:58:39.014356Z","shell.execute_reply":"2022-07-09T16:58:39.140220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['transformed_text'] = train_df['text'].apply(transform_text)\ntest_df['transformed_text'] = test_df['text'].apply(transform_text)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:58:50.664864Z","iopub.execute_input":"2022-07-09T16:58:50.665248Z","iopub.status.idle":"2022-07-09T16:59:18.846491Z","shell.execute_reply.started":"2022-07-09T16:58:50.665214Z","shell.execute_reply":"2022-07-09T16:59:18.845280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.sample(50)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:59:22.822858Z","iopub.execute_input":"2022-07-09T16:59:22.824054Z","iopub.status.idle":"2022-07-09T16:59:22.844743Z","shell.execute_reply.started":"2022-07-09T16:59:22.823974Z","shell.execute_reply":"2022-07-09T16:59:22.844047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from wordcloud import WordCloud\nwc = WordCloud(width=500,height=500,min_font_size=10,background_color='white')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:59:37.285435Z","iopub.execute_input":"2022-07-09T16:59:37.285805Z","iopub.status.idle":"2022-07-09T16:59:37.326729Z","shell.execute_reply.started":"2022-07-09T16:59:37.285776Z","shell.execute_reply":"2022-07-09T16:59:37.325865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disaster_wc = wc.generate(train_df[train_df['target'] == 1]['transformed_text'].str.cat(sep=\" \"))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:59:46.824182Z","iopub.execute_input":"2022-07-09T16:59:46.824562Z","iopub.status.idle":"2022-07-09T16:59:47.436237Z","shell.execute_reply.started":"2022-07-09T16:59:46.824523Z","shell.execute_reply":"2022-07-09T16:59:47.435284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nplt.imshow(disaster_wc)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:59:55.398828Z","iopub.execute_input":"2022-07-09T16:59:55.399529Z","iopub.status.idle":"2022-07-09T16:59:55.674548Z","shell.execute_reply.started":"2022-07-09T16:59:55.399488Z","shell.execute_reply":"2022-07-09T16:59:55.673707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"notdiaster_wc = wc.generate(train_df[train_df['target'] == 0]['transformed_text'].str.cat(sep=\" \"))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:00:07.896017Z","iopub.execute_input":"2022-07-09T17:00:07.896389Z","iopub.status.idle":"2022-07-09T17:00:08.779634Z","shell.execute_reply.started":"2022-07-09T17:00:07.896358Z","shell.execute_reply":"2022-07-09T17:00:08.778501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nplt.imshow(notdiaster_wc)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:00:17.224659Z","iopub.execute_input":"2022-07-09T17:00:17.225312Z","iopub.status.idle":"2022-07-09T17:00:17.573709Z","shell.execute_reply.started":"2022-07-09T17:00:17.225272Z","shell.execute_reply":"2022-07-09T17:00:17.570455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"notds_corpus = []\nfor msg in train_df[train_df['target'] == 1]['transformed_text'].tolist():\n    for word in msg.split():\n        notds_corpus.append(word)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:00:28.568266Z","iopub.execute_input":"2022-07-09T17:00:28.568677Z","iopub.status.idle":"2022-07-09T17:00:28.583107Z","shell.execute_reply.started":"2022-07-09T17:00:28.568631Z","shell.execute_reply":"2022-07-09T17:00:28.582156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(notds_corpus)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:00:41.176336Z","iopub.execute_input":"2022-07-09T17:00:41.176709Z","iopub.status.idle":"2022-07-09T17:00:41.182619Z","shell.execute_reply.started":"2022-07-09T17:00:41.176680Z","shell.execute_reply":"2022-07-09T17:00:41.181851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\n\nsns.barplot(pd.DataFrame(Counter(notds_corpus).most_common(30))[0],pd.DataFrame(Counter(notds_corpus).most_common(30))[1])\nplt.xticks(rotation='vertical')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:00:49.895155Z","iopub.execute_input":"2022-07-09T17:00:49.895837Z","iopub.status.idle":"2022-07-09T17:00:50.280031Z","shell.execute_reply.started":"2022-07-09T17:00:49.895797Z","shell.execute_reply":"2022-07-09T17:00:50.278910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_corpus = []\nfor msg in train_df[train_df['target'] == 0]['transformed_text'].tolist():\n    for word in msg.split():\n        ds_corpus.append(word)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:01:02.073358Z","iopub.execute_input":"2022-07-09T17:01:02.073774Z","iopub.status.idle":"2022-07-09T17:01:02.091050Z","shell.execute_reply.started":"2022-07-09T17:01:02.073737Z","shell.execute_reply":"2022-07-09T17:01:02.090073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(ds_corpus)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:01:14.665180Z","iopub.execute_input":"2022-07-09T17:01:14.665562Z","iopub.status.idle":"2022-07-09T17:01:14.672781Z","shell.execute_reply.started":"2022-07-09T17:01:14.665523Z","shell.execute_reply":"2022-07-09T17:01:14.671596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\n\nsns.barplot(pd.DataFrame(Counter(ds_corpus).most_common(30))[0],pd.DataFrame(Counter(ds_corpus).most_common(30))[1])\nplt.xticks(rotation='vertical')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:01:24.249152Z","iopub.execute_input":"2022-07-09T17:01:24.249561Z","iopub.status.idle":"2022-07-09T17:01:24.624117Z","shell.execute_reply.started":"2022-07-09T17:01:24.249527Z","shell.execute_reply":"2022-07-09T17:01:24.623040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model building","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:01:52.405680Z","iopub.execute_input":"2022-07-09T17:01:52.406124Z","iopub.status.idle":"2022-07-09T17:02:02.474399Z","shell.execute_reply.started":"2022-07-09T17:01:52.406088Z","shell.execute_reply":"2022-07-09T17:02:02.473290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_message = []\ntesting_message = []\ntraining_labels = []\nfor i in range(len(train_df)):\n    training_message.append(train_df.loc[i,'text'])\n    training_labels.append(train_df.loc[i,'target'])\n    \nfor i in range(len(test_df)):\n    testing_message.append(test_df.loc[i,'text'])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:03:04.421755Z","iopub.execute_input":"2022-07-09T17:03:04.422499Z","iopub.status.idle":"2022-07-09T17:03:04.676543Z","shell.execute_reply.started":"2022-07-09T17:03:04.422458Z","shell.execute_reply":"2022-07-09T17:03:04.675151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_size = 23000\nembedding_dim = 32\nmax_length = 100\ntrunc_type='post'\npadding_type='post'\noov_tok = \"<OOV>\"","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:03:16.790137Z","iopub.execute_input":"2022-07-09T17:03:16.790548Z","iopub.status.idle":"2022-07-09T17:03:16.796072Z","shell.execute_reply.started":"2022-07-09T17:03:16.790517Z","shell.execute_reply":"2022-07-09T17:03:16.795179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tokenization Process:\ntokenizer = Tokenizer(num_words = vocab_size, oov_token=oov_tok)\ntokenizer.fit_on_texts(training_message)\nword_index = tokenizer.word_index","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:03:27.879966Z","iopub.execute_input":"2022-07-09T17:03:27.880379Z","iopub.status.idle":"2022-07-09T17:03:28.117130Z","shell.execute_reply.started":"2022-07-09T17:03:27.880347Z","shell.execute_reply":"2022-07-09T17:03:28.115970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# texts_to_sequences Process:\ntraining_message = tokenizer.texts_to_sequences(training_message)\ntesting_message = tokenizer.texts_to_sequences(testing_message)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:03:40.887973Z","iopub.execute_input":"2022-07-09T17:03:40.888389Z","iopub.status.idle":"2022-07-09T17:03:41.115567Z","shell.execute_reply.started":"2022-07-09T17:03:40.888353Z","shell.execute_reply":"2022-07-09T17:03:41.114483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pad_sequences Process:\ntraining_padded = pad_sequences(training_message,maxlen=max_length, truncating=trunc_type, padding=padding_type)\ntesting_padded = pad_sequences(testing_message,maxlen=max_length, truncating=trunc_type, padding=padding_type)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:03:57.685863Z","iopub.execute_input":"2022-07-09T17:03:57.686281Z","iopub.status.idle":"2022-07-09T17:03:57.754958Z","shell.execute_reply.started":"2022-07-09T17:03:57.686244Z","shell.execute_reply":"2022-07-09T17:03:57.753859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_padded = np.array(training_padded)\ntesting_padded = np.array(testing_padded)\ntraining_labels = np.array(training_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:04:07.844289Z","iopub.execute_input":"2022-07-09T17:04:07.845201Z","iopub.status.idle":"2022-07-09T17:04:07.853822Z","shell.execute_reply.started":"2022-07-09T17:04:07.845160Z","shell.execute_reply":"2022-07-09T17:04:07.852692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),\n    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(12)),\n    tf.keras.layers.Dense(1, activation = 'sigmoid')\n])\n\nmodel.compile(\n    loss='binary_crossentropy',\n    optimizer='Adamax',\n    metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()]\n)\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:04:19.605148Z","iopub.execute_input":"2022-07-09T17:04:19.605506Z","iopub.status.idle":"2022-07-09T17:04:20.272207Z","shell.execute_reply.started":"2022-07-09T17:04:19.605477Z","shell.execute_reply":"2022-07-09T17:04:20.271133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    training_padded, \n    training_labels, \n    epochs = 10, \n    batch_size = 64,  \n    validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:04:46.774601Z","iopub.execute_input":"2022-07-09T17:04:46.774979Z","iopub.status.idle":"2022-07-09T17:06:02.568742Z","shell.execute_reply.started":"2022-07-09T17:04:46.774947Z","shell.execute_reply":"2022-07-09T17:06:02.567916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"], color=\"r\")\nplt.plot(history.history[\"val_accuracy\"], color=\"g\")\nplt.legend([\"Training\", \"Validation\"])\nplt.xlabel(\"epochs\")\nplt.ylabel(\"accuracy\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:06:07.989360Z","iopub.execute_input":"2022-07-09T17:06:07.989738Z","iopub.status.idle":"2022-07-09T17:06:08.196153Z","shell.execute_reply.started":"2022-07-09T17:06:07.989705Z","shell.execute_reply":"2022-07-09T17:06:08.195067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict = model.predict(testing_padded).round().astype(int)\n\ntest_predict","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:06:20.075126Z","iopub.execute_input":"2022-07-09T17:06:20.075524Z","iopub.status.idle":"2022-07-09T17:06:21.599251Z","shell.execute_reply.started":"2022-07-09T17:06:20.075493Z","shell.execute_reply":"2022-07-09T17:06:21.598429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':test_df['id'],'target':test_predict.ravel()})\nsubmission.to_csv('submission_.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T17:06:32.135814Z","iopub.execute_input":"2022-07-09T17:06:32.136185Z","iopub.status.idle":"2022-07-09T17:06:32.151158Z","shell.execute_reply.started":"2022-07-09T17:06:32.136153Z","shell.execute_reply":"2022-07-09T17:06:32.150242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}