{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f2b98927fa588cb7bad6851daccf975f8d9c711"},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f65337244a9591ddd2e457a0cead4b37e521baf1"},"cell_type":"code","source":"test = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8cb6418dfa9a518c51043a1c8441f8432d5b0cb4"},"cell_type":"code","source":"qid_test = pd.read_csv('../input/test.csv')[['qid']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75ce308dbe00ed89bc83c0d95dbd8ee08af7ec5e"},"cell_type":"code","source":"sample_submission = pd.read_csv('../input/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cca14561d2d489b373cab026e6c6fa08c36af407"},"cell_type":"code","source":"import tensorflow as tf\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"de5764b813ce110a0434f0e7f02b06f562d87f63"},"cell_type":"markdown","source":"# Classification with fastai"},{"metadata":{"trusted":true,"_uuid":"59bdefed644a8c75fbc6fc6b4b908c534bc518ba"},"cell_type":"code","source":"import fastai\nfrom fastai import *\nfrom fastai.text import * \nimport pandas as pd\nimport numpy as np\nfrom functools import partial\nimport io\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17a0db0ea5b2fa518225ae3645e737c4cc292e6a"},"cell_type":"code","source":"from sklearn.metrics import f1_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2174ff100cee7b0818c974605c7939cd4876749a","_kg_hide-output":true,"scrolled":true},"cell_type":"code","source":"df = pd.DataFrame({'label':train.target, 'text':train.question_text})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33d4f1a94b678a947a98a4966d0e449d05ef6c9b"},"cell_type":"code","source":"test = pd.DataFrame({'text':test.question_text})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74f6d9fe92f640fddd9b7b629688f9e3ae689d17"},"cell_type":"code","source":"# split data into training and validation set\ndf_trn, df_val = train_test_split(df, stratify = df['label'], test_size = 0.25, random_state = 42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9217cde847d23aa3599447aa8843dc0c2b172585"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b1b8730353b3e264129a13f1a04c66d6d66853c"},"cell_type":"code","source":"df_trn.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84a10aca48319835e8a3908678dbac7b6c95c34d"},"cell_type":"code","source":"# Language model data\ndata_lm = TextLMDataBunch.from_df(train_df = df_trn, valid_df = df_val, test_df=test, path = \"\", text_cols='text', label_cols='label')\n\n# Classifier model data\ndata_clas = TextClasDataBunch.from_df(path = \"\", train_df = df_trn, valid_df = df_val, test_df = test,text_cols='text',label_cols='label', vocab=data_lm.train_ds.vocab, bs=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f19839ee7cd87ad3b3028c42f4b3877064a8b2d"},"cell_type":"code","source":"learn = language_model_learner(data_lm, AWD_LSTM)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba690c84f2b4d529c0f54778b0bff73ac556fcd3"},"cell_type":"code","source":"moms = (0.8,0.7)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6458e734b9fce2787e87b86834e72c1f86321aa"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(3, slice(1e-2), moms=moms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e8041392609ad4b5c0b073346d1fa7c80053d6d"},"cell_type":"code","source":"learn.save_encoder('enc')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebefb7da1555e8a8ad1b711fa1f34981f0102cf7"},"cell_type":"code","source":"learn = text_classifier_learner(data_clas, AWD_LSTM)\nlearn.load_encoder('enc')\nlearn.fit_one_cycle(1, moms=moms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46c0ec3b9a600effb7a5e7496d9f7b805669bbeb"},"cell_type":"code","source":"res = learn.get_preds(ds_type=DatasetType.Valid)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e295e95177c96291afb6a495db7307874f06a51"},"cell_type":"code","source":"# check f1 score on valid\npredictions = np.argmax(res[0], axis=1)\nf1_score(res[1], predictions) # 0.537","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f68d9da129edd39517e206f547d8a9a708c4d803"},"cell_type":"code","source":"pd.crosstab(predictions, res[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01243d6523990df5c5889dfb692e36de4eb637d6"},"cell_type":"code","source":"# check on test set\ntest_res = learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b6b3cbcd232954275e99a234478fe31c71bd363"},"cell_type":"code","source":"test_pred = np.argmax(test_res[0], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ba3db2152225c1f5cd7d2940866188fc647092c"},"cell_type":"code","source":"submission_df = pd.DataFrame({'qid':qid_test.qid, 'prediction':test_pred})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ad72fa7af9ff42a2c673f59fce31677a9f1b5ce"},"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b01b57ada828f4a547a62f96da2a0d9d6a737f1"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}