{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10737,"databundleVersionId":290346,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-01-04T10:37:42.986064Z","iopub.execute_input":"2024-01-04T10:37:42.986483Z","iopub.status.idle":"2024-01-04T10:37:43.516259Z","shell.execute_reply.started":"2024-01-04T10:37:42.986451Z","shell.execute_reply":"2024-01-04T10:37:43.515022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport nltk\nimport numpy as np\nimport pandas as pd\nfrom nltk.corpus import stopwords\nfrom sklearn.metrics import f1_score\nfrom sklearn.model_selection import KFold\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer\nfrom sklearn.naive_bayes import GaussianNB, MultinomialNB, ComplementNB, BernoulliNB","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:42:23.984254Z","iopub.execute_input":"2024-01-04T10:42:23.984709Z","iopub.status.idle":"2024-01-04T10:42:24.168370Z","shell.execute_reply.started":"2024-01-04T10:42:23.984675Z","shell.execute_reply":"2024-01-04T10:42:24.166993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_all(df):\n    with pd.option_context(\"display.max_rows\", None, \n                           \"display.max_columns\", None):\n        display(df)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:43:47.173665Z","iopub.execute_input":"2024-01-04T10:43:47.174186Z","iopub.status.idle":"2024-01-04T10:43:47.180925Z","shell.execute_reply.started":"2024-01-04T10:43:47.174148Z","shell.execute_reply":"2024-01-04T10:43:47.179634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')\ntest = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:44:37.818174Z","iopub.execute_input":"2024-01-04T10:44:37.818605Z","iopub.status.idle":"2024-01-04T10:44:44.786153Z","shell.execute_reply.started":"2024-01-04T10:44:37.818573Z","shell.execute_reply":"2024-01-04T10:44:44.784601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_all(train.head())","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:44:55.478911Z","iopub.execute_input":"2024-01-04T10:44:55.479878Z","iopub.status.idle":"2024-01-04T10:44:55.501664Z","shell.execute_reply.started":"2024-01-04T10:44:55.479824Z","shell.execute_reply":"2024-01-04T10:44:55.500015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:45:10.827749Z","iopub.execute_input":"2024-01-04T10:45:10.828200Z","iopub.status.idle":"2024-01-04T10:45:10.859219Z","shell.execute_reply.started":"2024-01-04T10:45:10.828165Z","shell.execute_reply":"2024-01-04T10:45:10.857638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_text = train['question_text']\ntest_text = test['question_text']\ntrain_target = train['target']\nall_text = pd.concat([train_text, test_text])","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:48:29.786013Z","iopub.execute_input":"2024-01-04T10:48:29.786506Z","iopub.status.idle":"2024-01-04T10:48:29.840665Z","shell.execute_reply.started":"2024-01-04T10:48:29.786467Z","shell.execute_reply":"2024-01-04T10:48:29.838920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Vectorizing\ntfidf_vectorizer = TfidfVectorizer()\ntfidf_vectorizer.fit(train_text)\n\ncount_vectorizer = CountVectorizer()\ncount_vectorizer.fit(train_text)\n\ntrain_text_features_cv = count_vectorizer.transform(\n    train_text)\ntest_text_features_cv = count_vectorizer.transform(\ntest_text)\n\ntrain_text_features_tf = tfidf_vectorizer.transform(\ntrain_text)\ntest_text_features_tf = tfidf_vectorizer.transform(\ntest_text)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:52:27.350424Z","iopub.execute_input":"2024-01-04T10:52:27.351685Z","iopub.status.idle":"2024-01-04T10:54:53.364345Z","shell.execute_reply.started":"2024-01-04T10:52:27.351641Z","shell.execute_reply":"2024-01-04T10:54:53.362644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_text.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:55:25.765451Z","iopub.execute_input":"2024-01-04T10:55:25.765924Z","iopub.status.idle":"2024-01-04T10:55:25.776074Z","shell.execute_reply.started":"2024-01-04T10:55:25.765887Z","shell.execute_reply":"2024-01-04T10:55:25.774726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kfold = KFold(n_splits = 5, shuffle = True, random_state=42)\ntest_preds = 0\noof_preds = np.zeros([train.shape[0],])\n\nfor i, (train_idx, valid_idx) in enumerate(kfold.split(train)):\n    x_train, x_valid = train_text_features_tf[train_idx, :], train_text_features_tf[valid_idx,:]\n    y_train, y_valid = train_target[train_idx], train_target[valid_idx]\n    \n    classifier = LogisticRegression()\n    print('fitting.............')\n    classifier.fit(x_train, y_train)\n    print('predicting..............')\n    print('\\n')\n    oof_preds[valid_idx] = classifier.predict_proba(x_valid)[:,1]\n    test_preds += 0.2*classifier.predict_proba(test_text_features_tf)[:,1]","metadata":{"execution":{"iopub.status.busy":"2024-01-04T11:05:05.861108Z","iopub.execute_input":"2024-01-04T11:05:05.861654Z","iopub.status.idle":"2024-01-04T11:08:06.374028Z","shell.execute_reply.started":"2024-01-04T11:05:05.861617Z","shell.execute_reply":"2024-01-04T11:08:06.372167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_train = (oof_preds > .25).astype(int)\nf1_score(train_target, pred_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T11:09:05.342586Z","iopub.execute_input":"2024-01-04T11:09:05.343091Z","iopub.status.idle":"2024-01-04T11:09:05.942795Z","shell.execute_reply.started":"2024-01-04T11:09:05.343050Z","shell.execute_reply":"2024-01-04T11:09:05.941256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1 = pd.DataFrame.from_dict({'qid': test['qid']})\nsubmission1['prediction'] = (test_preds > 0.25).astype(int)\nsubmission1.to_csv('submission.csv', index=False)\nsubmission1['prediction'] = (test_preds > 0.25)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T11:09:14.501840Z","iopub.execute_input":"2024-01-04T11:09:14.502293Z","iopub.status.idle":"2024-01-04T11:09:15.639356Z","shell.execute_reply.started":"2024-01-04T11:09:14.502261Z","shell.execute_reply":"2024-01-04T11:09:15.637999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}