{"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.122453,"end_time":"2022-05-08T16:24:22.121771","exception":false,"start_time":"2022-05-08T16:24:21.999318","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.436763Z","iopub.execute_input":"2022-08-09T10:45:30.437337Z","iopub.status.idle":"2022-08-09T10:45:30.446348Z","shell.execute_reply.started":"2022-08-09T10:45:30.437276Z","shell.execute_reply":"2022-08-09T10:45:30.444923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport string \nimport re\n\nimport nltk\nfrom nltk.corpus import stopwords\nfrom nltk.tokenize import sent_tokenize, word_tokenize\nfrom wordcloud import WordCloud, STOPWORDS\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom nltk.stem import PorterStemmer, WordNetLemmatizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\n\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Embedding, LSTM, Dropout\n\nfrom tensorflow.keras.utils import plot_model\n\nfrom sklearn import metrics\nfrom functools import reduce\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"papermill":{"duration":7.140972,"end_time":"2022-05-08T16:24:29.360518","exception":false,"start_time":"2022-05-08T16:24:22.219546","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.485259Z","iopub.execute_input":"2022-08-09T10:45:30.485695Z","iopub.status.idle":"2022-08-09T10:45:30.496113Z","shell.execute_reply.started":"2022-08-09T10:45:30.485661Z","shell.execute_reply":"2022-08-09T10:45:30.494900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')","metadata":{"papermill":{"duration":0.148661,"end_time":"2022-05-08T16:24:29.605572","exception":false,"start_time":"2022-05-08T16:24:29.456911","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.530913Z","iopub.execute_input":"2022-08-09T10:45:30.532066Z","iopub.status.idle":"2022-08-09T10:45:30.561653Z","shell.execute_reply.started":"2022-08-09T10:45:30.532008Z","shell.execute_reply":"2022-08-09T10:45:30.560360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Text Analysis","metadata":{"papermill":{"duration":0.096607,"end_time":"2022-05-08T16:24:29.798542","exception":false,"start_time":"2022-05-08T16:24:29.701935","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data.head().style.background_gradient(cmap='coolwarm')","metadata":{"papermill":{"duration":0.196801,"end_time":"2022-05-08T16:24:30.092626","exception":false,"start_time":"2022-05-08T16:24:29.895825","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.577311Z","iopub.execute_input":"2022-08-09T10:45:30.577725Z","iopub.status.idle":"2022-08-09T10:45:30.595399Z","shell.execute_reply.started":"2022-08-09T10:45:30.577689Z","shell.execute_reply":"2022-08-09T10:45:30.594128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of rows is = \", train_data.shape[0], \" \\nNumber of columns is = \" , train_data.shape[1]) ","metadata":{"papermill":{"duration":0.108901,"end_time":"2022-05-08T16:24:30.297358","exception":false,"start_time":"2022-05-08T16:24:30.188457","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.624629Z","iopub.execute_input":"2022-08-09T10:45:30.625027Z","iopub.status.idle":"2022-08-09T10:45:30.631156Z","shell.execute_reply.started":"2022-08-09T10:45:30.624992Z","shell.execute_reply":"2022-08-09T10:45:30.629969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"papermill":{"duration":0.116669,"end_time":"2022-05-08T16:24:30.510809","exception":false,"start_time":"2022-05-08T16:24:30.39414","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.668575Z","iopub.execute_input":"2022-08-09T10:45:30.669328Z","iopub.status.idle":"2022-08-09T10:45:30.682392Z","shell.execute_reply.started":"2022-08-09T10:45:30.669260Z","shell.execute_reply":"2022-08-09T10:45:30.681078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Heat Map","metadata":{"papermill":{"duration":0.096941,"end_time":"2022-05-08T16:24:30.704431","exception":false,"start_time":"2022-05-08T16:24:30.60749","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(11,8)})\nsns.heatmap(train_data.isnull(),yticklabels=False,cbar=False,cmap=\"coolwarm\")","metadata":{"papermill":{"duration":0.401855,"end_time":"2022-05-08T16:24:31.204107","exception":false,"start_time":"2022-05-08T16:24:30.802252","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.709943Z","iopub.execute_input":"2022-08-09T10:45:30.711530Z","iopub.status.idle":"2022-08-09T10:45:30.938866Z","shell.execute_reply.started":"2022-08-09T10:45:30.711475Z","shell.execute_reply":"2022-08-09T10:45:30.937747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here each red line indicates there is missing value and as per heat map of keyword and location have missing value. As for NLP model building we will be using only text and target, so there is no need to handle missing value.","metadata":{"papermill":{"duration":0.099033,"end_time":"2022-05-08T16:24:31.402835","exception":false,"start_time":"2022-05-08T16:24:31.303802","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"Analysis and Visualisation of Target","metadata":{"papermill":{"duration":0.097711,"end_time":"2022-05-08T16:24:31.598884","exception":false,"start_time":"2022-05-08T16:24:31.501173","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data['target'].value_counts()","metadata":{"papermill":{"duration":0.114559,"end_time":"2022-05-08T16:24:31.812873","exception":false,"start_time":"2022-05-08T16:24:31.698314","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.942102Z","iopub.execute_input":"2022-08-09T10:45:30.942787Z","iopub.status.idle":"2022-08-09T10:45:30.950964Z","shell.execute_reply.started":"2022-08-09T10:45:30.942750Z","shell.execute_reply":"2022-08-09T10:45:30.949674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5,3))\ncolors = [\"blue\", \"red\"]\n\nsns.countplot(x = 'target', data=train_data, palette=colors)\nplt.title('Target Distributions \\n (0: Non Disaster || 1: Disaster)', fontsize=14)","metadata":{"papermill":{"duration":0.335536,"end_time":"2022-05-08T16:24:32.274043","exception":false,"start_time":"2022-05-08T16:24:31.938507","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:30.952445Z","iopub.execute_input":"2022-08-09T10:45:30.952896Z","iopub.status.idle":"2022-08-09T10:45:31.136818Z","shell.execute_reply.started":"2022-08-09T10:45:30.952853Z","shell.execute_reply":"2022-08-09T10:45:31.135514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Analysis and Visualisation of Features","metadata":{"papermill":{"duration":0.100013,"end_time":"2022-05-08T16:24:32.476041","exception":false,"start_time":"2022-05-08T16:24:32.376028","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data[\"keyword\"].nunique()","metadata":{"papermill":{"duration":0.109557,"end_time":"2022-05-08T16:24:32.938169","exception":false,"start_time":"2022-05-08T16:24:32.828612","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:31.139019Z","iopub.execute_input":"2022-08-09T10:45:31.139446Z","iopub.status.idle":"2022-08-09T10:45:31.147042Z","shell.execute_reply.started":"2022-08-09T10:45:31.139414Z","shell.execute_reply":"2022-08-09T10:45:31.145937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"top 20 keywords","metadata":{"papermill":{"duration":0.099702,"end_time":"2022-05-08T16:24:33.138944","exception":false,"start_time":"2022-05-08T16:24:33.039242","status":"completed"},"tags":[]}},{"cell_type":"code","source":"chains=train_data['keyword'].value_counts()[:20]\nsns.barplot(x=chains,y=chains.index,palette='deep')\nplt.title(\"Top 20 Keywords\")\nplt.xlabel(\"Count of Keywords\")","metadata":{"papermill":{"duration":0.599759,"end_time":"2022-05-08T16:24:33.838675","exception":false,"start_time":"2022-05-08T16:24:33.238916","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:31.148511Z","iopub.execute_input":"2022-08-09T10:45:31.149675Z","iopub.status.idle":"2022-08-09T10:45:31.517019Z","shell.execute_reply.started":"2022-08-09T10:45:31.149631Z","shell.execute_reply":"2022-08-09T10:45:31.515806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disaster_keywords = train_data.loc[train_data[\"target\"] == 1][\"keyword\"].value_counts()\nnondisaster_keywords = train_data.loc[train_data[\"target\"] == 0][\"keyword\"].value_counts()\n\nfig, ax = plt.subplots(1,2, figsize=(20,8))\nsns.barplot(y=disaster_keywords[0:20].index, x=disaster_keywords[0:20], orient='h', ax=ax[0], palette=\"Reds_d\")\nax[0].set_title(\"Top 20 Keywords - Disaster Tweets\")\nax[0].set_xlabel(\"Keyword Frequency\")\n\nsns.barplot(y=nondisaster_keywords[0:20].index, x=nondisaster_keywords[0:20], orient='h', ax=ax[1], palette=\"Blues_d\")\nax[1].set_title(\"Top 20 Keywords - Non-Disaster Tweets\")\nax[1].set_xlabel(\"Keyword Frequency\")\n\n\nplt.tight_layout()\nplt.show()","metadata":{"papermill":{"duration":1.27627,"end_time":"2022-05-08T16:24:35.418909","exception":false,"start_time":"2022-05-08T16:24:34.142639","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:31.518993Z","iopub.execute_input":"2022-08-09T10:45:31.519478Z","iopub.status.idle":"2022-08-09T10:45:32.499704Z","shell.execute_reply.started":"2022-08-09T10:45:31.519432Z","shell.execute_reply":"2022-08-09T10:45:32.498452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_disaster_keyword = train_data.groupby('keyword').mean()['target'].sort_values(ascending = False).head(20)\ntop_nondisaster_keyword = train_data.groupby('keyword').mean()['target'].sort_values().head(20)","metadata":{"papermill":{"duration":0.121678,"end_time":"2022-05-08T16:24:35.860151","exception":false,"start_time":"2022-05-08T16:24:35.738473","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:32.501261Z","iopub.execute_input":"2022-08-09T10:45:32.509619Z","iopub.status.idle":"2022-08-09T10:45:32.523992Z","shell.execute_reply.started":"2022-08-09T10:45:32.509564Z","shell.execute_reply":"2022-08-09T10:45:32.522807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize=(20,8))\n\nsns.barplot(y=top_disaster_keyword[0:20].index, x=disaster_keywords[0:20], orient='h', ax=ax[0], palette=\"Reds_d\")\nax[0].set_title(\"Top 20 Keywords - Highest used Disaster Keyword\")\nax[0].set_xlabel(\"Keyword Frequency\")\n\n\nsns.barplot(y=top_nondisaster_keyword[0:20].index, x=top_nondisaster_keyword[0:20], orient='h', ax=ax[1], palette=\"Blues_d\")\nax[1].set_title(\"Top 20 Keywords - Least used Non-Disaster Tweets\")\nax[1].set_xlabel(\"Keyword Frequency\")\n\n\nplt.tight_layout()\nplt.show()","metadata":{"papermill":{"duration":1.31973,"end_time":"2022-05-08T16:24:37.284587","exception":false,"start_time":"2022-05-08T16:24:35.964857","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:32.526456Z","iopub.execute_input":"2022-08-09T10:45:32.527331Z","iopub.status.idle":"2022-08-09T10:45:33.483819Z","shell.execute_reply.started":"2022-08-09T10:45:32.527271Z","shell.execute_reply":"2022-08-09T10:45:33.482651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"locations = train_data[\"location\"].value_counts()","metadata":{"papermill":{"duration":0.115645,"end_time":"2022-05-08T16:24:37.720976","exception":false,"start_time":"2022-05-08T16:24:37.605331","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:33.485566Z","iopub.execute_input":"2022-08-09T10:45:33.486336Z","iopub.status.idle":"2022-08-09T10:45:33.495978Z","shell.execute_reply.started":"2022-08-09T10:45:33.486265Z","shell.execute_reply":"2022-08-09T10:45:33.495094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\n\n\nsns.barplot(y=locations[0:20].index, x=locations[0:20], orient='h')\n\nplt.title(\"Top 20 Locations\")\nplt.show()","metadata":{"papermill":{"duration":0.560766,"end_time":"2022-05-08T16:24:38.602645","exception":false,"start_time":"2022-05-08T16:24:38.041879","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:33.501262Z","iopub.execute_input":"2022-08-09T10:45:33.501648Z","iopub.status.idle":"2022-08-09T10:45:33.864196Z","shell.execute_reply.started":"2022-08-09T10:45:33.501616Z","shell.execute_reply":"2022-08-09T10:45:33.862855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_data['text']))","metadata":{"papermill":{"duration":0.116686,"end_time":"2022-05-08T16:24:39.047182","exception":false,"start_time":"2022-05-08T16:24:38.930496","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:33.865807Z","iopub.execute_input":"2022-08-09T10:45:33.866152Z","iopub.status.idle":"2022-08-09T10:45:33.871498Z","shell.execute_reply.started":"2022-08-09T10:45:33.866120Z","shell.execute_reply":"2022-08-09T10:45:33.870375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"length\"]  = train_data[\"text\"].apply(len)\ntrain_data.head()","metadata":{"papermill":{"duration":0.129975,"end_time":"2022-05-08T16:24:39.504239","exception":false,"start_time":"2022-05-08T16:24:39.374264","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:33.872895Z","iopub.execute_input":"2022-08-09T10:45:33.873851Z","iopub.status.idle":"2022-08-09T10:45:33.895714Z","shell.execute_reply.started":"2022-08-09T10:45:33.873816Z","shell.execute_reply":"2022-08-09T10:45:33.894109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['length'].describe()","metadata":{"papermill":{"duration":0.124822,"end_time":"2022-05-08T16:24:39.738614","exception":false,"start_time":"2022-05-08T16:24:39.613792","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:33.897780Z","iopub.execute_input":"2022-08-09T10:45:33.898901Z","iopub.status.idle":"2022-08-09T10:45:33.910463Z","shell.execute_reply.started":"2022-08-09T10:45:33.898852Z","shell.execute_reply":"2022-08-09T10:45:33.909351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[train_data['length']==157]['text'].iloc[0]","metadata":{"papermill":{"duration":0.119986,"end_time":"2022-05-08T16:24:39.966667","exception":false,"start_time":"2022-05-08T16:24:39.846681","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:33.911574Z","iopub.execute_input":"2022-08-09T10:45:33.911903Z","iopub.status.idle":"2022-08-09T10:45:33.919852Z","shell.execute_reply.started":"2022-08-09T10:45:33.911874Z","shell.execute_reply":"2022-08-09T10:45:33.918988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Tweets length ","metadata":{"papermill":{"duration":0.11025,"end_time":"2022-05-08T16:24:40.409379","exception":false,"start_time":"2022-05-08T16:24:40.299129","status":"completed"},"tags":[]}},{"cell_type":"code","source":"plt.figure(figsize=(5,3))\nsns.histplot(train_data[\"length\"], kde=True,color='purple',bins=30)\nplt.title(\"Length of tweets\")\nplt.xlabel(\"Number of Characters\")\nplt.ylabel(\"Density\")\nplt.show()","metadata":{"papermill":{"duration":0.549466,"end_time":"2022-05-08T16:24:41.068848","exception":false,"start_time":"2022-05-08T16:24:40.519382","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:33.921250Z","iopub.execute_input":"2022-08-09T10:45:33.921814Z","iopub.status.idle":"2022-08-09T10:45:34.261070Z","shell.execute_reply.started":"2022-08-09T10:45:33.921782Z","shell.execute_reply":"2022-08-09T10:45:34.259740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.hist(column='length', by = 'target',bins =60, figsize= (10,3))","metadata":{"papermill":{"duration":1.042383,"end_time":"2022-05-08T16:24:42.449643","exception":false,"start_time":"2022-05-08T16:24:41.40726","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:34.262454Z","iopub.execute_input":"2022-08-09T10:45:34.262798Z","iopub.status.idle":"2022-08-09T10:45:34.831255Z","shell.execute_reply.started":"2022-08-09T10:45:34.262766Z","shell.execute_reply":"2022-08-09T10:45:34.830191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def count_words(x):\n    return len(x.split())\n\ntrain_data[\"num_words\"] = train_data[\"text\"].apply(count_words)\n\nplt.figure(figsize=(5,3))\nsns.histplot(train_data[\"num_words\"],kde=True,color='purple',bins=30)\nplt.title(\"Histogram of Number of Words per Tweet\")\nplt.xlabel(\"Number of Words\")\nplt.ylabel(\"Density\")\nplt.show()","metadata":{"papermill":{"duration":0.549793,"end_time":"2022-05-08T16:24:43.577622","exception":false,"start_time":"2022-05-08T16:24:43.027829","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:34.832865Z","iopub.execute_input":"2022-08-09T10:45:34.833179Z","iopub.status.idle":"2022-08-09T10:45:35.490661Z","shell.execute_reply.started":"2022-08-09T10:45:34.833149Z","shell.execute_reply":"2022-08-09T10:45:35.489562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.hist(column='num_words', by = 'target',bins =60, figsize= (10,3))","metadata":{"papermill":{"duration":0.973753,"end_time":"2022-05-08T16:24:44.889389","exception":false,"start_time":"2022-05-08T16:24:43.915636","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:35.492175Z","iopub.execute_input":"2022-08-09T10:45:35.492588Z","iopub.status.idle":"2022-08-09T10:45:36.068361Z","shell.execute_reply.started":"2022-08-09T10:45:35.492540Z","shell.execute_reply":"2022-08-09T10:45:36.067210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Text Preprocessing","metadata":{"papermill":{"duration":0.114752,"end_time":"2022-05-08T16:24:45.11926","exception":false,"start_time":"2022-05-08T16:24:45.004508","status":"completed"},"tags":[]}},{"cell_type":"code","source":"string.punctuation","metadata":{"papermill":{"duration":0.127578,"end_time":"2022-05-08T16:24:45.821928","exception":false,"start_time":"2022-05-08T16:24:45.69435","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.070127Z","iopub.execute_input":"2022-08-09T10:45:36.070614Z","iopub.status.idle":"2022-08-09T10:45:36.078355Z","shell.execute_reply.started":"2022-08-09T10:45:36.070568Z","shell.execute_reply":"2022-08-09T10:45:36.077088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def toclean_text(text):\n\n    \n    clean_text = [char for char in text if char not in string.punctuation]\n   \n    clean_text = ''.join(clean_text)\n    \n        \n    return clean_text","metadata":{"papermill":{"duration":0.12259,"end_time":"2022-05-08T16:24:46.290135","exception":false,"start_time":"2022-05-08T16:24:46.167545","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.079951Z","iopub.execute_input":"2022-08-09T10:45:36.081159Z","iopub.status.idle":"2022-08-09T10:45:36.087647Z","shell.execute_reply.started":"2022-08-09T10:45:36.081112Z","shell.execute_reply":"2022-08-09T10:45:36.086533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['clean_text'] = train_data['text'].apply(toclean_text)","metadata":{"papermill":{"duration":0.230275,"end_time":"2022-05-08T16:24:46.862096","exception":false,"start_time":"2022-05-08T16:24:46.631821","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.089471Z","iopub.execute_input":"2022-08-09T10:45:36.090410Z","iopub.status.idle":"2022-08-09T10:45:36.203992Z","shell.execute_reply.started":"2022-08-09T10:45:36.090363Z","shell.execute_reply":"2022-08-09T10:45:36.202916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"papermill":{"duration":0.132069,"end_time":"2022-05-08T16:24:47.108771","exception":false,"start_time":"2022-05-08T16:24:46.976702","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.205304Z","iopub.execute_input":"2022-08-09T10:45:36.205642Z","iopub.status.idle":"2022-08-09T10:45:36.218718Z","shell.execute_reply.started":"2022-08-09T10:45:36.205613Z","shell.execute_reply":"2022-08-09T10:45:36.217755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"abbreviations = {\n    \"$\" : \" dollar \",\n    \"€\" : \" euro \",\n    \"4ao\" : \"for adults only\",\n    \"a.m\" : \"before midday\",\n    \"a3\" : \"anytime anywhere anyplace\",\n    \"aamof\" : \"as a matter of fact\",\n    \"acct\" : \"account\",\n    \"adih\" : \"another day in hell\",\n    \"afaic\" : \"as far as i am concerned\",\n    \"afaict\" : \"as far as i can tell\",\n    \"afaik\" : \"as far as i know\",\n    \"afair\" : \"as far as i remember\",\n    \"afk\" : \"away from keyboard\",\n    \"app\" : \"application\",\n    \"approx\" : \"approximately\",\n    \"apps\" : \"applications\",\n    \"asap\" : \"as soon as possible\",\n    \"asl\" : \"age, sex, location\",\n    \"atk\" : \"at the keyboard\",\n    \"ave.\" : \"avenue\",\n    \"aymm\" : \"are you my mother\",\n    \"ayor\" : \"at your own risk\", \n    \"b&b\" : \"bed and breakfast\",\n    \"b+b\" : \"bed and breakfast\",\n    \"b.c\" : \"before christ\",\n    \"b2b\" : \"business to business\",\n    \"b2c\" : \"business to customer\",\n    \"b4\" : \"before\",\n    \"b4n\" : \"bye for now\",\n    \"b@u\" : \"back at you\",\n    \"bae\" : \"before anyone else\",\n    \"bak\" : \"back at keyboard\",\n    \"bbbg\" : \"bye bye be good\",\n    \"bbc\" : \"british broadcasting corporation\",\n    \"bbias\" : \"be back in a second\",\n    \"bbl\" : \"be back later\",\n    \"bbs\" : \"be back soon\",\n    \"be4\" : \"before\",\n    \"bfn\" : \"bye for now\",\n    \"blvd\" : \"boulevard\",\n    \"bout\" : \"about\",\n    \"brb\" : \"be right back\",\n    \"bros\" : \"brothers\",\n    \"brt\" : \"be right there\",\n    \"bsaaw\" : \"big smile and a wink\",\n    \"btw\" : \"by the way\",\n    \"bwl\" : \"bursting with laughter\",\n    \"c/o\" : \"care of\",\n    \"cet\" : \"central european time\",\n    \"cf\" : \"compare\",\n    \"cia\" : \"central intelligence agency\",\n    \"csl\" : \"can not stop laughing\",\n    \"cu\" : \"see you\",\n    \"cul8r\" : \"see you later\",\n    \"cv\" : \"curriculum vitae\",\n    \"cwot\" : \"complete waste of time\",\n    \"cya\" : \"see you\",\n    \"cyt\" : \"see you tomorrow\",\n    \"dae\" : \"does anyone else\",\n    \"dbmib\" : \"do not bother me i am busy\",\n    \"diy\" : \"do it yourself\",\n    \"dm\" : \"direct message\",\n    \"dwh\" : \"during work hours\",\n    \"e123\" : \"easy as one two three\",\n    \"eet\" : \"eastern european time\",\n    \"eg\" : \"example\",\n    \"embm\" : \"early morning business meeting\",\n    \"encl\" : \"enclosed\",\n    \"encl.\" : \"enclosed\",\n    \"etc\" : \"and so on\",\n    \"faq\" : \"frequently asked questions\",\n    \"fawc\" : \"for anyone who cares\",\n    \"fb\" : \"facebook\",\n    \"fc\" : \"fingers crossed\",\n    \"fig\" : \"figure\",\n    \"fimh\" : \"forever in my heart\", \n    \"ft.\" : \"feet\",\n    \"ft\" : \"featuring\",\n    \"ftl\" : \"for the loss\",\n    \"ftw\" : \"for the win\",\n    \"fwiw\" : \"for what it is worth\",\n    \"fyi\" : \"for your information\",\n    \"g9\" : \"genius\",\n    \"gahoy\" : \"get a hold of yourself\",\n    \"gal\" : \"get a life\",\n    \"gcse\" : \"general certificate of secondary education\",\n    \"gfn\" : \"gone for now\",\n    \"gg\" : \"good game\",\n    \"gl\" : \"good luck\",\n    \"glhf\" : \"good luck have fun\",\n    \"gmt\" : \"greenwich mean time\",\n    \"gmta\" : \"great minds think alike\",\n    \"gn\" : \"good night\",\n    \"g.o.a.t\" : \"greatest of all time\",\n    \"goat\" : \"greatest of all time\",\n    \"goi\" : \"get over it\",\n    \"gps\" : \"global positioning system\",\n    \"gr8\" : \"great\",\n    \"gratz\" : \"congratulations\",\n    \"gyal\" : \"girl\",\n    \"h&c\" : \"hot and cold\",\n    \"hp\" : \"horsepower\",\n    \"hr\" : \"hour\",\n    \"hrh\" : \"his royal highness\",\n    \"ht\" : \"height\",\n    \"ibrb\" : \"i will be right back\",\n    \"ic\" : \"i see\",\n    \"icq\" : \"i seek you\",\n    \"icymi\" : \"in case you missed it\",\n    \"idc\" : \"i do not care\",\n    \"idgadf\" : \"i do not give a damn fuck\",\n    \"idgaf\" : \"i do not give a fuck\",\n    \"idk\" : \"i do not know\",\n    \"ie\" : \"that is\",\n    \"i.e\" : \"that is\",\n    \"ifyp\" : \"i feel your pain\",\n    \"IG\" : \"instagram\",\n    \"iirc\" : \"if i remember correctly\",\n    \"ilu\" : \"i love you\",\n    \"ily\" : \"i love you\",\n    \"imho\" : \"in my humble opinion\",\n    \"imo\" : \"in my opinion\",\n    \"imu\" : \"i miss you\",\n    \"iow\" : \"in other words\",\n    \"irl\" : \"in real life\",\n    \"j4f\" : \"just for fun\",\n    \"jic\" : \"just in case\",\n    \"jk\" : \"just kidding\",\n    \"jsyk\" : \"just so you know\",\n    \"l8r\" : \"later\",\n    \"lb\" : \"pound\",\n    \"lbs\" : \"pounds\",\n    \"ldr\" : \"long distance relationship\",\n    \"lmao\" : \"laugh my ass off\",\n    \"lmfao\" : \"laugh my fucking ass off\",\n    \"lol\" : \"laughing out loud\",\n    \"ltd\" : \"limited\",\n    \"ltns\" : \"long time no see\",\n    \"m8\" : \"mate\",\n    \"mf\" : \"motherfucker\",\n    \"mfs\" : \"motherfuckers\",\n    \"mfw\" : \"my face when\",\n    \"mofo\" : \"motherfucker\",\n    \"mph\" : \"miles per hour\",\n    \"mr\" : \"mister\",\n    \"mrw\" : \"my reaction when\",\n    \"ms\" : \"miss\",\n    \"mte\" : \"my thoughts exactly\",\n    \"nagi\" : \"not a good idea\",\n    \"nbc\" : \"national broadcasting company\",\n    \"nbd\" : \"not big deal\",\n    \"nfs\" : \"not for sale\",\n    \"ngl\" : \"not going to lie\",\n    \"nhs\" : \"national health service\",\n    \"nrn\" : \"no reply necessary\",\n    \"nsfl\" : \"not safe for life\",\n    \"nsfw\" : \"not safe for work\",\n    \"nth\" : \"nice to have\",\n    \"nvr\" : \"never\",\n    \"nyc\" : \"new york city\",\n    \"oc\" : \"original content\",\n    \"og\" : \"original\",\n    \"ohp\" : \"overhead projector\",\n    \"oic\" : \"oh i see\",\n    \"omdb\" : \"over my dead body\",\n    \"omg\" : \"oh my god\",\n    \"omw\" : \"on my way\",\n    \"p.a\" : \"per annum\",\n    \"p.m\" : \"after midday\",\n    \"pm\" : \"prime minister\",\n    \"poc\" : \"people of color\",\n    \"pov\" : \"point of view\",\n    \"pp\" : \"pages\",\n    \"ppl\" : \"people\",\n    \"prw\" : \"parents are watching\",\n    \"ps\" : \"postscript\",\n    \"pt\" : \"point\",\n    \"ptb\" : \"please text back\",\n    \"pto\" : \"please turn over\",\n    \"qpsa\" : \"what happens\",\n    \"ratchet\" : \"rude\",\n    \"rbtl\" : \"read between the lines\",\n    \"rlrt\" : \"real life retweet\", \n    \"rofl\" : \"rolling on the floor laughing\",\n    \"roflol\" : \"rolling on the floor laughing out loud\",\n    \"rotflmao\" : \"rolling on the floor laughing my ass off\",\n    \"rt\" : \"retweet\",\n    \"ruok\" : \"are you ok\",\n    \"sfw\" : \"safe for work\",\n    \"sk8\" : \"skate\",\n    \"smh\" : \"shake my head\",\n    \"sq\" : \"square\",\n    \"srsly\" : \"seriously\", \n    \"ssdd\" : \"same stuff different day\",\n    \"tbh\" : \"to be honest\",\n    \"tbs\" : \"tablespooful\",\n    \"tbsp\" : \"tablespooful\",\n    \"tfw\" : \"that feeling when\",\n    \"thks\" : \"thank you\",\n    \"tho\" : \"though\",\n    \"thx\" : \"thank you\",\n    \"tia\" : \"thanks in advance\",\n    \"til\" : \"today i learned\",\n    \"tl;dr\" : \"too long i did not read\",\n    \"tldr\" : \"too long i did not read\",\n    \"tmb\" : \"tweet me back\",\n    \"tntl\" : \"trying not to laugh\",\n    \"ttyl\" : \"talk to you later\",\n    \"u\" : \"you\",\n    \"u2\" : \"you too\",\n    \"u4e\" : \"yours for ever\",\n    \"utc\" : \"coordinated universal time\",\n    \"w/\" : \"with\",\n    \"w/o\" : \"without\",\n    \"w8\" : \"wait\",\n    \"wassup\" : \"what is up\",\n    \"wb\" : \"welcome back\",\n    \"wtf\" : \"what the fuck\",\n    \"wtg\" : \"way to go\",\n    \"wtpa\" : \"where the party at\",\n    \"wuf\" : \"where are you from\",\n    \"wuzup\" : \"what is up\",\n    \"wywh\" : \"wish you were here\",\n    \"yd\" : \"yard\",\n    \"ygtr\" : \"you got that right\",\n    \"ynk\" : \"you never know\",\n    \"zzz\" : \"sleeping bored and tired\"\n}","metadata":{"papermill":{"duration":0.372459,"end_time":"2022-05-08T16:24:48.058864","exception":false,"start_time":"2022-05-08T16:24:47.686405","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.220178Z","iopub.execute_input":"2022-08-09T10:45:36.220829Z","iopub.status.idle":"2022-08-09T10:45:36.246010Z","shell.execute_reply.started":"2022-08-09T10:45:36.220796Z","shell.execute_reply":"2022-08-09T10:45:36.244806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Removing URLs and replacing it with URL\ndef remove_URL(text):\n    url = re.compile(r'https?://\\S+|www\\.\\S+')\n    return url.sub(r'URL',text)\n\ndef remove_HTML(text):\n    html=re.compile(r'<.*?>')\n    return html.sub(r'',text)\n\ndef remove_not_ASCII(text):\n    text = ''.join([word for word in text if word in string.printable])\n    return text\n\ndef word_abbrev(word):\n    return abbreviations[word.lower()] if word.lower() in abbreviations.keys() else word\n\ndef replace_abbrev(text):\n    string = \"\"\n    for word in text.split():\n        string += word_abbrev(word) + \" \"        \n    return string\n\ndef remove_mention(text):\n    at=re.compile(r'@\\S+')\n    return at.sub(r'USER',text)\n\ndef remove_number(text):\n    num = re.compile(r'[-+]?[.\\d]*[\\d]+[:,.\\d]*')\n    return num.sub(r'NUMBER', text)\n\ndef remove_emoji(text):\n    emoji_pattern = re.compile(\"[\"\n                           u\"\\U0001F600-\\U0001F64F\"  # emoticons\n                           u\"\\U0001F300-\\U0001F5FF\"  # symbols & pictographs\n                           u\"\\U0001F680-\\U0001F6FF\"  # transport & map symbols\n                           u\"\\U0001F1E0-\\U0001F1FF\"  # flags (iOS)\n                           u\"\\U00002702-\\U000027B0\"\n                           u\"\\U000024C2-\\U0001F251\"\n                           \"]+\", flags=re.UNICODE)\n    return emoji_pattern.sub(r'EMOJI', text)\n\ndef transcription_sad(text):\n    eyes = \"[8:=;]\"\n    nose = \"['`\\-]\"\n    smiley = re.compile(r'[8:=;][\\'\\-]?[(\\\\/]')\n    return smiley.sub(r'SADFACE', text)\n\ndef transcription_smile(text):\n    eyes = \"[8:=;]\"\n    nose = \"['`\\-]\"\n    smiley = re.compile(r'[8:=;][\\'\\-]?[)dDp]')\n    #smiley = re.compile(r'#{eyes}#{nose}[)d]+|[)d]+#{nose}#{eyes}/i')\n    return smiley.sub(r'SMILE', text)\n\ndef transcription_heart(text):\n    heart = re.compile(r'<3')\n    return heart.sub(r'HEART', text)","metadata":{"papermill":{"duration":0.132653,"end_time":"2022-05-08T16:24:48.309044","exception":false,"start_time":"2022-05-08T16:24:48.176391","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.247783Z","iopub.execute_input":"2022-08-09T10:45:36.248310Z","iopub.status.idle":"2022-08-09T10:45:36.263258Z","shell.execute_reply.started":"2022-08-09T10:45:36.248258Z","shell.execute_reply":"2022-08-09T10:45:36.262349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_tweet(text):\n\n    text = remove_URL(text)\n    text = remove_HTML(text)\n    text = remove_not_ASCII(text)\n\n    text = replace_abbrev(text)  \n    text = remove_mention(text)\n    text = remove_number(text)\n\n    text = remove_emoji(text)\n    text = transcription_sad(text)\n    text = transcription_smile(text)\n    text = transcription_heart(text)\n  \n    return text","metadata":{"papermill":{"duration":0.125627,"end_time":"2022-05-08T16:24:48.550778","exception":false,"start_time":"2022-05-08T16:24:48.425151","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.264878Z","iopub.execute_input":"2022-08-09T10:45:36.265573Z","iopub.status.idle":"2022-08-09T10:45:36.280149Z","shell.execute_reply.started":"2022-08-09T10:45:36.265536Z","shell.execute_reply":"2022-08-09T10:45:36.279261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Function clean_tweet() will remove all the noise in the text.","metadata":{"papermill":{"duration":0.114475,"end_time":"2022-05-08T16:24:48.779631","exception":false,"start_time":"2022-05-08T16:24:48.665156","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data[\"clean_text\"] = train_data[\"clean_text\"].apply(clean_tweet)","metadata":{"papermill":{"duration":0.58557,"end_time":"2022-05-08T16:24:49.483454","exception":false,"start_time":"2022-05-08T16:24:48.897884","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.281519Z","iopub.execute_input":"2022-08-09T10:45:36.282520Z","iopub.status.idle":"2022-08-09T10:45:36.637454Z","shell.execute_reply.started":"2022-08-09T10:45:36.282484Z","shell.execute_reply":"2022-08-09T10:45:36.636487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"papermill":{"duration":0.131,"end_time":"2022-05-08T16:24:49.729434","exception":false,"start_time":"2022-05-08T16:24:49.598434","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.639891Z","iopub.execute_input":"2022-08-09T10:45:36.640237Z","iopub.status.idle":"2022-08-09T10:45:36.657564Z","shell.execute_reply.started":"2022-08-09T10:45:36.640205Z","shell.execute_reply":"2022-08-09T10:45:36.656329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(stopwords.words('english'))","metadata":{"papermill":{"duration":0.129858,"end_time":"2022-05-08T16:24:50.433453","exception":false,"start_time":"2022-05-08T16:24:50.303595","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.665588Z","iopub.execute_input":"2022-08-09T10:45:36.665998Z","iopub.status.idle":"2022-08-09T10:45:36.672710Z","shell.execute_reply.started":"2022-08-09T10:45:36.665964Z","shell.execute_reply":"2022-08-09T10:45:36.671524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def toremove_stopword(text):\n    remove_stopword = [word for word in text.split() if word.lower() not in stopwords.words('english')]\n\n    return remove_stopword","metadata":{"papermill":{"duration":0.12206,"end_time":"2022-05-08T16:24:50.670328","exception":false,"start_time":"2022-05-08T16:24:50.548268","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.674277Z","iopub.execute_input":"2022-08-09T10:45:36.675343Z","iopub.status.idle":"2022-08-09T10:45:36.684478Z","shell.execute_reply.started":"2022-08-09T10:45:36.675269Z","shell.execute_reply":"2022-08-09T10:45:36.683350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['clean_text'] = train_data['clean_text'].apply(toremove_stopword)","metadata":{"papermill":{"duration":17.672723,"end_time":"2022-05-08T16:25:08.458698","exception":false,"start_time":"2022-05-08T16:24:50.785975","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:36.685815Z","iopub.execute_input":"2022-08-09T10:45:36.686169Z","iopub.status.idle":"2022-08-09T10:45:51.569521Z","shell.execute_reply.started":"2022-08-09T10:45:36.686110Z","shell.execute_reply":"2022-08-09T10:45:51.568289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"papermill":{"duration":0.134705,"end_time":"2022-05-08T16:25:08.710214","exception":false,"start_time":"2022-05-08T16:25:08.575509","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.570933Z","iopub.execute_input":"2022-08-09T10:45:51.571386Z","iopub.status.idle":"2022-08-09T10:45:51.589896Z","shell.execute_reply.started":"2022-08-09T10:45:51.571340Z","shell.execute_reply":"2022-08-09T10:45:51.588637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_features=3000\ntokenizer=Tokenizer(num_words=max_features,split=' ')\ntokenizer.fit_on_texts(train_data['clean_text'].values)\nX = tokenizer.texts_to_sequences(train_data['clean_text'].values)\nX = pad_sequences(X)","metadata":{"papermill":{"duration":0.34499,"end_time":"2022-05-08T16:25:10.10312","exception":false,"start_time":"2022-05-08T16:25:09.75813","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.592222Z","iopub.execute_input":"2022-08-09T10:45:51.592714Z","iopub.status.idle":"2022-08-09T10:45:51.779092Z","shell.execute_reply.started":"2022-08-09T10:45:51.592670Z","shell.execute_reply":"2022-08-09T10:45:51.778250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"papermill":{"duration":0.124851,"end_time":"2022-05-08T16:25:10.579362","exception":false,"start_time":"2022-05-08T16:25:10.454511","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.780130Z","iopub.execute_input":"2022-08-09T10:45:51.781153Z","iopub.status.idle":"2022-08-09T10:45:51.787982Z","shell.execute_reply.started":"2022-08-09T10:45:51.781112Z","shell.execute_reply":"2022-08-09T10:45:51.786896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X[0]","metadata":{"papermill":{"duration":0.128212,"end_time":"2022-05-08T16:25:11.055498","exception":false,"start_time":"2022-05-08T16:25:10.927286","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.789226Z","iopub.execute_input":"2022-08-09T10:45:51.789554Z","iopub.status.idle":"2022-08-09T10:45:51.800847Z","shell.execute_reply.started":"2022-08-09T10:45:51.789524Z","shell.execute_reply":"2022-08-09T10:45:51.799611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer.sequences_to_texts([[ 713,  154,   56, 1434,   14]])","metadata":{"papermill":{"duration":0.125831,"end_time":"2022-05-08T16:25:11.535203","exception":false,"start_time":"2022-05-08T16:25:11.409372","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.802361Z","iopub.execute_input":"2022-08-09T10:45:51.802756Z","iopub.status.idle":"2022-08-09T10:45:51.811886Z","shell.execute_reply.started":"2022-08-09T10:45:51.802725Z","shell.execute_reply":"2022-08-09T10:45:51.811055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"LSTM Model","metadata":{"papermill":{"duration":0.117269,"end_time":"2022-05-08T16:25:12.002193","exception":false,"start_time":"2022-05-08T16:25:11.884924","status":"completed"},"tags":[]}},{"cell_type":"code","source":"y = train_data['target']","metadata":{"papermill":{"duration":0.123839,"end_time":"2022-05-08T16:25:12.481837","exception":false,"start_time":"2022-05-08T16:25:12.357998","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.813665Z","iopub.execute_input":"2022-08-09T10:45:51.814458Z","iopub.status.idle":"2022-08-09T10:45:51.821700Z","shell.execute_reply.started":"2022-08-09T10:45:51.814421Z","shell.execute_reply":"2022-08-09T10:45:51.820532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.2, random_state =41)","metadata":{"papermill":{"duration":0.128322,"end_time":"2022-05-08T16:25:12.7271","exception":false,"start_time":"2022-05-08T16:25:12.598778","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.823395Z","iopub.execute_input":"2022-08-09T10:45:51.824070Z","iopub.status.idle":"2022-08-09T10:45:51.834340Z","shell.execute_reply.started":"2022-08-09T10:45:51.824037Z","shell.execute_reply":"2022-08-09T10:45:51.833352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'","metadata":{"papermill":{"duration":0.122304,"end_time":"2022-05-08T16:25:14.934386","exception":false,"start_time":"2022-05-08T16:25:14.812082","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.835732Z","iopub.execute_input":"2022-08-09T10:45:51.836609Z","iopub.status.idle":"2022-08-09T10:45:51.841640Z","shell.execute_reply.started":"2022-08-09T10:45:51.836575Z","shell.execute_reply":"2022-08-09T10:45:51.840573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embed_dim = 32\nlstm_out = 32\nmodel = Sequential()\nmodel.add(Embedding(max_features, embed_dim,input_length = X.shape[1]))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.4))\nmodel.add(Dense(1,activation='sigmoid'))\nadam = optimizers.Adam(learning_rate=0.002)\nmodel.compile(loss = 'binary_crossentropy', optimizer=adam ,metrics = ['accuracy'])\nprint(model.summary())","metadata":{"papermill":{"duration":0.459879,"end_time":"2022-05-08T16:25:15.51228","exception":false,"start_time":"2022-05-08T16:25:15.052401","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.842999Z","iopub.execute_input":"2022-08-09T10:45:51.843853Z","iopub.status.idle":"2022-08-09T10:45:51.994589Z","shell.execute_reply.started":"2022-08-09T10:45:51.843818Z","shell.execute_reply":"2022-08-09T10:45:51.993347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, epochs = 10, batch_size=32, validation_data=(X_test, y_test))","metadata":{"papermill":{"duration":41.818349,"end_time":"2022-05-08T16:25:57.716522","exception":false,"start_time":"2022-05-08T16:25:15.898173","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:45:51.996258Z","iopub.execute_input":"2022-08-09T10:45:51.996983Z","iopub.status.idle":"2022-08-09T10:47:07.345123Z","shell.execute_reply.started":"2022-08-09T10:45:51.996938Z","shell.execute_reply":"2022-08-09T10:47:07.344269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"  y_pred = model.predict(X_test).round()","metadata":{"papermill":{"duration":0.809288,"end_time":"2022-05-08T16:25:58.861948","exception":false,"start_time":"2022-05-08T16:25:58.05266","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:07.346631Z","iopub.execute_input":"2022-08-09T10:47:07.347230Z","iopub.status.idle":"2022-08-09T10:47:08.123458Z","shell.execute_reply.started":"2022-08-09T10:47:07.347197Z","shell.execute_reply":"2022-08-09T10:47:08.122253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"papermill":{"duration":0.3496,"end_time":"2022-05-08T16:25:59.547523","exception":false,"start_time":"2022-05-08T16:25:59.197923","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:08.124985Z","iopub.execute_input":"2022-08-09T10:47:08.125362Z","iopub.status.idle":"2022-08-09T10:47:08.132227Z","shell.execute_reply.started":"2022-08-09T10:47:08.125326Z","shell.execute_reply":"2022-08-09T10:47:08.131476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Evaluation","metadata":{"papermill":{"duration":0.342746,"end_time":"2022-05-08T16:26:00.255155","exception":false,"start_time":"2022-05-08T16:25:59.912409","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_accuracy = round(metrics.accuracy_score(y_train,model.predict(X_train).round())*100)\ntrain_accuracy","metadata":{"papermill":{"duration":1.007117,"end_time":"2022-05-08T16:26:01.598741","exception":false,"start_time":"2022-05-08T16:26:00.591624","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:08.133727Z","iopub.execute_input":"2022-08-09T10:47:08.134354Z","iopub.status.idle":"2022-08-09T10:47:09.702178Z","shell.execute_reply.started":"2022-08-09T10:47:08.134316Z","shell.execute_reply":"2022-08-09T10:47:09.701311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Accuracy  is  : ', (metrics.accuracy_score(y_test, y_pred)))\nprint('Recall  is    : ', (metrics.recall_score(y_test, y_pred)))\nprint('Precision  is : ', (metrics.precision_score(y_test, y_pred)))","metadata":{"papermill":{"duration":0.352756,"end_time":"2022-05-08T16:26:02.287462","exception":false,"start_time":"2022-05-08T16:26:01.934706","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:09.703945Z","iopub.execute_input":"2022-08-09T10:47:09.705213Z","iopub.status.idle":"2022-08-09T10:47:09.717695Z","shell.execute_reply.started":"2022-08-09T10:47:09.705175Z","shell.execute_reply":"2022-08-09T10:47:09.716366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conm = confusion_matrix(y_test,y_pred)\nconm","metadata":{"papermill":{"duration":0.359782,"end_time":"2022-05-08T16:26:03.653672","exception":false,"start_time":"2022-05-08T16:26:03.29389","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:09.719591Z","iopub.execute_input":"2022-08-09T10:47:09.720386Z","iopub.status.idle":"2022-08-09T10:47:09.732843Z","shell.execute_reply.started":"2022-08-09T10:47:09.720335Z","shell.execute_reply":"2022-08-09T10:47:09.731523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7, 5))\nsns.heatmap(conm, annot=True, fmt='d', cmap='cool')\nplt.show()","metadata":{"papermill":{"duration":0.61697,"end_time":"2022-05-08T16:26:04.608542","exception":false,"start_time":"2022-05-08T16:26:03.991572","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:09.734852Z","iopub.execute_input":"2022-08-09T10:47:09.735191Z","iopub.status.idle":"2022-08-09T10:47:09.987882Z","shell.execute_reply.started":"2022-08-09T10:47:09.735161Z","shell.execute_reply":"2022-08-09T10:47:09.986572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))","metadata":{"papermill":{"duration":0.354435,"end_time":"2022-05-08T16:26:05.980853","exception":false,"start_time":"2022-05-08T16:26:05.626418","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:09.989516Z","iopub.execute_input":"2022-08-09T10:47:09.989980Z","iopub.status.idle":"2022-08-09T10:47:10.007927Z","shell.execute_reply.started":"2022-08-09T10:47:09.989933Z","shell.execute_reply":"2022-08-09T10:47:10.006638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n","metadata":{"papermill":{"duration":0.338659,"end_time":"2022-05-08T16:26:06.660875","exception":false,"start_time":"2022-05-08T16:26:06.322216","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_data = pd.read_csv('../input/nlp-getting-started/test.csv')","metadata":{"papermill":{"duration":0.370451,"end_time":"2022-05-08T16:26:08.745734","exception":false,"start_time":"2022-05-08T16:26:08.375283","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.009442Z","iopub.execute_input":"2022-08-09T10:47:10.010090Z","iopub.status.idle":"2022-08-09T10:47:10.029853Z","shell.execute_reply.started":"2022-08-09T10:47:10.010052Z","shell.execute_reply":"2022-08-09T10:47:10.028802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head().style.background_gradient(cmap='coolwarm')","metadata":{"papermill":{"duration":0.355049,"end_time":"2022-05-08T16:26:09.447122","exception":false,"start_time":"2022-05-08T16:26:09.092073","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.031270Z","iopub.execute_input":"2022-08-09T10:47:10.031713Z","iopub.status.idle":"2022-08-09T10:47:10.049149Z","shell.execute_reply.started":"2022-08-09T10:47:10.031672Z","shell.execute_reply":"2022-08-09T10:47:10.048236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['clean_text'] = test_data['text'].apply(toclean_text)\ntest_data[\"clean_text\"] = test_data[\"clean_text\"].apply(clean_tweet)","metadata":{"papermill":{"duration":0.620903,"end_time":"2022-05-08T16:26:11.092668","exception":false,"start_time":"2022-05-08T16:26:10.471765","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.050711Z","iopub.execute_input":"2022-08-09T10:47:10.051365Z","iopub.status.idle":"2022-08-09T10:47:10.260845Z","shell.execute_reply.started":"2022-08-09T10:47:10.051330Z","shell.execute_reply":"2022-08-09T10:47:10.260001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['clean_text'].head()","metadata":{"papermill":{"duration":0.352325,"end_time":"2022-05-08T16:26:11.784741","exception":false,"start_time":"2022-05-08T16:26:11.432416","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.262328Z","iopub.execute_input":"2022-08-09T10:47:10.262921Z","iopub.status.idle":"2022-08-09T10:47:10.270197Z","shell.execute_reply.started":"2022-08-09T10:47:10.262887Z","shell.execute_reply":"2022-08-09T10:47:10.269250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l =50\nmax_features=5000\ntokenizer=Tokenizer(num_words=max_features,split=' ')\ntokenizer.fit_on_texts(train_data['clean_text'].values)\nX = tokenizer.texts_to_sequences(train_data['clean_text'].values)\nX = pad_sequences(X, maxlen =l)","metadata":{"papermill":{"duration":0.605206,"end_time":"2022-05-08T16:26:13.425813","exception":false,"start_time":"2022-05-08T16:26:12.820607","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.271550Z","iopub.execute_input":"2022-08-09T10:47:10.272389Z","iopub.status.idle":"2022-08-09T10:47:10.461889Z","shell.execute_reply.started":"2022-08-09T10:47:10.272356Z","shell.execute_reply":"2022-08-09T10:47:10.460807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer.fit_on_texts(train_data['clean_text'].values)\ntest_token = tokenizer.texts_to_sequences(test_data['clean_text'].values)\ntest_token = pad_sequences(test_token, maxlen =l)","metadata":{"papermill":{"duration":0.526102,"end_time":"2022-05-08T16:26:14.29246","exception":false,"start_time":"2022-05-08T16:26:13.766358","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.463539Z","iopub.execute_input":"2022-08-09T10:47:10.463977Z","iopub.status.idle":"2022-08-09T10:47:10.636913Z","shell.execute_reply.started":"2022-08-09T10:47:10.463929Z","shell.execute_reply":"2022-08-09T10:47:10.635684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embed_dim = 100\nlstm_out = 100\nmodel = Sequential()\nmodel.add(Embedding(max_features, embed_dim,input_length = X.shape[1]))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(lstm_out, dropout=0.2, return_sequences=True,recurrent_dropout=0.4))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(lstm_out,dropout=0.2, recurrent_dropout=0.2))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1,activation='sigmoid'))\nadam = optimizers.Adam(learning_rate=2e-3)\nmodel.compile(loss = 'binary_crossentropy', optimizer=adam ,metrics = ['accuracy'])\nprint(model.summary())","metadata":{"papermill":{"duration":0.674853,"end_time":"2022-05-08T16:26:15.311286","exception":false,"start_time":"2022-05-08T16:26:14.636433","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.638407Z","iopub.execute_input":"2022-08-09T10:47:10.638834Z","iopub.status.idle":"2022-08-09T10:47:10.905625Z","shell.execute_reply.started":"2022-08-09T10:47:10.638800Z","shell.execute_reply":"2022-08-09T10:47:10.904370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)","metadata":{"papermill":{"duration":0.358782,"end_time":"2022-05-08T16:26:17.619283","exception":false,"start_time":"2022-05-08T16:26:17.260501","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.907192Z","iopub.execute_input":"2022-08-09T10:47:10.908603Z","iopub.status.idle":"2022-08-09T10:47:10.913640Z","shell.execute_reply.started":"2022-08-09T10:47:10.908555Z","shell.execute_reply":"2022-08-09T10:47:10.912707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X,y, epochs = 10,validation_split = 0.2 ,callbacks=[es_callback], batch_size=32)","metadata":{"papermill":{"duration":153.206056,"end_time":"2022-05-08T16:28:51.17093","exception":false,"start_time":"2022-05-08T16:26:17.964874","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:47:10.916179Z","iopub.execute_input":"2022-08-09T10:47:10.916515Z","iopub.status.idle":"2022-08-09T10:52:45.615221Z","shell.execute_reply.started":"2022-08-09T10:47:10.916486Z","shell.execute_reply":"2022-08-09T10:52:45.614059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat = model.predict(test_token).round()\nsubmission = pd.read_csv(\"/kaggle/input/nlp-getting-started/sample_submission.csv\")\nsubmission['target'] = np.round(y_hat).astype('int')\nsubmission.to_csv('submission.csv', index=False)\nsubmission.describe().style.background_gradient(cmap='coolwarm')\n","metadata":{"papermill":{"duration":3.281059,"end_time":"2022-05-08T16:28:55.11701","exception":false,"start_time":"2022-05-08T16:28:51.835951","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:52:45.616609Z","iopub.execute_input":"2022-08-09T10:52:45.617374Z","iopub.status.idle":"2022-08-09T10:52:54.197734Z","shell.execute_reply.started":"2022-08-09T10:52:45.617339Z","shell.execute_reply":"2022-08-09T10:52:54.196355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.target.value_counts().plot.bar();","metadata":{"papermill":{"duration":0.973082,"end_time":"2022-05-08T16:28:56.773497","exception":false,"start_time":"2022-05-08T16:28:55.800415","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-09T10:52:54.199177Z","iopub.execute_input":"2022-08-09T10:52:54.199646Z","iopub.status.idle":"2022-08-09T10:52:54.417333Z","shell.execute_reply.started":"2022-08-09T10:52:54.199601Z","shell.execute_reply":"2022-08-09T10:52:54.416184Z"},"trusted":true},"execution_count":null,"outputs":[]}]}