{"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\n# import os\n# for 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-06-22T19:34:23.866767Z","iopub.execute_input":"2021-06-22T19:34:23.867056Z","iopub.status.idle":"2021-06-22T19:34:23.875358Z","shell.execute_reply.started":"2021-06-22T19:34:23.866989Z","shell.execute_reply":"2021-06-22T19:34:23.874623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Libraries installation and import","metadata":{"execution":{"iopub.status.busy":"2021-06-18T13:43:51.918351Z","iopub.execute_input":"2021-06-18T13:43:51.9189Z","iopub.status.idle":"2021-06-18T13:43:51.923857Z","shell.execute_reply.started":"2021-06-18T13:43:51.918751Z","shell.execute_reply":"2021-06-18T13:43:51.922829Z"}}},{"cell_type":"code","source":"!pip3 install hvplot[pandas]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:24.040296Z","iopub.execute_input":"2021-06-22T19:34:24.040634Z","iopub.status.idle":"2021-06-22T19:34:33.168912Z","shell.execute_reply.started":"2021-06-22T19:34:24.0406Z","shell.execute_reply":"2021-06-22T19:34:33.167984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nimport spacy","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:33.172425Z","iopub.execute_input":"2021-06-22T19:34:33.1727Z","iopub.status.idle":"2021-06-22T19:34:35.857683Z","shell.execute_reply.started":"2021-06-22T19:34:33.172659Z","shell.execute_reply":"2021-06-22T19:34:35.856823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from functools import partial\nfrom pathlib import Path\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:35.859423Z","iopub.execute_input":"2021-06-22T19:34:35.859749Z","iopub.status.idle":"2021-06-22T19:34:35.864191Z","shell.execute_reply.started":"2021-06-22T19:34:35.859722Z","shell.execute_reply":"2021-06-22T19:34:35.862737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split, cross_validate","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:35.865924Z","iopub.execute_input":"2021-06-22T19:34:35.866269Z","iopub.status.idle":"2021-06-22T19:34:35.887106Z","shell.execute_reply.started":"2021-06-22T19:34:35.866233Z","shell.execute_reply":"2021-06-22T19:34:35.886388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport seaborn as sns\nimport hvplot.pandas \n\nimport yellowbrick\nfrom yellowbrick.text import FreqDistVisualizer, UMAPVisualizer","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:35.888211Z","iopub.execute_input":"2021-06-22T19:34:35.88855Z","iopub.status.idle":"2021-06-22T19:34:56.761031Z","shell.execute_reply.started":"2021-06-22T19:34:35.888517Z","shell.execute_reply":"2021-06-22T19:34:56.760147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset","metadata":{"execution":{"iopub.status.busy":"2021-06-18T13:51:29.741267Z","iopub.execute_input":"2021-06-18T13:51:29.74167Z","iopub.status.idle":"2021-06-18T13:51:31.501732Z","shell.execute_reply.started":"2021-06-18T13:51:29.741637Z","shell.execute_reply":"2021-06-18T13:51:31.50071Z"}}},{"cell_type":"code","source":"# reading csv files and train & test file paths\npath = Path(\"/kaggle/input/coleridgeinitiative-show-us-the-data\")\ntrain_df = pd.read_csv(path / 'train.csv')\nsample_sub = pd.read_csv(path / 'sample_submission.csv')\ntrain_files_path = path / 'train'\ntest_files_path = path / 'test'","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:56.76227Z","iopub.execute_input":"2021-06-22T19:34:56.76261Z","iopub.status.idle":"2021-06-22T19:34:56.891073Z","shell.execute_reply.started":"2021-06-22T19:34:56.762577Z","shell.execute_reply":"2021-06-22T19:34:56.890154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:56.892331Z","iopub.execute_input":"2021-06-22T19:34:56.892723Z","iopub.status.idle":"2021-06-22T19:34:56.914105Z","shell.execute_reply.started":"2021-06-22T19:34:56.892654Z","shell.execute_reply":"2021-06-22T19:34:56.913037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Retrieve introduction / abstract","metadata":{}},{"cell_type":"code","source":"def retrieve_first_section(filename, train_files_path=train_files_path):\n    \"\"\"Return the first section of the article (most probably abstract or introduction).\"\"\"\n    \n    json_path = (train_files_path / filename).with_suffix('.json')\n    first_section = []\n    \n    with json_path.open() as f:\n        json_decode = json.load(f)\n        for data in json_decode:\n            if data.get('text').strip() != '' and not first_section:\n                first_section.append(data.get('text'))\n\n    return first_section[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:56.916847Z","iopub.execute_input":"2021-06-22T19:34:56.91719Z","iopub.status.idle":"2021-06-22T19:34:56.922513Z","shell.execute_reply.started":"2021-06-22T19:34:56.917155Z","shell.execute_reply":"2021-06-22T19:34:56.921521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas()   #tqdm is used to show any code running with a progress bar. \ntrain_df['text'] = train_df['Id'].progress_apply(retrieve_first_section)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:34:56.924175Z","iopub.execute_input":"2021-06-22T19:34:56.924744Z","iopub.status.idle":"2021-06-22T19:36:00.518652Z","shell.execute_reply.started":"2021-06-22T19:34:56.924707Z","shell.execute_reply":"2021-06-22T19:36:00.517794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas()\nsample_sub['text'] = sample_sub['Id'].progress_apply(partial(retrieve_first_section, train_files_path=test_files_path))","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:00.51999Z","iopub.execute_input":"2021-06-22T19:36:00.520345Z","iopub.status.idle":"2021-06-22T19:36:00.553504Z","shell.execute_reply.started":"2021-06-22T19:36:00.520307Z","shell.execute_reply":"2021-06-22T19:36:00.552687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:00.554775Z","iopub.execute_input":"2021-06-22T19:36:00.555118Z","iopub.status.idle":"2021-06-22T19:36:00.566809Z","shell.execute_reply.started":"2021-06-22T19:36:00.555083Z","shell.execute_reply":"2021-06-22T19:36:00.565801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop every first_section with more than 100000 caracters\ntrain_df = train_df[train_df.text.str.len() < 100000]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:00.57833Z","iopub.execute_input":"2021-06-22T19:36:00.578708Z","iopub.status.idle":"2021-06-22T19:36:00.617629Z","shell.execute_reply.started":"2021-06-22T19:36:00.57866Z","shell.execute_reply":"2021-06-22T19:36:00.61689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.index.size","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:00.618842Z","iopub.execute_input":"2021-06-22T19:36:00.619154Z","iopub.status.idle":"2021-06-22T19:36:00.627208Z","shell.execute_reply.started":"2021-06-22T19:36:00.619122Z","shell.execute_reply":"2021-06-22T19:36:00.626419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove badly represented labels/articles\ntrain_df = train_df[train_df['cleaned_label'].isin(train_df['cleaned_label'].value_counts()[train_df['cleaned_label'].value_counts()>5].index)]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:00.628435Z","iopub.execute_input":"2021-06-22T19:36:00.6288Z","iopub.status.idle":"2021-06-22T19:36:00.646448Z","shell.execute_reply.started":"2021-06-22T19:36:00.628766Z","shell.execute_reply":"2021-06-22T19:36:00.645738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.index.size","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:00.647588Z","iopub.execute_input":"2021-06-22T19:36:00.647959Z","iopub.status.idle":"2021-06-22T19:36:00.653145Z","shell.execute_reply.started":"2021-06-22T19:36:00.647925Z","shell.execute_reply":"2021-06-22T19:36:00.652143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Text cleaning","metadata":{}},{"cell_type":"code","source":"import spacy\n\nis_using_gpu = spacy.prefer_gpu()\nnlp = spacy.load(\"en_core_web_lg\")","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:08.276092Z","iopub.execute_input":"2021-06-22T19:36:08.276417Z","iopub.status.idle":"2021-06-22T19:36:27.778825Z","shell.execute_reply.started":"2021-06-22T19:36:08.276388Z","shell.execute_reply":"2021-06-22T19:36:27.777917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corpus = train_df.text.str.lower()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:27.780306Z","iopub.execute_input":"2021-06-22T19:36:27.780642Z","iopub.status.idle":"2021-06-22T19:36:27.90052Z","shell.execute_reply.started":"2021-06-22T19:36:27.780607Z","shell.execute_reply":"2021-06-22T19:36:27.899679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lemmatize(doc):\n    return [token.lemma_ for token in doc \n            if token.is_alpha \n            and not token.is_stop \n            and (len(token.text) > 2)] ","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:27.902327Z","iopub.execute_input":"2021-06-22T19:36:27.902705Z","iopub.status.idle":"2021-06-22T19:36:27.906975Z","shell.execute_reply.started":"2021-06-22T19:36:27.902654Z","shell.execute_reply":"2021-06-22T19:36:27.906186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(docs, batch_size=200):\n    preprocessed = []\n    for doc in tqdm(nlp.pipe(docs, batch_size=batch_size)):\n        preprocessed.append(' '.join(lemmatize(doc)))\n    return preprocessed","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:27.908485Z","iopub.execute_input":"2021-06-22T19:36:27.909046Z","iopub.status.idle":"2021-06-22T19:36:27.916386Z","shell.execute_reply.started":"2021-06-22T19:36:27.90901Z","shell.execute_reply":"2021-06-22T19:36:27.915564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['preprocessed'] = preprocess(corpus)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:36:27.917816Z","iopub.execute_input":"2021-06-22T19:36:27.9182Z","iopub.status.idle":"2021-06-22T19:41:02.953478Z","shell.execute_reply.started":"2021-06-22T19:36:27.918167Z","shell.execute_reply":"2021-06-22T19:41:02.952366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.preprocessed.str.len().hvplot.hist()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:02.95484Z","iopub.execute_input":"2021-06-22T19:41:02.955343Z","iopub.status.idle":"2021-06-22T19:41:03.054374Z","shell.execute_reply.started":"2021-06-22T19:41:02.955302Z","shell.execute_reply":"2021-06-22T19:41:03.05343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop every first_section with more than 2000 preprocessed words\ntrain_df = train_df[train_df.preprocessed.str.len() < 20000]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:03.0557Z","iopub.execute_input":"2021-06-22T19:41:03.056043Z","iopub.status.idle":"2021-06-22T19:41:03.078141Z","shell.execute_reply.started":"2021-06-22T19:41:03.056007Z","shell.execute_reply":"2021-06-22T19:41:03.077223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"markdown","source":"## Sample  size","metadata":{}},{"cell_type":"code","source":"train_df.index.size","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:10.145877Z","iopub.execute_input":"2021-06-22T19:41:10.146214Z","iopub.status.idle":"2021-06-22T19:41:10.151704Z","shell.execute_reply.started":"2021-06-22T19:41:10.146181Z","shell.execute_reply":"2021-06-22T19:41:10.150756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:10.734594Z","iopub.execute_input":"2021-06-22T19:41:10.735046Z","iopub.status.idle":"2021-06-22T19:41:10.762824Z","shell.execute_reply.started":"2021-06-22T19:41:10.735009Z","shell.execute_reply":"2021-06-22T19:41:10.761617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Labels distribution\n\nCheck labels balance and unicity","metadata":{}},{"cell_type":"code","source":"labels = train_df.cleaned_label.unique()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:15.854785Z","iopub.execute_input":"2021-06-22T19:41:15.855114Z","iopub.status.idle":"2021-06-22T19:41:15.86137Z","shell.execute_reply.started":"2021-06-22T19:41:15.855084Z","shell.execute_reply":"2021-06-22T19:41:15.860318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.size","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:16.284513Z","iopub.execute_input":"2021-06-22T19:41:16.284879Z","iopub.status.idle":"2021-06-22T19:41:16.289887Z","shell.execute_reply.started":"2021-06-22T19:41:16.284847Z","shell.execute_reply":"2021-06-22T19:41:16.288848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(train_df.cleaned_label).value_counts().head(50).hvplot.bar(width=900, height=1000, rot=70)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:16.754752Z","iopub.execute_input":"2021-06-22T19:41:16.755097Z","iopub.status.idle":"2021-06-22T19:41:16.874833Z","shell.execute_reply.started":"2021-06-22T19:41:16.755069Z","shell.execute_reply":"2021-06-22T19:41:16.869726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Token frequency distribution","metadata":{}},{"cell_type":"code","source":"vectorizer = CountVectorizer()\ndocs = vectorizer.fit_transform(train_df.preprocessed)\nfeatures = vectorizer.get_feature_names()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:18.170765Z","iopub.execute_input":"2021-06-22T19:41:18.171088Z","iopub.status.idle":"2021-06-22T19:41:22.009337Z","shell.execute_reply.started":"2021-06-22T19:41:18.17106Z","shell.execute_reply":"2021-06-22T19:41:22.008436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualizer = FreqDistVisualizer(features=features, orient=\"v\", size=(1920, 1080))\nvisualizer.fit(docs)\nvisualizer.ax.set_xlabel(\"Tokens\", fontsize=22)\nvisualizer.ax.set_ylabel(\"Frequency [#]\", fontsize=22)\nvisualizer.ax.tick_params(axis='x', labelsize=16, rotation=60)\nvisualizer.ax.tick_params(axis='y', labelsize=16)\nvisualizer.set_title(\"Frequency Distribution of Top 50 tokens\")\nyellowbrick.style.rcmod.set_style(rc={\"legend.fontsize\": 32})","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:22.010859Z","iopub.execute_input":"2021-06-22T19:41:22.011209Z","iopub.status.idle":"2021-06-22T19:41:22.647358Z","shell.execute_reply.started":"2021-06-22T19:41:22.011171Z","shell.execute_reply":"2021-06-22T19:41:22.646408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Wordcloud representation","metadata":{}},{"cell_type":"code","source":"!pip install wordcloud","metadata":{"execution":{"iopub.status.busy":"2021-06-22T18:36:50.909677Z","iopub.status.idle":"2021-06-22T18:36:50.910232Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from wordcloud import WordCloud\n\ntext = \" \".join(article for article in train_df.preprocessed)\nwordcloud = WordCloud(\n    max_font_size=50, max_words=100, background_color=\"white\", width=250, height=180\n).generate(text)\nplt.figure(figsize=(16, 10))\nplt.imshow(wordcloud, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:26.292802Z","iopub.execute_input":"2021-06-22T19:41:26.293156Z","iopub.status.idle":"2021-06-22T19:41:51.576323Z","shell.execute_reply.started":"2021-06-22T19:41:26.29312Z","shell.execute_reply":"2021-06-22T19:41:51.575543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Baseline","metadata":{}},{"cell_type":"code","source":"le = LabelEncoder()\nlabels = le.fit_transform(train_df.cleaned_label)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:51.578099Z","iopub.execute_input":"2021-06-22T19:41:51.578451Z","iopub.status.idle":"2021-06-22T19:41:51.588216Z","shell.execute_reply.started":"2021-06-22T19:41:51.578415Z","shell.execute_reply":"2021-06-22T19:41:51.587438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.model_selection import KFold, StratifiedKFold, StratifiedShuffleSplit","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:51.590038Z","iopub.execute_input":"2021-06-22T19:41:51.590638Z","iopub.status.idle":"2021-06-22T19:41:51.601518Z","shell.execute_reply.started":"2021-06-22T19:41:51.590488Z","shell.execute_reply":"2021-06-22T19:41:51.600791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:51.602802Z","iopub.execute_input":"2021-06-22T19:41:51.603278Z","iopub.status.idle":"2021-06-22T19:41:51.610975Z","shell.execute_reply.started":"2021-06-22T19:41:51.603244Z","shell.execute_reply":"2021-06-22T19:41:51.610217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scorings = {\n    \"jaccard\": \"jaccard_weighted\",\n    \"accuracy\": \"accuracy\",\n    \"balanced_accuracy\": \"balanced_accuracy\",\n    \"precision\": \"precision_weighted\",\n    \"recall\": \"recall_weighted\",\n    \"f1\": \"f1_weighted\",\n    \"roc_auc\": \"roc_auc_ovr_weighted\",\n}","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:41:51.612334Z","iopub.execute_input":"2021-06-22T19:41:51.612918Z","iopub.status.idle":"2021-06-22T19:41:51.620971Z","shell.execute_reply.started":"2021-06-22T19:41:51.612881Z","shell.execute_reply":"2021-06-22T19:41:51.620217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Logistic regression","metadata":{}},{"cell_type":"code","source":"tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2))\nlr = LogisticRegression(class_weight='balanced', solver='liblinear')\npipe = make_pipeline(tfidf, lr)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:42:18.55356Z","iopub.execute_input":"2021-06-22T19:42:18.553937Z","iopub.status.idle":"2021-06-22T19:42:18.558409Z","shell.execute_reply.started":"2021-06-22T19:42:18.553905Z","shell.execute_reply":"2021-06-22T19:42:18.557602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = cross_validate(pipe, train_df.preprocessed, train_df.cleaned_label, cv=cv, scoring=scorings, n_jobs=-1)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:42:22.183125Z","iopub.execute_input":"2021-06-22T19:42:22.183447Z","iopub.status.idle":"2021-06-22T19:47:09.755409Z","shell.execute_reply.started":"2021-06-22T19:42:22.183417Z","shell.execute_reply":"2021-06-22T19:47:09.754337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for scoring, scores in res.items():\n    if not scoring.endswith(\"time\"):\n        print(\n            f\"{' '.join(scoring.split('_')[1:])}: {scores.mean():0.2f} (+/- {scores.std() * 2:0.2f})\"\n        )","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:47:09.760218Z","iopub.execute_input":"2021-06-22T19:47:09.762298Z","iopub.status.idle":"2021-06-22T19:47:09.774209Z","shell.execute_reply.started":"2021-06-22T19:47:09.762256Z","shell.execute_reply":"2021-06-22T19:47:09.77303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Actual training\npipe.fit(train_df.preprocessed, y=train_df.cleaned_label)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:47:41.313744Z","iopub.execute_input":"2021-06-22T19:47:41.314124Z","iopub.status.idle":"2021-06-22T19:49:36.535324Z","shell.execute_reply.started":"2021-06-22T19:47:41.314086Z","shell.execute_reply":"2021-06-22T19:49:36.534239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save model","metadata":{}},{"cell_type":"code","source":"import joblib","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:50:19.846004Z","iopub.execute_input":"2021-06-22T19:50:19.846352Z","iopub.status.idle":"2021-06-22T19:50:19.850341Z","shell.execute_reply.started":"2021-06-22T19:50:19.846319Z","shell.execute_reply":"2021-06-22T19:50:19.849368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"./baseline_lr_model.pkl\"","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:50:20.498991Z","iopub.execute_input":"2021-06-22T19:50:20.499365Z","iopub.status.idle":"2021-06-22T19:50:20.503234Z","shell.execute_reply.started":"2021-06-22T19:50:20.499332Z","shell.execute_reply":"2021-06-22T19:50:20.502279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"joblib.dump(pipe, filename)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:50:22.433007Z","iopub.execute_input":"2021-06-22T19:50:22.433331Z","iopub.status.idle":"2021-06-22T19:50:31.322323Z","shell.execute_reply.started":"2021-06-22T19:50:22.433301Z","shell.execute_reply":"2021-06-22T19:50:31.321497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  Load model","metadata":{}},{"cell_type":"code","source":"baseline_model = joblib.load(filename)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:50:34.233098Z","iopub.execute_input":"2021-06-22T19:50:34.233435Z","iopub.status.idle":"2021-06-22T19:50:40.002413Z","shell.execute_reply.started":"2021-06-22T19:50:34.233404Z","shell.execute_reply":"2021-06-22T19:50:40.001539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predictions","metadata":{}},{"cell_type":"code","source":"sample_sub_preprocessed = preprocess(sample_sub.text.str.lower())","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:50:42.151204Z","iopub.execute_input":"2021-06-22T19:50:42.151524Z","iopub.status.idle":"2021-06-22T19:50:42.251507Z","shell.execute_reply.started":"2021-06-22T19:50:42.151494Z","shell.execute_reply":"2021-06-22T19:50:42.250528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub['PredictionString'] = baseline_model.predict(sample_sub_preprocessed)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:50:44.484989Z","iopub.execute_input":"2021-06-22T19:50:44.485317Z","iopub.status.idle":"2021-06-22T19:50:44.495829Z","shell.execute_reply.started":"2021-06-22T19:50:44.485287Z","shell.execute_reply":"2021-06-22T19:50:44.494773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.drop(columns=['text']).to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T19:50:49.176466Z","iopub.execute_input":"2021-06-22T19:50:49.176873Z","iopub.status.idle":"2021-06-22T19:50:49.24384Z","shell.execute_reply.started":"2021-06-22T19:50:49.176836Z","shell.execute_reply":"2021-06-22T19:50:49.243026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}