{"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-10-16T02:12:54.360066Z","iopub.execute_input":"2021-10-16T02:12:54.361078Z","iopub.status.idle":"2021-10-16T02:12:54.393184Z","shell.execute_reply.started":"2021-10-16T02:12:54.360972Z","shell.execute_reply":"2021-10-16T02:12:54.392224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')\ntest_df = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:12:54.395094Z","iopub.execute_input":"2021-10-16T02:12:54.395921Z","iopub.status.idle":"2021-10-16T02:13:00.466682Z","shell.execute_reply.started":"2021-10-16T02:12:54.395869Z","shell.execute_reply":"2021-10-16T02:13:00.46582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:13:01.351654Z","iopub.execute_input":"2021-10-16T02:13:01.352042Z","iopub.status.idle":"2021-10-16T02:13:01.377628Z","shell.execute_reply.started":"2021-10-16T02:13:01.351996Z","shell.execute_reply":"2021-10-16T02:13:01.376186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:13:04.536301Z","iopub.execute_input":"2021-10-16T02:13:04.536615Z","iopub.status.idle":"2021-10-16T02:13:04.867108Z","shell.execute_reply.started":"2021-10-16T02:13:04.536582Z","shell.execute_reply":"2021-10-16T02:13:04.866279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:15:19.161819Z","iopub.execute_input":"2021-10-16T02:15:19.162693Z","iopub.status.idle":"2021-10-16T02:15:19.179918Z","shell.execute_reply.started":"2021-10-16T02:15:19.162649Z","shell.execute_reply":"2021-10-16T02:15:19.17906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\nimport nltk\n\nfrom nltk.tokenize import word_tokenize\nfrom nltk.corpus import stopwords\n\nnltk.download(['punkt', 'stopwords'])\n\n\ndef preprocess(text):\n    # O(1) optmization\n    stopwords_set = set(stopwords.words('english'))\n    return ' '.join(\n        token.lower() for token in word_tokenize(text)\n        if token.isalpha() and token not in stopwords_set\n    )","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:15:23.15298Z","iopub.execute_input":"2021-10-16T02:15:23.153301Z","iopub.status.idle":"2021-10-16T02:15:43.641657Z","shell.execute_reply.started":"2021-10-16T02:15:23.153264Z","shell.execute_reply":"2021-10-16T02:15:43.640586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer\n\nvectorizer = TfidfVectorizer(\n    ngram_range=(1,2),\n    max_features=500\n)\n\nX = vectorizer.fit_transform(train_df['question_text'].apply(preprocess))","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:15:43.644012Z","iopub.execute_input":"2021-10-16T02:15:43.644461Z","iopub.status.idle":"2021-10-16T02:25:13.416095Z","shell.execute_reply.started":"2021-10-16T02:15:43.644411Z","shell.execute_reply":"2021-10-16T02:25:13.414691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_df['target'].values","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:25:13.417575Z","iopub.execute_input":"2021-10-16T02:25:13.417804Z","iopub.status.idle":"2021-10-16T02:25:13.422466Z","shell.execute_reply.started":"2021-10-16T02:25:13.417776Z","shell.execute_reply":"2021-10-16T02:25:13.42156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:25:13.425975Z","iopub.execute_input":"2021-10-16T02:25:13.426365Z","iopub.status.idle":"2021-10-16T02:25:13.441017Z","shell.execute_reply.started":"2021-10-16T02:25:13.426296Z","shell.execute_reply":"2021-10-16T02:25:13.440209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:25:13.442754Z","iopub.execute_input":"2021-10-16T02:25:13.443355Z","iopub.status.idle":"2021-10-16T02:25:13.453668Z","shell.execute_reply.started":"2021-10-16T02:25:13.443299Z","shell.execute_reply":"2021-10-16T02:25:13.452955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\n\nsmote = SMOTE()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:44:47.918941Z","iopub.execute_input":"2021-10-16T03:44:47.919534Z","iopub.status.idle":"2021-10-16T03:44:47.925052Z","shell.execute_reply.started":"2021-10-16T03:44:47.919476Z","shell.execute_reply":"2021-10-16T03:44:47.924044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_, y_ = smote.fit_resample(X, y)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:44:54.682144Z","iopub.execute_input":"2021-10-16T03:44:54.683139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y_).value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X_, y_, test_size=0.33, random_state=42)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBClassifier\n\nmodel = XGBClassifier(use_label_encoder=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred = model.predict(X_val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\n\nprint(metrics.classification_report(y_val_pred, y_val))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:14:21.525573Z","iopub.execute_input":"2021-10-16T03:14:21.525908Z","iopub.status.idle":"2021-10-16T03:14:21.8854Z","shell.execute_reply.started":"2021-10-16T03:14:21.525863Z","shell.execute_reply":"2021-10-16T03:14:21.884605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:14:23.328684Z","iopub.execute_input":"2021-10-16T03:14:23.328989Z","iopub.status.idle":"2021-10-16T03:14:23.340021Z","shell.execute_reply.started":"2021-10-16T03:14:23.328958Z","shell.execute_reply":"2021-10-16T03:14:23.339076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = vectorizer.transform(test_df['question_text'].apply(preprocess))","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:14:26.100921Z","iopub.execute_input":"2021-10-16T03:14:26.101251Z","iopub.status.idle":"2021-10-16T03:17:01.514253Z","shell.execute_reply.started":"2021-10-16T03:14:26.101211Z","shell.execute_reply":"2021-10-16T03:17:01.51344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:17:01.515788Z","iopub.execute_input":"2021-10-16T03:17:01.516165Z","iopub.status.idle":"2021-10-16T03:17:01.536899Z","shell.execute_reply.started":"2021-10-16T03:17:01.516133Z","shell.execute_reply":"2021-10-16T03:17:01.536208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df.loc[:, ('qid',)]\nsubmission_df['prediction'] = pd.Series(y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:17:01.538155Z","iopub.execute_input":"2021-10-16T03:17:01.538572Z","iopub.status.idle":"2021-10-16T03:17:01.550616Z","shell.execute_reply.started":"2021-10-16T03:17:01.538525Z","shell.execute_reply":"2021-10-16T03:17:01.550044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:17:01.552233Z","iopub.execute_input":"2021-10-16T03:17:01.552479Z","iopub.status.idle":"2021-10-16T03:17:01.566262Z","shell.execute_reply.started":"2021-10-16T03:17:01.55245Z","shell.execute_reply":"2021-10-16T03:17:01.565666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T03:17:01.567721Z","iopub.execute_input":"2021-10-16T03:17:01.568115Z","iopub.status.idle":"2021-10-16T03:17:02.498476Z","shell.execute_reply.started":"2021-10-16T03:17:01.568085Z","shell.execute_reply":"2021-10-16T03:17:02.497774Z"},"trusted":true},"execution_count":null,"outputs":[]}]}