{"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":"2021-07-24T16:24:37.241446Z","iopub.execute_input":"2021-07-24T16:24:37.241976Z","iopub.status.idle":"2021-07-24T16:24:37.257680Z","shell.execute_reply.started":"2021-07-24T16:24:37.241891Z","shell.execute_reply":"2021-07-24T16:24:37.256652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/commonlitreadabilityprize/train.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T16:31:25.997908Z","iopub.execute_input":"2021-07-24T16:31:25.998530Z","iopub.status.idle":"2021-07-24T16:31:26.257092Z","shell.execute_reply.started":"2021-07-24T16:31:25.998479Z","shell.execute_reply":"2021-07-24T16:31:26.256436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Null check\ndata.isnull().sum()/len(data) * 100","metadata":{"execution":{"iopub.status.busy":"2021-07-24T16:31:49.267282Z","iopub.execute_input":"2021-07-24T16:31:49.267660Z","iopub.status.idle":"2021-07-24T16:31:49.294830Z","shell.execute_reply.started":"2021-07-24T16:31:49.267626Z","shell.execute_reply":"2021-07-24T16:31:49.294168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop(['url_legal', 'license'], 1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T16:32:07.504664Z","iopub.execute_input":"2021-07-24T16:32:07.505171Z","iopub.status.idle":"2021-07-24T16:32:07.514984Z","shell.execute_reply.started":"2021-07-24T16:32:07.505120Z","shell.execute_reply":"2021-07-24T16:32:07.513824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from gensim.parsing.preprocessing import remove_stopwords\n\ndocs = data['excerpt'].str.lower().str.replace('[^a-z\\s]', '')\n\ndocs = docs.apply(remove_stopwords)\ndocs[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:00:33.933455Z","iopub.execute_input":"2021-07-24T17:00:33.933857Z","iopub.status.idle":"2021-07-24T17:00:35.334229Z","shell.execute_reply.started":"2021-07-24T17:00:33.933827Z","shell.execute_reply":"2021-07-24T17:00:35.333133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\n\ntokenizer = Tokenizer()\n\ntokenizer.fit_on_texts(docs)\nvocab = list(tokenizer.word_index)\n\nprint('Total number of unique tokens in corpus: %d' % len(vocab))","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:00:48.080707Z","iopub.execute_input":"2021-07-24T17:00:48.081127Z","iopub.status.idle":"2021-07-24T17:00:53.714324Z","shell.execute_reply.started":"2021-07-24T17:00:48.081094Z","shell.execute_reply":"2021-07-24T17:00:53.713368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x_seq = tokenizer.texts_to_sequences(docs)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:01:02.012036Z","iopub.execute_input":"2021-07-24T17:01:02.012410Z","iopub.status.idle":"2021-07-24T17:01:02.168587Z","shell.execute_reply.started":"2021-07-24T17:01:02.012378Z","shell.execute_reply":"2021-07-24T17:01:02.167415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"docs_size = []\nfor doc in train_x_seq:\n    size = len(doc)\n    docs_size.append(size)\npd.Series(docs_size).plot.box()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:01:11.774536Z","iopub.execute_input":"2021-07-24T17:01:11.774894Z","iopub.status.idle":"2021-07-24T17:01:11.990475Z","shell.execute_reply.started":"2021-07-24T17:01:11.774865Z","shell.execute_reply":"2021-07-24T17:01:11.989405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_doc_len = 115\n\ntrain_x_padded = pad_sequences(train_x_seq, padding='post', maxlen=max_doc_len)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:01:28.261796Z","iopub.execute_input":"2021-07-24T17:01:28.262372Z","iopub.status.idle":"2021-07-24T17:01:28.330643Z","shell.execute_reply.started":"2021-07-24T17:01:28.262337Z","shell.execute_reply":"2021-07-24T17:01:28.329872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"docs[:5]","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:01:34.238307Z","iopub.execute_input":"2021-07-24T17:01:34.238665Z","iopub.status.idle":"2021-07-24T17:01:34.246651Z","shell.execute_reply.started":"2021-07-24T17:01:34.238635Z","shell.execute_reply":"2021-07-24T17:01:34.245444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(train_x_padded[:5])","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:01:47.684393Z","iopub.execute_input":"2021-07-24T17:01:47.685019Z","iopub.status.idle":"2021-07-24T17:01:47.706529Z","shell.execute_reply.started":"2021-07-24T17:01:47.684968Z","shell.execute_reply":"2021-07-24T17:01:47.705455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\n\nvocab_size = len(vocab) + 1\nembedding_dim = 300\n\nmodel = Sequential()\nmodel.add(layers.Embedding(vocab_size, embedding_dim,\n                          input_length=max_doc_len,\n                          trainable=True))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(1))\nmodel.compile(optimizer='adam', loss='mse', metrics=[\"mae\"])\nhistory = model.fit(train_x_padded, data['target'], epochs=25, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:07:10.445700Z","iopub.execute_input":"2021-07-24T17:07:10.446233Z","iopub.status.idle":"2021-07-24T17:10:56.944215Z","shell.execute_reply.started":"2021-07-24T17:07:10.446196Z","shell.execute_reply":"2021-07-24T17:10:56.943505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sgd optimizer\n# loss: 0.3717 - mae: 0.5002\n\n# rmsprop\n# loss: 0.0658 - mae: 0.2122\n\n# adam\n# loss: 0.0594 - mae: 0.1468  # Finalized","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/commonlitreadabilityprize/test.csv')\ntest_data = test_data[['id', 'excerpt']]\n\ntest_docs = test_data['excerpt'].str.lower().str.replace('[^a-z\\s]', '')\n\ntest_docs = test_docs.apply(remove_stopwords)\n\ntest_x_seq = tokenizer.texts_to_sequences(test_docs)\ntest_x_padded = pad_sequences(test_x_seq, padding='post', maxlen=max_doc_len)\ntest_docs","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:12:10.338047Z","iopub.execute_input":"2021-07-24T17:12:10.338434Z","iopub.status.idle":"2021-07-24T17:12:10.367084Z","shell.execute_reply.started":"2021-07-24T17:12:10.338401Z","shell.execute_reply":"2021-07-24T17:12:10.366089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(test_x_padded)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:12:11.756265Z","iopub.execute_input":"2021-07-24T17:12:11.756746Z","iopub.status.idle":"2021-07-24T17:12:11.777307Z","shell.execute_reply.started":"2021-07-24T17:12:11.756714Z","shell.execute_reply":"2021-07-24T17:12:11.776276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y_pred = model.predict(test_x_padded)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:12:12.852719Z","iopub.execute_input":"2021-07-24T17:12:12.853064Z","iopub.status.idle":"2021-07-24T17:12:12.977307Z","shell.execute_reply.started":"2021-07-24T17:12:12.853034Z","shell.execute_reply":"2021-07-24T17:12:12.976419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv('/kaggle/input/commonlitreadabilityprize/sample_submission.csv')\nsubmission_df","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:12:17.148464Z","iopub.execute_input":"2021-07-24T17:12:17.148815Z","iopub.status.idle":"2021-07-24T17:12:17.166810Z","shell.execute_reply.started":"2021-07-24T17:12:17.148785Z","shell.execute_reply":"2021-07-24T17:12:17.166138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['target'] = test_y_pred","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:12:17.852559Z","iopub.execute_input":"2021-07-24T17:12:17.852910Z","iopub.status.idle":"2021-07-24T17:12:17.858401Z","shell.execute_reply.started":"2021-07-24T17:12:17.852879Z","shell.execute_reply":"2021-07-24T17:12:17.857040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:12:19.964413Z","iopub.execute_input":"2021-07-24T17:12:19.964770Z","iopub.status.idle":"2021-07-24T17:12:19.974075Z","shell.execute_reply.started":"2021-07-24T17:12:19.964732Z","shell.execute_reply":"2021-07-24T17:12:19.973044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}