{"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":"7dd35798575161958278653d7325b3a6b3dc094e"},"cell_type":"code","source":"print(os.listdir(\"../input/alta-2018-challenge\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/alta2018traindata/train_data.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c09ea598097f11221444d0277d38ecf0ed3af608"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"32a27e58f99b19a313b56288012dc72a6291f1a4"},"cell_type":"code","source":"train['first_ipc_mark_section'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27b49a1c3e82bdccf202b80bd77a2a6945b888e0"},"cell_type":"code","source":"train['first_ipc_mark_section'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"db5b1e57016182948cbcef08163a4d41b790a0da"},"cell_type":"markdown","source":"We can see that there is high imbalance in the data. We may need to do something about this, but let's create a baseline that ignores  the imbalance."},{"metadata":{"_uuid":"e7bb5df8b74e78fdff619e6fd3565575d533933f"},"cell_type":"markdown","source":"# Baselines\n\nThe first baseline will simply output the most common class: A. The following code uses sklearn as a framework to build the baseline and evaluate it using 5-fold cross-validation and F1-microaveraged evaluation metric."},{"metadata":{"trusted":true,"_uuid":"9829c889bbdce7994b48742f4360726b2908777b"},"cell_type":"code","source":"from sklearn.dummy import DummyClassifier\nfrom sklearn.model_selection import cross_val_score\nfirst_baseline = DummyClassifier(strategy=\"most_frequent\")\nfirst_baseline_scores = cross_val_score(first_baseline, train[['id']], train['first_ipc_mark_section'], cv=5, scoring='f1_micro')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f808305c6522f8e2c79c31ef2e02b48a79629102"},"cell_type":"code","source":"print(first_baseline_scores.mean())\nprint(first_baseline_scores.std())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8c22a6057e88b3461bdd440efe9370a6e37fedf3"},"cell_type":"markdown","source":"The second baseline uses naive Bayes classification with tf.idf of the words as the features."},{"metadata":{"trusted":true,"_uuid":"1ddf4437b8bf410616b78e63429feee339dbb90c"},"cell_type":"code","source":"train['filename'] = ['../input/alta2018patents/patents/patents/'] + train['id'].astype(str) + ['.txt']\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7089816d63244bd1112911bc38429d84f1b7f091"},"cell_type":"code","source":"from sklearn.pipeline import Pipeline\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn import naive_bayes\ntfidf = TfidfVectorizer(input='filename', encoding='iso8859-1')\nnb = naive_bayes.MultinomialNB()\nsecond_baseline = Pipeline(steps=[('tfidf', tfidf), ('nb', nb)])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"358ee36df0b9923e04a3e96b2d7b8318c735a26c"},"cell_type":"code","source":"second_baseline_scores = cross_val_score(second_baseline, train['filename'], train['first_ipc_mark_section'], cv=5, scoring='f1_micro')\nsecond_baseline_scores.mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf29dd96427c1a7850adb363928be99c58cebb92"},"cell_type":"code","source":"second_baseline_scores.std()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"64934c6fa0bba4e36dfac1559802aeadcc04d1a0"},"cell_type":"markdown","source":"The following code uses the entire data set for training the system using naive Bayes and create a run to be submitted to the competition."},{"metadata":{"trusted":true,"_uuid":"4afea021e22873d661f194ed0012b5c745381567"},"cell_type":"code","source":"second_baseline.fit(train['filename'], train['first_ipc_mark_section'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b41f0a7df21cade5d65f8af4a1e822f3b78358d3"},"cell_type":"code","source":"test = pd.read_csv('../input/alta2018testdata/test_data.csv')\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"162a81c27de3d8bb22b647a9632cd3ed92f18f13"},"cell_type":"code","source":"test['filename'] = ['../input/alta2018patents/patents/patents/'] + test['id'].astype(str) + ['.txt']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1332c738b9663da57d462e0ae4053d52f3e19d52"},"cell_type":"code","source":"test['first_ipc_mark_section'] = second_baseline.predict(test['filename'])\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f49cd4f0d79cb9c163d5dc2c29c893a909a5b77"},"cell_type":"code","source":"test['first_ipc_mark_section'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"042f88af53e7a595a99acea43f374233861c0086"},"cell_type":"markdown","source":"We can see that the labels associated to the test data by the second baseline are only the three most frequent labels in the training data. There is clearly room for improvement. Anyway, the code below creates a CSV file that has been submitted as the Naive Bayes baseline."},{"metadata":{"trusted":true,"_uuid":"6086d6bbfa1e168b592e24de7e8f22d1811ae97a"},"cell_type":"code","source":"test[['id', 'first_ipc_mark_section']].to_csv('test_results.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"collapsed":true,"_uuid":"30dc2fa314e4aefaf8ca71fd7f0e2df7b819e786"},"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}