{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "884cc4c7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
InputTags
0Title: What is the effective differencial effe...['electronics']
1Title: Heat sensor with fan cooling Body: Can ...['electronics']
2Title: Outlet Installation--more wires than my...['electronics']
3Title: Buck Converter Operation Question Body:...['electronics']
4Title: Urgent help in area of ASIC design, ver...['electronics']
\n", "
" ], "text/plain": [ " Input Tags\n", "0 Title: What is the effective differencial effe... ['electronics']\n", "1 Title: Heat sensor with fan cooling Body: Can ... ['electronics']\n", "2 Title: Outlet Installation--more wires than my... ['electronics']\n", "3 Title: Buck Converter Operation Question Body:... ['electronics']\n", "4 Title: Urgent help in area of ASIC design, ver... ['electronics']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "dataset = pd.read_csv(\"/home/darth/#/SEQuestionClassifier/data/hackerank/combined_data.csv\")\n", "dataset.head()" ] }, { "cell_type": "code", "execution_count": 2, "id": "d9597ebd", "metadata": {}, "outputs": [], "source": [ "df = dataset" ] }, { "cell_type": "code", "execution_count": 5, "id": "6626fbc7", "metadata": {}, "outputs": [], "source": [ "import ast\n", "\n", "def clean_tags(tag_string):\n", " # Convert the string to a list\n", " tag_list = ast.literal_eval(tag_string)\n", " # Join the list into a comma-separated string\n", " return ', '.join(tag_list)\n", "\n", "df['Tags'] = df['Tags'].apply(clean_tags)" ] }, { "cell_type": "markdown", "id": "5c32d48e", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "52801f74", "metadata": {}, "outputs": [], "source": [ "from sklearn.feature_extraction.text import TfidfVectorizer\n", "from sklearn.preprocessing import LabelEncoder\n", "\n", "def vectorirse_text(text):\n", " \"\"\" Recieves text as input and returns TF-IDF vectors\"\"\"\n", " tfidf = TfidfVectorizer(max_features=500000)\n", " X = tfidf.fit_transform(text)\n", " return X\n", "\n", "def label_encoding(input):\n", " label_encoder = LabelEncoder()\n", " return label_encoder.fit_transform(input)\n", "\n", "\n", "X = vectorirse_text(df['Input'])\n", "y = label_encoding(df['Tags'])\n", "\n", "# Import necessary libraries\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1380ee74", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n", "\n", "# Assuming df is already loaded\n", "# And the following functions are available from your preprocessing notebook:\n", "# - vectorise_text\n", "# - label_encoding\n", "\n", "# Step 1: Preproces\n", "\n", "# Step 2: Train Logistic Regression\n", "model = LogisticRegression(max_iter=1000)\n", "model.fit(X, y)\n", "\n", "# Step 3: Predict on the entire dataset\n", "y_preds = model.predict(X)\n", "\n", "# Step 4: Evaluate\n", "acc = accuracy_score(y, y_preds)\n", "print(f\"āœ… Accuracy: {acc:.4f}\\n\")\n", "\n", "print(\"āœ… Classification Report:\\n\")\n", "print(classification_report(y, y_preds))\n", "\n", "print(\"āœ… Confusion Matrix:\\n\")\n", "print(confusion_matrix(y, y_preds))\n", "\n", "# Step 5: Save results to a CSV\n", "output_df = pd.DataFrame({\n", " 'y_true': y,\n", " 'y_pred': y_preds\n", "})\n", "\n", "output_df.to_csv('part-1.csv', index=False)\n", "\n", "print(\"\\nāœ… part-1.csv saved successfully!\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "79783f66", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(2)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "80fb05a1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "major02", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }