File size: 3,769 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 | {
"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
}
|