{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":113366,"databundleVersionId":13605326,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-09-20T17:42:57.540476Z","iopub.execute_input":"2025-09-20T17:42:57.540668Z","iopub.status.idle":"2025-09-20T17:42:57.809050Z","shell.execute_reply.started":"2025-09-20T17:42:57.540651Z","shell.execute_reply":"2025-09-20T17:42:57.808347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport re\nimport nltk\nfrom nltk.corpus import stopwords\nfrom nltk.stem import WordNetLemmatizer\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import classification_report, accuracy_score\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-12T05:53:43.984772Z","iopub.execute_input":"2025-09-12T05:53:43.985385Z","iopub.status.idle":"2025-09-12T05:53:45.730392Z","shell.execute_reply.started":"2025-09-12T05:53:43.985363Z","shell.execute_reply":"2025-09-12T05:53:45.729696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# ---------------- Load data ----------------\ntrain_df = pd.read_csv(\"/kaggle/input/comments-classification/Dataset/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/comments-classification/Dataset/test.csv\")\n\ntrain_df = train_df.rename(columns={\"comment_text\": \"text\", \"psychotic_depression\": \"label\"})\ntest_df = test_df.rename(columns={\"comment_text\": \"text\"})\n\n# ---------------- Preprocessing ----------------\nnltk.download(\"stopwords\")\nnltk.download(\"wordnet\")\n\nstop_words = set(stopwords.words(\"english\"))\nlemmatizer = WordNetLemmatizer()\n\ndef clean_text(text):\n    text = str(text).lower()\n    text = re.sub(r\"[^a-z\\s]\", \"\", text)\n    tokens = text.split()\n    tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]\n    return \" \".join(tokens)\n\ntrain_df[\"cleaned_text\"] = train_df[\"text\"].apply(clean_text)\ntest_df[\"cleaned_text\"] = test_df[\"text\"].apply(clean_text)\n\n# ---------------- TF-IDF with bigrams ----------------\nvectorizer = TfidfVectorizer(max_features=20000, ngram_range=(1,2))\nX = vectorizer.fit_transform(train_df[\"cleaned_text\"])\ny = train_df[\"label\"]\n\nX_test_final = vectorizer.transform(test_df[\"cleaned_text\"])\n\n# ---------------- Train/Validation split ----------------\nX_train, X_val, y_train, y_val = train_test_split(\n    X, y, test_size=0.2, random_state=42, stratify=y\n)\n\n# ---------------- Improved Logistic Regression ----------------\nmodel = LogisticRegression(max_iter=300, class_weight=\"balanced\")  \nmodel.fit(X_train, y_train)\n\n# ---------------- Validation ----------------\ny_val_pred = model.predict(X_val)\nprint(\"Validation Accuracy:\", accuracy_score(y_val, y_val_pred))\nprint(classification_report(y_val, y_val_pred))\n\n# ---------------- Predict on test.csv ----------------\ntest_predictions = model.predict(X_test_final)\n\n# ---------------- Save submission ----------------\nsubmission = pd.DataFrame({\n    \"ID\": test_df[\"id\"],\n    \"psychotic_depression\": test_predictions\n})\n\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(submission.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-12T06:05:35.313676Z","iopub.execute_input":"2025-09-12T06:05:35.313933Z","iopub.status.idle":"2025-09-12T06:06:42.620389Z","shell.execute_reply.started":"2025-09-12T06:05:35.313914Z","shell.execute_reply":"2025-09-12T06:06:42.619743Z"}},"outputs":[],"execution_count":null}]}