{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-03T06:05:57.747482Z","iopub.execute_input":"2022-08-03T06:05:57.748180Z","iopub.status.idle":"2022-08-03T06:05:57.788676Z","shell.execute_reply.started":"2022-08-03T06:05:57.748030Z","shell.execute_reply":"2022-08-03T06:05:57.787573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:57.895034Z","iopub.execute_input":"2022-08-03T06:05:57.896184Z","iopub.status.idle":"2022-08-03T06:05:57.961957Z","shell.execute_reply.started":"2022-08-03T06:05:57.896115Z","shell.execute_reply":"2022-08-03T06:05:57.960721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:57.963849Z","iopub.execute_input":"2022-08-03T06:05:57.964214Z","iopub.status.idle":"2022-08-03T06:05:57.971253Z","shell.execute_reply.started":"2022-08-03T06:05:57.964180Z","shell.execute_reply":"2022-08-03T06:05:57.970426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:58.023197Z","iopub.execute_input":"2022-08-03T06:05:58.023947Z","iopub.status.idle":"2022-08-03T06:05:58.034348Z","shell.execute_reply.started":"2022-08-03T06:05:58.023904Z","shell.execute_reply":"2022-08-03T06:05:58.033244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.fillna('Unknown')\ndata.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:58.110520Z","iopub.execute_input":"2022-08-03T06:05:58.111400Z","iopub.status.idle":"2022-08-03T06:05:58.125778Z","shell.execute_reply.started":"2022-08-03T06:05:58.111346Z","shell.execute_reply":"2022-08-03T06:05:58.124377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['char_len'] = data['text'].apply(lambda x:len(x))\ndata['word_len'] = data['text'].apply(lambda x:len(x.split(\" \")))\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:58.227861Z","iopub.execute_input":"2022-08-03T06:05:58.228708Z","iopub.status.idle":"2022-08-03T06:05:58.263671Z","shell.execute_reply.started":"2022-08-03T06:05:58.228655Z","shell.execute_reply":"2022-08-03T06:05:58.262721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nsns.kdeplot(data = data, x='char_len',hue='target')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:58.304244Z","iopub.execute_input":"2022-08-03T06:05:58.305440Z","iopub.status.idle":"2022-08-03T06:05:59.206576Z","shell.execute_reply.started":"2022-08-03T06:05:58.305384Z","shell.execute_reply":"2022-08-03T06:05:59.205413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.kdeplot(data = data, x='word_len',hue='target')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:59.208306Z","iopub.execute_input":"2022-08-03T06:05:59.208701Z","iopub.status.idle":"2022-08-03T06:05:59.422096Z","shell.execute_reply.started":"2022-08-03T06:05:59.208667Z","shell.execute_reply":"2022-08-03T06:05:59.420747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.iloc[0,3]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:59.423642Z","iopub.execute_input":"2022-08-03T06:05:59.424101Z","iopub.status.idle":"2022-08-03T06:05:59.431163Z","shell.execute_reply.started":"2022-08-03T06:05:59.424067Z","shell.execute_reply":"2022-08-03T06:05:59.430007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\ndata['text'] = data['text'].apply(lambda x:re.sub(\"[,@#&!*'.]\",\"\",x))","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:59.434187Z","iopub.execute_input":"2022-08-03T06:05:59.434613Z","iopub.status.idle":"2022-08-03T06:05:59.467079Z","shell.execute_reply.started":"2022-08-03T06:05:59.434551Z","shell.execute_reply":"2022-08-03T06:05:59.466074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.iloc[2,3]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:59.468346Z","iopub.execute_input":"2022-08-03T06:05:59.468714Z","iopub.status.idle":"2022-08-03T06:05:59.476376Z","shell.execute_reply.started":"2022-08-03T06:05:59.468680Z","shell.execute_reply":"2022-08-03T06:05:59.475160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from nltk.tokenize import TweetTokenizer\nfrom nltk.corpus import stopwords\nfrom nltk.stem import PorterStemmer\nstemmer = PorterStemmer()\ntk=TweetTokenizer(preserve_case=False,reduce_len=True)\ndef tweeter(sentence):\n    tok_sent = tk.tokenize(sentence)\n    text = [stemmer.stem(word.lower()) for word in tok_sent if word not in set(stopwords.words('english')) and word.isalpha()==True]\n    sent=\"\"\n    for word in text:\n        sent+=word+\" \"\n    return sent[:-1]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:05:59.477865Z","iopub.execute_input":"2022-08-03T06:05:59.478276Z","iopub.status.idle":"2022-08-03T06:06:00.175069Z","shell.execute_reply.started":"2022-08-03T06:05:59.478242Z","shell.execute_reply":"2022-08-03T06:06:00.173678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['text'] = data['text'].apply(lambda x:tweeter(x))\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:00.176545Z","iopub.execute_input":"2022-08-03T06:06:00.176953Z","iopub.status.idle":"2022-08-03T06:06:23.179949Z","shell.execute_reply.started":"2022-08-03T06:06:00.176916Z","shell.execute_reply":"2022-08-03T06:06:23.178597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.iloc[-1,3]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.181748Z","iopub.execute_input":"2022-08-03T06:06:23.182541Z","iopub.status.idle":"2022-08-03T06:06:23.192727Z","shell.execute_reply.started":"2022-08-03T06:06:23.182490Z","shell.execute_reply":"2022-08-03T06:06:23.191261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['location'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.194993Z","iopub.execute_input":"2022-08-03T06:06:23.195520Z","iopub.status.idle":"2022-08-03T06:06:23.223232Z","shell.execute_reply.started":"2022-08-03T06:06:23.195481Z","shell.execute_reply":"2022-08-03T06:06:23.221805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_need = data[['text','target']]\ndata_need.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.228302Z","iopub.execute_input":"2022-08-03T06:06:23.228800Z","iopub.status.idle":"2022-08-03T06:06:23.244095Z","shell.execute_reply.started":"2022-08-03T06:06:23.228762Z","shell.execute_reply":"2022-08-03T06:06:23.242435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data_need['text']\ny = data_need['target']","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.245528Z","iopub.execute_input":"2022-08-03T06:06:23.246302Z","iopub.status.idle":"2022-08-03T06:06:23.252903Z","shell.execute_reply.started":"2022-08-03T06:06:23.246251Z","shell.execute_reply":"2022-08-03T06:06:23.251762Z"},"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.15, random_state=42)\nX_train.shape,X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.254448Z","iopub.execute_input":"2022-08-03T06:06:23.255211Z","iopub.status.idle":"2022-08-03T06:06:23.267998Z","shell.execute_reply.started":"2022-08-03T06:06:23.255167Z","shell.execute_reply":"2022-08-03T06:06:23.266888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.270038Z","iopub.execute_input":"2022-08-03T06:06:23.270664Z","iopub.status.idle":"2022-08-03T06:06:23.282732Z","shell.execute_reply.started":"2022-08-03T06:06:23.270614Z","shell.execute_reply":"2022-08-03T06:06:23.281459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer\ntf = TfidfVectorizer(min_df=0.001)\nX_train_tf = tf.fit_transform(X_train)\nX_train_tf.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.284739Z","iopub.execute_input":"2022-08-03T06:06:23.286219Z","iopub.status.idle":"2022-08-03T06:06:23.398872Z","shell.execute_reply.started":"2022-08-03T06:06:23.286181Z","shell.execute_reply":"2022-08-03T06:06:23.397213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_tf = X_train_tf.toarray()\nX_train_tf.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.400886Z","iopub.execute_input":"2022-08-03T06:06:23.401528Z","iopub.status.idle":"2022-08-03T06:06:23.514561Z","shell.execute_reply.started":"2022-08-03T06:06:23.401451Z","shell.execute_reply":"2022-08-03T06:06:23.513404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_tf = tf.transform(X_test)\nX_test_tf = X_test_tf.toarray()\nX_test_tf.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.515980Z","iopub.execute_input":"2022-08-03T06:06:23.516510Z","iopub.status.idle":"2022-08-03T06:06:23.556851Z","shell.execute_reply.started":"2022-08-03T06:06:23.516462Z","shell.execute_reply":"2022-08-03T06:06:23.555668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, BatchNormalization, Dropout\nmodel = Sequential()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:23.558285Z","iopub.execute_input":"2022-08-03T06:06:23.559200Z","iopub.status.idle":"2022-08-03T06:06:33.186071Z","shell.execute_reply.started":"2022-08-03T06:06:23.559163Z","shell.execute_reply":"2022-08-03T06:06:33.184960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Dense(1500,activation='relu',input_shape=(1569,)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1000,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dense(500,activation='relu'))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(250,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dense(100,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dense(10,activation='relu'))\nmodel.add(Dense(1,activation='sigmoid'))\nmodel.compile(optimizer='adam',loss = 'binary_crossentropy',metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:33.187469Z","iopub.execute_input":"2022-08-03T06:06:33.188121Z","iopub.status.idle":"2022-08-03T06:06:33.395898Z","shell.execute_reply.started":"2022-08-03T06:06:33.188080Z","shell.execute_reply":"2022-08-03T06:06:33.394571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping\ncheckpoint = ModelCheckpoint('weights.hdf5', monitor='val_loss', save_best_only=True)\nearly_stop = EarlyStopping(patience=5)\nmodel_history = model.fit(X_train_tf,y_train,epochs=30,validation_split=0.2,callbacks=[checkpoint,early_stop])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:06:33.397312Z","iopub.execute_input":"2022-08-03T06:06:33.397877Z","iopub.status.idle":"2022-08-03T06:07:04.707539Z","shell.execute_reply.started":"2022-08-03T06:06:33.397838Z","shell.execute_reply":"2022-08-03T06:07:04.706181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = model_history.history['loss']\nval_loss = model_history.history['val_loss']\nacc = model_history.history['accuracy']\nval_acc = model_history.history['val_accuracy']","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:07:04.709067Z","iopub.execute_input":"2022-08-03T06:07:04.709432Z","iopub.status.idle":"2022-08-03T06:07:04.715530Z","shell.execute_reply.started":"2022-08-03T06:07:04.709399Z","shell.execute_reply":"2022-08-03T06:07:04.714289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot loss and accuracy\n\nfig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (19,7))\nsns.set_style(\"darkgrid\")\n\nax[0].plot(acc, '*-',label = 'Training accuracy')\nax[0].plot(val_acc, '*-',label = 'Validation accuracy')\nax[0].set_xlabel('Epochs')\nax[0].set_ylabel('Accuracy')\nax[0].set_title('Epochs & Training Accuracy', fontsize = 17)\nax[0].legend(loc='best')\n\n\nax[1].plot(loss, '*-',label = 'Training loss')\nax[1].plot(val_loss, '*-',label = 'Validation loss')\nax[1].set_xlabel('Epochs')\nax[1].set_ylabel('loss')\nax[1].set_title('Epochs & loss', fontsize = 17)\nax[1].legend(loc='best')\nsns.set_style(\"darkgrid\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:07:04.717419Z","iopub.execute_input":"2022-08-03T06:07:04.717826Z","iopub.status.idle":"2022-08-03T06:07:16.736054Z","shell.execute_reply.started":"2022-08-03T06:07:04.717792Z","shell.execute_reply":"2022-08-03T06:07:16.734718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('weights.hdf5')\npredict = model.predict(X_test_tf)\npredict = [1 if i > 0.5 else 0 for i in predict]\nfrom sklearn.metrics import classification_report\nprint(classification_report(y_test,predict))","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:26:42.462503Z","iopub.execute_input":"2022-08-03T06:26:42.462897Z","iopub.status.idle":"2022-08-03T06:26:42.784512Z","shell.execute_reply.started":"2022-08-03T06:26:42.462863Z","shell.execute_reply":"2022-08-03T06:26:42.783461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_data(data):\n    data['text'] = data['text'].apply(lambda x:re.sub(\"[,@#&!*'.]\",\"\",x))\n    data['text'] = data['text'].apply(lambda x:tweeter(x))\n    data_need = data['text']\n    data_tf = tf.transform(data_need)\n    data_tf = data_tf.toarray()\n    predict = model.predict(data_tf)\n    predict = [1 if i > 0.5 else 0 for i in predict]\n    final_data = pd.DataFrame(zip(data['id'],predict),columns=['id','target'])\n    return final_data\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:37:48.650277Z","iopub.execute_input":"2022-08-03T06:37:48.651597Z","iopub.status.idle":"2022-08-03T06:37:48.661170Z","shell.execute_reply.started":"2022-08-03T06:37:48.651543Z","shell.execute_reply":"2022-08-03T06:37:48.659734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')\nfinal = predict_data(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:37:49.329841Z","iopub.execute_input":"2022-08-03T06:37:49.330311Z","iopub.status.idle":"2022-08-03T06:37:59.889517Z","shell.execute_reply.started":"2022-08-03T06:37:49.330271Z","shell.execute_reply":"2022-08-03T06:37:59.888367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:37:21.295978Z","iopub.execute_input":"2022-08-03T06:37:21.296427Z","iopub.status.idle":"2022-08-03T06:37:21.314366Z","shell.execute_reply.started":"2022-08-03T06:37:21.296391Z","shell.execute_reply":"2022-08-03T06:37:21.313243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:38:09.549486Z","iopub.execute_input":"2022-08-03T06:38:09.549907Z","iopub.status.idle":"2022-08-03T06:38:09.562340Z","shell.execute_reply.started":"2022-08-03T06:38:09.549871Z","shell.execute_reply":"2022-08-03T06:38:09.561450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_data = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')\nprint(classification_report(sub_data['target'],final['target']))","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:43:00.416010Z","iopub.execute_input":"2022-08-03T06:43:00.417092Z","iopub.status.idle":"2022-08-03T06:43:00.440152Z","shell.execute_reply.started":"2022-08-03T06:43:00.417037Z","shell.execute_reply":"2022-08-03T06:43:00.439217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}