{"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":"markdown","source":"### Data Exploration","metadata":{"id":"yfhnvoCap5SD"}},{"cell_type":"code","source":"import pandas as pd","metadata":{"id":"R1hfiO4Nq8zZ","execution":{"iopub.status.busy":"2023-06-06T13:47:16.603440Z","iopub.execute_input":"2023-06-06T13:47:16.603920Z","iopub.status.idle":"2023-06-06T13:47:16.629188Z","shell.execute_reply.started":"2023-06-06T13:47:16.603884Z","shell.execute_reply":"2023-06-06T13:47:16.628175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_fname = '/kaggle/input/quora-insincere-questions-classification/train.csv'\ntest_fname = '/kaggle/input/quora-insincere-questions-classification/test.csv'\nsubmission_fname = '/kaggle/input/quora-insincere-questions-classification/sample_submission.csv'","metadata":{"id":"i3MGeXJqrGdn","execution":{"iopub.status.busy":"2023-06-06T13:47:16.644283Z","iopub.execute_input":"2023-06-06T13:47:16.645268Z","iopub.status.idle":"2023-06-06T13:47:16.649950Z","shell.execute_reply.started":"2023-06-06T13:47:16.645231Z","shell.execute_reply":"2023-06-06T13:47:16.648798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(train_fname)\ntest_df = pd.read_csv(test_fname)\nsubmission_df = pd.read_csv(submission_fname)","metadata":{"id":"TzoKkZS_rXbW","execution":{"iopub.status.busy":"2023-06-06T13:47:16.652088Z","iopub.execute_input":"2023-06-06T13:47:16.653168Z","iopub.status.idle":"2023-06-06T13:47:23.395303Z","shell.execute_reply.started":"2023-06-06T13:47:16.653126Z","shell.execute_reply":"2023-06-06T13:47:23.394064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"id":"ppnDRNQUraCS","outputId":"5c10dc9b-e4ac-4eca-963a-b3c4668f3cee","execution":{"iopub.status.busy":"2023-06-06T13:47:23.396660Z","iopub.execute_input":"2023-06-06T13:47:23.397147Z","iopub.status.idle":"2023-06-06T13:47:23.432404Z","shell.execute_reply.started":"2023-06-06T13:47:23.397105Z","shell.execute_reply":"2023-06-06T13:47:23.431258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Text Preprocessing","metadata":{"id":"GfE5KrxRt72U"}},{"cell_type":"markdown","source":"### Tokenization","metadata":{"id":"CfCQfZ2dgprv"}},{"cell_type":"code","source":"import nltk\nfrom nltk.tokenize import word_tokenize","metadata":{"id":"lwAGB2aafSNs","execution":{"iopub.status.busy":"2023-06-06T13:47:23.433788Z","iopub.execute_input":"2023-06-06T13:47:23.434818Z","iopub.status.idle":"2023-06-06T13:47:24.853422Z","shell.execute_reply.started":"2023-06-06T13:47:23.434781Z","shell.execute_reply":"2023-06-06T13:47:24.852216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nltk.download('punkt')\n","metadata":{"id":"Ap6Rd5mHfSKR","outputId":"4084a33f-3eca-4523-d802-962c967f13b9","execution":{"iopub.status.busy":"2023-06-06T13:47:24.856463Z","iopub.execute_input":"2023-06-06T13:47:24.856844Z","iopub.status.idle":"2023-06-06T13:47:25.045392Z","shell.execute_reply.started":"2023-06-06T13:47:24.856806Z","shell.execute_reply":"2023-06-06T13:47:25.044352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Removing Stop Words","metadata":{"id":"Yn1Za7PKgl0I"}},{"cell_type":"code","source":"from nltk.corpus import stopwords","metadata":{"id":"BUGk7WXaglAO","execution":{"iopub.status.busy":"2023-06-06T13:47:25.046902Z","iopub.execute_input":"2023-06-06T13:47:25.047636Z","iopub.status.idle":"2023-06-06T13:47:25.051966Z","shell.execute_reply.started":"2023-06-06T13:47:25.047604Z","shell.execute_reply":"2023-06-06T13:47:25.050927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nltk.download('stopwords')","metadata":{"id":"9RgWoh__gieM","outputId":"7c5270e0-94f9-4076-8b35-e8eb75e7248c","execution":{"iopub.status.busy":"2023-06-06T13:47:25.053156Z","iopub.execute_input":"2023-06-06T13:47:25.053518Z","iopub.status.idle":"2023-06-06T13:47:25.071027Z","shell.execute_reply.started":"2023-06-06T13:47:25.053490Z","shell.execute_reply":"2023-06-06T13:47:25.069742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"english_stopwords = stopwords.words('english')","metadata":{"id":"DLCU8ffNfSHy","execution":{"iopub.status.busy":"2023-06-06T13:47:25.072361Z","iopub.execute_input":"2023-06-06T13:47:25.072781Z","iopub.status.idle":"2023-06-06T13:47:25.081054Z","shell.execute_reply.started":"2023-06-06T13:47:25.072745Z","shell.execute_reply":"2023-06-06T13:47:25.079801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\", \".join(english_stopwords)","metadata":{"id":"I56wLgjXgxiQ","outputId":"b8ebe13c-dc47-4281-fb64-f03cad228143","execution":{"iopub.status.busy":"2023-06-06T13:47:25.082670Z","iopub.execute_input":"2023-06-06T13:47:25.083129Z","iopub.status.idle":"2023-06-06T13:47:25.092858Z","shell.execute_reply.started":"2023-06-06T13:47:25.083086Z","shell.execute_reply":"2023-06-06T13:47:25.091884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Stemming","metadata":{"id":"QDtxCk4dg9Y5"}},{"cell_type":"code","source":"from nltk.stem.snowball import SnowballStemmer","metadata":{"id":"z-lURHZ_hC08","execution":{"iopub.status.busy":"2023-06-06T13:47:25.094425Z","iopub.execute_input":"2023-06-06T13:47:25.094890Z","iopub.status.idle":"2023-06-06T13:47:25.104378Z","shell.execute_reply.started":"2023-06-06T13:47:25.094848Z","shell.execute_reply":"2023-06-06T13:47:25.103423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stemmer = SnowballStemmer(language='english')","metadata":{"id":"hhuRoxu5YXzG","execution":{"iopub.status.busy":"2023-06-06T13:47:25.105828Z","iopub.execute_input":"2023-06-06T13:47:25.106153Z","iopub.status.idle":"2023-06-06T13:47:25.117177Z","shell.execute_reply.started":"2023-06-06T13:47:25.106120Z","shell.execute_reply":"2023-06-06T13:47:25.116144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tokenize(text):\n  return [stemmer.stem(word) for word in word_tokenize(text) if word.lower() not in english_stopwords]","metadata":{"id":"359DyslSX19w","execution":{"iopub.status.busy":"2023-06-06T13:47:25.118684Z","iopub.execute_input":"2023-06-06T13:47:25.119068Z","iopub.status.idle":"2023-06-06T13:47:25.129035Z","shell.execute_reply.started":"2023-06-06T13:47:25.119036Z","shell.execute_reply":"2023-06-06T13:47:25.128209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Count Vectorizer","metadata":{"id":"cZwK8sGjh1bD"}},{"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer","metadata":{"id":"8Y_k1Z3dhyKZ","execution":{"iopub.status.busy":"2023-06-06T13:47:25.130652Z","iopub.execute_input":"2023-06-06T13:47:25.131139Z","iopub.status.idle":"2023-06-06T13:47:25.139917Z","shell.execute_reply.started":"2023-06-06T13:47:25.131054Z","shell.execute_reply":"2023-06-06T13:47:25.138773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer = CountVectorizer(lowercase=True, tokenizer=tokenize, stop_words=english_stopwords, max_features=1000)","metadata":{"id":"HJOUKySJXumw","execution":{"iopub.status.busy":"2023-06-06T13:47:25.144793Z","iopub.execute_input":"2023-06-06T13:47:25.145168Z","iopub.status.idle":"2023-06-06T13:47:25.153132Z","shell.execute_reply.started":"2023-06-06T13:47:25.145138Z","shell.execute_reply":"2023-06-06T13:47:25.151781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nvectorizer.fit(df['question_text'])","metadata":{"id":"bL5DV-YEY7qZ","outputId":"daf0f98f-d4a8-4078-c74c-fabe274d9faf","execution":{"iopub.status.busy":"2023-06-06T13:47:25.154526Z","iopub.execute_input":"2023-06-06T13:47:25.155232Z","iopub.status.idle":"2023-06-06T13:54:00.128627Z","shell.execute_reply.started":"2023-06-06T13:47:25.155186Z","shell.execute_reply":"2023-06-06T13:54:00.127320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer.get_feature_names_out()[:100]","metadata":{"id":"ntsIKumbZEyA","outputId":"3c99908c-0acf-4706-d57a-c8bb46b26fba","execution":{"iopub.status.busy":"2023-06-06T13:54:00.129978Z","iopub.execute_input":"2023-06-06T13:54:00.130341Z","iopub.status.idle":"2023-06-06T13:54:00.141589Z","shell.execute_reply.started":"2023-06-06T13:54:00.130313Z","shell.execute_reply":"2023-06-06T13:54:00.139778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ninputs = vectorizer.transform(df['question_text'])","metadata":{"id":"hS18xeV2Zorm","outputId":"e2268434-479d-4c44-fa26-9147458df702","execution":{"iopub.status.busy":"2023-06-06T13:54:00.143497Z","iopub.execute_input":"2023-06-06T13:54:00.144037Z","iopub.status.idle":"2023-06-06T14:00:37.325589Z","shell.execute_reply.started":"2023-06-06T13:54:00.143992Z","shell.execute_reply":"2023-06-06T14:00:37.323980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest_inputs = vectorizer.transform(test_df['question_text'])","metadata":{"id":"VLHatwhcmOe2","outputId":"6f19c9ec-cad1-42ab-c945-cf27dae5dc4f","execution":{"iopub.status.busy":"2023-06-06T14:00:37.326926Z","iopub.execute_input":"2023-06-06T14:00:37.327260Z","iopub.status.idle":"2023-06-06T14:02:32.987051Z","shell.execute_reply.started":"2023-06-06T14:00:37.327232Z","shell.execute_reply":"2023-06-06T14:02:32.985408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Train Input Shape: {inputs.shape}\")\nprint(f\"Test Input Shape: {test_inputs.shape}\")","metadata":{"id":"VErbwA_4Z039","outputId":"30a2b973-f4c1-4bcc-cbc9-1e3f4130af14","execution":{"iopub.status.busy":"2023-06-06T14:02:32.989784Z","iopub.execute_input":"2023-06-06T14:02:32.990388Z","iopub.status.idle":"2023-06-06T14:02:32.997211Z","shell.execute_reply.started":"2023-06-06T14:02:32.990347Z","shell.execute_reply":"2023-06-06T14:02:32.995636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Machine Learning for Text Classification","metadata":{"id":"ZF8FYm3PakUm"}},{"cell_type":"markdown","source":"### Create training and validation set","metadata":{"id":"50B4_QeEbB7z"}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"id":"tyopyXjhbJ6u","execution":{"iopub.status.busy":"2023-06-06T14:02:32.998627Z","iopub.execute_input":"2023-06-06T14:02:32.999040Z","iopub.status.idle":"2023-06-06T14:02:33.012695Z","shell.execute_reply.started":"2023-06-06T14:02:32.999006Z","shell.execute_reply":"2023-06-06T14:02:33.011201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_inputs, val_inputs, train_targets, val_targets = train_test_split(inputs, df['target'], test_size=0.3, random_state=42)","metadata":{"id":"9U2RsGhabbKH","execution":{"iopub.status.busy":"2023-06-06T14:02:33.014261Z","iopub.execute_input":"2023-06-06T14:02:33.014989Z","iopub.status.idle":"2023-06-06T14:02:33.380439Z","shell.execute_reply.started":"2023-06-06T14:02:33.014938Z","shell.execute_reply":"2023-06-06T14:02:33.379205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Train Input Shape: {train_inputs.shape}\")\nprint(f\"Validation Input Shape: {val_inputs.shape}\")\nprint(f\"Train Target Shape: {train_targets.shape}\")\nprint(f\"Validation Target Shape: {val_targets.shape}\")","metadata":{"id":"HeSp0eW-bvgy","outputId":"d6601076-5998-4096-d7ae-9a1f96fb0077","execution":{"iopub.status.busy":"2023-06-06T14:02:33.381919Z","iopub.execute_input":"2023-06-06T14:02:33.382342Z","iopub.status.idle":"2023-06-06T14:02:33.390163Z","shell.execute_reply.started":"2023-06-06T14:02:33.382304Z","shell.execute_reply":"2023-06-06T14:02:33.388049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Logistic Regression Model","metadata":{"id":"MrJ72QdAcC-h"}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"id":"pb5e-3YpcGgM","execution":{"iopub.status.busy":"2023-06-06T14:02:33.391967Z","iopub.execute_input":"2023-06-06T14:02:33.392417Z","iopub.status.idle":"2023-06-06T14:02:33.404903Z","shell.execute_reply.started":"2023-06-06T14:02:33.392381Z","shell.execute_reply":"2023-06-06T14:02:33.403726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LogisticRegression(max_iter=1000, solver='sag')","metadata":{"id":"dG5FSuoycNzs","execution":{"iopub.status.busy":"2023-06-06T14:02:33.406637Z","iopub.execute_input":"2023-06-06T14:02:33.407319Z","iopub.status.idle":"2023-06-06T14:02:33.420044Z","shell.execute_reply.started":"2023-06-06T14:02:33.407281Z","shell.execute_reply":"2023-06-06T14:02:33.418875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_inputs, train_targets)","metadata":{"id":"WLfasmnCcQL6","outputId":"537167fa-ff81-4d3a-9a35-50b4301fd3e8","execution":{"iopub.status.busy":"2023-06-06T14:02:33.421814Z","iopub.execute_input":"2023-06-06T14:02:33.422605Z","iopub.status.idle":"2023-06-06T14:09:29.218044Z","shell.execute_reply.started":"2023-06-06T14:02:33.422562Z","shell.execute_reply":"2023-06-06T14:09:29.216849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_preds = model.predict(train_inputs)","metadata":{"id":"GUAV_2_dcwsM","execution":{"iopub.status.busy":"2023-06-06T14:09:29.219431Z","iopub.execute_input":"2023-06-06T14:09:29.220514Z","iopub.status.idle":"2023-06-06T14:09:29.258833Z","shell.execute_reply.started":"2023-06-06T14:09:29.220466Z","shell.execute_reply":"2023-06-06T14:09:29.257689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_preds= model.predict(val_inputs)","metadata":{"id":"ztJSNcedpi2k","execution":{"iopub.status.busy":"2023-06-06T14:09:29.260338Z","iopub.execute_input":"2023-06-06T14:09:29.260799Z","iopub.status.idle":"2023-06-06T14:09:29.297925Z","shell.execute_reply.started":"2023-06-06T14:09:29.260757Z","shell.execute_reply":"2023-06-06T14:09:29.296797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Accuracy and F1 Score","metadata":{"id":"xax2gDwpnw_l"}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.metrics import f1_score","metadata":{"id":"fK6rxAiEdvkK","execution":{"iopub.status.busy":"2023-06-06T14:09:29.299621Z","iopub.execute_input":"2023-06-06T14:09:29.300505Z","iopub.status.idle":"2023-06-06T14:09:29.305349Z","shell.execute_reply.started":"2023-06-06T14:09:29.300460Z","shell.execute_reply":"2023-06-06T14:09:29.304219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training Accuracy {accuracy_score(train_targets, train_preds)}\")\nprint(f\"Training F1 Score {f1_score(train_targets, train_preds)}\")","metadata":{"id":"Gmnfr8EbeFm9","outputId":"b4c68f8f-2a7a-4185-c1ff-2a555565341c","execution":{"iopub.status.busy":"2023-06-06T14:09:29.306750Z","iopub.execute_input":"2023-06-06T14:09:29.307169Z","iopub.status.idle":"2023-06-06T14:09:29.693799Z","shell.execute_reply.started":"2023-06-06T14:09:29.307130Z","shell.execute_reply":"2023-06-06T14:09:29.692873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Validation Accuracy {accuracy_score(val_targets, val_preds)}\")\nprint(f\"Validation F1 Score {f1_score(val_targets, val_preds)}\")","metadata":{"id":"Ofhx4I6LeKEi","outputId":"6fa77118-bdde-4aa5-8bf7-6cb459fd8134","execution":{"iopub.status.busy":"2023-06-06T14:09:29.695236Z","iopub.execute_input":"2023-06-06T14:09:29.695846Z","iopub.status.idle":"2023-06-06T14:09:29.861096Z","shell.execute_reply.started":"2023-06-06T14:09:29.695813Z","shell.execute_reply":"2023-06-06T14:09:29.859923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Making Predictions for Kaggle","metadata":{"id":"ixUmFNPMgibi"}},{"cell_type":"code","source":"test_df","metadata":{"id":"3QqmtFUVhkYU","outputId":"cd4095f8-a6ca-447f-d925-7487fbe60f25","execution":{"iopub.status.busy":"2023-06-06T14:09:29.862413Z","iopub.execute_input":"2023-06-06T14:09:29.862765Z","iopub.status.idle":"2023-06-06T14:09:29.878324Z","shell.execute_reply.started":"2023-06-06T14:09:29.862732Z","shell.execute_reply":"2023-06-06T14:09:29.877005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = model.predict(test_inputs)","metadata":{"id":"VXDJQ3xfhmCI","execution":{"iopub.status.busy":"2023-06-06T14:09:29.880523Z","iopub.execute_input":"2023-06-06T14:09:29.881342Z","iopub.status.idle":"2023-06-06T14:09:29.905848Z","shell.execute_reply.started":"2023-06-06T14:09:29.881297Z","shell.execute_reply":"2023-06-06T14:09:29.904734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"id":"0ivRZXPIht-q","outputId":"c646e690-e2c6-4def-c86d-a2a298d50730","execution":{"iopub.status.busy":"2023-06-06T14:09:29.907587Z","iopub.execute_input":"2023-06-06T14:09:29.908036Z","iopub.status.idle":"2023-06-06T14:09:29.921665Z","shell.execute_reply.started":"2023-06-06T14:09:29.907994Z","shell.execute_reply":"2023-06-06T14:09:29.920370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['prediction'] = test_preds","metadata":{"id":"KUMccao5h_nm","execution":{"iopub.status.busy":"2023-06-06T14:09:29.923110Z","iopub.execute_input":"2023-06-06T14:09:29.923542Z","iopub.status.idle":"2023-06-06T14:09:29.930029Z","shell.execute_reply.started":"2023-06-06T14:09:29.923501Z","shell.execute_reply":"2023-06-06T14:09:29.929113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['prediction'].value_counts()","metadata":{"id":"Y8ChXR6ZiRzO","outputId":"5e648b01-351c-4ade-f8a0-1dcaf3e09525","execution":{"iopub.status.busy":"2023-06-06T14:09:29.931598Z","iopub.execute_input":"2023-06-06T14:09:29.932062Z","iopub.status.idle":"2023-06-06T14:09:29.952648Z","shell.execute_reply.started":"2023-06-06T14:09:29.932027Z","shell.execute_reply":"2023-06-06T14:09:29.951474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=None)","metadata":{"id":"7lmq3aC4iW01","execution":{"iopub.status.busy":"2023-06-06T14:09:29.954428Z","iopub.execute_input":"2023-06-06T14:09:29.955220Z","iopub.status.idle":"2023-06-06T14:09:30.540061Z","shell.execute_reply.started":"2023-06-06T14:09:29.955016Z","shell.execute_reply":"2023-06-06T14:09:30.538925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The End","metadata":{"id":"NkaisVAxig89"}}]}