File size: 23,854 Bytes
8236fa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "896dff63-949c-4e4f-95ac-bbb1720a5ac5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(50425, 2)\n"
     ]
    },
    {
     "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>category</th>\n",
       "      <th>description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Household</td>\n",
       "      <td>Paper Plane Design Framed Wall Hanging Motivat...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Household</td>\n",
       "      <td>SAF 'Floral' Framed Painting (Wood, 30 inch x ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Household</td>\n",
       "      <td>SAF 'UV Textured Modern Art Print Framed' Pain...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    category                                        description\n",
       "0  Household  Paper Plane Design Framed Wall Hanging Motivat...\n",
       "1  Household  SAF 'Floral' Framed Painting (Wood, 30 inch x ...\n",
       "2  Household  SAF 'UV Textured Modern Art Print Framed' Pain..."
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"ecommerce_dataset.csv\", names=[\"category\",\"description\"], header=None)\n",
    "print(df.shape)\n",
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5be7b869-ca63-4766-b522-cccf7e8a6554",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Household                 19313\n",
       "Books                     11820\n",
       "Electronics               10621\n",
       "Clothing & Accessories     8671\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.category.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cf32bed7-57c4-465e-aec1-ae587b1b2f14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50424, 2)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna(inplace=True)\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e07fc9a6-d2be-4bd2-b00b-e3fb343c1a5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Household', 'Books', 'Clothing_Accessories', 'Electronics'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"category\"] = df[\"category\"].replace(\"Clothing & Accessories\", \"Clothing_Accessories\")\n",
    "df.category.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fd497abf-7427-4cb8-86c5-11a63c6f43e7",
   "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>category</th>\n",
       "      <th>description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>Paper Plane Design Framed Wall Hanging Motivat...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>SAF 'Floral' Framed Painting (Wood, 30 inch x ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>SAF 'UV Textured Modern Art Print Framed' Pain...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             category                                        description\n",
       "0  __label__Household  Paper Plane Design Framed Wall Hanging Motivat...\n",
       "1  __label__Household  SAF 'Floral' Framed Painting (Wood, 30 inch x ...\n",
       "2  __label__Household  SAF 'UV Textured Modern Art Print Framed' Pain..."
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['category']= \"__label__\" + df['category'].astype(str)\n",
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5d6a8569-7072-44e2-904c-55905792c1ca",
   "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>category</th>\n",
       "      <th>description</th>\n",
       "      <th>category_description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>Paper Plane Design Framed Wall Hanging Motivat...</td>\n",
       "      <td>__label__HouseholdPaper Plane Design Framed Wa...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>SAF 'Floral' Framed Painting (Wood, 30 inch x ...</td>\n",
       "      <td>__label__HouseholdSAF 'Floral' Framed Painting...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>SAF 'UV Textured Modern Art Print Framed' Pain...</td>\n",
       "      <td>__label__HouseholdSAF 'UV Textured Modern Art ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             category                                        description  \\\n",
       "0  __label__Household  Paper Plane Design Framed Wall Hanging Motivat...   \n",
       "1  __label__Household  SAF 'Floral' Framed Painting (Wood, 30 inch x ...   \n",
       "2  __label__Household  SAF 'UV Textured Modern Art Print Framed' Pain...   \n",
       "\n",
       "                                category_description  \n",
       "0  __label__HouseholdPaper Plane Design Framed Wa...  \n",
       "1  __label__HouseholdSAF 'Floral' Framed Painting...  \n",
       "2  __label__HouseholdSAF 'UV Textured Modern Art ...  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['category_description'] = df['category'] + df['description']\n",
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ed7d5864-9a78-4051-b1db-a741dafed046",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'__label__HouseholdPaper Plane Design Framed Wall Hanging Motivational Office Decor Art Prints (8.7 X 8.7 inch) - Set of 4 Painting made up in synthetic frame with uv textured print which gives multi effects and attracts towards it. This is an special series of paintings which makes your wall very beautiful and gives a royal touch. This painting is ready to hang, you would be proud to possess this unique painting that is a niche apart. We use only the most modern and efficient printing technology on our prints, with only the and inks and precision epson, roland and hp printers. This innovative hd printing technique results in durable and spectacular looking prints of the highest that last a lifetime. We print solely with top-notch 100% inks, to achieve brilliant and true colours. Due to their high level of uv resistance, our prints retain their beautiful colours for many years. Add colour and style to your living space with this digitally printed painting. Some are for pleasure and some for eternal bliss.so bring home this elegant print that is lushed with rich colors that makes it nothing but sheer elegance to be to your friends and family.it would be treasured forever by whoever your lucky recipient is. Liven up your place with these intriguing paintings that are high definition hd graphic digital prints for home, office or any room.'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['category_description'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5a78a159-fec8-4853-9e0e-0726f8c88c63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"viki's bookcase bookshelf 3 shelf shelve white hi\""
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "text = \"  VIKI's | Bookcase/Bookshelf (3-Shelf/Shelve, White) | ? . hi\"\n",
    "text = re.sub(r'[^\\w\\s\\']',' ', text)\n",
    "text = re.sub(' +', ' ', text)\n",
    "text.strip().lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2d75eb26-e145-44a8-851b-cd65e01a4064",
   "metadata": {},
   "outputs": [],
   "source": [
    "def preprocess(text):\n",
    "    text = re.sub(r'[^\\w\\s\\']',' ', text)\n",
    "    text = re.sub(' +', ' ', text)\n",
    "    return text.strip().lower() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "045d57d6-dd19-455e-b0b6-a42e31cd9436",
   "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>category</th>\n",
       "      <th>description</th>\n",
       "      <th>category_description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>Paper Plane Design Framed Wall Hanging Motivat...</td>\n",
       "      <td>__label__householdpaper plane design framed wa...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>SAF 'Floral' Framed Painting (Wood, 30 inch x ...</td>\n",
       "      <td>__label__householdsaf 'floral' framed painting...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>SAF 'UV Textured Modern Art Print Framed' Pain...</td>\n",
       "      <td>__label__householdsaf 'uv textured modern art ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>SAF Flower Print Framed Painting (Synthetic, 1...</td>\n",
       "      <td>__label__householdsaf flower print framed pain...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>Incredible Gifts India Wooden Happy Birthday U...</td>\n",
       "      <td>__label__householdincredible gifts india woode...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             category                                        description  \\\n",
       "0  __label__Household  Paper Plane Design Framed Wall Hanging Motivat...   \n",
       "1  __label__Household  SAF 'Floral' Framed Painting (Wood, 30 inch x ...   \n",
       "2  __label__Household  SAF 'UV Textured Modern Art Print Framed' Pain...   \n",
       "3  __label__Household  SAF Flower Print Framed Painting (Synthetic, 1...   \n",
       "4  __label__Household  Incredible Gifts India Wooden Happy Birthday U...   \n",
       "\n",
       "                                category_description  \n",
       "0  __label__householdpaper plane design framed wa...  \n",
       "1  __label__householdsaf 'floral' framed painting...  \n",
       "2  __label__householdsaf 'uv textured modern art ...  \n",
       "3  __label__householdsaf flower print framed pain...  \n",
       "4  __label__householdincredible gifts india woode...  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['category_description'] = df['category_description'].map(preprocess)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d367f657-68c4-4e43-999c-45473f524989",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train, test = train_test_split(df, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5460d8ed-150a-4d60-abce-df9ac9cbb909",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((40339, 3), (10085, 3))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.shape, test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d93db7bc-0246-4b2f-b1c7-c427f44454c4",
   "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>category</th>\n",
       "      <th>description</th>\n",
       "      <th>category_description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12647</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>Pigeon 12466 1.5-Litre Electric Kettle (Multic...</td>\n",
       "      <td>__label__householdpigeon 12466 1 5 litre elect...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26989</th>\n",
       "      <td>__label__Books</td>\n",
       "      <td>The DSSSL Book: An XML/SGML Programming Language</td>\n",
       "      <td>__label__booksthe dsssl book an xml sgml progr...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25574</th>\n",
       "      <td>__label__Books</td>\n",
       "      <td>Dipa Karmakar: The Small Wonder (India's First...</td>\n",
       "      <td>__label__booksdipa karmakar the small wonder i...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17443</th>\n",
       "      <td>__label__Household</td>\n",
       "      <td>Km Self Adhesive Plastic Hooks, 7.5 X 3.5Cm, L...</td>\n",
       "      <td>__label__householdkm self adhesive plastic hoo...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42938</th>\n",
       "      <td>__label__Electronics</td>\n",
       "      <td>Lenovo L-Series LI2264d 21.5-inch FHD IPS Moni...</td>\n",
       "      <td>__label__electronicslenovo l series li2264d 21...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   category  \\\n",
       "12647    __label__Household   \n",
       "26989        __label__Books   \n",
       "25574        __label__Books   \n",
       "17443    __label__Household   \n",
       "42938  __label__Electronics   \n",
       "\n",
       "                                             description  \\\n",
       "12647  Pigeon 12466 1.5-Litre Electric Kettle (Multic...   \n",
       "26989  The DSSSL Book: An XML/SGML Programming Language    \n",
       "25574  Dipa Karmakar: The Small Wonder (India's First...   \n",
       "17443  Km Self Adhesive Plastic Hooks, 7.5 X 3.5Cm, L...   \n",
       "42938  Lenovo L-Series LI2264d 21.5-inch FHD IPS Moni...   \n",
       "\n",
       "                                    category_description  \n",
       "12647  __label__householdpigeon 12466 1 5 litre elect...  \n",
       "26989  __label__booksthe dsssl book an xml sgml progr...  \n",
       "25574  __label__booksdipa karmakar the small wonder i...  \n",
       "17443  __label__householdkm self adhesive plastic hoo...  \n",
       "42938  __label__electronicslenovo l series li2264d 21...  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d3e82a2f-0bee-4b4c-b1bf-2fc696ac0504",
   "metadata": {},
   "outputs": [],
   "source": [
    "train.to_csv(\"ecommerce.train\", columns=[\"category_description\"], index=False, header=False)\n",
    "test.to_csv(\"ecommerce.test\", columns=[\"category_description\"], index=False, header=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "034d85ef-eeea-4f2c-a70a-132dca6b3b0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9275, 0.0522911051212938, 0.0522911051212938)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import fasttext\n",
    "\n",
    "model = fasttext.train_supervised(input=\"ecommerce.train\")\n",
    "model.test(\"ecommerce.test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4d0b64e-5125-4771-95f3-c12963dba389",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.predict(\"wintech assemble desktop pc cpu 500 gb sata hdd 4 gb ram intel c2d processor 3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1ce44e55-bf70-4a6b-8e6f-4dc3602ba5a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(50425, 2)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(0.0, 'to'),\n",
       " (0.0, 'and'),\n",
       " (0.0, 'a'),\n",
       " (0.0, 'with'),\n",
       " (0.0, 'for'),\n",
       " (0.0, 'is'),\n",
       " (0.0, '</s>'),\n",
       " (0.0, 'gnomon'),\n",
       " (0.0, \"piglet's\"),\n",
       " (0.0, 'fantasia')]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df= pd.read_csv(\"ecommerce_dataset.csv\", names=[\"category\", \"description\"], header=None)\n",
    "print(df.shape)\n",
    "df.head(3)\n",
    "\n",
    "df.dropna(inplace=True)\n",
    "df.shape\n",
    "df.category.unique()\n",
    "\n",
    "df[\"category\"] = df[\"category\"].replace(\"Clothing & Accessories\", \"Clothing_Accessories\")\n",
    "\n",
    "df.category.unique()\n",
    "\n",
    "df['category'] = '__label__' + df['category'].astype(str)\n",
    "df.head(5)\n",
    "\n",
    "df['category_description'] = df['category'] + ' ' + df['description']\n",
    "df.head(3)\n",
    "\n",
    "import re\n",
    "\n",
    "text = \"  VIKI's | Bookcase/Bookshelf (3-Shelf/Shelve, White) | ? . hi\"\n",
    "text = re.sub(r'[^\\w\\s\\']',' ', text)\n",
    "text = re.sub(' +', ' ', text)\n",
    "text.strip().lower()\n",
    "\n",
    "def preprocess(text):\n",
    "    text = re.sub(r'[^\\w\\s\\']',' ', text)\n",
    "    text = re.sub(' +', ' ', text)\n",
    "    return text.strip().lower() \n",
    "\n",
    "df['category_description'] = df['category_description'].map(preprocess)\n",
    "df.head()\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train, test = train_test_split(df, test_size=0.2)\n",
    "train.shape, test.shape\n",
    "\n",
    "train.to_csv(\"ecommerce.train\", columns=[\"category_description\"], index=False, header=False)\n",
    "test.to_csv(\"ecommerce.test\", columns=[\"category_description\"], index=False, header=False)\n",
    "\n",
    "import fasttext\n",
    "\n",
    "model = fasttext.train_supervised(input=\"ecommerce.train\")\n",
    "model.test(\"ecommerce.test\")\n",
    "\n",
    "model.predict(\"wintech assemble desktop pc cpu 500 gb sata hdd 4 gb ram intel c2d processor 3\")\n",
    "model.predict(\"ockey men's cotton t shirt fabric details 80 cotton 20 polyester super combed cotton rich fabric\")\n",
    "model.predict(\"think and grow rich deluxe edition\")\n",
    "\n",
    "model.get_nearest_neighbors(\"painting\")\n",
    "model.get_nearest_neighbors(\"sony\")\n",
    "model.get_nearest_neighbors(\"banglore\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b1ae628-f0b4-4bfc-bee2-5d7f286660cf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}