{ "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": [ "
\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", "
categorydescription
0HouseholdPaper Plane Design Framed Wall Hanging Motivat...
1HouseholdSAF 'Floral' Framed Painting (Wood, 30 inch x ...
2HouseholdSAF 'UV Textured Modern Art Print Framed' Pain...
\n", "
" ], "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": [ "
\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", "
categorydescription
0__label__HouseholdPaper Plane Design Framed Wall Hanging Motivat...
1__label__HouseholdSAF 'Floral' Framed Painting (Wood, 30 inch x ...
2__label__HouseholdSAF 'UV Textured Modern Art Print Framed' Pain...
\n", "
" ], "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": [ "
\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", "
categorydescriptioncategory_description
0__label__HouseholdPaper Plane Design Framed Wall Hanging Motivat...__label__HouseholdPaper Plane Design Framed Wa...
1__label__HouseholdSAF 'Floral' Framed Painting (Wood, 30 inch x ...__label__HouseholdSAF 'Floral' Framed Painting...
2__label__HouseholdSAF 'UV Textured Modern Art Print Framed' Pain...__label__HouseholdSAF 'UV Textured Modern Art ...
\n", "
" ], "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": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", "
categorydescriptioncategory_description
0__label__HouseholdPaper Plane Design Framed Wall Hanging Motivat...__label__householdpaper plane design framed wa...
1__label__HouseholdSAF 'Floral' Framed Painting (Wood, 30 inch x ...__label__householdsaf 'floral' framed painting...
2__label__HouseholdSAF 'UV Textured Modern Art Print Framed' Pain...__label__householdsaf 'uv textured modern art ...
3__label__HouseholdSAF Flower Print Framed Painting (Synthetic, 1...__label__householdsaf flower print framed pain...
4__label__HouseholdIncredible Gifts India Wooden Happy Birthday U...__label__householdincredible gifts india woode...
\n", "
" ], "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": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", "
categorydescriptioncategory_description
12647__label__HouseholdPigeon 12466 1.5-Litre Electric Kettle (Multic...__label__householdpigeon 12466 1 5 litre elect...
26989__label__BooksThe DSSSL Book: An XML/SGML Programming Language__label__booksthe dsssl book an xml sgml progr...
25574__label__BooksDipa Karmakar: The Small Wonder (India's First...__label__booksdipa karmakar the small wonder i...
17443__label__HouseholdKm Self Adhesive Plastic Hooks, 7.5 X 3.5Cm, L...__label__householdkm self adhesive plastic hoo...
42938__label__ElectronicsLenovo L-Series LI2264d 21.5-inch FHD IPS Moni...__label__electronicslenovo l series li2264d 21...
\n", "
" ], "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, ''),\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 }