{"cells":[{"metadata":{},"cell_type":"markdown","source":"# I. Importing required libraries and data","execution_count":null},{"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\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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nfrom keras.preprocessing.text import Tokenizer\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.wrappers.scikit_learn import KerasClassifier\nimport nltk\nfrom nltk.stem import WordNetLemmatizer\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import f1_score, make_scorer\nfrom sklearn.metrics import roc_auc_score\nimport scikitplot as skplt\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data_train = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')\ndata_val = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')\nprint(data_train.shape)\nprint(data_train.isnull().sum())\nprint(data_val.isnull().sum())\nprint(data_train['target'].value_counts())\ndata_train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# II. Lemmatization","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def lemmatize(data):\n    lemmatizer = WordNetLemmatizer()\n    lem_data = []\n    for text in data:\n        lem_text = ''\n        for word in text.split():\n            word = word.lower()\n            lem_word = lemmatizer.lemmatize(word)\n            lem_word = lemmatizer.lemmatize(lem_word, pos='v')\n            lem_text = lem_text + ' ' + lem_word\n        lem_data.append(lem_text)\n    return lem_data\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = data_train['question_text']\ny = data_train['target']\nques_id = data_val['qid']\nX_val = data_val['question_text']\nX_lem = lemmatize(X.tolist())\nX_val = lemmatize(X_val.tolist())\nX_lem[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_lem, y, random_state=0)\ntokenizer = Tokenizer()\ntokenizer.fit_on_texts(X_train)\nword2idx = tokenizer.word_index\nvocab_size = len(word2idx)\nmax_len = 100\ntrain_seq = tokenizer.texts_to_sequences(X_train)\ntrain_pad = pad_sequences(train_seq, maxlen=max_len)\ntest_seq = tokenizer.texts_to_sequences(X_test)\ntest_pad = pad_sequences(test_seq, maxlen=max_len)\nval_seq = tokenizer.texts_to_sequences(X_val)\nval_pad = pad_sequences(val_seq, maxlen=max_len)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# III. Vectorization using Tfidf","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"tfidf = TfidfVectorizer(ngram_range=(1,3), min_df=5, max_df=0.9)\nX_train_tfidf = tfidf.fit_transform(X_train)\nX_test_tfidf = tfidf.transform(X_test)\nX_val_tfidf = tfidf.transform(X_val)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# IV. Training the Classifier using RandomizedSearchCV","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"clf = LogisticRegression(max_iter=1000)\nparams = {\n    'C':[0.001,0.01,0.1,1,10],\n}\nscorer=make_scorer(f1_score)\ngrid = RandomizedSearchCV(clf, params, scoring=scorer)\ncv_results = grid.fit(X_train_tfidf, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cv_results.best_params_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cv_results.cv_results_","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# V. Model prediction and evaluation","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"**Here, we use the ROC curve as a metric for evaluating our classification model. Since the dataset is imbalanced with high number of '0' labels as compared '1' labels, accuracy_score is an inappropriate metric as it tends to be biased towards the majority class. Hence, area under the ROC curve is used instead, whose value is between 0.5 and 1, and the higher, the better.**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"best_clf = cv_results.best_estimator_\nbest_clf.fit(X_train_tfidf, y_train)\ny_pred = best_clf.predict(X_test_tfidf)\nprint(y_pred)\ny_val = best_clf.predict(X_val_tfidf)\ny_val\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_prob = best_clf.predict_proba(X_test_tfidf)\npos_class_prob = [prob[1] for prob in y_prob]\nprint(roc_auc_score(y_test, pos_class_prob))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plotting ROC curve for test data.\nskplt.metrics.plot_roc_curve(y_test, y_prob)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Submitting predictions.\ndf = pd.DataFrame()\ndf['qid'] = ques_id\ndf['prediction'] = y_val\ndf.to_csv('submission.csv',index=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}