{"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":"markdown","source":"## **Imports**","metadata":{"id":"W9D8_JYrfNMY"}},{"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","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","id":"zTKeO7j8ZPet","execution":{"iopub.status.busy":"2022-08-12T18:43:25.044070Z","iopub.execute_input":"2022-08-12T18:43:25.044605Z","iopub.status.idle":"2022-08-12T18:43:37.278613Z","shell.execute_reply.started":"2022-08-12T18:43:25.044481Z","shell.execute_reply":"2022-08-12T18:43:37.276587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras.backend import set_session\n# import tensorflow as tf\n# config = tf.ConfigProto()\n# config.gpu_options.allow_growth = True\n# sess = tf.Session(config=config)\n# set_session(sess)","metadata":{"id":"5rhNUC_ZZPex","outputId":"66bffad8-bb31-4832-9491-9a775710e5d7","execution":{"iopub.status.busy":"2022-08-12T18:43:37.282146Z","iopub.execute_input":"2022-08-12T18:43:37.282978Z","iopub.status.idle":"2022-08-12T18:43:37.289630Z","shell.execute_reply.started":"2022-08-12T18:43:37.282934Z","shell.execute_reply":"2022-08-12T18:43:37.288067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nlp_spacy = sps.load('en_core_web_sm')\ncount_ = CountVectorizer()","metadata":{"id":"QMZKiGgyZPey","execution":{"iopub.status.busy":"2022-08-12T18:43:37.524918Z","iopub.execute_input":"2022-08-12T18:43:37.525395Z","iopub.status.idle":"2022-08-12T18:43:38.505784Z","shell.execute_reply.started":"2022-08-12T18:43:37.525356Z","shell.execute_reply":"2022-08-12T18:43:38.503723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Preprocessing Function**","metadata":{"id":"oPkYHbVNZPey"}},{"cell_type":"code","source":"def text_preprocessing(data_sets):\n  \"\"\"\n  This function is used to preprocess the text, the data should be passed in the form of list.\n  function it performs: remove punctuation, stop words and numeric values.\n  input : list of text\n  output: refined words of text\n  \"\"\"\n  import re\n  nlp_spacy = sps.load('en_core_web_sm')\n  cleaned = []\n  for sentence in data_sets:\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\n    sen = ' '.join(cln_sent)\n    result = re.sub('([0-9])+[a-z]+','', sen)\n    result = re.sub('\\W{2,}','', result)\n    cleaned.append(result)\n  return cleaned","metadata":{"id":"9MPYDNWZZPe0","execution":{"iopub.status.busy":"2022-08-12T18:43:39.151180Z","iopub.execute_input":"2022-08-12T18:43:39.152130Z","iopub.status.idle":"2022-08-12T18:43:39.161412Z","shell.execute_reply.started":"2022-08-12T18:43:39.152062Z","shell.execute_reply":"2022-08-12T18:43:39.159940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = '../input/feedback-prize-effectiveness'","metadata":{"id":"Gn_zTKIdZPe0","execution":{"iopub.status.busy":"2022-08-12T18:43:56.702540Z","iopub.execute_input":"2022-08-12T18:43:56.703150Z","iopub.status.idle":"2022-08-12T18:43:56.711509Z","shell.execute_reply.started":"2022-08-12T18:43:56.703112Z","shell.execute_reply":"2022-08-12T18:43:56.709567Z"},"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":{"id":"CtqZQ3_KZPe1","execution":{"iopub.status.busy":"2022-08-12T18:43:57.939824Z","iopub.execute_input":"2022-08-12T18:43:57.941581Z","iopub.status.idle":"2022-08-12T18:43:58.316634Z","shell.execute_reply.started":"2022-08-12T18:43:57.941513Z","shell.execute_reply":"2022-08-12T18:43:58.315278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(5)","metadata":{"id":"WQLVppvLZPe1","outputId":"949c1ce7-30ec-4556-fa1b-7228a002a4dc","execution":{"iopub.status.busy":"2022-08-12T18:43:59.329605Z","iopub.execute_input":"2022-08-12T18:43:59.330778Z","iopub.status.idle":"2022-08-12T18:43:59.357397Z","shell.execute_reply.started":"2022-08-12T18:43:59.330730Z","shell.execute_reply":"2022-08-12T18:43:59.355878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"id":"5caEUeRuZPe2","outputId":"c861f609-027e-4e72-a592-36a13646eb25","execution":{"iopub.status.busy":"2022-08-12T18:43:59.587767Z","iopub.execute_input":"2022-08-12T18:43:59.588198Z","iopub.status.idle":"2022-08-12T18:43:59.609755Z","shell.execute_reply.started":"2022-08-12T18:43:59.588161Z","shell.execute_reply":"2022-08-12T18:43:59.608678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.discourse_effectiveness.value_counts()","metadata":{"id":"Mt73bdPaZPe2","outputId":"8990a4f0-3f10-4b4d-e487-222e35e52e7d","execution":{"iopub.status.busy":"2022-08-12T18:43:59.887077Z","iopub.execute_input":"2022-08-12T18:43:59.887584Z","iopub.status.idle":"2022-08-12T18:43:59.909351Z","shell.execute_reply.started":"2022-08-12T18:43:59.887548Z","shell.execute_reply":"2022-08-12T18:43:59.907665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.discourse_effectiveness.value_counts()","metadata":{"id":"oj8t8RNdZPe3","outputId":"ebc643aa-4e4e-43b7-87a2-8f060c15fe76","execution":{"iopub.status.busy":"2022-08-12T18:44:00.296012Z","iopub.execute_input":"2022-08-12T18:44:00.296534Z","iopub.status.idle":"2022-08-12T18:44:00.312472Z","shell.execute_reply.started":"2022-08-12T18:44:00.296495Z","shell.execute_reply":"2022-08-12T18:44:00.310576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.discourse_effectiveness.value_counts()/train_data.shape[0]*100","metadata":{"id":"zU4YOIRIZPe3","outputId":"6e3e9308-d990-4c03-ef3c-3a49bb0beb51","execution":{"iopub.status.busy":"2022-08-12T18:44:00.658617Z","iopub.execute_input":"2022-08-12T18:44:00.659101Z","iopub.status.idle":"2022-08-12T18:44:00.676268Z","shell.execute_reply.started":"2022-08-12T18:44:00.659063Z","shell.execute_reply":"2022-08-12T18:44:00.674526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.discourse_effectiveness.replace({'Ineffective':0, 'Adequate':1, 'Effective':2}, inplace=True)","metadata":{"id":"tszDMLCaZPe3","execution":{"iopub.status.busy":"2022-08-12T18:44:01.057449Z","iopub.execute_input":"2022-08-12T18:44:01.059138Z","iopub.status.idle":"2022-08-12T18:44:01.085038Z","shell.execute_reply.started":"2022-08-12T18:44:01.059057Z","shell.execute_reply":"2022-08-12T18:44:01.083696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**It appears that the dataset is imbalanced, so we will make it balanced**","metadata":{"id":"PkCarCvuZPe3"}},{"cell_type":"code","source":"datafor_0 = train_data.loc[train_data.discourse_effectiveness==0,:]","metadata":{"id":"Tx_dYlGyZPe4","execution":{"iopub.status.busy":"2022-08-12T18:44:01.777765Z","iopub.execute_input":"2022-08-12T18:44:01.778204Z","iopub.status.idle":"2022-08-12T18:44:01.790515Z","shell.execute_reply.started":"2022-08-12T18:44:01.778168Z","shell.execute_reply":"2022-08-12T18:44:01.789241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datafor_0","metadata":{"id":"OYWiNC1xZPe4","outputId":"50b5c4c4-dcb9-4125-fea5-284d4314c278","execution":{"iopub.status.busy":"2022-08-12T18:44:02.139964Z","iopub.execute_input":"2022-08-12T18:44:02.140402Z","iopub.status.idle":"2022-08-12T18:44:02.160889Z","shell.execute_reply.started":"2022-08-12T18:44:02.140369Z","shell.execute_reply":"2022-08-12T18:44:02.159605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datafor_2 = train_data.loc[train_data.discourse_effectiveness==2,:]","metadata":{"id":"Wc_xGqXcZPe4","execution":{"iopub.status.busy":"2022-08-12T18:44:02.502940Z","iopub.execute_input":"2022-08-12T18:44:02.503713Z","iopub.status.idle":"2022-08-12T18:44:02.511605Z","shell.execute_reply.started":"2022-08-12T18:44:02.503671Z","shell.execute_reply":"2022-08-12T18:44:02.510219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datafor_2","metadata":{"id":"NwMOc0rAZPe4","outputId":"679fb3ab-5e51-4983-cf49-2cf822107a00","execution":{"iopub.status.busy":"2022-08-12T18:44:03.041868Z","iopub.execute_input":"2022-08-12T18:44:03.042313Z","iopub.status.idle":"2022-08-12T18:44:03.060172Z","shell.execute_reply.started":"2022-08-12T18:44:03.042279Z","shell.execute_reply":"2022-08-12T18:44:03.058881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datafor_1 = train_data.loc[train_data.discourse_effectiveness==1,:]","metadata":{"id":"wWBj64mnZPe4","execution":{"iopub.status.busy":"2022-08-12T18:44:03.294895Z","iopub.execute_input":"2022-08-12T18:44:03.295963Z","iopub.status.idle":"2022-08-12T18:44:03.306537Z","shell.execute_reply.started":"2022-08-12T18:44:03.295895Z","shell.execute_reply":"2022-08-12T18:44:03.305235Z"},"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":{"id":"_yAV-vjrZPe4","execution":{"iopub.status.busy":"2022-08-12T18:44:03.521895Z","iopub.execute_input":"2022-08-12T18:44:03.523593Z","iopub.status.idle":"2022-08-12T18:44:03.539131Z","shell.execute_reply.started":"2022-08-12T18:44:03.523537Z","shell.execute_reply":"2022-08-12T18:44:03.537639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced","metadata":{"id":"XIr1lkBLZPe5","outputId":"41db3540-b5c8-4c38-bb67-35f22cd16194","execution":{"iopub.status.busy":"2022-08-12T18:44:04.083036Z","iopub.execute_input":"2022-08-12T18:44:04.084091Z","iopub.status.idle":"2022-08-12T18:44:04.103641Z","shell.execute_reply.started":"2022-08-12T18:44:04.084046Z","shell.execute_reply":"2022-08-12T18:44:04.102536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_data_balanced.discourse_effectiveness.value_counts()/train_data_balanced.shape[0])* 100","metadata":{"id":"0wzu9crkZPe5","outputId":"8be07098-180d-4e44-bdd1-7fbf318040e5","execution":{"iopub.status.busy":"2022-08-12T18:44:04.330611Z","iopub.execute_input":"2022-08-12T18:44:04.332967Z","iopub.status.idle":"2022-08-12T18:44:04.345853Z","shell.execute_reply.started":"2022-08-12T18:44:04.332893Z","shell.execute_reply":"2022-08-12T18:44:04.344626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.reset_index(inplace=True)","metadata":{"id":"9CCNYnicZPe5","execution":{"iopub.status.busy":"2022-08-12T18:44:04.698093Z","iopub.execute_input":"2022-08-12T18:44:04.698740Z","iopub.status.idle":"2022-08-12T18:44:04.706284Z","shell.execute_reply.started":"2022-08-12T18:44:04.698700Z","shell.execute_reply":"2022-08-12T18:44:04.705050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.drop('index', axis=1, inplace=True)","metadata":{"id":"jljT9lgSZPe5","execution":{"iopub.status.busy":"2022-08-12T18:44:05.606164Z","iopub.execute_input":"2022-08-12T18:44:05.607352Z","iopub.status.idle":"2022-08-12T18:44:05.622097Z","shell.execute_reply.started":"2022-08-12T18:44:05.607312Z","shell.execute_reply":"2022-08-12T18:44:05.620775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced","metadata":{"id":"8In3fpF0ZPe6","outputId":"98bb5542-8bb9-438a-c3e8-20cbd4fdfe15","execution":{"iopub.status.busy":"2022-08-12T18:44:05.910111Z","iopub.execute_input":"2022-08-12T18:44:05.910916Z","iopub.status.idle":"2022-08-12T18:44:05.929261Z","shell.execute_reply.started":"2022-08-12T18:44:05.910875Z","shell.execute_reply":"2022-08-12T18:44:05.928067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Pre processing the data**","metadata":{"id":"mgDKKZl-hDV9"}},{"cell_type":"markdown","source":"\nSince the preprocessing is very time consuming, I saved the output list in a text file and access the file instead of preprocessing the data everytime I start a new runtime.","metadata":{"id":"g40mXbSShWtV"}},{"cell_type":"code","source":"# %%time\n# output = text_preprocessing(train_data_balanced.discourse_text.to_list())","metadata":{"execution":{"iopub.execute_input":"2022-07-22T11:18:03.675281Z","iopub.status.busy":"2022-07-22T11:18:03.674583Z","iopub.status.idle":"2022-07-22T11:35:45.248052Z","shell.execute_reply":"2022-07-22T11:35:45.246684Z","shell.execute_reply.started":"2022-07-22T11:18:03.675247Z"},"id":"DKrE815wZPe6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open(r'sales.txt', 'w', encoding=\"utf-8\") as fp:\n#     for item in output:\n#         # write each item on a new line\n#         fp.write(\"%s\\n\" % item)\n#     print('Done')","metadata":{"id":"9GQHDCXGZPe6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = []\nwith open(r'../input/salesfordataset/sales.txt', 'r') as fp:\n    for line in fp:\n        # remove linebreak from a current name\n        # linebreak is the last character of each line\n        x = line[:-1]\n\n        # add current item to the list\n        output.append(x)\n\n# display list\nprint(output[2000])","metadata":{"id":"KRWqPj1rZPe7","outputId":"fb17eaac-7337-4e7c-d010-d1cfb56c5970","execution":{"iopub.status.busy":"2022-08-12T18:44:23.563748Z","iopub.execute_input":"2022-08-12T18:44:23.564228Z","iopub.status.idle":"2022-08-12T18:44:23.742183Z","shell.execute_reply.started":"2022-08-12T18:44:23.564190Z","shell.execute_reply":"2022-08-12T18:44:23.740400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced['cleaned_data'] = output","metadata":{"id":"pO9OF__rZPe7","execution":{"iopub.status.busy":"2022-08-12T18:44:27.777042Z","iopub.execute_input":"2022-08-12T18:44:27.777730Z","iopub.status.idle":"2022-08-12T18:44:27.794666Z","shell.execute_reply.started":"2022-08-12T18:44:27.777679Z","shell.execute_reply":"2022-08-12T18:44:27.792019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced['word_count'] = train_data_balanced.cleaned_data.apply(lambda x: len(x.split(' ')))","metadata":{"id":"yLfgPRCkZPe7","execution":{"iopub.status.busy":"2022-08-12T18:44:28.096278Z","iopub.execute_input":"2022-08-12T18:44:28.096746Z","iopub.status.idle":"2022-08-12T18:44:28.208808Z","shell.execute_reply.started":"2022-08-12T18:44:28.096711Z","shell.execute_reply":"2022-08-12T18:44:28.207357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced","metadata":{"id":"Yr0WAIklZPe7","outputId":"072b7028-7cd7-4425-dd04-189254d82aa3","execution":{"iopub.status.busy":"2022-08-12T18:44:28.514669Z","iopub.execute_input":"2022-08-12T18:44:28.515176Z","iopub.status.idle":"2022-08-12T18:44:28.537335Z","shell.execute_reply.started":"2022-08-12T18:44:28.515140Z","shell.execute_reply":"2022-08-12T18:44:28.536376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.discourse_text[200]","metadata":{"id":"IBlkwpeBZPe7","outputId":"74498b0e-db13-4037-b6d4-7c43263b48de","execution":{"iopub.status.busy":"2022-08-12T18:44:29.928583Z","iopub.execute_input":"2022-08-12T18:44:29.929998Z","iopub.status.idle":"2022-08-12T18:44:29.940288Z","shell.execute_reply.started":"2022-08-12T18:44:29.929930Z","shell.execute_reply":"2022-08-12T18:44:29.938595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.cleaned_data[200]","metadata":{"id":"3Wsnfe8nZPe7","outputId":"0bbf6273-2ea4-4072-ae99-5588b927b668","execution":{"iopub.status.busy":"2022-08-12T18:44:30.101072Z","iopub.execute_input":"2022-08-12T18:44:30.101849Z","iopub.status.idle":"2022-08-12T18:44:30.110497Z","shell.execute_reply.started":"2022-08-12T18:44:30.101806Z","shell.execute_reply":"2022-08-12T18:44:30.109179Z"},"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":{"id":"M1fpvdqiZPe7","outputId":"0dde6311-30a3-4803-f03d-57b134cd8daa","execution":{"iopub.status.busy":"2022-08-12T18:44:32.990807Z","iopub.execute_input":"2022-08-12T18:44:32.991810Z","iopub.status.idle":"2022-08-12T18:44:32.999324Z","shell.execute_reply.started":"2022-08-12T18:44:32.991766Z","shell.execute_reply":"2022-08-12T18:44:32.998013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Text Vectorization**\n\nThe features must be float and not string datatype in order for the model to process it.","metadata":{"id":"MKwU9SmVh1wa"}},{"cell_type":"code","source":"np.max(train_data_balanced['word_count'])","metadata":{"id":"yBt8h7lJZPe8","outputId":"f1bccf49-b76a-47c6-fc86-3d903ef7633d","execution":{"iopub.status.busy":"2022-08-12T18:44:39.771411Z","iopub.execute_input":"2022-08-12T18:44:39.771937Z","iopub.status.idle":"2022-08-12T18:44:39.780661Z","shell.execute_reply.started":"2022-08-12T18:44:39.771900Z","shell.execute_reply":"2022-08-12T18:44:39.779532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.median(train_data_balanced['word_count'])","metadata":{"id":"4hkb0VEUZPe8","outputId":"4246e49c-ff08-4559-dfa6-b3f269eef13a","execution":{"iopub.status.busy":"2022-08-12T18:44:39.958413Z","iopub.execute_input":"2022-08-12T18:44:39.959596Z","iopub.status.idle":"2022-08-12T18:44:39.971950Z","shell.execute_reply.started":"2022-08-12T18:44:39.959551Z","shell.execute_reply":"2022-08-12T18:44:39.970188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scipy.stats.mode(train_data_balanced['word_count'])","metadata":{"id":"z6ji-M_jZPe8","outputId":"af9cf018-8209-4edf-82f3-d2a5b5bbdafe","execution":{"iopub.status.busy":"2022-08-12T18:44:40.211930Z","iopub.execute_input":"2022-08-12T18:44:40.212744Z","iopub.status.idle":"2022-08-12T18:44:40.225254Z","shell.execute_reply.started":"2022-08-12T18:44:40.212698Z","shell.execute_reply":"2022-08-12T18:44:40.224107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced['word_count'].value_counts()","metadata":{"id":"qUIipZWbZPe8","outputId":"5461a953-bb87-409f-eed0-47d5fa4fbdb3","execution":{"iopub.status.busy":"2022-08-12T18:44:40.482318Z","iopub.execute_input":"2022-08-12T18:44:40.483850Z","iopub.status.idle":"2022-08-12T18:44:40.497838Z","shell.execute_reply.started":"2022-08-12T18:44:40.483791Z","shell.execute_reply":"2022-08-12T18:44:40.496104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_.fit(train_data_balanced.cleaned_data)","metadata":{"id":"zwtsJpWnZPe8","outputId":"55f7f484-d0c1-4906-83eb-39a14e7f8fa0","execution":{"iopub.status.busy":"2022-08-12T18:44:42.198168Z","iopub.execute_input":"2022-08-12T18:44:42.198640Z","iopub.status.idle":"2022-08-12T18:44:43.487197Z","shell.execute_reply.started":"2022-08-12T18:44:42.198602Z","shell.execute_reply":"2022-08-12T18:44:43.486244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(count_.vocabulary_)","metadata":{"id":"J1kZqyPZZPe8","outputId":"84812c1e-9c77-4f8e-aad7-a45fa039bff5","execution":{"iopub.status.busy":"2022-08-12T18:44:43.488673Z","iopub.execute_input":"2022-08-12T18:44:43.489792Z","iopub.status.idle":"2022-08-12T18:44:43.496289Z","shell.execute_reply.started":"2022-08-12T18:44:43.489755Z","shell.execute_reply":"2022-08-12T18:44:43.494877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocabulary_training = 25000\nlength_of_sentence = 40","metadata":{"id":"XPtZIVzOZPe8","execution":{"iopub.status.busy":"2022-08-12T18:44:43.498074Z","iopub.execute_input":"2022-08-12T18:44:43.498521Z","iopub.status.idle":"2022-08-12T18:44:43.507260Z","shell.execute_reply.started":"2022-08-12T18:44:43.498486Z","shell.execute_reply":"2022-08-12T18:44:43.505991Z"},"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":{"id":"4q8EH9_KZPe8","execution":{"iopub.status.busy":"2022-08-12T18:44:43.605822Z","iopub.execute_input":"2022-08-12T18:44:43.606484Z","iopub.status.idle":"2022-08-12T18:44:43.691668Z","shell.execute_reply.started":"2022-08-12T18:44:43.606437Z","shell.execute_reply":"2022-08-12T18:44:43.690358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_vectorizer.adapt(train_data_balanced.cleaned_data)","metadata":{"id":"0CYH1vPYZPe9","execution":{"iopub.status.busy":"2022-08-12T18:44:45.980257Z","iopub.execute_input":"2022-08-12T18:44:45.981685Z","iopub.status.idle":"2022-08-12T18:44:48.106523Z","shell.execute_reply.started":"2022-08-12T18:44:45.981623Z","shell.execute_reply":"2022-08-12T18:44:48.105276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_balanced.cleaned_data[100]","metadata":{"id":"4-AxR3_bZPe9","outputId":"a7f0aa51-8ecf-4cd1-e3aa-6a30a5bc2b73","execution":{"iopub.status.busy":"2022-08-12T18:44:51.491958Z","iopub.execute_input":"2022-08-12T18:44:51.492485Z","iopub.status.idle":"2022-08-12T18:44:51.503185Z","shell.execute_reply.started":"2022-08-12T18:44:51.492447Z","shell.execute_reply":"2022-08-12T18:44:51.501645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_vectorizer(train_data_balanced.cleaned_data[100])","metadata":{"id":"RZIU4uEGZPe9","outputId":"de19efd4-f174-41a5-e226-9ea2fcda4043","execution":{"iopub.status.busy":"2022-08-12T18:44:51.862710Z","iopub.execute_input":"2022-08-12T18:44:51.864159Z","iopub.status.idle":"2022-08-12T18:44:51.962067Z","shell.execute_reply.started":"2022-08-12T18:44:51.864101Z","shell.execute_reply":"2022-08-12T18:44:51.960532Z"},"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=64)","metadata":{"id":"NRgdNy_qZPe9","execution":{"iopub.status.busy":"2022-08-12T18:44:53.991537Z","iopub.execute_input":"2022-08-12T18:44:53.992026Z","iopub.status.idle":"2022-08-12T18:44:54.000372Z","shell.execute_reply.started":"2022-08-12T18:44:53.991991Z","shell.execute_reply":"2022-08-12T18:44:53.999129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_embedding(text_vectorizer(train_data_balanced.cleaned_data[100]))","metadata":{"id":"6YbJk14YZPe9","outputId":"3ba97c65-b2a4-4449-dc07-a66827f03af2","execution":{"iopub.status.busy":"2022-08-12T18:44:54.334311Z","iopub.execute_input":"2022-08-12T18:44:54.334799Z","iopub.status.idle":"2022-08-12T18:44:54.389280Z","shell.execute_reply.started":"2022-08-12T18:44:54.334759Z","shell.execute_reply":"2022-08-12T18:44:54.387577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We will later add the text vectorization and embedding functions in our model**","metadata":{"id":"ltwlISAhiImJ"}},{"cell_type":"markdown","source":"# **Creating Dataset for training**","metadata":{"id":"vulQBPzOifNb"}},{"cell_type":"code","source":"Y = tf.keras.utils.to_categorical(train_data_balanced.discourse_effectiveness)","metadata":{"id":"Hum9nmMFZPe9","execution":{"iopub.status.busy":"2022-08-12T18:45:11.863352Z","iopub.execute_input":"2022-08-12T18:45:11.864234Z","iopub.status.idle":"2022-08-12T18:45:11.872674Z","shell.execute_reply.started":"2022-08-12T18:45:11.864194Z","shell.execute_reply":"2022-08-12T18:45:11.870970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data_balanced.cleaned_data","metadata":{"id":"pTq9BBpFZPe-","execution":{"iopub.status.busy":"2022-08-12T18:45:12.136500Z","iopub.execute_input":"2022-08-12T18:45:12.136935Z","iopub.status.idle":"2022-08-12T18:45:12.144514Z","shell.execute_reply.started":"2022-08-12T18:45:12.136900Z","shell.execute_reply":"2022-08-12T18:45:12.142790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test= train_test_split(X, Y)","metadata":{"id":"MqgAM2K8ZPe-","execution":{"iopub.status.busy":"2022-08-12T18:45:12.913468Z","iopub.execute_input":"2022-08-12T18:45:12.915029Z","iopub.status.idle":"2022-08-12T18:45:12.934153Z","shell.execute_reply.started":"2022-08-12T18:45:12.914974Z","shell.execute_reply":"2022-08-12T18:45:12.932829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.sum(axis=0)/len(y_train)*100","metadata":{"id":"ZJH1K1JjZPe-","outputId":"7ab52a9b-0e15-412f-ec47-c9b1c7cee406","execution":{"iopub.status.busy":"2022-08-12T18:45:13.376093Z","iopub.execute_input":"2022-08-12T18:45:13.376593Z","iopub.status.idle":"2022-08-12T18:45:13.385245Z","shell.execute_reply.started":"2022-08-12T18:45:13.376555Z","shell.execute_reply":"2022-08-12T18:45:13.384088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.sum(axis=0)/len(y_test)*100","metadata":{"id":"0zwVaPzhZPe-","outputId":"10dde4fa-aa81-4195-a5c9-4cbe9d6e57bc","execution":{"iopub.status.busy":"2022-08-12T18:45:13.813697Z","iopub.execute_input":"2022-08-12T18:45:13.814493Z","iopub.status.idle":"2022-08-12T18:45:13.824903Z","shell.execute_reply.started":"2022-08-12T18:45:13.814444Z","shell.execute_reply":"2022-08-12T18:45:13.823716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))\ntesting_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))","metadata":{"id":"DtNjuZ7KZPe-","execution":{"iopub.status.busy":"2022-08-12T18:45:14.310680Z","iopub.execute_input":"2022-08-12T18:45:14.311500Z","iopub.status.idle":"2022-08-12T18:45:14.354341Z","shell.execute_reply.started":"2022-08-12T18:45:14.311452Z","shell.execute_reply":"2022-08-12T18:45:14.352924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_dataset = training_dataset.batch(32).prefetch(tf.data.AUTOTUNE)\ntesting_dataset = testing_dataset.batch(32).prefetch(tf.data.AUTOTUNE)","metadata":{"id":"sxPUXxvUZPe_","execution":{"iopub.status.busy":"2022-08-12T18:45:14.577440Z","iopub.execute_input":"2022-08-12T18:45:14.577855Z","iopub.status.idle":"2022-08-12T18:45:14.587287Z","shell.execute_reply.started":"2022-08-12T18:45:14.577822Z","shell.execute_reply":"2022-08-12T18:45:14.585442Z"},"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(48, return_sequences=True)(embedding_layer)\nconv_layer = layers.Conv1D(128, 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)\ndropout_layer = layers.Dropout(0.3)(dense_layer)\noutput_layer = layers.Dense(3, activation='sigmoid')(dropout_layer)\nbase_model = tf.keras.Model(input_layer, output_layer)","metadata":{"id":"ttoit69SZPe_","execution":{"iopub.status.busy":"2022-08-12T18:45:14.941224Z","iopub.execute_input":"2022-08-12T18:45:14.941768Z","iopub.status.idle":"2022-08-12T18:45:16.046281Z","shell.execute_reply.started":"2022-08-12T18:45:14.941727Z","shell.execute_reply":"2022-08-12T18:45:16.044019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"id":"OAC0nKcyZPe_","outputId":"8b2883f2-58c1-4229-ee89-a18bb87a35d7","execution":{"iopub.status.busy":"2022-08-12T18:45:16.049671Z","iopub.execute_input":"2022-08-12T18:45:16.050370Z","iopub.status.idle":"2022-08-12T18:45:16.059842Z","shell.execute_reply.started":"2022-08-12T18:45:16.050236Z","shell.execute_reply":"2022-08-12T18:45:16.058555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(base_model,show_shapes=True)","metadata":{"id":"jhoc6tmRZPe_","outputId":"24d88fc0-2595-4716-8a00-f84f89e6932b","execution":{"iopub.status.busy":"2022-08-12T18:45:16.061345Z","iopub.execute_input":"2022-08-12T18:45:16.061779Z","iopub.status.idle":"2022-08-12T18:45:17.486035Z","shell.execute_reply.started":"2022-08-12T18:45:16.061743Z","shell.execute_reply":"2022-08-12T18:45:17.484793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.compile(loss=tf.keras.losses.categorical_crossentropy,\n               optimizer=tf.keras.optimizers.Adam(),\n               metrics=['acc'])","metadata":{"id":"v9FjYURVZPe_","execution":{"iopub.status.busy":"2022-08-12T18:45:17.488968Z","iopub.execute_input":"2022-08-12T18:45:17.489790Z","iopub.status.idle":"2022-08-12T18:45:17.511808Z","shell.execute_reply.started":"2022-08-12T18:45:17.489744Z","shell.execute_reply":"2022-08-12T18:45:17.510707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Training**","metadata":{"id":"crDLUJkJi6OS"}},{"cell_type":"code","source":"base_model.fit(training_dataset,\n              epochs=5,\n              validation_data=testing_dataset,\n              validation_steps=int(0.10 * len(testing_dataset))\n              )","metadata":{"id":"QauwRAM6ity5","outputId":"d4648a72-524e-46c5-f6a8-e5b3afbba93a","execution":{"iopub.status.busy":"2022-08-12T18:45:25.171829Z","iopub.execute_input":"2022-08-12T18:45:25.172277Z","iopub.status.idle":"2022-08-12T18:56:07.834350Z","shell.execute_reply.started":"2022-08-12T18:45:25.172236Z","shell.execute_reply":"2022-08-12T18:56:07.832613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Evaluation**","metadata":{"id":"2Hw66r1ji3NF"}},{"cell_type":"code","source":"base_model.evaluate(testing_dataset)","metadata":{"id":"Bg7jPNr-ZPe_","outputId":"403314be-6513-47b4-dffc-c8ab189ce7f5","execution":{"iopub.status.busy":"2022-08-12T18:56:07.837252Z","iopub.execute_input":"2022-08-12T18:56:07.838014Z","iopub.status.idle":"2022-08-12T18:56:16.301979Z","shell.execute_reply.started":"2022-08-12T18:56:07.837974Z","shell.execute_reply":"2022-08-12T18:56:16.300715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"id":"iBA6jMQSZPfA","outputId":"adbe413e-2eb5-4d88-8f61-38a373f7b3a3","execution":{"iopub.status.busy":"2022-08-12T18:56:16.303259Z","iopub.execute_input":"2022-08-12T18:56:16.304555Z","iopub.status.idle":"2022-08-12T18:56:16.321029Z","shell.execute_reply.started":"2022-08-12T18:56:16.304516Z","shell.execute_reply":"2022-08-12T18:56:16.319678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nout_test = text_preprocessing(test_data.discourse_text.to_list())","metadata":{"id":"VytXg_W4ZPfA","outputId":"ce170816-d481-4548-8210-c7b644217eb9","execution":{"iopub.status.busy":"2022-08-12T18:56:16.323956Z","iopub.execute_input":"2022-08-12T18:56:16.324443Z","iopub.status.idle":"2022-08-12T18:56:17.271954Z","shell.execute_reply.started":"2022-08-12T18:56:16.324368Z","shell.execute_reply":"2022-08-12T18:56:17.270329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_proba = base_model.predict(out_test,verbose=1)","metadata":{"id":"kd9GFhjLZPfA","outputId":"49f24938-36d8-42cf-ef92-8ed4436fdd39","execution":{"iopub.status.busy":"2022-08-12T18:56:17.273966Z","iopub.execute_input":"2022-08-12T18:56:17.274346Z","iopub.status.idle":"2022-08-12T18:56:18.884139Z","shell.execute_reply.started":"2022-08-12T18:56:17.274314Z","shell.execute_reply":"2022-08-12T18:56:18.883106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(suppress=True)","metadata":{"id":"uKs9R50sZPfA","execution":{"iopub.status.busy":"2022-08-12T18:56:18.885610Z","iopub.execute_input":"2022-08-12T18:56:18.885968Z","iopub.status.idle":"2022-08-12T18:56:18.891442Z","shell.execute_reply.started":"2022-08-12T18:56:18.885936Z","shell.execute_reply":"2022-08-12T18:56:18.890399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_proba","metadata":{"id":"k0TtGe6iZPfA","outputId":"3fdf85eb-5f72-4500-dec0-1c0d3c02e5fd","execution":{"iopub.status.busy":"2022-08-12T18:56:18.892843Z","iopub.execute_input":"2022-08-12T18:56:18.893912Z","iopub.status.idle":"2022-08-12T18:56:18.908013Z","shell.execute_reply.started":"2022-08-12T18:56:18.893871Z","shell.execute_reply":"2022-08-12T18:56:18.906103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Saving and Submission**","metadata":{"id":"TUk_Ftt3jEN1"}},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/feedback-prize-effectiveness/sample_submission.csv\")","metadata":{"id":"TcW5PCyUZPfA","execution":{"iopub.status.busy":"2022-08-12T18:56:18.910814Z","iopub.execute_input":"2022-08-12T18:56:18.911344Z","iopub.status.idle":"2022-08-12T18:56:18.937506Z","shell.execute_reply.started":"2022-08-12T18:56:18.911296Z","shell.execute_reply":"2022-08-12T18:56:18.936494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=0\nwhile i<10:\n  submission['Ineffective'][i]=pred_proba[i][0]\n  submission['Adequate'][i]=pred_proba[i][1]\n  submission['Effective'][i]=pred_proba[i][2]\n  i=i+1","metadata":{"id":"mRz9doHbjdBO","outputId":"0afe9645-15ce-400f-be4d-cda48b21b5bc","execution":{"iopub.status.busy":"2022-08-12T18:56:18.941646Z","iopub.execute_input":"2022-08-12T18:56:18.942037Z","iopub.status.idle":"2022-08-12T18:56:18.960941Z","shell.execute_reply.started":"2022-08-12T18:56:18.942005Z","shell.execute_reply":"2022-08-12T18:56:18.959601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"id":"NS6lScqtZPfA","outputId":"f1f145ae-3c06-46c5-cd56-a214691b617e","execution":{"iopub.status.busy":"2022-08-12T18:56:18.964092Z","iopub.execute_input":"2022-08-12T18:56:18.964492Z","iopub.status.idle":"2022-08-12T18:56:18.978638Z","shell.execute_reply.started":"2022-08-12T18:56:18.964457Z","shell.execute_reply":"2022-08-12T18:56:18.977032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"id":"6BOTPOboZPfB","execution":{"iopub.status.busy":"2022-08-12T18:56:18.980555Z","iopub.execute_input":"2022-08-12T18:56:18.981032Z","iopub.status.idle":"2022-08-12T18:56:18.993519Z","shell.execute_reply.started":"2022-08-12T18:56:18.980998Z","shell.execute_reply":"2022-08-12T18:56:18.991704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.save_weights('/content/input')","metadata":{"id":"drlXJS_FZPfB","execution":{"iopub.status.busy":"2022-08-12T18:56:18.995108Z","iopub.execute_input":"2022-08-12T18:56:18.995733Z","iopub.status.idle":"2022-08-12T18:56:19.075711Z","shell.execute_reply.started":"2022-08-12T18:56:18.995696Z","shell.execute_reply":"2022-08-12T18:56:19.074486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Thanks for joining me!**","metadata":{"id":"suwWXO2cl3QA"}}]}