File size: 6,501 Bytes
74c6a41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "884cc4c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Input</th>\n",
       "      <th>Tags</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Title: What is the effective differencial effe...</td>\n",
       "      <td>['electronics']</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Title: Heat sensor with fan cooling Body: Can ...</td>\n",
       "      <td>['electronics']</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Title: Outlet Installation--more wires than my...</td>\n",
       "      <td>['electronics']</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Title: Buck Converter Operation Question Body:...</td>\n",
       "      <td>['electronics']</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Title: Urgent help in area of ASIC design, ver...</td>\n",
       "      <td>['electronics']</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "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
}