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source": [
|
| 8 |
+
"# Importing Libraries"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 4,
|
| 14 |
+
"id": "17a99550",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"import pandas as pd\n",
|
| 19 |
+
"import numpy as np\n",
|
| 20 |
+
"import emoji\n",
|
| 21 |
+
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
|
| 22 |
+
"from sklearn.preprocessing import FunctionTransformer\n",
|
| 23 |
+
"from sklearn.pipeline import Pipeline\n",
|
| 24 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 25 |
+
"from sklearn.svm import SVC\n",
|
| 26 |
+
"from sklearn.ensemble import RandomForestClassifier\n",
|
| 27 |
+
"from sklearn.tree import DecisionTreeClassifier\n",
|
| 28 |
+
"from sklearn.naive_bayes import MultinomialNB\n",
|
| 29 |
+
"from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix\n",
|
| 30 |
+
"from sklearn.feature_selection import SelectKBest, mutual_info_classif\n",
|
| 31 |
+
"from tensorflow import keras\n",
|
| 32 |
+
"from tensorflow.keras import layers\n",
|
| 33 |
+
"from sklearn.model_selection import GridSearchCV\n",
|
| 34 |
+
"from sklearn.metrics import classification_report\n",
|
| 35 |
+
"import joblib\n",
|
| 36 |
+
"from sklearn.metrics import accuracy_score"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"cell_type": "code",
|
| 41 |
+
"execution_count": 5,
|
| 42 |
+
"id": "28a813d3",
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [
|
| 45 |
+
{
|
| 46 |
+
"name": "stdout",
|
| 47 |
+
"output_type": "stream",
|
| 48 |
+
"text": [
|
| 49 |
+
" label text\n",
|
| 50 |
+
"0 neutral Original product and delivery was on time. Pac...\n",
|
| 51 |
+
"1 positive Full original quality is owsome with warranty ...\n",
|
| 52 |
+
"2 neutral Looks beautiful and real\n",
|
| 53 |
+
"3 neutral Watch is really nice, it’s original as per my ...\n",
|
| 54 |
+
"4 negative I have returned this package due very low qual...\n",
|
| 55 |
+
"... ... ...\n",
|
| 56 |
+
"8802 positive Quality was up to the mark, perfect size and t...\n",
|
| 57 |
+
"8803 positive Got my order on time.Everything is good.same a...\n",
|
| 58 |
+
"8804 positive Beautiful and quality product. I was expecting...\n",
|
| 59 |
+
"8805 positive found it as advertised , timely delivered with...\n",
|
| 60 |
+
"8806 positive good quilty nice product. highly recommend. my...\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"[8807 rows x 2 columns]\n"
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
],
|
| 66 |
+
"source": [
|
| 67 |
+
"data = pd.read_csv('daraz-reviews.csv', sep=',', skiprows=1, header=None, names=['label', 'text'])\n",
|
| 68 |
+
"print(data)"
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"cell_type": "code",
|
| 73 |
+
"execution_count": 6,
|
| 74 |
+
"id": "ec6940fb",
|
| 75 |
+
"metadata": {},
|
| 76 |
+
"outputs": [
|
| 77 |
+
{
|
| 78 |
+
"name": "stdout",
|
| 79 |
+
"output_type": "stream",
|
| 80 |
+
"text": [
|
| 81 |
+
"<class 'pandas.core.frame.DataFrame'>\n",
|
| 82 |
+
"RangeIndex: 8807 entries, 0 to 8806\n",
|
| 83 |
+
"Data columns (total 2 columns):\n",
|
| 84 |
+
" # Column Non-Null Count Dtype \n",
|
| 85 |
+
"--- ------ -------------- ----- \n",
|
| 86 |
+
" 0 label 8807 non-null object\n",
|
| 87 |
+
" 1 text 8807 non-null object\n",
|
| 88 |
+
"dtypes: object(2)\n",
|
| 89 |
+
"memory usage: 137.7+ KB\n"
|
| 90 |
+
]
|
| 91 |
+
}
|
| 92 |
+
],
|
| 93 |
+
"source": [
|
| 94 |
+
"data.info()"
|
| 95 |
+
]
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"cell_type": "code",
|
| 99 |
+
"execution_count": 7,
|
| 100 |
+
"id": "91a67eac",
|
| 101 |
+
"metadata": {},
|
| 102 |
+
"outputs": [],
|
| 103 |
+
"source": [
|
| 104 |
+
"# data = pd.read_excel(\"version2.xlsx\", skiprows=1, header=None, names=['label', 'text'])\n",
|
| 105 |
+
"# data"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"cell_type": "code",
|
| 110 |
+
"execution_count": 8,
|
| 111 |
+
"id": "49f2adf7",
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"outputs": [],
|
| 114 |
+
"source": [
|
| 115 |
+
"# data2.Sentiment.value_counts()"
|
| 116 |
+
]
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
"cell_type": "markdown",
|
| 120 |
+
"id": "44750173",
|
| 121 |
+
"metadata": {},
|
| 122 |
+
"source": [
|
| 123 |
+
"# Preprocessing"
|
| 124 |
+
]
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"cell_type": "code",
|
| 128 |
+
"execution_count": 9,
|
| 129 |
+
"id": "bcc9ff83",
|
| 130 |
+
"metadata": {},
|
| 131 |
+
"outputs": [],
|
| 132 |
+
"source": [
|
| 133 |
+
"data.label = data.label.str.lower()\n",
|
| 134 |
+
"data = data[data['label'].isin(['positive', 'negative', 'neutral'])]\n"
|
| 135 |
+
]
|
| 136 |
+
},
|
| 137 |
+
{
|
| 138 |
+
"cell_type": "code",
|
| 139 |
+
"execution_count": 10,
|
| 140 |
+
"id": "5041cc7e",
|
| 141 |
+
"metadata": {},
|
| 142 |
+
"outputs": [
|
| 143 |
+
{
|
| 144 |
+
"name": "stdout",
|
| 145 |
+
"output_type": "stream",
|
| 146 |
+
"text": [
|
| 147 |
+
"0 neutral\n",
|
| 148 |
+
"1 positive\n",
|
| 149 |
+
"2 neutral\n",
|
| 150 |
+
"3 neutral\n",
|
| 151 |
+
"4 negative\n",
|
| 152 |
+
" ... \n",
|
| 153 |
+
"8802 positive\n",
|
| 154 |
+
"8803 positive\n",
|
| 155 |
+
"8804 positive\n",
|
| 156 |
+
"8805 positive\n",
|
| 157 |
+
"8806 positive\n",
|
| 158 |
+
"Name: label, Length: 8614, dtype: object\n",
|
| 159 |
+
"\n",
|
| 160 |
+
"0 Original product and delivery was on time. Pac...\n",
|
| 161 |
+
"1 Full original quality is owsome with warranty ...\n",
|
| 162 |
+
"2 Looks beautiful and real\n",
|
| 163 |
+
"3 Watch is really nice, it’s original as per my ...\n",
|
| 164 |
+
"4 I have returned this package due very low qual...\n",
|
| 165 |
+
" ... \n",
|
| 166 |
+
"8802 Quality was up to the mark, perfect size and t...\n",
|
| 167 |
+
"8803 Got my order on time.Everything is good.same a...\n",
|
| 168 |
+
"8804 Beautiful and quality product. I was expecting...\n",
|
| 169 |
+
"8805 found it as advertised , timely delivered with...\n",
|
| 170 |
+
"8806 good quilty nice product. highly recommend. my...\n",
|
| 171 |
+
"Name: text, Length: 8614, dtype: object\n"
|
| 172 |
+
]
|
| 173 |
+
}
|
| 174 |
+
],
|
| 175 |
+
"source": [
|
| 176 |
+
"print(data.label)\n",
|
| 177 |
+
"print()\n",
|
| 178 |
+
"print(data.text)"
|
| 179 |
+
]
|
| 180 |
+
},
|
| 181 |
+
{
|
| 182 |
+
"cell_type": "code",
|
| 183 |
+
"execution_count": 11,
|
| 184 |
+
"id": "1416992f",
|
| 185 |
+
"metadata": {},
|
| 186 |
+
"outputs": [],
|
| 187 |
+
"source": [
|
| 188 |
+
"def preprocess_text(text):\n",
|
| 189 |
+
" text = text.astype(str)\n",
|
| 190 |
+
" text = text.str.lower()\n",
|
| 191 |
+
" text = text.str.replace('[^\\w\\s]', '')\n",
|
| 192 |
+
" text = text.str.replace(r'\\b\\d+\\b', 'NUMBER')\n",
|
| 193 |
+
" text = text.str.replace(r'(http|https)://\\S+|www\\.\\S+', 'URL')\n",
|
| 194 |
+
" text = text.apply(lambda x: emoji.demojize(x))\n",
|
| 195 |
+
" return text\n",
|
| 196 |
+
"\n",
|
| 197 |
+
"def preprocess_label(label):\n",
|
| 198 |
+
" label = label.map({'positive': 'positive', 'negative': 'negative', 'neutral': 'neutral'})\n",
|
| 199 |
+
" return label\n"
|
| 200 |
+
]
|
| 201 |
+
},
|
| 202 |
+
{
|
| 203 |
+
"cell_type": "markdown",
|
| 204 |
+
"id": "c65d0fd2",
|
| 205 |
+
"metadata": {},
|
| 206 |
+
"source": [
|
| 207 |
+
"# Feature Extraction"
|
| 208 |
+
]
|
| 209 |
+
},
|
| 210 |
+
{
|
| 211 |
+
"cell_type": "code",
|
| 212 |
+
"execution_count": 12,
|
| 213 |
+
"id": "ef5c7285",
|
| 214 |
+
"metadata": {},
|
| 215 |
+
"outputs": [
|
| 216 |
+
{
|
| 217 |
+
"data": {
|
| 218 |
+
"text/plain": [
|
| 219 |
+
"['tfidf_vectorizer.pkl']"
|
| 220 |
+
]
|
| 221 |
+
},
|
| 222 |
+
"execution_count": 12,
|
| 223 |
+
"metadata": {},
|
| 224 |
+
"output_type": "execute_result"
|
| 225 |
+
}
|
| 226 |
+
],
|
| 227 |
+
"source": [
|
| 228 |
+
"pipeline = Pipeline([\n",
|
| 229 |
+
"# ('preprocess', FunctionTransformer(preprocess_text)),\n",
|
| 230 |
+
" ('vectorize', TfidfVectorizer())\n",
|
| 231 |
+
"])\n",
|
| 232 |
+
"joblib.dump(pipeline.named_steps['vectorize'], 'tfidf_vectorizer.pkl')"
|
| 233 |
+
]
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
"cell_type": "code",
|
| 237 |
+
"execution_count": 13,
|
| 238 |
+
"id": "d78a5365",
|
| 239 |
+
"metadata": {},
|
| 240 |
+
"outputs": [
|
| 241 |
+
{
|
| 242 |
+
"name": "stdout",
|
| 243 |
+
"output_type": "stream",
|
| 244 |
+
"text": [
|
| 245 |
+
"Feature vector size: 7126\n"
|
| 246 |
+
]
|
| 247 |
+
}
|
| 248 |
+
],
|
| 249 |
+
"source": [
|
| 250 |
+
"pipeline.fit(data['text'])\n",
|
| 251 |
+
"feature_vector_size = len(pipeline.named_steps['vectorize'].vocabulary_)\n",
|
| 252 |
+
"print(\"Feature vector size:\", feature_vector_size)\n"
|
| 253 |
+
]
|
| 254 |
+
},
|
| 255 |
+
{
|
| 256 |
+
"cell_type": "code",
|
| 257 |
+
"execution_count": 14,
|
| 258 |
+
"id": "74e76b0c",
|
| 259 |
+
"metadata": {},
|
| 260 |
+
"outputs": [
|
| 261 |
+
{
|
| 262 |
+
"data": {
|
| 263 |
+
"text/plain": [
|
| 264 |
+
"0 2\n",
|
| 265 |
+
"1 0\n",
|
| 266 |
+
"2 2\n",
|
| 267 |
+
"3 2\n",
|
| 268 |
+
"4 1\n",
|
| 269 |
+
" ..\n",
|
| 270 |
+
"8802 0\n",
|
| 271 |
+
"8803 0\n",
|
| 272 |
+
"8804 0\n",
|
| 273 |
+
"8805 0\n",
|
| 274 |
+
"8806 0\n",
|
| 275 |
+
"Name: label, Length: 8614, dtype: int64"
|
| 276 |
+
]
|
| 277 |
+
},
|
| 278 |
+
"execution_count": 14,
|
| 279 |
+
"metadata": {},
|
| 280 |
+
"output_type": "execute_result"
|
| 281 |
+
}
|
| 282 |
+
],
|
| 283 |
+
"source": [
|
| 284 |
+
"X = pipeline.fit_transform(data['text']) \n",
|
| 285 |
+
"y = preprocess_label(data.label).map({'positive': 0, 'negative': 1,'neutral': 2})\n",
|
| 286 |
+
"y\n"
|
| 287 |
+
]
|
| 288 |
+
},
|
| 289 |
+
{
|
| 290 |
+
"cell_type": "code",
|
| 291 |
+
"execution_count": 15,
|
| 292 |
+
"id": "de908a37",
|
| 293 |
+
"metadata": {},
|
| 294 |
+
"outputs": [
|
| 295 |
+
{
|
| 296 |
+
"data": {
|
| 297 |
+
"text/plain": [
|
| 298 |
+
"<8614x7126 sparse matrix of type '<class 'numpy.float64'>'\n",
|
| 299 |
+
"\twith 112734 stored elements in Compressed Sparse Row format>"
|
| 300 |
+
]
|
| 301 |
+
},
|
| 302 |
+
"execution_count": 15,
|
| 303 |
+
"metadata": {},
|
| 304 |
+
"output_type": "execute_result"
|
| 305 |
+
}
|
| 306 |
+
],
|
| 307 |
+
"source": [
|
| 308 |
+
"X"
|
| 309 |
+
]
|
| 310 |
+
},
|
| 311 |
+
{
|
| 312 |
+
"cell_type": "markdown",
|
| 313 |
+
"id": "d4c6dcdc",
|
| 314 |
+
"metadata": {},
|
| 315 |
+
"source": [
|
| 316 |
+
"# train_test_split"
|
| 317 |
+
]
|
| 318 |
+
},
|
| 319 |
+
{
|
| 320 |
+
"cell_type": "code",
|
| 321 |
+
"execution_count": 16,
|
| 322 |
+
"id": "9f881976",
|
| 323 |
+
"metadata": {},
|
| 324 |
+
"outputs": [],
|
| 325 |
+
"source": [
|
| 326 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
|
| 327 |
+
]
|
| 328 |
+
},
|
| 329 |
+
{
|
| 330 |
+
"cell_type": "markdown",
|
| 331 |
+
"id": "b015fe08",
|
| 332 |
+
"metadata": {},
|
| 333 |
+
"source": [
|
| 334 |
+
"# SVM Classifier"
|
| 335 |
+
]
|
| 336 |
+
},
|
| 337 |
+
{
|
| 338 |
+
"cell_type": "code",
|
| 339 |
+
"execution_count": 17,
|
| 340 |
+
"id": "9bb954b1",
|
| 341 |
+
"metadata": {},
|
| 342 |
+
"outputs": [
|
| 343 |
+
{
|
| 344 |
+
"name": "stdout",
|
| 345 |
+
"output_type": "stream",
|
| 346 |
+
"text": [
|
| 347 |
+
"SVM Classifier:\n",
|
| 348 |
+
"Recall: 0.8769587928032502\n",
|
| 349 |
+
"Precision: 0.8736508537438776\n",
|
| 350 |
+
"F1-Score: 0.8738901374932919\n",
|
| 351 |
+
"Accuracy: 0.8769587928032502\n",
|
| 352 |
+
"Confusion Matrix: \n",
|
| 353 |
+
"[[867 22 34]\n",
|
| 354 |
+
" [ 23 447 23]\n",
|
| 355 |
+
" [ 57 53 197]]\n"
|
| 356 |
+
]
|
| 357 |
+
}
|
| 358 |
+
],
|
| 359 |
+
"source": [
|
| 360 |
+
"svm_clf = SVC()\n",
|
| 361 |
+
"svm_clf.fit(X_train, y_train)\n",
|
| 362 |
+
"svm_y_pred = svm_clf.predict(X_test)\n",
|
| 363 |
+
"svm_recall = recall_score(y_test, svm_y_pred,average='weighted')\n",
|
| 364 |
+
"svm_precision = precision_score(y_test, svm_y_pred,average='weighted')\n",
|
| 365 |
+
"svm_f1 = f1_score(y_test, svm_y_pred,average='weighted')\n",
|
| 366 |
+
"svm_cm = confusion_matrix(y_test, svm_y_pred)\n",
|
| 367 |
+
"svm_accuracy = accuracy_score(y_test, svm_y_pred)\n",
|
| 368 |
+
"print(\"SVM Classifier:\")\n",
|
| 369 |
+
"print(f\"Recall: \",svm_recall)\n",
|
| 370 |
+
"print(f\"Precision: \",svm_precision)\n",
|
| 371 |
+
"print(f\"F1-Score: \",svm_f1)\n",
|
| 372 |
+
"print(f\"Accuracy: {svm_accuracy}\")\n",
|
| 373 |
+
"print(f\"Confusion Matrix: \")\n",
|
| 374 |
+
"print(svm_cm)"
|
| 375 |
+
]
|
| 376 |
+
},
|
| 377 |
+
{
|
| 378 |
+
"cell_type": "markdown",
|
| 379 |
+
"id": "6e7d71e3",
|
| 380 |
+
"metadata": {},
|
| 381 |
+
"source": [
|
| 382 |
+
" # Random Forest Classifier"
|
| 383 |
+
]
|
| 384 |
+
},
|
| 385 |
+
{
|
| 386 |
+
"cell_type": "code",
|
| 387 |
+
"execution_count": 127,
|
| 388 |
+
"id": "a9487210",
|
| 389 |
+
"metadata": {},
|
| 390 |
+
"outputs": [
|
| 391 |
+
{
|
| 392 |
+
"name": "stdout",
|
| 393 |
+
"output_type": "stream",
|
| 394 |
+
"text": [
|
| 395 |
+
"Random Forest Classifier:\n",
|
| 396 |
+
"Recall: 0.8421358096343586\n",
|
| 397 |
+
"Precision: 0.8384955269333478\n",
|
| 398 |
+
"F1-Score: 0.8335350723496863\n",
|
| 399 |
+
"Confusion Matrix: \n",
|
| 400 |
+
"[[874 28 21]\n",
|
| 401 |
+
" [ 54 420 19]\n",
|
| 402 |
+
" [ 91 59 157]]\n"
|
| 403 |
+
]
|
| 404 |
+
}
|
| 405 |
+
],
|
| 406 |
+
"source": [
|
| 407 |
+
"rf_clf = RandomForestClassifier()\n",
|
| 408 |
+
"rf_clf.fit(X_train, y_train)\n",
|
| 409 |
+
"rf_y_pred = rf_clf.predict(X_test)\n",
|
| 410 |
+
"rf_recall = recall_score(y_test, rf_y_pred,average='weighted')\n",
|
| 411 |
+
"rf_precision = precision_score(y_test, rf_y_pred,average='weighted')\n",
|
| 412 |
+
"rf_f1 = f1_score(y_test, rf_y_pred,average='weighted')\n",
|
| 413 |
+
"rf_cm = confusion_matrix(y_test, rf_y_pred)\n",
|
| 414 |
+
"print(\"Random Forest Classifier:\")\n",
|
| 415 |
+
"print(f\"Recall: \",rf_recall)\n",
|
| 416 |
+
"print(f\"Precision: \",rf_precision)\n",
|
| 417 |
+
"print(f\"F1-Score: \",rf_f1)\n",
|
| 418 |
+
"print(f\"Confusion Matrix: \")\n",
|
| 419 |
+
"print(rf_cm)"
|
| 420 |
+
]
|
| 421 |
+
},
|
| 422 |
+
{
|
| 423 |
+
"cell_type": "markdown",
|
| 424 |
+
"id": "d7afc7ce",
|
| 425 |
+
"metadata": {},
|
| 426 |
+
"source": [
|
| 427 |
+
"# Decision Tree Classifier"
|
| 428 |
+
]
|
| 429 |
+
},
|
| 430 |
+
{
|
| 431 |
+
"cell_type": "code",
|
| 432 |
+
"execution_count": 40,
|
| 433 |
+
"id": "5b4a1ae5",
|
| 434 |
+
"metadata": {},
|
| 435 |
+
"outputs": [
|
| 436 |
+
{
|
| 437 |
+
"name": "stdout",
|
| 438 |
+
"output_type": "stream",
|
| 439 |
+
"text": [
|
| 440 |
+
"Decision Tree Classifier:\n",
|
| 441 |
+
"Recall: 0.7951247823563552\n",
|
| 442 |
+
"Precision: 0.7912243934842538\n",
|
| 443 |
+
"F1-Score: 0.7927707956848741\n",
|
| 444 |
+
"Confusion Matrix: \n",
|
| 445 |
+
"[[819 57 47]\n",
|
| 446 |
+
" [ 64 375 54]\n",
|
| 447 |
+
" [ 70 61 176]]\n"
|
| 448 |
+
]
|
| 449 |
+
}
|
| 450 |
+
],
|
| 451 |
+
"source": [
|
| 452 |
+
"dt_clf = DecisionTreeClassifier()\n",
|
| 453 |
+
"dt_clf.fit(X_train, y_train)\n",
|
| 454 |
+
"dt_y_pred = dt_clf.predict(X_test)\n",
|
| 455 |
+
"dt_recall = recall_score(y_test, dt_y_pred,average='weighted')\n",
|
| 456 |
+
"dt_precision = precision_score(y_test, dt_y_pred,average='weighted')\n",
|
| 457 |
+
"dt_f1 = f1_score(y_test, dt_y_pred,average='weighted')\n",
|
| 458 |
+
"dt_cm = confusion_matrix(y_test, dt_y_pred)\n",
|
| 459 |
+
"print(\"Decision Tree Classifier:\")\n",
|
| 460 |
+
"print(f\"Recall: \",dt_recall)\n",
|
| 461 |
+
"print(f\"Precision: \",dt_precision)\n",
|
| 462 |
+
"print(f\"F1-Score:\",dt_f1)\n",
|
| 463 |
+
"print(f\"Confusion Matrix: \")\n",
|
| 464 |
+
"print(dt_cm)"
|
| 465 |
+
]
|
| 466 |
+
},
|
| 467 |
+
{
|
| 468 |
+
"cell_type": "markdown",
|
| 469 |
+
"id": "78cfc7a4",
|
| 470 |
+
"metadata": {},
|
| 471 |
+
"source": [
|
| 472 |
+
"# Multinomial Naive Bayes Classifier"
|
| 473 |
+
]
|
| 474 |
+
},
|
| 475 |
+
{
|
| 476 |
+
"cell_type": "code",
|
| 477 |
+
"execution_count": 41,
|
| 478 |
+
"id": "fb651d69",
|
| 479 |
+
"metadata": {},
|
| 480 |
+
"outputs": [
|
| 481 |
+
{
|
| 482 |
+
"name": "stdout",
|
| 483 |
+
"output_type": "stream",
|
| 484 |
+
"text": [
|
| 485 |
+
"Multinomial Naive Bayes Classifier:\n",
|
| 486 |
+
"Recall: 0.7643644805571678\n",
|
| 487 |
+
"Precision: 0.7913909919351131\n",
|
| 488 |
+
"F1-Score: 0.7182165890778468\n",
|
| 489 |
+
"Confusion Matrix: \n",
|
| 490 |
+
"[[905 14 4]\n",
|
| 491 |
+
" [122 370 1]\n",
|
| 492 |
+
" [206 59 42]]\n"
|
| 493 |
+
]
|
| 494 |
+
}
|
| 495 |
+
],
|
| 496 |
+
"source": [
|
| 497 |
+
"nb_clf = MultinomialNB()\n",
|
| 498 |
+
"nb_clf.fit(X_train, y_train)\n",
|
| 499 |
+
"nb_y_pred = nb_clf.predict(X_test)\n",
|
| 500 |
+
"nb_recall = recall_score(y_test, nb_y_pred,average='weighted')\n",
|
| 501 |
+
"nb_precision = precision_score(y_test, nb_y_pred,average='weighted')\n",
|
| 502 |
+
"nb_f1 = f1_score(y_test, nb_y_pred,average='weighted')\n",
|
| 503 |
+
"nb_cm = confusion_matrix(y_test, nb_y_pred)\n",
|
| 504 |
+
"print(\"Multinomial Naive Bayes Classifier:\")\n",
|
| 505 |
+
"print(f\"Recall: {nb_recall}\")\n",
|
| 506 |
+
"print(f\"Precision: {nb_precision}\")\n",
|
| 507 |
+
"print(f\"F1-Score: {nb_f1}\")\n",
|
| 508 |
+
"print(f\"Confusion Matrix: \")\n",
|
| 509 |
+
"print(nb_cm)"
|
| 510 |
+
]
|
| 511 |
+
},
|
| 512 |
+
{
|
| 513 |
+
"cell_type": "markdown",
|
| 514 |
+
"id": "26eee28a",
|
| 515 |
+
"metadata": {},
|
| 516 |
+
"source": [
|
| 517 |
+
"# svm hyperparameter tuning"
|
| 518 |
+
]
|
| 519 |
+
},
|
| 520 |
+
{
|
| 521 |
+
"cell_type": "code",
|
| 522 |
+
"execution_count": 63,
|
| 523 |
+
"id": "a49652eb",
|
| 524 |
+
"metadata": {},
|
| 525 |
+
"outputs": [
|
| 526 |
+
{
|
| 527 |
+
"name": "stdout",
|
| 528 |
+
"output_type": "stream",
|
| 529 |
+
"text": [
|
| 530 |
+
"Best SVM Classifier:\n",
|
| 531 |
+
"Recall: 0.8618688334300638\n",
|
| 532 |
+
"Precision: 0.8581743414945051\n",
|
| 533 |
+
"F1-Score: 0.859244203684205\n",
|
| 534 |
+
"Confusion Matrix: \n",
|
| 535 |
+
"[[861 25 37]\n",
|
| 536 |
+
" [ 30 429 34]\n",
|
| 537 |
+
" [ 61 51 195]]\n"
|
| 538 |
+
]
|
| 539 |
+
}
|
| 540 |
+
],
|
| 541 |
+
"source": [
|
| 542 |
+
"param_grid = {\n",
|
| 543 |
+
" 'C': [0.1, 1, 10, 100],\n",
|
| 544 |
+
" 'kernel': ['linear', 'rbf'],\n",
|
| 545 |
+
" 'gamma': ['scale', 'auto', 0.1, 0.01, 0.001]\n",
|
| 546 |
+
"}\n",
|
| 547 |
+
"\n",
|
| 548 |
+
"svm_clf = SVC()\n",
|
| 549 |
+
"\n",
|
| 550 |
+
"grid_search = GridSearchCV(svm_clf, param_grid, cv=10, scoring='f1_weighted')\n",
|
| 551 |
+
"\n",
|
| 552 |
+
"grid_search.fit(X_train, y_train)\n",
|
| 553 |
+
"\n",
|
| 554 |
+
"best_svm_clf = grid_search.best_estimator_\n",
|
| 555 |
+
"\n",
|
| 556 |
+
"svm_y_pred = best_svm_clf.predict(X_test)\n",
|
| 557 |
+
"\n",
|
| 558 |
+
"svm_recall = recall_score(y_test, svm_y_pred, average='weighted')\n",
|
| 559 |
+
"svm_precision = precision_score(y_test, svm_y_pred, average='weighted')\n",
|
| 560 |
+
"svm_f1 = f1_score(y_test, svm_y_pred, average='weighted')\n",
|
| 561 |
+
"svm_cm = confusion_matrix(y_test, svm_y_pred)\n",
|
| 562 |
+
"\n",
|
| 563 |
+
"print(\"Best SVM Classifier:\")\n",
|
| 564 |
+
"print(f\"Recall: {svm_recall}\")\n",
|
| 565 |
+
"print(f\"Precision: {svm_precision}\")\n",
|
| 566 |
+
"print(f\"F1-Score: {svm_f1}\")\n",
|
| 567 |
+
"print(f\"Confusion Matrix: \")\n",
|
| 568 |
+
"print(svm_cm)"
|
| 569 |
+
]
|
| 570 |
+
},
|
| 571 |
+
{
|
| 572 |
+
"cell_type": "markdown",
|
| 573 |
+
"id": "b1289504",
|
| 574 |
+
"metadata": {},
|
| 575 |
+
"source": [
|
| 576 |
+
"# Neural Network Classifier"
|
| 577 |
+
]
|
| 578 |
+
},
|
| 579 |
+
{
|
| 580 |
+
"cell_type": "code",
|
| 581 |
+
"execution_count": 42,
|
| 582 |
+
"id": "7e4b42ca",
|
| 583 |
+
"metadata": {},
|
| 584 |
+
"outputs": [],
|
| 585 |
+
"source": [
|
| 586 |
+
"model = keras.Sequential([\n",
|
| 587 |
+
" layers.Dense(512, activation=\"relu\", input_shape=(7916,)),\n",
|
| 588 |
+
" layers.Dense(3, activation=\"softmax\")\n",
|
| 589 |
+
"])\n"
|
| 590 |
+
]
|
| 591 |
+
},
|
| 592 |
+
{
|
| 593 |
+
"cell_type": "code",
|
| 594 |
+
"execution_count": 19,
|
| 595 |
+
"id": "423e6ba9",
|
| 596 |
+
"metadata": {},
|
| 597 |
+
"outputs": [],
|
| 598 |
+
"source": [
|
| 599 |
+
"model.compile(optimizer = \"rmsprop\",\n",
|
| 600 |
+
" loss= \"sparse_categorical_crossentropy\",\n",
|
| 601 |
+
" metrics=[\"accuracy\"])"
|
| 602 |
+
]
|
| 603 |
+
},
|
| 604 |
+
{
|
| 605 |
+
"cell_type": "code",
|
| 606 |
+
"execution_count": 20,
|
| 607 |
+
"id": "eaf3c214",
|
| 608 |
+
"metadata": {},
|
| 609 |
+
"outputs": [
|
| 610 |
+
{
|
| 611 |
+
"name": "stdout",
|
| 612 |
+
"output_type": "stream",
|
| 613 |
+
"text": [
|
| 614 |
+
"Epoch 1/10\n",
|
| 615 |
+
"23/23 [==============================] - 3s 88ms/step - loss: 0.9380 - accuracy: 0.5410\n",
|
| 616 |
+
"Epoch 2/10\n",
|
| 617 |
+
"23/23 [==============================] - 2s 86ms/step - loss: 0.7119 - accuracy: 0.7112\n",
|
| 618 |
+
"Epoch 3/10\n",
|
| 619 |
+
"23/23 [==============================] - 2s 84ms/step - loss: 0.5505 - accuracy: 0.8202\n",
|
| 620 |
+
"Epoch 4/10\n",
|
| 621 |
+
"23/23 [==============================] - 2s 87ms/step - loss: 0.4464 - accuracy: 0.8595\n",
|
| 622 |
+
"Epoch 5/10\n",
|
| 623 |
+
"23/23 [==============================] - 2s 85ms/step - loss: 0.3774 - accuracy: 0.8836\n",
|
| 624 |
+
"Epoch 6/10\n",
|
| 625 |
+
"23/23 [==============================] - 2s 88ms/step - loss: 0.3321 - accuracy: 0.8932\n",
|
| 626 |
+
"Epoch 7/10\n",
|
| 627 |
+
"23/23 [==============================] - 2s 86ms/step - loss: 0.2990 - accuracy: 0.9042\n",
|
| 628 |
+
"Epoch 8/10\n",
|
| 629 |
+
"23/23 [==============================] - 2s 84ms/step - loss: 0.2745 - accuracy: 0.9112\n",
|
| 630 |
+
"Epoch 9/10\n",
|
| 631 |
+
"23/23 [==============================] - 2s 90ms/step - loss: 0.2534 - accuracy: 0.9196\n",
|
| 632 |
+
"Epoch 10/10\n",
|
| 633 |
+
"23/23 [==============================] - 2s 84ms/step - loss: 0.2361 - accuracy: 0.9267\n"
|
| 634 |
+
]
|
| 635 |
+
},
|
| 636 |
+
{
|
| 637 |
+
"data": {
|
| 638 |
+
"text/plain": [
|
| 639 |
+
"<keras.callbacks.History at 0x2ba2e6dce80>"
|
| 640 |
+
]
|
| 641 |
+
},
|
| 642 |
+
"execution_count": 20,
|
| 643 |
+
"metadata": {},
|
| 644 |
+
"output_type": "execute_result"
|
| 645 |
+
}
|
| 646 |
+
],
|
| 647 |
+
"source": [
|
| 648 |
+
"X_train_dense = X_train.toarray()\n",
|
| 649 |
+
"model.fit(X_train_dense, y_train, epochs=10, batch_size=300)\n"
|
| 650 |
+
]
|
| 651 |
+
},
|
| 652 |
+
{
|
| 653 |
+
"cell_type": "code",
|
| 654 |
+
"execution_count": 21,
|
| 655 |
+
"id": "ff61b23b",
|
| 656 |
+
"metadata": {},
|
| 657 |
+
"outputs": [
|
| 658 |
+
{
|
| 659 |
+
"name": "stdout",
|
| 660 |
+
"output_type": "stream",
|
| 661 |
+
"text": [
|
| 662 |
+
"Number of features (n): 7916\n"
|
| 663 |
+
]
|
| 664 |
+
}
|
| 665 |
+
],
|
| 666 |
+
"source": [
|
| 667 |
+
"n = X_train_dense.shape[1]\n",
|
| 668 |
+
"print(\"Number of features (n):\", n)"
|
| 669 |
+
]
|
| 670 |
+
},
|
| 671 |
+
{
|
| 672 |
+
"cell_type": "code",
|
| 673 |
+
"execution_count": 22,
|
| 674 |
+
"id": "28dfe4c5",
|
| 675 |
+
"metadata": {},
|
| 676 |
+
"outputs": [
|
| 677 |
+
{
|
| 678 |
+
"name": "stdout",
|
| 679 |
+
"output_type": "stream",
|
| 680 |
+
"text": [
|
| 681 |
+
"54/54 [==============================] - 1s 4ms/step\n"
|
| 682 |
+
]
|
| 683 |
+
},
|
| 684 |
+
{
|
| 685 |
+
"data": {
|
| 686 |
+
"text/plain": [
|
| 687 |
+
"array([0.01290104, 0.96764606, 0.01945294], dtype=float32)"
|
| 688 |
+
]
|
| 689 |
+
},
|
| 690 |
+
"execution_count": 22,
|
| 691 |
+
"metadata": {},
|
| 692 |
+
"output_type": "execute_result"
|
| 693 |
+
}
|
| 694 |
+
],
|
| 695 |
+
"source": [
|
| 696 |
+
"predictions = model.predict(X_test)\n",
|
| 697 |
+
"predictions[0]"
|
| 698 |
+
]
|
| 699 |
+
},
|
| 700 |
+
{
|
| 701 |
+
"cell_type": "code",
|
| 702 |
+
"execution_count": 23,
|
| 703 |
+
"id": "5a120e21",
|
| 704 |
+
"metadata": {},
|
| 705 |
+
"outputs": [
|
| 706 |
+
{
|
| 707 |
+
"data": {
|
| 708 |
+
"text/plain": [
|
| 709 |
+
"1"
|
| 710 |
+
]
|
| 711 |
+
},
|
| 712 |
+
"execution_count": 23,
|
| 713 |
+
"metadata": {},
|
| 714 |
+
"output_type": "execute_result"
|
| 715 |
+
}
|
| 716 |
+
],
|
| 717 |
+
"source": [
|
| 718 |
+
"predictions[0].argmax()"
|
| 719 |
+
]
|
| 720 |
+
},
|
| 721 |
+
{
|
| 722 |
+
"cell_type": "code",
|
| 723 |
+
"execution_count": 24,
|
| 724 |
+
"id": "1d1901f2",
|
| 725 |
+
"metadata": {},
|
| 726 |
+
"outputs": [
|
| 727 |
+
{
|
| 728 |
+
"name": "stdout",
|
| 729 |
+
"output_type": "stream",
|
| 730 |
+
"text": [
|
| 731 |
+
"54/54 [==============================] - 2s 24ms/step - loss: 0.3702 - accuracy: 0.8654\n",
|
| 732 |
+
"Test accuracy: 0.865351140499115\n"
|
| 733 |
+
]
|
| 734 |
+
}
|
| 735 |
+
],
|
| 736 |
+
"source": [
|
| 737 |
+
"X_test_dense = X_test.toarray()\n",
|
| 738 |
+
"test_loss, test_acc = model.evaluate(X_test_dense, y_test)\n",
|
| 739 |
+
"print('Test accuracy:', test_acc)\n"
|
| 740 |
+
]
|
| 741 |
+
},
|
| 742 |
+
{
|
| 743 |
+
"cell_type": "markdown",
|
| 744 |
+
"id": "dfe7a4ff",
|
| 745 |
+
"metadata": {},
|
| 746 |
+
"source": [
|
| 747 |
+
"# Saving preprocessed Data"
|
| 748 |
+
]
|
| 749 |
+
},
|
| 750 |
+
{
|
| 751 |
+
"cell_type": "code",
|
| 752 |
+
"execution_count": 18,
|
| 753 |
+
"id": "7bf3d145",
|
| 754 |
+
"metadata": {},
|
| 755 |
+
"outputs": [
|
| 756 |
+
{
|
| 757 |
+
"name": "stderr",
|
| 758 |
+
"output_type": "stream",
|
| 759 |
+
"text": [
|
| 760 |
+
"C:\\Users\\sher6\\AppData\\Local\\Temp\\ipykernel_22216\\620722207.py:4: FutureWarning: The default value of regex will change from True to False in a future version.\n",
|
| 761 |
+
" text = text.str.replace('[^\\w\\s]', '')\n",
|
| 762 |
+
"C:\\Users\\sher6\\AppData\\Local\\Temp\\ipykernel_22216\\620722207.py:5: FutureWarning: The default value of regex will change from True to False in a future version.\n",
|
| 763 |
+
" text = text.str.replace(r'\\b\\d+\\b', 'NUMBER')\n",
|
| 764 |
+
"C:\\Users\\sher6\\AppData\\Local\\Temp\\ipykernel_22216\\620722207.py:6: FutureWarning: The default value of regex will change from True to False in a future version.\n",
|
| 765 |
+
" text = text.str.replace(r'(http|https)://\\S+|www\\.\\S+', 'URL')\n"
|
| 766 |
+
]
|
| 767 |
+
}
|
| 768 |
+
],
|
| 769 |
+
"source": [
|
| 770 |
+
"preprocessed_text = preprocess_text(data['text'])\n",
|
| 771 |
+
"preprocessed_data = pd.DataFrame({'label': data['label'], 'text': preprocessed_text})\n",
|
| 772 |
+
"preprocessed_data.to_csv('preprocessed_data.csv', index=False)"
|
| 773 |
+
]
|
| 774 |
+
},
|
| 775 |
+
{
|
| 776 |
+
"cell_type": "markdown",
|
| 777 |
+
"id": "83bd9840",
|
| 778 |
+
"metadata": {},
|
| 779 |
+
"source": [
|
| 780 |
+
"# Saving Model"
|
| 781 |
+
]
|
| 782 |
+
},
|
| 783 |
+
{
|
| 784 |
+
"cell_type": "code",
|
| 785 |
+
"execution_count": 19,
|
| 786 |
+
"id": "4d38fbca",
|
| 787 |
+
"metadata": {},
|
| 788 |
+
"outputs": [
|
| 789 |
+
{
|
| 790 |
+
"data": {
|
| 791 |
+
"text/plain": [
|
| 792 |
+
"['best_model2.pkl']"
|
| 793 |
+
]
|
| 794 |
+
},
|
| 795 |
+
"execution_count": 19,
|
| 796 |
+
"metadata": {},
|
| 797 |
+
"output_type": "execute_result"
|
| 798 |
+
}
|
| 799 |
+
],
|
| 800 |
+
"source": [
|
| 801 |
+
"joblib.dump(svm_clf, 'best_model2.pkl')\n"
|
| 802 |
+
]
|
| 803 |
+
},
|
| 804 |
+
{
|
| 805 |
+
"cell_type": "markdown",
|
| 806 |
+
"id": "644ba7c2",
|
| 807 |
+
"metadata": {},
|
| 808 |
+
"source": [
|
| 809 |
+
"# Prediction on Dataset provided during viva"
|
| 810 |
+
]
|
| 811 |
+
},
|
| 812 |
+
{
|
| 813 |
+
"cell_type": "code",
|
| 814 |
+
"execution_count": 29,
|
| 815 |
+
"id": "07b372a8",
|
| 816 |
+
"metadata": {
|
| 817 |
+
"scrolled": true
|
| 818 |
+
},
|
| 819 |
+
"outputs": [
|
| 820 |
+
{
|
| 821 |
+
"name": "stdout",
|
| 822 |
+
"output_type": "stream",
|
| 823 |
+
"text": [
|
| 824 |
+
"label 0\n",
|
| 825 |
+
"text 1\n",
|
| 826 |
+
"dtype: int64\n",
|
| 827 |
+
"F1-Score for new data: 0.839031204867194\n",
|
| 828 |
+
"Accuracy for new data: 0.8423076923076923\n",
|
| 829 |
+
"Recall for new data: 0.8423076923076923\n"
|
| 830 |
+
]
|
| 831 |
+
}
|
| 832 |
+
],
|
| 833 |
+
"source": [
|
| 834 |
+
"loaded_model = joblib.load('best_model2.pkl')\n",
|
| 835 |
+
"new_data = pd.read_csv('Nouman1.csv', sep=',', header=None, names=['label', 'text'])\n",
|
| 836 |
+
"print(new_data.isna().sum())\n",
|
| 837 |
+
"new_data.dropna(inplace=True)\n",
|
| 838 |
+
"new_data_processed = pipeline.transform(new_data['text'])\n",
|
| 839 |
+
"new_predictions = loaded_model.predict(new_data_processed)\n",
|
| 840 |
+
"predictions_df = pd.DataFrame(new_predictions, columns=['Predictions'])\n",
|
| 841 |
+
"predictions_df.to_csv('Nouman1Labbeled.csv', index=False)\n",
|
| 842 |
+
"new_labels = preprocess_label(new_data.label).map({'positive': 0, 'negative': 1, 'neutral': 2})\n",
|
| 843 |
+
"f1_score_new = f1_score(new_labels, new_predictions, average='weighted')\n",
|
| 844 |
+
"accuracy_new = accuracy_score(new_labels, new_predictions)\n",
|
| 845 |
+
"recall_new = recall_score(new_labels, new_predictions, average='weighted')\n",
|
| 846 |
+
"\n",
|
| 847 |
+
"print(f\"F1-Score for new data: {f1_score_new}\")\n",
|
| 848 |
+
"print(f\"Accuracy for new data: {accuracy_new}\")\n",
|
| 849 |
+
"print(f\"Recall for new data: {recall_new}\")"
|
| 850 |
+
]
|
| 851 |
+
},
|
| 852 |
+
{
|
| 853 |
+
"cell_type": "code",
|
| 854 |
+
"execution_count": 30,
|
| 855 |
+
"id": "842b4d07",
|
| 856 |
+
"metadata": {},
|
| 857 |
+
"outputs": [
|
| 858 |
+
{
|
| 859 |
+
"data": {
|
| 860 |
+
"text/html": [
|
| 861 |
+
"<div>\n",
|
| 862 |
+
"<style scoped>\n",
|
| 863 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 864 |
+
" vertical-align: middle;\n",
|
| 865 |
+
" }\n",
|
| 866 |
+
"\n",
|
| 867 |
+
" .dataframe tbody tr th {\n",
|
| 868 |
+
" vertical-align: top;\n",
|
| 869 |
+
" }\n",
|
| 870 |
+
"\n",
|
| 871 |
+
" .dataframe thead th {\n",
|
| 872 |
+
" text-align: right;\n",
|
| 873 |
+
" }\n",
|
| 874 |
+
"</style>\n",
|
| 875 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 876 |
+
" <thead>\n",
|
| 877 |
+
" <tr style=\"text-align: right;\">\n",
|
| 878 |
+
" <th></th>\n",
|
| 879 |
+
" <th>Predictions</th>\n",
|
| 880 |
+
" </tr>\n",
|
| 881 |
+
" </thead>\n",
|
| 882 |
+
" <tbody>\n",
|
| 883 |
+
" <tr>\n",
|
| 884 |
+
" <th>0</th>\n",
|
| 885 |
+
" <td>2</td>\n",
|
| 886 |
+
" </tr>\n",
|
| 887 |
+
" <tr>\n",
|
| 888 |
+
" <th>1</th>\n",
|
| 889 |
+
" <td>2</td>\n",
|
| 890 |
+
" </tr>\n",
|
| 891 |
+
" <tr>\n",
|
| 892 |
+
" <th>2</th>\n",
|
| 893 |
+
" <td>0</td>\n",
|
| 894 |
+
" </tr>\n",
|
| 895 |
+
" <tr>\n",
|
| 896 |
+
" <th>3</th>\n",
|
| 897 |
+
" <td>0</td>\n",
|
| 898 |
+
" </tr>\n",
|
| 899 |
+
" <tr>\n",
|
| 900 |
+
" <th>4</th>\n",
|
| 901 |
+
" <td>2</td>\n",
|
| 902 |
+
" </tr>\n",
|
| 903 |
+
" <tr>\n",
|
| 904 |
+
" <th>...</th>\n",
|
| 905 |
+
" <td>...</td>\n",
|
| 906 |
+
" </tr>\n",
|
| 907 |
+
" <tr>\n",
|
| 908 |
+
" <th>255</th>\n",
|
| 909 |
+
" <td>0</td>\n",
|
| 910 |
+
" </tr>\n",
|
| 911 |
+
" <tr>\n",
|
| 912 |
+
" <th>256</th>\n",
|
| 913 |
+
" <td>0</td>\n",
|
| 914 |
+
" </tr>\n",
|
| 915 |
+
" <tr>\n",
|
| 916 |
+
" <th>257</th>\n",
|
| 917 |
+
" <td>1</td>\n",
|
| 918 |
+
" </tr>\n",
|
| 919 |
+
" <tr>\n",
|
| 920 |
+
" <th>258</th>\n",
|
| 921 |
+
" <td>1</td>\n",
|
| 922 |
+
" </tr>\n",
|
| 923 |
+
" <tr>\n",
|
| 924 |
+
" <th>259</th>\n",
|
| 925 |
+
" <td>1</td>\n",
|
| 926 |
+
" </tr>\n",
|
| 927 |
+
" </tbody>\n",
|
| 928 |
+
"</table>\n",
|
| 929 |
+
"<p>260 rows × 1 columns</p>\n",
|
| 930 |
+
"</div>"
|
| 931 |
+
],
|
| 932 |
+
"text/plain": [
|
| 933 |
+
" Predictions\n",
|
| 934 |
+
"0 2\n",
|
| 935 |
+
"1 2\n",
|
| 936 |
+
"2 0\n",
|
| 937 |
+
"3 0\n",
|
| 938 |
+
"4 2\n",
|
| 939 |
+
".. ...\n",
|
| 940 |
+
"255 0\n",
|
| 941 |
+
"256 0\n",
|
| 942 |
+
"257 1\n",
|
| 943 |
+
"258 1\n",
|
| 944 |
+
"259 1\n",
|
| 945 |
+
"\n",
|
| 946 |
+
"[260 rows x 1 columns]"
|
| 947 |
+
]
|
| 948 |
+
},
|
| 949 |
+
"execution_count": 30,
|
| 950 |
+
"metadata": {},
|
| 951 |
+
"output_type": "execute_result"
|
| 952 |
+
}
|
| 953 |
+
],
|
| 954 |
+
"source": [
|
| 955 |
+
"predictions_df"
|
| 956 |
+
]
|
| 957 |
+
},
|
| 958 |
+
{
|
| 959 |
+
"cell_type": "code",
|
| 960 |
+
"execution_count": null,
|
| 961 |
+
"id": "61d07be8",
|
| 962 |
+
"metadata": {},
|
| 963 |
+
"outputs": [],
|
| 964 |
+
"source": []
|
| 965 |
+
}
|
| 966 |
+
],
|
| 967 |
+
"metadata": {
|
| 968 |
+
"kernelspec": {
|
| 969 |
+
"display_name": "Python 3 (ipykernel)",
|
| 970 |
+
"language": "python",
|
| 971 |
+
"name": "python3"
|
| 972 |
+
},
|
| 973 |
+
"language_info": {
|
| 974 |
+
"codemirror_mode": {
|
| 975 |
+
"name": "ipython",
|
| 976 |
+
"version": 3
|
| 977 |
+
},
|
| 978 |
+
"file_extension": ".py",
|
| 979 |
+
"mimetype": "text/x-python",
|
| 980 |
+
"name": "python",
|
| 981 |
+
"nbconvert_exporter": "python",
|
| 982 |
+
"pygments_lexer": "ipython3",
|
| 983 |
+
"version": "3.9.12"
|
| 984 |
+
}
|
| 985 |
+
},
|
| 986 |
+
"nbformat": 4,
|
| 987 |
+
"nbformat_minor": 5
|
| 988 |
+
}
|
Daraz-Reviews.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|