{"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":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nimport matplotlib.pyplot as plt\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.model_selection import train_test_split\nimport spacy as sps\nimport scipy\nimport re\nimport seaborn as sns\nfrom sklearn.preprocessing import OneHotEncoder","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-11T09:36:58.590285Z","iopub.execute_input":"2022-08-11T09:36:58.590641Z","iopub.status.idle":"2022-08-11T09:37:06.468307Z","shell.execute_reply.started":"2022-08-11T09:36:58.590559Z","shell.execute_reply":"2022-08-11T09:37:06.467035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_hot = OneHotEncoder()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:37:11.122760Z","iopub.execute_input":"2022-08-11T09:37:11.123366Z","iopub.status.idle":"2022-08-11T09:37:11.128469Z","shell.execute_reply.started":"2022-08-11T09:37:11.123332Z","shell.execute_reply":"2022-08-11T09:37:11.127109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nlp_spacy = sps.load('en_core_web_sm')\ncount_ = CountVectorizer()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:37:16.560040Z","iopub.execute_input":"2022-08-11T09:37:16.560524Z","iopub.status.idle":"2022-08-11T09:37:18.377685Z","shell.execute_reply.started":"2022-08-11T09:37:16.560486Z","shell.execute_reply":"2022-08-11T09:37:18.376584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Preprocessing Function**","metadata":{}},{"cell_type":"code","source":"def text_preprocessing_new(sentence):\n    \"\"\"\n      This function is used to preprocess the text, the data should be passed in the form of string.\n      function it performs: remove punctuation, stop words and numeric values.\n      input : text\n      output: refined setence \n    \"\"\"\n    cln_sent = []\n    for token in nlp_spacy(sentence):\n      if token.is_punct or token.is_stop or token.is_digit:\n        pass\n      else:\n        cln_sent.append(str(token.lemma_))\n    sen = ' '.join(cln_sent)\n    result = re.sub('([0-9])+[a-z]+','', sen)\n    result = re.sub('\\W{2,}','', result)\n    return result","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:37:19.040933Z","iopub.execute_input":"2022-08-11T09:37:19.041437Z","iopub.status.idle":"2022-08-11T09:37:19.056409Z","shell.execute_reply.started":"2022-08-11T09:37:19.041388Z","shell.execute_reply":"2022-08-11T09:37:19.054955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = '../input/feedback-prize-effectiveness'","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:37:24.320836Z","iopub.execute_input":"2022-08-11T09:37:24.321218Z","iopub.status.idle":"2022-08-11T09:37:24.325922Z","shell.execute_reply.started":"2022-08-11T09:37:24.321172Z","shell.execute_reply":"2022-08-11T09:37:24.324806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(data_path+'/train.csv')\ntest_data = pd.read_csv(data_path+'/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:37:26.194786Z","iopub.execute_input":"2022-08-11T09:37:26.195274Z","iopub.status.idle":"2022-08-11T09:37:26.514097Z","shell.execute_reply.started":"2022-08-11T09:37:26.195190Z","shell.execute_reply":"2022-08-11T09:37:26.513048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:37:32.002775Z","iopub.execute_input":"2022-08-11T09:37:32.003621Z","iopub.status.idle":"2022-08-11T09:37:32.023788Z","shell.execute_reply.started":"2022-08-11T09:37:32.003584Z","shell.execute_reply":"2022-08-11T09:37:32.022881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:37:42.995080Z","iopub.execute_input":"2022-08-11T09:37:42.996062Z","iopub.status.idle":"2022-08-11T09:37:43.017665Z","shell.execute_reply.started":"2022-08-11T09:37:42.996016Z","shell.execute_reply":"2022-08-11T09:37:43.016059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.discourse_effectiveness.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:38:01.390069Z","iopub.execute_input":"2022-08-11T09:38:01.390826Z","iopub.status.idle":"2022-08-11T09:38:01.412808Z","shell.execute_reply.started":"2022-08-11T09:38:01.390780Z","shell.execute_reply":"2022-08-11T09:38:01.411914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.discourse_effectiveness.value_counts()/train_data.shape[0]*100","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:38:03.954152Z","iopub.execute_input":"2022-08-11T09:38:03.954885Z","iopub.status.idle":"2022-08-11T09:38:03.965745Z","shell.execute_reply.started":"2022-08-11T09:38:03.954842Z","shell.execute_reply":"2022-08-11T09:38:03.964826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.discourse_effectiveness.replace({'Ineffective':0, 'Adequate':1, 'Effective':2}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:38:09.296770Z","iopub.execute_input":"2022-08-11T09:38:09.297437Z","iopub.status.idle":"2022-08-11T09:38:09.320118Z","shell.execute_reply.started":"2022-08-11T09:38:09.297397Z","shell.execute_reply":"2022-08-11T09:38:09.319250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain_data['clean_text'] = train_data.discourse_text.apply(text_preprocessing_new)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:38:11.340910Z","iopub.execute_input":"2022-08-11T09:38:11.341273Z","iopub.status.idle":"2022-08-11T09:45:20.935298Z","shell.execute_reply.started":"2022-08-11T09:38:11.341242Z","shell.execute_reply":"2022-08-11T09:45:20.934273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As it is visible that the dataset is imbalanced so make it balanced we will multiply**","metadata":{}},{"cell_type":"code","source":"datafor_0 = train_data.loc[train_data.discourse_effectiveness==0,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:45:54.453944Z","iopub.execute_input":"2022-08-11T09:45:54.454391Z","iopub.status.idle":"2022-08-11T09:45:54.472145Z","shell.execute_reply.started":"2022-08-11T09:45:54.454352Z","shell.execute_reply":"2022-08-11T09:45:54.471159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datafor_2 = train_data.loc[train_data.discourse_effectiveness==2,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:45:56.463893Z","iopub.execute_input":"2022-08-11T09:45:56.464274Z","iopub.status.idle":"2022-08-11T09:45:56.472738Z","shell.execute_reply.started":"2022-08-11T09:45:56.464244Z","shell.execute_reply":"2022-08-11T09:45:56.471702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datafor_1 = train_data.loc[train_data.discourse_effectiveness==1,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:46:02.120117Z","iopub.execute_input":"2022-08-11T09:46:02.120820Z","iopub.status.idle":"2022-08-11T09:46:02.128948Z","shell.execute_reply.started":"2022-08-11T09:46:02.120781Z","shell.execute_reply":"2022-08-11T09:46:02.127849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced = pd.concat([datafor_1,datafor_2,datafor_2,datafor_0,datafor_0,datafor_0], axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:46:07.750898Z","iopub.execute_input":"2022-08-11T09:46:07.751283Z","iopub.status.idle":"2022-08-11T09:46:07.772249Z","shell.execute_reply.started":"2022-08-11T09:46:07.751251Z","shell.execute_reply":"2022-08-11T09:46:07.771258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:46:10.008687Z","iopub.execute_input":"2022-08-11T09:46:10.009434Z","iopub.status.idle":"2022-08-11T09:46:10.026511Z","shell.execute_reply.started":"2022-08-11T09:46:10.009398Z","shell.execute_reply":"2022-08-11T09:46:10.025404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_data_balanced.discourse_effectiveness.value_counts()/train_data_balanced.shape[0])* 100","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:46:25.980032Z","iopub.execute_input":"2022-08-11T09:46:25.980399Z","iopub.status.idle":"2022-08-11T09:46:25.990655Z","shell.execute_reply.started":"2022-08-11T09:46:25.980369Z","shell.execute_reply":"2022-08-11T09:46:25.989429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:46:40.882014Z","iopub.execute_input":"2022-08-11T09:46:40.882695Z","iopub.status.idle":"2022-08-11T09:46:40.888192Z","shell.execute_reply.started":"2022-08-11T09:46:40.882656Z","shell.execute_reply":"2022-08-11T09:46:40.886766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.drop('index', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:46:43.402838Z","iopub.execute_input":"2022-08-11T09:46:43.403498Z","iopub.status.idle":"2022-08-11T09:46:43.429113Z","shell.execute_reply.started":"2022-08-11T09:46:43.403462Z","shell.execute_reply":"2022-08-11T09:46:43.428249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:46:51.033265Z","iopub.execute_input":"2022-08-11T09:46:51.033621Z","iopub.status.idle":"2022-08-11T09:46:51.051599Z","shell.execute_reply.started":"2022-08-11T09:46:51.033591Z","shell.execute_reply":"2022-08-11T09:46:51.049935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced['word_count'] = train_data_balanced.clean_text.apply(lambda x: len(x.split(' ')))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:01.412905Z","iopub.execute_input":"2022-08-11T09:47:01.413278Z","iopub.status.idle":"2022-08-11T09:47:01.499891Z","shell.execute_reply.started":"2022-08-11T09:47:01.413248Z","shell.execute_reply":"2022-08-11T09:47:01.499036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.discourse_text[200]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:04.160106Z","iopub.execute_input":"2022-08-11T09:47:04.160778Z","iopub.status.idle":"2022-08-11T09:47:04.167551Z","shell.execute_reply.started":"2022-08-11T09:47:04.160742Z","shell.execute_reply":"2022-08-11T09:47:04.166590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.clean_text[200]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:10.336980Z","iopub.execute_input":"2022-08-11T09:47:10.337643Z","iopub.status.idle":"2022-08-11T09:47:10.344322Z","shell.execute_reply.started":"2022-08-11T09:47:10.337607Z","shell.execute_reply":"2022-08-11T09:47:10.343250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Average Word Count for sentence is {}'.format(np.mean(train_data_balanced['word_count'])))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:13.765618Z","iopub.execute_input":"2022-08-11T09:47:13.766185Z","iopub.status.idle":"2022-08-11T09:47:13.772377Z","shell.execute_reply.started":"2022-08-11T09:47:13.766149Z","shell.execute_reply":"2022-08-11T09:47:13.771117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(train_data_balanced['word_count'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:19.077040Z","iopub.execute_input":"2022-08-11T09:47:19.078107Z","iopub.status.idle":"2022-08-11T09:47:19.086290Z","shell.execute_reply.started":"2022-08-11T09:47:19.078062Z","shell.execute_reply":"2022-08-11T09:47:19.085175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.median(train_data_balanced['word_count'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:21.630057Z","iopub.execute_input":"2022-08-11T09:47:21.630747Z","iopub.status.idle":"2022-08-11T09:47:21.639350Z","shell.execute_reply.started":"2022-08-11T09:47:21.630711Z","shell.execute_reply":"2022-08-11T09:47:21.638126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scipy.stats.mode(train_data_balanced['word_count'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:27.414059Z","iopub.execute_input":"2022-08-11T09:47:27.414728Z","iopub.status.idle":"2022-08-11T09:47:27.425379Z","shell.execute_reply.started":"2022-08-11T09:47:27.414693Z","shell.execute_reply":"2022-08-11T09:47:27.424032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nsns.boxplot(train_data_balanced['word_count'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:29.731875Z","iopub.execute_input":"2022-08-11T09:47:29.734527Z","iopub.status.idle":"2022-08-11T09:47:29.949858Z","shell.execute_reply.started":"2022-08-11T09:47:29.734484Z","shell.execute_reply":"2022-08-11T09:47:29.948926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.loc[train_data_balanced.word_count < 50, ['word_count']].count()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:35.766816Z","iopub.execute_input":"2022-08-11T09:47:35.767185Z","iopub.status.idle":"2022-08-11T09:47:35.787182Z","shell.execute_reply.started":"2022-08-11T09:47:35.767154Z","shell.execute_reply":"2022-08-11T09:47:35.786177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.loc[train_data_balanced.word_count > 50, ['word_count']].count()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:41.298948Z","iopub.execute_input":"2022-08-11T09:47:41.299707Z","iopub.status.idle":"2022-08-11T09:47:41.312699Z","shell.execute_reply.started":"2022-08-11T09:47:41.299660Z","shell.execute_reply":"2022-08-11T09:47:41.311653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_dataset = train_data_balanced.loc[train_data_balanced.word_count < 50,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:47.931000Z","iopub.execute_input":"2022-08-11T09:47:47.931999Z","iopub.status.idle":"2022-08-11T09:47:47.945062Z","shell.execute_reply.started":"2022-08-11T09:47:47.931952Z","shell.execute_reply":"2022-08-11T09:47:47.943915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_.fit(new_dataset.clean_text)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:50.112713Z","iopub.execute_input":"2022-08-11T09:47:50.113718Z","iopub.status.idle":"2022-08-11T09:47:50.835536Z","shell.execute_reply.started":"2022-08-11T09:47:50.113678Z","shell.execute_reply":"2022-08-11T09:47:50.834584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(count_.vocabulary_)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:54.912864Z","iopub.execute_input":"2022-08-11T09:47:54.913736Z","iopub.status.idle":"2022-08-11T09:47:54.921613Z","shell.execute_reply.started":"2022-08-11T09:47:54.913681Z","shell.execute_reply":"2022-08-11T09:47:54.920275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"position_X = one_hot.fit_transform(new_dataset[['discourse_type']])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:47:57.238926Z","iopub.execute_input":"2022-08-11T09:47:57.239612Z","iopub.status.idle":"2022-08-11T09:47:57.261260Z","shell.execute_reply.started":"2022-08-11T09:47:57.239575Z","shell.execute_reply":"2022-08-11T09:47:57.260372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"position_X.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:06.205677Z","iopub.execute_input":"2022-08-11T09:48:06.206872Z","iopub.status.idle":"2022-08-11T09:48:06.214745Z","shell.execute_reply.started":"2022-08-11T09:48:06.206823Z","shell.execute_reply":"2022-08-11T09:48:06.213459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"position = pd.DataFrame(position_X.todense())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:08.690097Z","iopub.execute_input":"2022-08-11T09:48:08.691034Z","iopub.status.idle":"2022-08-11T09:48:08.696696Z","shell.execute_reply.started":"2022-08-11T09:48:08.690996Z","shell.execute_reply":"2022-08-11T09:48:08.695057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"position","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:13.524168Z","iopub.execute_input":"2022-08-11T09:48:13.524887Z","iopub.status.idle":"2022-08-11T09:48:13.550926Z","shell.execute_reply.started":"2022-08-11T09:48:13.524849Z","shell.execute_reply":"2022-08-11T09:48:13.550005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocabulary_training = 15000\nlength_of_sentence = 30","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:29.356800Z","iopub.execute_input":"2022-08-11T09:48:29.357163Z","iopub.status.idle":"2022-08-11T09:48:29.364417Z","shell.execute_reply.started":"2022-08-11T09:48:29.357131Z","shell.execute_reply":"2022-08-11T09:48:29.363484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_vectorizer = tf.keras.layers.TextVectorization(max_tokens=vocabulary_training,\n                                                   output_sequence_length=length_of_sentence,\n                                                   standardize='lower_and_strip_punctuation',\n                                                   output_mode='int')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:31.660135Z","iopub.execute_input":"2022-08-11T09:48:31.660923Z","iopub.status.idle":"2022-08-11T09:48:34.328751Z","shell.execute_reply.started":"2022-08-11T09:48:31.660869Z","shell.execute_reply":"2022-08-11T09:48:34.327733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_vectorizer.adapt(new_dataset.clean_text)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:40.469730Z","iopub.execute_input":"2022-08-11T09:48:40.470188Z","iopub.status.idle":"2022-08-11T09:48:43.215916Z","shell.execute_reply.started":"2022-08-11T09:48:40.470148Z","shell.execute_reply":"2022-08-11T09:48:43.214907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_dataset.clean_text[100]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:43.217899Z","iopub.execute_input":"2022-08-11T09:48:43.218273Z","iopub.status.idle":"2022-08-11T09:48:43.228348Z","shell.execute_reply.started":"2022-08-11T09:48:43.218237Z","shell.execute_reply":"2022-08-11T09:48:43.227231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_vectorizer(new_dataset.clean_text[100])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:47.900870Z","iopub.execute_input":"2022-08-11T09:48:47.901786Z","iopub.status.idle":"2022-08-11T09:48:47.965900Z","shell.execute_reply.started":"2022-08-11T09:48:47.901750Z","shell.execute_reply":"2022-08-11T09:48:47.965029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_embedding = tf.keras.layers.Embedding(mask_zero=True,\n                         input_dim=vocabulary_training,\n                         output_dim=128)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:52.860096Z","iopub.execute_input":"2022-08-11T09:48:52.860529Z","iopub.status.idle":"2022-08-11T09:48:52.868557Z","shell.execute_reply.started":"2022-08-11T09:48:52.860493Z","shell.execute_reply":"2022-08-11T09:48:52.867269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_embedding(text_vectorizer(new_dataset.clean_text[100]))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:48:55.315085Z","iopub.execute_input":"2022-08-11T09:48:55.315831Z","iopub.status.idle":"2022-08-11T09:48:55.351411Z","shell.execute_reply.started":"2022-08-11T09:48:55.315791Z","shell.execute_reply":"2022-08-11T09:48:55.350476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = tf.keras.utils.to_categorical(new_dataset.discourse_effectiveness)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:00.552696Z","iopub.execute_input":"2022-08-11T09:49:00.553054Z","iopub.status.idle":"2022-08-11T09:49:00.560325Z","shell.execute_reply.started":"2022-08-11T09:49:00.553022Z","shell.execute_reply":"2022-08-11T09:49:00.558925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = new_dataset.clean_text","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:02.776849Z","iopub.execute_input":"2022-08-11T09:49:02.777197Z","iopub.status.idle":"2022-08-11T09:49:02.781636Z","shell.execute_reply.started":"2022-08-11T09:49:02.777168Z","shell.execute_reply":"2022-08-11T09:49:02.780553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test= train_test_split(X, Y)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:07.534813Z","iopub.execute_input":"2022-08-11T09:49:07.535167Z","iopub.status.idle":"2022-08-11T09:49:07.549844Z","shell.execute_reply.started":"2022-08-11T09:49:07.535137Z","shell.execute_reply":"2022-08-11T09:49:07.548971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos_train, pos_test, pos_ytrain, pos_ytest= train_test_split(position, Y)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:09.431797Z","iopub.execute_input":"2022-08-11T09:49:09.432746Z","iopub.status.idle":"2022-08-11T09:49:09.448353Z","shell.execute_reply.started":"2022-08-11T09:49:09.432698Z","shell.execute_reply":"2022-08-11T09:49:09.447248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.sum(axis=0)/len(y_train)*100","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:14.181207Z","iopub.execute_input":"2022-08-11T09:49:14.181623Z","iopub.status.idle":"2022-08-11T09:49:14.190017Z","shell.execute_reply.started":"2022-08-11T09:49:14.181590Z","shell.execute_reply":"2022-08-11T09:49:14.188785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.sum(axis=0)/len(y_test)*100","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:16.464695Z","iopub.execute_input":"2022-08-11T09:49:16.466515Z","iopub.status.idle":"2022-08-11T09:49:16.475308Z","shell.execute_reply.started":"2022-08-11T09:49:16.466466Z","shell.execute_reply":"2022-08-11T09:49:16.474451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_x = tf.data.Dataset.from_tensor_slices((x_train,pos_train))\ntesting_x = tf.data.Dataset.from_tensor_slices((x_test,pos_test))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:21.128108Z","iopub.execute_input":"2022-08-11T09:49:21.128799Z","iopub.status.idle":"2022-08-11T09:49:21.169291Z","shell.execute_reply.started":"2022-08-11T09:49:21.128762Z","shell.execute_reply":"2022-08-11T09:49:21.168384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_output_data = tf.data.Dataset.from_tensor_slices((y_train))\ntesting_output_data = tf.data.Dataset.from_tensor_slices((y_test))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:23.097834Z","iopub.execute_input":"2022-08-11T09:49:23.099095Z","iopub.status.idle":"2022-08-11T09:49:23.108293Z","shell.execute_reply.started":"2022-08-11T09:49:23.099046Z","shell.execute_reply":"2022-08-11T09:49:23.107255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.data.Dataset.zip((training_x, training_output_data))\ntest_dataset = tf.data.Dataset.zip((testing_x, testing_output_data))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:49:24.732620Z","iopub.execute_input":"2022-08-11T09:49:24.732980Z","iopub.status.idle":"2022-08-11T09:49:24.739565Z","shell.execute_reply.started":"2022-08-11T09:49:24.732950Z","shell.execute_reply":"2022-08-11T09:49:24.738579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset.batch(32).prefetch(tf.data.AUTOTUNE)\ntest_dataset = test_dataset.batch(32).prefetch(tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:51:30.761192Z","iopub.execute_input":"2022-08-11T09:51:30.761605Z","iopub.status.idle":"2022-08-11T09:51:30.771338Z","shell.execute_reply.started":"2022-08-11T09:51:30.761573Z","shell.execute_reply":"2022-08-11T09:51:30.770255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_layer = layers.Input(shape=(1, ), dtype=tf.string)\nvectorizer_layer = text_vectorizer(input_layer)\nembedding_layer = text_embedding(vectorizer_layer)\nlstm_layer = layers.LSTM(23, return_sequences=True)(embedding_layer)\nconv_layer = layers.Conv1D(64, kernel_size=3, activation='relu', name='convolution_layer')(lstm_layer)\npooling_layer = layers.GlobalMaxPool1D()(conv_layer)\nnormalization_layer = layers.BatchNormalization()(pooling_layer)\ndense_layer = layers.Dense(500, activation='relu')(normalization_layer)\ndrop_out = layers.Dropout(0.3)(dense_layer)\ndense_layer_level1 = layers.Dense(124, activation='relu')(drop_out)\n#dropout_layer = layers.Dropout(0.3)(dense_layer)\n#output_layer = layers.Dense(3, activation='sigmoid')(dropout_layer)\ntext_model = tf.keras.Model(input_layer, dense_layer_level1)\n\nposition_input_layer = layers.Input(shape=(7,))\nps_dense = layers.Dense(50, activation='relu')(position_input_layer)\ndrop_lev1 = layers.Dropout(0.2)(ps_dense)\nps_dense_2 = layers.Dense(5, activation='relu')(drop_lev1)\nposition_model = tf.keras.Model(position_input_layer, ps_dense_2)\n\n#Combine\nlayer_add = layers.Concatenate(name='concatinating_hybrid', axis=1)([text_model.output,position_model.output])\ncombined_dropout = layers.Dropout(0.3)(layer_add)\ncombined_dense = layers.Dense(128, activation='relu')(combined_dropout)\n#combined_dropout_1 = layers.Dropout(0.3)(combined_dense)\noutput_layer = layers.Dense(3, activation='softmax')(combined_dense)\n\n\nensembled_model = tf.keras.Model(inputs=[text_model.input, position_model.input],\n                                 outputs = output_layer)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:51:32.455993Z","iopub.execute_input":"2022-08-11T09:51:32.456366Z","iopub.status.idle":"2022-08-11T09:51:33.289274Z","shell.execute_reply.started":"2022-08-11T09:51:32.456334Z","shell.execute_reply":"2022-08-11T09:51:33.288271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensembled_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:51:38.983038Z","iopub.execute_input":"2022-08-11T09:51:38.983449Z","iopub.status.idle":"2022-08-11T09:51:38.991167Z","shell.execute_reply.started":"2022-08-11T09:51:38.983417Z","shell.execute_reply":"2022-08-11T09:51:38.990045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(ensembled_model,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:51:45.242980Z","iopub.execute_input":"2022-08-11T09:51:45.243354Z","iopub.status.idle":"2022-08-11T09:51:46.217957Z","shell.execute_reply.started":"2022-08-11T09:51:45.243322Z","shell.execute_reply":"2022-08-11T09:51:46.216911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensembled_model.compile(loss=tf.keras.losses.categorical_crossentropy,\n               optimizer=tf.keras.optimizers.Adam(learning_rate=0.00001),\n               metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:51:54.745985Z","iopub.execute_input":"2022-08-11T09:51:54.747167Z","iopub.status.idle":"2022-08-11T09:51:54.765602Z","shell.execute_reply.started":"2022-08-11T09:51:54.747118Z","shell.execute_reply":"2022-08-11T09:51:54.764521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = ensembled_model.fit(train_dataset,\n              epochs=30,\n              validation_data=test_dataset,\n              validation_steps=int(0.10 * len(test_dataset))\n              )","metadata":{"execution":{"iopub.status.busy":"2022-08-11T09:52:16.039082Z","iopub.execute_input":"2022-08-11T09:52:16.039654Z","iopub.status.idle":"2022-08-11T10:01:30.844635Z","shell.execute_reply.started":"2022-08-11T09:52:16.039608Z","shell.execute_reply":"2022-08-11T10:01:30.843673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensembled_model.evaluate(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:01:37.797653Z","iopub.execute_input":"2022-08-11T10:01:37.798009Z","iopub.status.idle":"2022-08-11T10:01:39.761340Z","shell.execute_reply.started":"2022-08-11T10:01:37.797977Z","shell.execute_reply":"2022-08-11T10:01:39.760397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:01:42.985422Z","iopub.execute_input":"2022-08-11T10:01:42.985776Z","iopub.status.idle":"2022-08-11T10:01:43.001907Z","shell.execute_reply.started":"2022-08-11T10:01:42.985745Z","shell.execute_reply":"2022-08-11T10:01:43.000743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest_data['clean_txt'] = test_data.discourse_text.apply(text_preprocessing_new)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:01:49.639901Z","iopub.execute_input":"2022-08-11T10:01:49.640602Z","iopub.status.idle":"2022-08-11T10:01:49.754341Z","shell.execute_reply.started":"2022-08-11T10:01:49.640563Z","shell.execute_reply":"2022-08-11T10:01:49.753194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['word_count'] = test_data.clean_txt.apply(lambda x : len(x.split(' ')))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:01:51.944291Z","iopub.execute_input":"2022-08-11T10:01:51.944661Z","iopub.status.idle":"2022-08-11T10:01:51.952849Z","shell.execute_reply.started":"2022-08-11T10:01:51.944630Z","shell.execute_reply":"2022-08-11T10:01:51.951780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.loc[test_data.word_count < 50, ['word_count']].count()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:01:57.203026Z","iopub.execute_input":"2022-08-11T10:01:57.203392Z","iopub.status.idle":"2022-08-11T10:01:57.216808Z","shell.execute_reply.started":"2022-08-11T10:01:57.203362Z","shell.execute_reply":"2022-08-11T10:01:57.215894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos_test = one_hot.transform(test_data[['discourse_type']]).toarray()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:01:59.727156Z","iopub.execute_input":"2022-08-11T10:01:59.727555Z","iopub.status.idle":"2022-08-11T10:01:59.734866Z","shell.execute_reply.started":"2022-08-11T10:01:59.727523Z","shell.execute_reply":"2022-08-11T10:01:59.733608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_proba = ensembled_model.predict((test_data['clean_txt'],pos_test),verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:08.398656Z","iopub.execute_input":"2022-08-11T10:02:08.399666Z","iopub.status.idle":"2022-08-11T10:02:09.491391Z","shell.execute_reply.started":"2022-08-11T10:02:08.399623Z","shell.execute_reply":"2022-08-11T10:02:09.490467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_proba","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:11.528944Z","iopub.execute_input":"2022-08-11T10:02:11.529341Z","iopub.status.idle":"2022-08-11T10:02:11.537490Z","shell.execute_reply.started":"2022-08-11T10:02:11.529307Z","shell.execute_reply":"2022-08-11T10:02:11.536242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(suppress=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:20.765854Z","iopub.execute_input":"2022-08-11T10:02:20.766243Z","iopub.status.idle":"2022-08-11T10:02:20.771393Z","shell.execute_reply.started":"2022-08-11T10:02:20.766189Z","shell.execute_reply":"2022-08-11T10:02:20.770224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_proba","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:23.156694Z","iopub.execute_input":"2022-08-11T10:02:23.157379Z","iopub.status.idle":"2022-08-11T10:02:23.164208Z","shell.execute_reply.started":"2022-08-11T10:02:23.157340Z","shell.execute_reply":"2022-08-11T10:02:23.163268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/feedback-prize-effectiveness/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:32.314003Z","iopub.execute_input":"2022-08-11T10:02:32.314374Z","iopub.status.idle":"2022-08-11T10:02:32.328503Z","shell.execute_reply.started":"2022-08-11T10:02:32.314342Z","shell.execute_reply":"2022-08-11T10:02:32.327503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Ineffective'] = pred_proba[:,0]\nsubmission['Adequate'] = pred_proba[:,1]\nsubmission['Effective'] = pred_proba[:,2]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:34.170806Z","iopub.execute_input":"2022-08-11T10:02:34.171453Z","iopub.status.idle":"2022-08-11T10:02:34.178490Z","shell.execute_reply.started":"2022-08-11T10:02:34.171414Z","shell.execute_reply":"2022-08-11T10:02:34.177419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:38.999221Z","iopub.execute_input":"2022-08-11T10:02:38.999591Z","iopub.status.idle":"2022-08-11T10:02:39.011707Z","shell.execute_reply.started":"2022-08-11T10:02:38.999560Z","shell.execute_reply":"2022-08-11T10:02:39.010525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T10:02:47.295016Z","iopub.execute_input":"2022-08-11T10:02:47.295384Z","iopub.status.idle":"2022-08-11T10:02:47.304079Z","shell.execute_reply.started":"2022-08-11T10:02:47.295353Z","shell.execute_reply":"2022-08-11T10:02:47.302885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}