{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "fad722b3-d528-45d0-b3b3-4f829a4f51f6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(50425, 2)\n" ] } ], "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" ] }, { "cell_type": "code", "execution_count": null, "id": "f975e1e4-7857-49f8-8945-897e404ea7d2", "metadata": {}, "outputs": [], "source": [ "import fasttext\n", "\n", "\n", "model = fasttext.train_supervised(\n", " input=\"ecommerce.train\",\n", " epoch=25, # more training iterations\n", " lr=1.0, # learning rate\n", " wordNgrams=2, # include word n-grams\n", " minn=2, # enable subword info (handles typos/unseen words)\n", " maxn=5,\n", " minCount=1 # keep even rare words\n", ")\n", "\n", "\n", "print(model.test(\"ecommerce.test\"))\n", "\n", "# Predictions\n", "print(model.predict(\"wintech assemble desktop pc cpu 500 gb sata hdd 4 gb ram intel c2d processor 3\"))\n", "print(model.predict(\"ockey men's cotton t shirt fabric details 80 cotton 20 polyester super combed cotton rich fabric\"))\n", "print(model.predict(\"think and grow rich deluxe edition\"))\n", "\n", "\n", "print(model.get_nearest_neighbors(\"painting\"))\n", "print(model.get_nearest_neighbors(\"sony\"))\n", "print(model.get_nearest_neighbors(\"bangalore\")) \n", "print(model.get_nearest_neighbors(\"banglore\")) \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "dc0f66d8-0a4a-4d1a-a36f-fb8e0af1c7ea", "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 }