{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing the libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import models,layers\nfrom tensorflow.keras.preprocessing import sequence,text\nimport nltk\nimport re\nfrom nltk.stem.porter import PorterStemmer\nfrom nltk.corpus import stopwords\ntrain_df=pd.read_csv('../input/nlp-getting-started/train.csv')\ntest_df=pd.read_csv('../input/nlp-getting-started/test.csv')\nsub_df=pd.read_csv('../input/nlp-getting-started/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:24:47.322691Z","iopub.execute_input":"2022-07-14T16:24:47.323106Z","iopub.status.idle":"2022-07-14T16:24:47.381286Z","shell.execute_reply.started":"2022-07-14T16:24:47.323077Z","shell.execute_reply":"2022-07-14T16:24:47.380203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:17:42.726746Z","iopub.execute_input":"2022-07-14T16:17:42.727484Z","iopub.status.idle":"2022-07-14T16:17:42.756515Z","shell.execute_reply.started":"2022-07-14T16:17:42.727437Z","shell.execute_reply":"2022-07-14T16:17:42.755255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:17:55.020754Z","iopub.execute_input":"2022-07-14T16:17:55.021359Z","iopub.status.idle":"2022-07-14T16:17:55.047075Z","shell.execute_reply.started":"2022-07-14T16:17:55.021314Z","shell.execute_reply":"2022-07-14T16:17:55.045647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:18:07.602655Z","iopub.execute_input":"2022-07-14T16:18:07.603276Z","iopub.status.idle":"2022-07-14T16:18:07.617188Z","shell.execute_reply.started":"2022-07-14T16:18:07.603228Z","shell.execute_reply":"2022-07-14T16:18:07.615769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=train_df.drop(columns=['keyword','location'],axis=1)\ntrain_df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:18:58.623435Z","iopub.execute_input":"2022-07-14T16:18:58.624119Z","iopub.status.idle":"2022-07-14T16:18:58.656865Z","shell.execute_reply.started":"2022-07-14T16:18:58.624070Z","shell.execute_reply":"2022-07-14T16:18:58.655476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(columns='id',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:19:24.421919Z","iopub.execute_input":"2022-07-14T16:19:24.422358Z","iopub.status.idle":"2022-07-14T16:19:24.431166Z","shell.execute_reply.started":"2022-07-14T16:19:24.422282Z","shell.execute_reply":"2022-07-14T16:19:24.429784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:19:30.563457Z","iopub.execute_input":"2022-07-14T16:19:30.564846Z","iopub.status.idle":"2022-07-14T16:19:30.580175Z","shell.execute_reply.started":"2022-07-14T16:19:30.564800Z","shell.execute_reply":"2022-07-14T16:19:30.578460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['target'].value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:20:13.212522Z","iopub.execute_input":"2022-07-14T16:20:13.213249Z","iopub.status.idle":"2022-07-14T16:20:13.228417Z","shell.execute_reply.started":"2022-07-14T16:20:13.213217Z","shell.execute_reply":"2022-07-14T16:20:13.223095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:25:03.982818Z","iopub.execute_input":"2022-07-14T16:25:03.983459Z","iopub.status.idle":"2022-07-14T16:25:04.004500Z","shell.execute_reply.started":"2022-07-14T16:25:03.983426Z","shell.execute_reply":"2022-07-14T16:25:04.003080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:25:15.485896Z","iopub.execute_input":"2022-07-14T16:25:15.487365Z","iopub.status.idle":"2022-07-14T16:25:15.510070Z","shell.execute_reply.started":"2022-07-14T16:25:15.487298Z","shell.execute_reply":"2022-07-14T16:25:15.508707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:25:29.372875Z","iopub.execute_input":"2022-07-14T16:25:29.373477Z","iopub.status.idle":"2022-07-14T16:25:29.383883Z","shell.execute_reply.started":"2022-07-14T16:25:29.373430Z","shell.execute_reply":"2022-07-14T16:25:29.382394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:25:36.842773Z","iopub.execute_input":"2022-07-14T16:25:36.844383Z","iopub.status.idle":"2022-07-14T16:25:36.860419Z","shell.execute_reply.started":"2022-07-14T16:25:36.844306Z","shell.execute_reply":"2022-07-14T16:25:36.858913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:26:00.359252Z","iopub.execute_input":"2022-07-14T16:26:00.362119Z","iopub.status.idle":"2022-07-14T16:26:00.379180Z","shell.execute_reply.started":"2022-07-14T16:26:00.362072Z","shell.execute_reply":"2022-07-14T16:26:00.377655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['text'][0]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:29:20.232580Z","iopub.execute_input":"2022-07-14T16:29:20.233012Z","iopub.status.idle":"2022-07-14T16:29:20.242492Z","shell.execute_reply.started":"2022-07-14T16:29:20.232983Z","shell.execute_reply":"2022-07-14T16:29:20.241248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"voc_size=10000\nps=PorterStemmer()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:30:12.802773Z","iopub.execute_input":"2022-07-14T16:30:12.803140Z","iopub.status.idle":"2022-07-14T16:30:12.809041Z","shell.execute_reply.started":"2022-07-14T16:30:12.803112Z","shell.execute_reply":"2022-07-14T16:30:12.807691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train_df['text']\ny=train_df['target']","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:30:45.133068Z","iopub.execute_input":"2022-07-14T16:30:45.133550Z","iopub.status.idle":"2022-07-14T16:30:45.139256Z","shell.execute_reply.started":"2022-07-14T16:30:45.133522Z","shell.execute_reply":"2022-07-14T16:30:45.137696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:30:49.701833Z","iopub.execute_input":"2022-07-14T16:30:49.702272Z","iopub.status.idle":"2022-07-14T16:30:49.713382Z","shell.execute_reply.started":"2022-07-14T16:30:49.702243Z","shell.execute_reply":"2022-07-14T16:30:49.712064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:30:56.321809Z","iopub.execute_input":"2022-07-14T16:30:56.322181Z","iopub.status.idle":"2022-07-14T16:30:56.332816Z","shell.execute_reply.started":"2022-07-14T16:30:56.322153Z","shell.execute_reply":"2022-07-14T16:30:56.331337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:31:21.542444Z","iopub.execute_input":"2022-07-14T16:31:21.542859Z","iopub.status.idle":"2022-07-14T16:31:21.550801Z","shell.execute_reply.started":"2022-07-14T16:31:21.542828Z","shell.execute_reply":"2022-07-14T16:31:21.549389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corpus=[]\n\nfor i in range(0,len(train_df)):\n    print(i)\n    review=re.sub('[^a-zA-Z]',' ',x[i])\n    review=review.lower()\n    review=review.split()\n    \n    review=[ps.stem(word) for word in review if not word in stopwords.words('english')]\n    review=' '.join(review)\n    corpus.append(review)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:35:21.333159Z","iopub.execute_input":"2022-07-14T16:35:21.333542Z","iopub.status.idle":"2022-07-14T16:35:47.121386Z","shell.execute_reply.started":"2022-07-14T16:35:21.333515Z","shell.execute_reply":"2022-07-14T16:35:47.119913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corpus","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:37:33.933911Z","iopub.execute_input":"2022-07-14T16:37:33.935562Z","iopub.status.idle":"2022-07-14T16:37:33.976775Z","shell.execute_reply.started":"2022-07-14T16:37:33.935512Z","shell.execute_reply":"2022-07-14T16:37:33.975453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_hot_reps1=[text.one_hot(word,voc_size) for word in corpus]\none_hot_reps1","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:37:58.356243Z","iopub.execute_input":"2022-07-14T16:37:58.357784Z","iopub.status.idle":"2022-07-14T16:37:58.758954Z","shell.execute_reply.started":"2022-07-14T16:37:58.357735Z","shell.execute_reply":"2022-07-14T16:37:58.757696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sent_length=500\n\nembedded_docs1=sequence.pad_sequences(one_hot_reps1,padding='pre',maxlen=sent_length)\nembedded_docs1","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:40:06.221825Z","iopub.execute_input":"2022-07-14T16:40:06.222703Z","iopub.status.idle":"2022-07-14T16:40:06.276271Z","shell.execute_reply.started":"2022-07-14T16:40:06.222653Z","shell.execute_reply":"2022-07-14T16:40:06.274835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=embedded_docs1.copy()\nx","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:43:27.551756Z","iopub.execute_input":"2022-07-14T16:43:27.552163Z","iopub.status.idle":"2022-07-14T16:43:27.568377Z","shell.execute_reply.started":"2022-07-14T16:43:27.552131Z","shell.execute_reply":"2022-07-14T16:43:27.566847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:45:01.542780Z","iopub.execute_input":"2022-07-14T16:45:01.543437Z","iopub.status.idle":"2022-07-14T16:45:01.554613Z","shell.execute_reply.started":"2022-07-14T16:45:01.543389Z","shell.execute_reply":"2022-07-14T16:45:01.553292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:43:49.612461Z","iopub.execute_input":"2022-07-14T16:43:49.612872Z","iopub.status.idle":"2022-07-14T16:43:49.623008Z","shell.execute_reply.started":"2022-07-14T16:43:49.612842Z","shell.execute_reply":"2022-07-14T16:43:49.621491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:44:38.141842Z","iopub.execute_input":"2022-07-14T16:44:38.142256Z","iopub.status.idle":"2022-07-14T16:44:38.159214Z","shell.execute_reply.started":"2022-07-14T16:44:38.142226Z","shell.execute_reply":"2022-07-14T16:44:38.157928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:44:45.173203Z","iopub.execute_input":"2022-07-14T16:44:45.173577Z","iopub.status.idle":"2022-07-14T16:44:45.182947Z","shell.execute_reply.started":"2022-07-14T16:44:45.173547Z","shell.execute_reply":"2022-07-14T16:44:45.181547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:44:52.691514Z","iopub.execute_input":"2022-07-14T16:44:52.692213Z","iopub.status.idle":"2022-07-14T16:44:52.701104Z","shell.execute_reply.started":"2022-07-14T16:44:52.692171Z","shell.execute_reply":"2022-07-14T16:44:52.699707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:45:08.372219Z","iopub.execute_input":"2022-07-14T16:45:08.373110Z","iopub.status.idle":"2022-07-14T16:45:08.382920Z","shell.execute_reply.started":"2022-07-14T16:45:08.373077Z","shell.execute_reply":"2022-07-14T16:45:08.381449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:45:35.912116Z","iopub.execute_input":"2022-07-14T16:45:35.913086Z","iopub.status.idle":"2022-07-14T16:45:35.921725Z","shell.execute_reply.started":"2022-07-14T16:45:35.913023Z","shell.execute_reply":"2022-07-14T16:45:35.920162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_vector=500","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:47:16.673663Z","iopub.execute_input":"2022-07-14T16:47:16.674068Z","iopub.status.idle":"2022-07-14T16:47:16.679498Z","shell.execute_reply.started":"2022-07-14T16:47:16.674026Z","shell.execute_reply":"2022-07-14T16:47:16.678209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:48:30.393210Z","iopub.execute_input":"2022-07-14T16:48:30.393611Z","iopub.status.idle":"2022-07-14T16:48:30.400060Z","shell.execute_reply.started":"2022-07-14T16:48:30.393577Z","shell.execute_reply":"2022-07-14T16:48:30.398652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn1=models.Sequential([\n    \n    layers.Embedding(voc_size,feature_vector,input_length=sent_length),\n    layers.Conv1D(64,8,activation='relu'),\n    layers.MaxPooling1D(2),\n    layers.Dropout(0.8),\n    \n    layers.Dense(40,activation='relu'),\n    layers.Dropout(0.9),\n    \n    layers.Dense(29,activation='relu'),\n    layers.GlobalMaxPooling1D(),\n    \n    layers.Dense(1,activation='sigmoid')\n    \n    \n    ])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:48:33.082191Z","iopub.execute_input":"2022-07-14T16:48:33.082560Z","iopub.status.idle":"2022-07-14T16:48:33.181506Z","shell.execute_reply.started":"2022-07-14T16:48:33.082529Z","shell.execute_reply":"2022-07-14T16:48:33.180298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:48:41.002544Z","iopub.execute_input":"2022-07-14T16:48:41.003716Z","iopub.status.idle":"2022-07-14T16:48:41.014300Z","shell.execute_reply.started":"2022-07-14T16:48:41.003668Z","shell.execute_reply":"2022-07-14T16:48:41.012591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn1.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nnn1.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=100)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:52:58.850894Z","iopub.execute_input":"2022-07-14T16:52:58.851377Z","iopub.status.idle":"2022-07-14T16:57:56.311451Z","shell.execute_reply.started":"2022-07-14T16:52:58.851347Z","shell.execute_reply":"2022-07-14T16:57:56.310134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn1.evaluate(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:59:12.870499Z","iopub.execute_input":"2022-07-14T16:59:12.871372Z","iopub.status.idle":"2022-07-14T16:59:13.917902Z","shell.execute_reply.started":"2022-07-14T16:59:12.871295Z","shell.execute_reply":"2022-07-14T16:59:13.916664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn1.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:59:27.115884Z","iopub.execute_input":"2022-07-14T16:59:27.116289Z","iopub.status.idle":"2022-07-14T16:59:27.422160Z","shell.execute_reply.started":"2022-07-14T16:59:27.116260Z","shell.execute_reply":"2022-07-14T16:59:27.421024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred1=nn1.predict(x_test)\ny_pred1=(y_pred1>0.5)\ny_pred1[:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:59:30.599937Z","iopub.execute_input":"2022-07-14T16:59:30.600422Z","iopub.status.idle":"2022-07-14T16:59:31.093502Z","shell.execute_reply.started":"2022-07-14T16:59:30.600392Z","shell.execute_reply":"2022-07-14T16:59:31.092203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle=LabelEncoder()\ny_pred1=pd.DataFrame(y_pred1,columns=['pred1'])\ny_pred1=le.fit_transform(y_pred1['pred1'])\ny_pred1[:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:00:48.530750Z","iopub.execute_input":"2022-07-14T17:00:48.531120Z","iopub.status.idle":"2022-07-14T17:00:48.542948Z","shell.execute_reply.started":"2022-07-14T17:00:48.531091Z","shell.execute_reply":"2022-07-14T17:00:48.541360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,classification_report,f1_score","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:22:39.166604Z","iopub.execute_input":"2022-07-14T17:22:39.167093Z","iopub.status.idle":"2022-07-14T17:22:39.173665Z","shell.execute_reply.started":"2022-07-14T17:22:39.167047Z","shell.execute_reply":"2022-07-14T17:22:39.172310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:01:01.510823Z","iopub.execute_input":"2022-07-14T17:01:01.511355Z","iopub.status.idle":"2022-07-14T17:01:01.526340Z","shell.execute_reply.started":"2022-07-14T17:01:01.511314Z","shell.execute_reply":"2022-07-14T17:01:01.525168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,y_pred1))","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:23:55.322596Z","iopub.execute_input":"2022-07-14T17:23:55.323066Z","iopub.status.idle":"2022-07-14T17:23:55.341364Z","shell.execute_reply.started":"2022-07-14T17:23:55.323036Z","shell.execute_reply":"2022-07-14T17:23:55.339526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:23:57.855197Z","iopub.execute_input":"2022-07-14T17:23:57.855565Z","iopub.status.idle":"2022-07-14T17:23:57.869345Z","shell.execute_reply.started":"2022-07-14T17:23:57.855536Z","shell.execute_reply":"2022-07-14T17:23:57.868050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying another neural model by using Bidirectional LSTM","metadata":{}},{"cell_type":"code","source":"nn2=models.Sequential([\n    \n    layers.Embedding(voc_size,feature_vector,input_length=sent_length),\n    layers.Dropout(0.5),\n    layers.Bidirectional(layers.LSTM(199)),\n    layers.Dropout(0.4),\n    layers.Dense(399,activation='relu'),\n    layers.Dropout(0.7),\n    layers.Dense(43,activation='relu'),\n    layers.Dropout(0.8),\n    layers.Dense(1,activation='sigmoid')\n    \n])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:01:57.520335Z","iopub.execute_input":"2022-07-14T17:01:57.520740Z","iopub.status.idle":"2022-07-14T17:01:58.174410Z","shell.execute_reply.started":"2022-07-14T17:01:57.520710Z","shell.execute_reply":"2022-07-14T17:01:58.173211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn2.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:02:08.079741Z","iopub.execute_input":"2022-07-14T17:02:08.080120Z","iopub.status.idle":"2022-07-14T17:02:08.089293Z","shell.execute_reply.started":"2022-07-14T17:02:08.080089Z","shell.execute_reply":"2022-07-14T17:02:08.087504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn2.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nnn2.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:10:19.428837Z","iopub.execute_input":"2022-07-14T17:10:19.429939Z","iopub.status.idle":"2022-07-14T17:13:44.276545Z","shell.execute_reply.started":"2022-07-14T17:10:19.429892Z","shell.execute_reply":"2022-07-14T17:13:44.275195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn2.evaluate(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:18:57.415180Z","iopub.execute_input":"2022-07-14T17:18:57.415794Z","iopub.status.idle":"2022-07-14T17:19:07.745359Z","shell.execute_reply.started":"2022-07-14T17:18:57.415744Z","shell.execute_reply":"2022-07-14T17:19:07.743849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn2.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:19:07.748185Z","iopub.execute_input":"2022-07-14T17:19:07.748613Z","iopub.status.idle":"2022-07-14T17:19:09.463994Z","shell.execute_reply.started":"2022-07-14T17:19:07.748571Z","shell.execute_reply":"2022-07-14T17:19:09.462576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying ML models","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom xgboost import XGBClassifier\nfrom catboost import CatBoostClassifier\nfrom lightgbm import LGBMClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:15:09.771495Z","iopub.execute_input":"2022-07-14T17:15:09.771962Z","iopub.status.idle":"2022-07-14T17:15:12.298365Z","shell.execute_reply.started":"2022-07-14T17:15:09.771929Z","shell.execute_reply":"2022-07-14T17:15:12.297151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr=LogisticRegression(max_iter=150,random_state=28)\nrfg=RandomForestClassifier(n_estimators=125,criterion='entropy')\ngnb=GaussianNB()\nxgb=XGBClassifier(n_estimators=199,learning_rate=0.05)\nlight=LGBMClassifier(learning_rate=0.001,n_estimators=101)\ncat=CatBoostClassifier(iterations=600,learning_rate=0.01,loss_function='cross_entropy')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:20:20.163940Z","iopub.execute_input":"2022-07-14T17:20:20.164338Z","iopub.status.idle":"2022-07-14T17:20:20.173002Z","shell.execute_reply.started":"2022-07-14T17:20:20.164308Z","shell.execute_reply":"2022-07-14T17:20:20.171307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf={'linear regression':lr,'random forest':rfg,'naive bayes':gnb,'xg boost':xgb,'light gbm':light,'cat boost':cat}","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:15:17.490658Z","iopub.execute_input":"2022-07-14T17:15:17.491110Z","iopub.status.idle":"2022-07-14T17:15:17.497528Z","shell.execute_reply.started":"2022-07-14T17:15:17.491080Z","shell.execute_reply":"2022-07-14T17:15:17.495837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.keys()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:15:24.046332Z","iopub.execute_input":"2022-07-14T17:15:24.046911Z","iopub.status.idle":"2022-07-14T17:15:24.057924Z","shell.execute_reply.started":"2022-07-14T17:15:24.046869Z","shell.execute_reply":"2022-07-14T17:15:24.056495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:20:48.246812Z","iopub.execute_input":"2022-07-14T17:20:48.247210Z","iopub.status.idle":"2022-07-14T17:20:48.256736Z","shell.execute_reply.started":"2022-07-14T17:20:48.247179Z","shell.execute_reply":"2022-07-14T17:20:48.255369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:21:00.285843Z","iopub.execute_input":"2022-07-14T17:21:00.287241Z","iopub.status.idle":"2022-07-14T17:21:00.305832Z","shell.execute_reply.started":"2022-07-14T17:21:00.287180Z","shell.execute_reply":"2022-07-14T17:21:00.304194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:21:40.266347Z","iopub.execute_input":"2022-07-14T17:21:40.266740Z","iopub.status.idle":"2022-07-14T17:21:40.275612Z","shell.execute_reply.started":"2022-07-14T17:21:40.266710Z","shell.execute_reply":"2022-07-14T17:21:40.274217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nscaler=MinMaxScaler()\nx_train=scaler.fit_transform(x_train)\nx_test=scaler.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:29:59.623722Z","iopub.execute_input":"2022-07-14T17:29:59.624152Z","iopub.status.idle":"2022-07-14T17:29:59.658715Z","shell.execute_reply.started":"2022-07-14T17:29:59.624122Z","shell.execute_reply":"2022-07-14T17:29:59.657457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:30:05.765256Z","iopub.execute_input":"2022-07-14T17:30:05.765836Z","iopub.status.idle":"2022-07-14T17:30:05.776742Z","shell.execute_reply.started":"2022-07-14T17:30:05.765641Z","shell.execute_reply":"2022-07-14T17:30:05.773723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:30:11.826112Z","iopub.execute_input":"2022-07-14T17:30:11.826585Z","iopub.status.idle":"2022-07-14T17:30:11.839185Z","shell.execute_reply.started":"2022-07-14T17:30:11.826544Z","shell.execute_reply":"2022-07-14T17:30:11.837791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def classify(x,y):\n    for key in clf.keys():\n        clf[key].fit(x_train,y_train)\n        y_pred2=clf[key].predict(x_test)\n        score=f1_score(y_test,y_pred2)\n        print(key,'--->',score)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:30:14.684775Z","iopub.execute_input":"2022-07-14T17:30:14.685233Z","iopub.status.idle":"2022-07-14T17:30:14.693134Z","shell.execute_reply.started":"2022-07-14T17:30:14.685203Z","shell.execute_reply":"2022-07-14T17:30:14.691374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classify(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:30:17.174893Z","iopub.execute_input":"2022-07-14T17:30:17.175362Z","iopub.status.idle":"2022-07-14T17:30:54.471681Z","shell.execute_reply.started":"2022-07-14T17:30:17.175310Z","shell.execute_reply":"2022-07-14T17:30:54.470384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:31:37.578850Z","iopub.execute_input":"2022-07-14T17:31:37.579725Z","iopub.status.idle":"2022-07-14T17:31:37.596294Z","shell.execute_reply.started":"2022-07-14T17:31:37.579666Z","shell.execute_reply":"2022-07-14T17:31:37.594694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=test_df.copy()\ntest","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:32:48.645254Z","iopub.execute_input":"2022-07-14T17:32:48.645698Z","iopub.status.idle":"2022-07-14T17:32:48.667450Z","shell.execute_reply.started":"2022-07-14T17:32:48.645658Z","shell.execute_reply":"2022-07-14T17:32:48.665974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=test.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:35:32.155181Z","iopub.execute_input":"2022-07-14T17:35:32.155754Z","iopub.status.idle":"2022-07-14T17:35:32.168045Z","shell.execute_reply.started":"2022-07-14T17:35:32.155701Z","shell.execute_reply":"2022-07-14T17:35:32.166528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=test_df.drop(columns=['id','keyword','location'],axis=1)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:33:43.485394Z","iopub.execute_input":"2022-07-14T17:33:43.485799Z","iopub.status.idle":"2022-07-14T17:33:43.498695Z","shell.execute_reply.started":"2022-07-14T17:33:43.485768Z","shell.execute_reply":"2022-07-14T17:33:43.497358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:34:10.326481Z","iopub.execute_input":"2022-07-14T17:34:10.327115Z","iopub.status.idle":"2022-07-14T17:34:10.335224Z","shell.execute_reply.started":"2022-07-14T17:34:10.327082Z","shell.execute_reply":"2022-07-14T17:34:10.333870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=test.copy()\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:36:31.814156Z","iopub.execute_input":"2022-07-14T17:36:31.814510Z","iopub.status.idle":"2022-07-14T17:36:31.834145Z","shell.execute_reply.started":"2022-07-14T17:36:31.814480Z","shell.execute_reply":"2022-07-14T17:36:31.832700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corpus2=[]\n\nfor i in range(0,len(test_df)):\n    print(i)\n    review=re.sub('[^a-zA-Z]',' ',test_df['text'][i])\n    review=review.lower()\n    review=review.split()\n    \n    review=[ps.stem(word) for word in review if not word in stopwords.words('english')]\n    review=' '.join(review)\n    corpus2.append(review)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:41:47.194452Z","iopub.execute_input":"2022-07-14T17:41:47.195100Z","iopub.status.idle":"2022-07-14T17:42:00.205274Z","shell.execute_reply.started":"2022-07-14T17:41:47.195026Z","shell.execute_reply":"2022-07-14T17:42:00.203700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corpus2","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:42:22.633083Z","iopub.execute_input":"2022-07-14T17:42:22.633470Z","iopub.status.idle":"2022-07-14T17:42:22.666539Z","shell.execute_reply.started":"2022-07-14T17:42:22.633441Z","shell.execute_reply":"2022-07-14T17:42:22.665364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onehot_repr2=[text.one_hot(words,voc_size)for words in corpus2] \nonehot_repr2","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:44:08.871611Z","iopub.execute_input":"2022-07-14T17:44:08.872132Z","iopub.status.idle":"2022-07-14T17:44:09.440639Z","shell.execute_reply.started":"2022-07-14T17:44:08.872071Z","shell.execute_reply":"2022-07-14T17:44:09.439383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedded_docs2=sequence.pad_sequences(onehot_repr2,padding='pre',maxlen=sent_length)\nprint(embedded_docs2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:45:42.980671Z","iopub.execute_input":"2022-07-14T17:45:42.981147Z","iopub.status.idle":"2022-07-14T17:45:43.011391Z","shell.execute_reply.started":"2022-07-14T17:45:42.981117Z","shell.execute_reply":"2022-07-14T17:45:43.010126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=embedded_docs2.copy()\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:46:02.251867Z","iopub.execute_input":"2022-07-14T17:46:02.253403Z","iopub.status.idle":"2022-07-14T17:46:02.272967Z","shell.execute_reply.started":"2022-07-14T17:46:02.253358Z","shell.execute_reply":"2022-07-14T17:46:02.271476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn=models.Sequential([\n    \n    layers.Embedding(voc_size,feature_vector,input_length=sent_length),\n    layers.Dropout(0.5),\n    layers.Bidirectional(layers.LSTM(199)),\n    layers.Dropout(0.4),\n    layers.Dense(399,activation='relu'),\n    layers.Dropout(0.7),\n    layers.Dense(43,activation='relu'),\n    layers.Dropout(0.8),\n    layers.Dense(1,activation='sigmoid')\n    \n])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T17:48:44.113473Z","iopub.execute_input":"2022-07-14T17:48:44.113979Z","iopub.status.idle":"2022-07-14T17:48:46.973391Z","shell.execute_reply.started":"2022-07-14T17:48:44.113937Z","shell.execute_reply":"2022-07-14T17:48:46.972126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nnn.fit(x,y,epochs=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:16:44.152663Z","iopub.execute_input":"2022-07-14T18:16:44.153060Z","iopub.status.idle":"2022-07-14T18:19:09.610359Z","shell.execute_reply.started":"2022-07-14T18:16:44.153031Z","shell.execute_reply":"2022-07-14T18:19:09.608884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn.evaluate(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:19:27.583297Z","iopub.execute_input":"2022-07-14T18:19:27.583815Z","iopub.status.idle":"2022-07-14T18:19:36.417148Z","shell.execute_reply.started":"2022-07-14T18:19:27.583769Z","shell.execute_reply":"2022-07-14T18:19:36.415831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=nn.predict(test_df)\npreds[:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:19:41.413390Z","iopub.execute_input":"2022-07-14T18:19:41.414074Z","iopub.status.idle":"2022-07-14T18:19:47.489266Z","shell.execute_reply.started":"2022-07-14T18:19:41.414026Z","shell.execute_reply":"2022-07-14T18:19:47.488106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=(preds>0.5)\npreds[:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:20:25.653842Z","iopub.execute_input":"2022-07-14T18:20:25.654492Z","iopub.status.idle":"2022-07-14T18:20:25.667435Z","shell.execute_reply.started":"2022-07-14T18:20:25.654443Z","shell.execute_reply":"2022-07-14T18:20:25.665949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=preds.reshape(-1,)\npreds[:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:20:33.433878Z","iopub.execute_input":"2022-07-14T18:20:33.434319Z","iopub.status.idle":"2022-07-14T18:20:33.443083Z","shell.execute_reply.started":"2022-07-14T18:20:33.434290Z","shell.execute_reply":"2022-07-14T18:20:33.441703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame({'id':test['id'],'target':preds})","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:20:36.503697Z","iopub.execute_input":"2022-07-14T18:20:36.504759Z","iopub.status.idle":"2022-07-14T18:20:36.513942Z","shell.execute_reply.started":"2022-07-14T18:20:36.504680Z","shell.execute_reply":"2022-07-14T18:20:36.512415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:20:39.420733Z","iopub.execute_input":"2022-07-14T18:20:39.421113Z","iopub.status.idle":"2022-07-14T18:20:39.434231Z","shell.execute_reply.started":"2022-07-14T18:20:39.421085Z","shell.execute_reply":"2022-07-14T18:20:39.432845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['target']=le.fit_transform(submission['target'])\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:21:11.234443Z","iopub.execute_input":"2022-07-14T18:21:11.234983Z","iopub.status.idle":"2022-07-14T18:21:11.259120Z","shell.execute_reply.started":"2022-07-14T18:21:11.234940Z","shell.execute_reply":"2022-07-14T18:21:11.257679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['target'].value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:22:46.141508Z","iopub.execute_input":"2022-07-14T18:22:46.142191Z","iopub.status.idle":"2022-07-14T18:22:46.161305Z","shell.execute_reply.started":"2022-07-14T18:22:46.142147Z","shell.execute_reply":"2022-07-14T18:22:46.159589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('nlp1.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T18:22:51.988864Z","iopub.execute_input":"2022-07-14T18:22:51.989275Z","iopub.status.idle":"2022-07-14T18:22:52.004798Z","shell.execute_reply.started":"2022-07-14T18:22:51.989244Z","shell.execute_reply":"2022-07-14T18:22:52.003481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}