Priyanhsu commited on
Commit
0e9c6bb
·
1 Parent(s): 65d362f

Upload BestTextClassifier.ipynb

Browse files
Files changed (1) hide show
  1. BestTextClassifier.ipynb +259 -0
BestTextClassifier.ipynb ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 1,
20
+ "metadata": {
21
+ "id": "2YpCZ5QwOGCL"
22
+ },
23
+ "outputs": [],
24
+ "source": [
25
+ "import pandas as pd\n",
26
+ "import numpy as np\n",
27
+ "from sklearn.naive_bayes import MultinomialNB\n",
28
+ "from sklearn.utils import shuffle\n",
29
+ "from sklearn.preprocessing import LabelEncoder\n",
30
+ "from sklearn.feature_extraction.text import CountVectorizer\n",
31
+ "import json\n"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "source": [
37
+ "# Read the dataset\n",
38
+ "df = pd.read_json(\"DATA.json\", encoding=\"utf-8\")"
39
+ ],
40
+ "metadata": {
41
+ "id": "1BpsBFfLZbW8"
42
+ },
43
+ "execution_count": 2,
44
+ "outputs": []
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "source": [
49
+ "# Shuffle the dataset\n",
50
+ "df = shuffle(df)"
51
+ ],
52
+ "metadata": {
53
+ "id": "lTijWIiEVjVc"
54
+ },
55
+ "execution_count": 3,
56
+ "outputs": []
57
+ },
58
+ {
59
+ "cell_type": "code",
60
+ "source": [
61
+ "# Create a training set and a test set\n",
62
+ "train_data = df[:int(len(df) * 0.8)]\n",
63
+ "test_data = df[int(len(df) * 0.8):]"
64
+ ],
65
+ "metadata": {
66
+ "id": "RDTKsDCsWGRk"
67
+ },
68
+ "execution_count": 4,
69
+ "outputs": []
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "source": [
74
+ "# Create a vocabulary of all the words in the training set\n",
75
+ "vocabulary = set()\n",
76
+ "for d in df[\"content\"]:\n",
77
+ " if isinstance(d, str):\n",
78
+ " vocabulary.update(d.split())"
79
+ ],
80
+ "metadata": {
81
+ "id": "rta9jPyxVm-I"
82
+ },
83
+ "execution_count": 6,
84
+ "outputs": []
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "source": [
89
+ "# Convert the list of text documents to a 2D array for the training set\n",
90
+ "bow_train = np.array([[str(doc).count(word) for word in vocabulary] for doc in train_data[\"content\"].fillna(\"\")])\n"
91
+ ],
92
+ "metadata": {
93
+ "id": "owPao6jdVprA"
94
+ },
95
+ "execution_count": 7,
96
+ "outputs": []
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "source": [
101
+ "# Convert the list of text documents to a 2D array for the test set\n",
102
+ "bow_test = np.array([[str(doc).count(word) for word in vocabulary] for doc in test_data[\"content\"].fillna(\"\")])"
103
+ ],
104
+ "metadata": {
105
+ "id": "Blc4X_qNV72O"
106
+ },
107
+ "execution_count": 8,
108
+ "outputs": []
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "source": [
113
+ "# Convert the labels to numeric values\n",
114
+ "label_encoder = LabelEncoder()\n",
115
+ "train_labels = label_encoder.fit_transform(train_data[\"label\"])"
116
+ ],
117
+ "metadata": {
118
+ "id": "pe1Y1uNEWRek"
119
+ },
120
+ "execution_count": 9,
121
+ "outputs": []
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "source": [
126
+ "# Create a Naive Bayes model\n",
127
+ "model = MultinomialNB()"
128
+ ],
129
+ "metadata": {
130
+ "id": "pv1ps_IwWV8M"
131
+ },
132
+ "execution_count": 10,
133
+ "outputs": []
134
+ },
135
+ {
136
+ "cell_type": "code",
137
+ "source": [
138
+ "# Train the model\n",
139
+ "model.fit(bow_train, train_labels)"
140
+ ],
141
+ "metadata": {
142
+ "colab": {
143
+ "base_uri": "https://localhost:8080/",
144
+ "height": 75
145
+ },
146
+ "id": "JYIiFXYdWY73",
147
+ "outputId": "a698f8a6-eeda-49ab-c434-f438c6967ad5"
148
+ },
149
+ "execution_count": 11,
150
+ "outputs": [
151
+ {
152
+ "output_type": "execute_result",
153
+ "data": {
154
+ "text/plain": [
155
+ "MultinomialNB()"
156
+ ],
157
+ "text/html": [
158
+ "<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>MultinomialNB()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">MultinomialNB</label><div class=\"sk-toggleable__content\"><pre>MultinomialNB()</pre></div></div></div></div></div>"
159
+ ]
160
+ },
161
+ "metadata": {},
162
+ "execution_count": 11
163
+ }
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "source": [
169
+ "# Test the model\n",
170
+ "test_labels = label_encoder.transform(test_data[\"label\"])\n",
171
+ "predictions = model.predict(bow_test)"
172
+ ],
173
+ "metadata": {
174
+ "id": "gDcK0uiWWeKc"
175
+ },
176
+ "execution_count": 12,
177
+ "outputs": []
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "source": [
182
+ "# Calculate the accuracy of the model\n",
183
+ "accuracy = (predictions == test_labels).mean()\n",
184
+ "print(\"Accuracy:\", accuracy)"
185
+ ],
186
+ "metadata": {
187
+ "colab": {
188
+ "base_uri": "https://localhost:8080/"
189
+ },
190
+ "id": "eBMyVM5hWjiL",
191
+ "outputId": "4c26f478-76b9-4404-9d61-10da839d6465"
192
+ },
193
+ "execution_count": 13,
194
+ "outputs": [
195
+ {
196
+ "output_type": "stream",
197
+ "name": "stdout",
198
+ "text": [
199
+ "Accuracy: 0.9348500517063082\n"
200
+ ]
201
+ }
202
+ ]
203
+ },
204
+ {
205
+ "cell_type": "code",
206
+ "source": [
207
+ "def test_model(text):\n",
208
+ " bow = {}\n",
209
+ " for word in text.split():\n",
210
+ " if word in vocabulary:\n",
211
+ " bow[word] = text.count(word)\n",
212
+ " bow = np.array([bow.get(word, 0) for word in vocabulary]).reshape(1, -1)\n",
213
+ " prediction = model.predict(bow)[0]\n",
214
+ " return label_encoder.inverse_transform([prediction])[0]\n",
215
+ "\n"
216
+ ],
217
+ "metadata": {
218
+ "id": "TFH--bOmWpEU"
219
+ },
220
+ "execution_count": 14,
221
+ "outputs": []
222
+ },
223
+ {
224
+ "cell_type": "code",
225
+ "source": [
226
+ "# Test the model with some text\n",
227
+ "text = \"Aura Azure Collagen Gummies Advantages, Official Website &amp; Reviews [2023]\"\n",
228
+ "prediction = test_model(text)\n",
229
+ "print(prediction)"
230
+ ],
231
+ "metadata": {
232
+ "colab": {
233
+ "base_uri": "https://localhost:8080/"
234
+ },
235
+ "id": "GDL4OZ_5Wsc7",
236
+ "outputId": "e97b7b46-3a18-446e-e5f6-186bf285d96f"
237
+ },
238
+ "execution_count": 15,
239
+ "outputs": [
240
+ {
241
+ "output_type": "stream",
242
+ "name": "stdout",
243
+ "text": [
244
+ "NSFW\n"
245
+ ]
246
+ }
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "source": [],
252
+ "metadata": {
253
+ "id": "CPxtDWlYWwYC"
254
+ },
255
+ "execution_count": null,
256
+ "outputs": []
257
+ }
258
+ ]
259
+ }