{"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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('../input/train.csv')\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"051d1f43560b120c1453732baf89fcfb7f6c026f"},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b40a5cecba4f81c329cb71c6bd1c68dd13b4b1e5"},"cell_type":"code","source":"train_data.isnull().any()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92586d47c59323ddc18f64bfccac21d3042e8066"},"cell_type":"code","source":"train_data['target'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1b568722d51e6e2c4e17f61e13be0632814e4f79"},"cell_type":"markdown","source":"#### We have few columns no initial EDA will be fast. Now what we see here is that we have class imbalance so we will need to stratify."},{"metadata":{"trusted":true,"_uuid":"a426b6f18f95e39be5d3fb8e556e7e314bee5f4e"},"cell_type":"markdown","source":"#### What we will need to do next is to do some feature extraction using TfidfVectorizer."},{"metadata":{"trusted":true,"_uuid":"1cef8d08579ef2d03a6c7534a96d613e493ae0f3"},"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer\n\n# TfidfVectorizer instance.\ntfidf = TfidfVectorizer()\n\n# Feature vector and target variable.\nX = train_data['question_text']\ny = train_data['target'].values\n\nX_vec = tfidf.fit_transform(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"70e6c7621bfe160aeae39a69897939baeb2b0d0a"},"cell_type":"code","source":"X_vec.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"abee2c2342f2543541e28e785219d053350d226f"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X_vec, y, stratify=y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fccdac71e79037c9822061505a813514815b3707"},"cell_type":"code","source":"from sklearn.naive_bayes import MultinomialNB\nfrom sklearn.metrics import accuracy_score\n\nclf = MultinomialNB()\nclf.fit(X_train, y_train)\ny_pred = clf.predict(X_test)\n\nprint(accuracy_score(y_test, y_pred) *100, 2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8789fe49ef84101695a1c575fe6a7023ba3ee35a"},"cell_type":"markdown","source":"#### This part will be reserved for making predictions for submission."},{"metadata":{"trusted":true,"_uuid":"1ceed4903f5ce0ccb105064664844cd2b14af069"},"cell_type":"code","source":"submission_sample = pd.read_csv('../input/sample_submission.csv')\nsubmission_sample.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39063a4fa4380ca0bad92473952d33a8644c3537"},"cell_type":"code","source":"test = pd.read_csv('../input/test.csv')\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"056320624ccf9085c9dba1ff75410b8646be512a"},"cell_type":"code","source":"test_vec = tfidf.transform(test['question_text'])\ntest_vec.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49a9fe596e9d2f166fc730f125bb38128835e799"},"cell_type":"code","source":"test_pred = clf.predict(test_vec)\ntest_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99555d91e24c9b6e42b53884880a4826b9787417"},"cell_type":"code","source":"y_pred_result = pd.Series(test_pred)\ny_pred_result.name = 'prediction'\nsubmit = pd.concat([test['qid'], y_pred_result], axis=1, names=['qid', 'prediction'])\nsubmit.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fdf01e2d2e0326f8536ba30b678726bcf4a2fa7c"},"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}