Packaged-Food-Explorer/Essential/networkgraphprob.ipynb
2023-05-12 20:14:23 +07:00

2042 lines
68 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### What are the vertices? <br />\n",
"ANS : V = product_name (food product) <br />\n",
"### What are the edges? Are they directed or undirected? <br />\n",
"ANS : E = Undirected (Relation of similarities between food)<br />\n",
"### Do the vertices and/or edges have associated values (“weights”, “distances”, etc.) and if so, what are <br />\n",
"ANS : Weights = Simlarities scores<br />"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Network Graph Problem"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read Data and create similarites score dataframe"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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>product_name</th>\n",
" <th>Nitroglycerin</th>\n",
" <th>Cheese twist</th>\n",
" <th>Pepperidge farm cookies</th>\n",
" <th>Asian home gourmet, spice paste for indian butter chicken, mild</th>\n",
" <th>Crunchy Granola Bars</th>\n",
" <th>Nature Valley Crunchy Oats 'n Dark Chocolate Granola Bar</th>\n",
" <th>Iced Party Rings</th>\n",
" <th>Golden crunchy creams</th>\n",
" <th>Guacamole Dip</th>\n",
" <th>Coconut oil</th>\n",
" <th>...</th>\n",
" <th>คุกกี้สเปลท์เนยสดผสมข้าวกล้องงอก</th>\n",
" <th>กระเทียบปลอดเปลือง</th>\n",
" <th>มะนาว</th>\n",
" <th>มะเขือเทศราชินี</th>\n",
" <th>กรีนโอ๊ค</th>\n",
" <th>ฟิลเลย์ไอช์เบิร์ท</th>\n",
" <th>ส้มสายน้ำผึ้ง</th>\n",
" <th>lactasoy</th>\n",
" <th>ท๊อฟฟี่เค้ก</th>\n",
" <th>Bavarian bread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>product_name</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Nitroglycerin</th>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Cheese twist</th>\n",
" <td>0.0</td>\n",
" <td>1.000000</td>\n",
" <td>0.994018</td>\n",
" <td>0.995840</td>\n",
" <td>0.996531</td>\n",
" <td>0.959094</td>\n",
" <td>0.991335</td>\n",
" <td>0.996205</td>\n",
" <td>0.0</td>\n",
" <td>0.991655</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.997557</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Pepperidge farm cookies</th>\n",
" <td>0.0</td>\n",
" <td>0.994018</td>\n",
" <td>1.000000</td>\n",
" <td>0.988383</td>\n",
" <td>0.999200</td>\n",
" <td>0.979432</td>\n",
" <td>0.999649</td>\n",
" <td>0.999609</td>\n",
" <td>0.0</td>\n",
" <td>0.977958</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.997638</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Asian home gourmet, spice paste for indian butter chicken, mild</th>\n",
" <td>0.0</td>\n",
" <td>0.995840</td>\n",
" <td>0.988383</td>\n",
" <td>1.000000</td>\n",
" <td>0.991282</td>\n",
" <td>0.944871</td>\n",
" <td>0.985161</td>\n",
" <td>0.991537</td>\n",
" <td>0.0</td>\n",
" <td>0.991367</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.995406</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Crunchy Granola Bars</th>\n",
" <td>0.0</td>\n",
" <td>0.996531</td>\n",
" <td>0.999200</td>\n",
" <td>0.991282</td>\n",
" <td>1.000000</td>\n",
" <td>0.975556</td>\n",
" <td>0.998478</td>\n",
" <td>0.999325</td>\n",
" <td>0.0</td>\n",
" <td>0.980052</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.998650</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ฟิลเลย์ไอช์เบิร์ท</th>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ส้มสายน้ำผึ้ง</th>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>lactasoy</th>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ท๊อฟฟี่เค้ก</th>\n",
" <td>0.0</td>\n",
" <td>0.997557</td>\n",
" <td>0.997638</td>\n",
" <td>0.995406</td>\n",
" <td>0.998650</td>\n",
" <td>0.967932</td>\n",
" <td>0.996187</td>\n",
" <td>0.998944</td>\n",
" <td>0.0</td>\n",
" <td>0.986198</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>1.000000</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Bavarian bread</th>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>100 rows × 100 columns</p>\n",
"</div>"
],
"text/plain": [
"product_name Nitroglycerin \\\n",
"product_name \n",
"Nitroglycerin 0.0 \n",
"Cheese twist 0.0 \n",
"Pepperidge farm cookies 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 \n",
"Crunchy Granola Bars 0.0 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 \n",
"lactasoy 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 \n",
"Bavarian bread 0.0 \n",
"\n",
"product_name Cheese twist \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 1.000000 \n",
"Pepperidge farm cookies 0.994018 \n",
"Asian home gourmet, spice paste for indian butt... 0.995840 \n",
"Crunchy Granola Bars 0.996531 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 0.997557 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Pepperidge farm cookies \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 0.994018 \n",
"Pepperidge farm cookies 1.000000 \n",
"Asian home gourmet, spice paste for indian butt... 0.988383 \n",
"Crunchy Granola Bars 0.999200 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 0.997638 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Asian home gourmet, spice paste for indian butter chicken, mild \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 0.995840 \n",
"Pepperidge farm cookies 0.988383 \n",
"Asian home gourmet, spice paste for indian butt... 1.000000 \n",
"Crunchy Granola Bars 0.991282 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 0.995406 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Crunchy Granola Bars \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 0.996531 \n",
"Pepperidge farm cookies 0.999200 \n",
"Asian home gourmet, spice paste for indian butt... 0.991282 \n",
"Crunchy Granola Bars 1.000000 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 0.998650 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Nature Valley Crunchy Oats 'n Dark Chocolate Granola Bar \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 0.959094 \n",
"Pepperidge farm cookies 0.979432 \n",
"Asian home gourmet, spice paste for indian butt... 0.944871 \n",
"Crunchy Granola Bars 0.975556 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 0.967932 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Iced Party Rings \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 0.991335 \n",
"Pepperidge farm cookies 0.999649 \n",
"Asian home gourmet, spice paste for indian butt... 0.985161 \n",
"Crunchy Granola Bars 0.998478 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 0.996187 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Golden crunchy creams \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 0.996205 \n",
"Pepperidge farm cookies 0.999609 \n",
"Asian home gourmet, spice paste for indian butt... 0.991537 \n",
"Crunchy Granola Bars 0.999325 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 0.998944 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Guacamole Dip \\\n",
"product_name \n",
"Nitroglycerin 0.0 \n",
"Cheese twist 0.0 \n",
"Pepperidge farm cookies 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 \n",
"Crunchy Granola Bars 0.0 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 \n",
"lactasoy 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 \n",
"Bavarian bread 0.0 \n",
"\n",
"product_name Coconut oil ... \\\n",
"product_name ... \n",
"Nitroglycerin 0.000000 ... \n",
"Cheese twist 0.991655 ... \n",
"Pepperidge farm cookies 0.977958 ... \n",
"Asian home gourmet, spice paste for indian butt... 0.991367 ... \n",
"Crunchy Granola Bars 0.980052 ... \n",
"... ... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 ... \n",
"ส้มสายน้ำผึ้ง 0.000000 ... \n",
"lactasoy 0.000000 ... \n",
"ท๊อฟฟี่เค้ก 0.986198 ... \n",
"Bavarian bread 0.000000 ... \n",
"\n",
"product_name คุกกี้สเปลท์เนยสดผสมข้าวกล้องงอก \\\n",
"product_name \n",
"Nitroglycerin 0.0 \n",
"Cheese twist 0.0 \n",
"Pepperidge farm cookies 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 \n",
"Crunchy Granola Bars 0.0 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 \n",
"lactasoy 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 \n",
"Bavarian bread 0.0 \n",
"\n",
"product_name กระเทียบปลอดเปลือง มะนาว \\\n",
"product_name \n",
"Nitroglycerin 0.0 0.0 \n",
"Cheese twist 0.0 0.0 \n",
"Pepperidge farm cookies 0.0 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 0.0 \n",
"Crunchy Granola Bars 0.0 0.0 \n",
"... ... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 0.0 \n",
"lactasoy 0.0 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 0.0 \n",
"Bavarian bread 0.0 0.0 \n",
"\n",
"product_name มะเขือเทศราชินี กรีนโอ๊ค \\\n",
"product_name \n",
"Nitroglycerin 0.0 0.0 \n",
"Cheese twist 0.0 0.0 \n",
"Pepperidge farm cookies 0.0 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 0.0 \n",
"Crunchy Granola Bars 0.0 0.0 \n",
"... ... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 0.0 \n",
"lactasoy 0.0 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 0.0 \n",
"Bavarian bread 0.0 0.0 \n",
"\n",
"product_name ฟิลเลย์ไอช์เบิร์ท \\\n",
"product_name \n",
"Nitroglycerin 0.0 \n",
"Cheese twist 0.0 \n",
"Pepperidge farm cookies 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 \n",
"Crunchy Granola Bars 0.0 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 \n",
"lactasoy 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 \n",
"Bavarian bread 0.0 \n",
"\n",
"product_name ส้มสายน้ำผึ้ง lactasoy \\\n",
"product_name \n",
"Nitroglycerin 0.0 0.0 \n",
"Cheese twist 0.0 0.0 \n",
"Pepperidge farm cookies 0.0 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 0.0 \n",
"Crunchy Granola Bars 0.0 0.0 \n",
"... ... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 0.0 \n",
"lactasoy 0.0 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 0.0 \n",
"Bavarian bread 0.0 0.0 \n",
"\n",
"product_name ท๊อฟฟี่เค้ก \\\n",
"product_name \n",
"Nitroglycerin 0.000000 \n",
"Cheese twist 0.997557 \n",
"Pepperidge farm cookies 0.997638 \n",
"Asian home gourmet, spice paste for indian butt... 0.995406 \n",
"Crunchy Granola Bars 0.998650 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.000000 \n",
"ส้มสายน้ำผึ้ง 0.000000 \n",
"lactasoy 0.000000 \n",
"ท๊อฟฟี่เค้ก 1.000000 \n",
"Bavarian bread 0.000000 \n",
"\n",
"product_name Bavarian bread \n",
"product_name \n",
"Nitroglycerin 0.0 \n",
"Cheese twist 0.0 \n",
"Pepperidge farm cookies 0.0 \n",
"Asian home gourmet, spice paste for indian butt... 0.0 \n",
"Crunchy Granola Bars 0.0 \n",
"... ... \n",
"ฟิลเลย์ไอช์เบิร์ท 0.0 \n",
"ส้มสายน้ำผึ้ง 0.0 \n",
"lactasoy 0.0 \n",
"ท๊อฟฟี่เค้ก 0.0 \n",
"Bavarian bread 0.0 \n",
"\n",
"[100 rows x 100 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sqlite3\n",
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.metrics.pairwise import cosine_similarity\n",
"import networkx as nx\n",
"import plotly.graph_objects as go\n",
"import os.path\n",
"\n",
"current_dir = os.path.abspath('')\n",
"db_path = (current_dir + r\"\\data\\food_data.db\")\n",
"\n",
"conn = sqlite3.connect(db_path)\n",
"query = \"SELECT * FROM food_data LIMIT 100\"\n",
"df = pd.read_sql_query(query, conn)\n",
"\n",
"df= df.fillna(0)\n",
"\n",
"nutrient_columns = [\n",
" 'energy-kcal_100g', 'fat_100g', 'saturated-fat_100g', 'unsaturated-fat_100g',\n",
" 'omega-3-fat_100g', 'omega-6-fat_100g', 'omega-9-fat_100g', 'trans-fat_100g',\n",
" 'cholesterol_100g', 'carbohydrates_100g', 'sugars_100g', 'sucrose_100g',\n",
" 'glucose_100g', 'fructose_100g', 'lactose_100g', 'maltose_100g', 'fiber_100g',\n",
" 'soluble-fiber_100g', 'insoluble-fiber_100g', 'proteins_100g', 'salt_100g',\n",
" 'added-salt_100g', 'sodium_100g', 'alcohol_100g', 'vitamin-a_100g',\n",
" 'beta-carotene_100g', 'vitamin-d_100g', 'vitamin-e_100g', 'vitamin-k_100g',\n",
" 'vitamin-c_100g', 'vitamin-b1_100g', 'vitamin-b2_100g', 'vitamin-pp_100g',\n",
" 'vitamin-b6_100g', 'vitamin-b9_100g', 'vitamin-b12_100g', 'bicarbonate_100g',\n",
" 'potassium_100g', 'chloride_100g', 'calcium_100g', 'phosphorus_100g', 'iron_100g',\n",
" 'magnesium_100g', 'zinc_100g', 'copper_100g', 'manganese_100g', 'fluoride_100g',\n",
" 'selenium_100g', 'chromium_100g', 'molybdenum_100g', 'iodine_100g',\n",
" 'caffeine_100g', 'cocoa_100g', 'carbon-footprint_100g'\n",
"]\n",
"\n",
"conn.close()\n",
"chunk_size = 1000 \n",
"\n",
"similarity_df = pd.DataFrame(index=df['product_name'], columns=df['product_name'])\n",
"for i in range(0, len(df), chunk_size):\n",
" chunk = df.iloc[i:i+chunk_size]\n",
" chunk_similarity_matrix = cosine_similarity(chunk[nutrient_columns])\n",
"\n",
" chunk_similarity_df = pd.DataFrame(chunk_similarity_matrix, index=chunk['product_name'], columns=chunk['product_name'])\n",
" similarity_df.update(chunk_similarity_df)\n",
"similarity_df = similarity_df.fillna(1.0)\n",
"\n",
"similarity_df"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Find similarities and put to weight of edges"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hoverinfo": "none",
"line": {
"color": "#888",
"width": 0.5
},
"mode": "lines",
"type": "scatter",
"x": [
-0.39281764944969905,
-0.39281764944969905,
null,
-0.39281764944969905,
0.37781992243081575,
null,
-0.39281764944969905,
0.3689288980818013,
null,
-0.39281764944969905,
0.842123690643775,
null,
-0.39281764944969905,
0.9999999999999999,
null,
-0.39281764944969905,
-0.9357631325130329,
null,
-0.39281764944969905,
-0.26035419108680397,
null,
-0.39281764944969905,
-0.9008296338905416,
null,
-0.39281764944969905,
0.26924361061589375,
null,
-0.39281764944969905,
-0.36835151483220746,
null,
-0.9008296338905416,
-0.9008296338905416,
null,
-0.9008296338905416,
0.37781992243081575,
null,
-0.9008296338905416,
0.3689288980818013,
null,
-0.9008296338905416,
0.842123690643775,
null,
-0.9008296338905416,
0.9999999999999999,
null,
-0.9008296338905416,
-0.9357631325130329,
null,
-0.9008296338905416,
-0.26035419108680397,
null,
-0.9008296338905416,
0.26924361061589375,
null,
-0.9008296338905416,
-0.36835151483220746,
null,
-0.26035419108680397,
-0.26035419108680397,
null,
-0.26035419108680397,
0.37781992243081575,
null,
-0.26035419108680397,
0.3689288980818013,
null,
-0.26035419108680397,
0.842123690643775,
null,
-0.26035419108680397,
0.9999999999999999,
null,
-0.26035419108680397,
-0.9357631325130329,
null,
-0.26035419108680397,
0.26924361061589375,
null,
-0.26035419108680397,
-0.36835151483220746,
null,
0.842123690643775,
0.842123690643775,
null,
0.842123690643775,
0.37781992243081575,
null,
0.842123690643775,
0.3689288980818013,
null,
0.842123690643775,
0.9999999999999999,
null,
0.842123690643775,
-0.9357631325130329,
null,
0.842123690643775,
0.26924361061589375,
null,
0.842123690643775,
-0.36835151483220746,
null,
0.37781992243081575,
0.37781992243081575,
null,
0.37781992243081575,
0.3689288980818013,
null,
0.37781992243081575,
0.9999999999999999,
null,
0.37781992243081575,
-0.9357631325130329,
null,
0.37781992243081575,
0.26924361061589375,
null,
0.37781992243081575,
-0.36835151483220746,
null,
0.9999999999999999,
0.9999999999999999,
null,
0.9999999999999999,
0.3689288980818013,
null,
0.9999999999999999,
-0.9357631325130329,
null,
0.9999999999999999,
0.26924361061589375,
null,
0.9999999999999999,
-0.36835151483220746,
null,
-0.36835151483220746,
-0.36835151483220746,
null,
-0.36835151483220746,
0.3689288980818013,
null,
-0.36835151483220746,
-0.9357631325130329,
null,
-0.36835151483220746,
0.26924361061589375,
null,
-0.9357631325130329,
-0.9357631325130329,
null,
-0.9357631325130329,
0.3689288980818013,
null,
-0.9357631325130329,
0.26924361061589375,
null,
0.26924361061589375,
0.26924361061589375,
null,
0.26924361061589375,
0.3689288980818013,
null,
0.3689288980818013,
0.3689288980818013,
null
],
"y": [
0.920031615743509,
0.920031615743509,
null,
0.920031615743509,
-0.2737264666080779,
null,
0.920031615743509,
-0.9423025595495664,
null,
0.920031615743509,
0.5282427548746664,
null,
0.920031615743509,
-0.22100977035070463,
null,
0.920031615743509,
0.37444909049334674,
null,
0.920031615743509,
0.08905504684099934,
null,
0.920031615743509,
-0.4087997708596114,
null,
0.920031615743509,
0.8251310355543052,
null,
0.920031615743509,
-0.8910709761388675,
null,
-0.4087997708596114,
-0.4087997708596114,
null,
-0.4087997708596114,
-0.2737264666080779,
null,
-0.4087997708596114,
-0.9423025595495664,
null,
-0.4087997708596114,
0.5282427548746664,
null,
-0.4087997708596114,
-0.22100977035070463,
null,
-0.4087997708596114,
0.37444909049334674,
null,
-0.4087997708596114,
0.08905504684099934,
null,
-0.4087997708596114,
0.8251310355543052,
null,
-0.4087997708596114,
-0.8910709761388675,
null,
0.08905504684099934,
0.08905504684099934,
null,
0.08905504684099934,
-0.2737264666080779,
null,
0.08905504684099934,
-0.9423025595495664,
null,
0.08905504684099934,
0.5282427548746664,
null,
0.08905504684099934,
-0.22100977035070463,
null,
0.08905504684099934,
0.37444909049334674,
null,
0.08905504684099934,
0.8251310355543052,
null,
0.08905504684099934,
-0.8910709761388675,
null,
0.5282427548746664,
0.5282427548746664,
null,
0.5282427548746664,
-0.2737264666080779,
null,
0.5282427548746664,
-0.9423025595495664,
null,
0.5282427548746664,
-0.22100977035070463,
null,
0.5282427548746664,
0.37444909049334674,
null,
0.5282427548746664,
0.8251310355543052,
null,
0.5282427548746664,
-0.8910709761388675,
null,
-0.2737264666080779,
-0.2737264666080779,
null,
-0.2737264666080779,
-0.9423025595495664,
null,
-0.2737264666080779,
-0.22100977035070463,
null,
-0.2737264666080779,
0.37444909049334674,
null,
-0.2737264666080779,
0.8251310355543052,
null,
-0.2737264666080779,
-0.8910709761388675,
null,
-0.22100977035070463,
-0.22100977035070463,
null,
-0.22100977035070463,
-0.9423025595495664,
null,
-0.22100977035070463,
0.37444909049334674,
null,
-0.22100977035070463,
0.8251310355543052,
null,
-0.22100977035070463,
-0.8910709761388675,
null,
-0.8910709761388675,
-0.8910709761388675,
null,
-0.8910709761388675,
-0.9423025595495664,
null,
-0.8910709761388675,
0.37444909049334674,
null,
-0.8910709761388675,
0.8251310355543052,
null,
0.37444909049334674,
0.37444909049334674,
null,
0.37444909049334674,
-0.9423025595495664,
null,
0.37444909049334674,
0.8251310355543052,
null,
0.8251310355543052,
0.8251310355543052,
null,
0.8251310355543052,
-0.9423025595495664,
null,
-0.9423025595495664,
-0.9423025595495664,
null
]
},
{
"hoverinfo": "text",
"marker": {
"color": "rgb(150,150,150)",
"line": {
"color": "rgb(255,255,255)",
"width": 2
},
"showscale": false,
"size": 10
},
"mode": "markers+text",
"text": [
"2 percent reduced fat milk",
"Golden crunchy creams",
"Iced Party Rings",
"Asian home gourmet, spice paste for indian butter chicken, mild",
"Cheese twist",
"Crunchy Granola Bars",
"ผลิตภัณฑ์นมพาสเจอไรซ์ - 946 ml",
"Nature Valley Crunchy Oats 'n Dark Chocolate Granola Bar",
"all butter Scottish shortbread biscuits with toffee pieces",
"Pepperidge farm cookies"
],
"textposition": "top center",
"type": "scatter",
"x": [
-0.39281764944969905,
-0.9008296338905416,
-0.26035419108680397,
0.842123690643775,
0.37781992243081575,
0.9999999999999999,
-0.36835151483220746,
-0.9357631325130329,
0.26924361061589375,
0.3689288980818013
],
"y": [
0.920031615743509,
-0.4087997708596114,
0.08905504684099934,
0.5282427548746664,
-0.2737264666080779,
-0.22100977035070463,
-0.8910709761388675,
0.37444909049334674,
0.8251310355543052,
-0.9423025595495664
]
}
],
"layout": {
"hovermode": "closest",
"margin": {
"b": 20,
"l": 5,
"r": 5,
"t": 40
},
"showlegend": false,
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"xaxis": {
"showgrid": false,
"showticklabels": false,
"zeroline": false
},
"yaxis": {
"showgrid": false,
"showticklabels": false,
"zeroline": false
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# * Similarities calculate by cosine similarities\n",
"\n",
"graph = nx.from_pandas_adjacency(similarity_df)\n",
"node_degrees = graph.degree()\n",
"sorted_nodes = sorted(node_degrees, key=lambda x: x[1], reverse=True)\n",
"top_nodes = [node[0] for node in sorted_nodes[:10]]\n",
"subgraph = graph.subgraph(top_nodes)\n",
"pos = nx.spring_layout(subgraph)\n",
"x, y = zip(*pos.values())\n",
"edge_trace = go.Scatter(\n",
" x=[],\n",
" y=[],\n",
" line=dict(width=0.5, color='#888'),\n",
" hoverinfo='none',\n",
" mode='lines')\n",
"\n",
"for edge in subgraph.edges():\n",
" x0, y0 = pos[edge[0]]\n",
" x1, y1 = pos[edge[1]]\n",
" edge_trace['x'] += tuple([x0, x1, None])\n",
" edge_trace['y'] += tuple([y0, y1, None])\n",
"\n",
"node_trace = go.Scatter(\n",
" x=x,\n",
" y=y,\n",
" mode='markers+text',\n",
" hoverinfo='text',\n",
" text=list(subgraph.nodes()),\n",
" textposition='top center',\n",
" marker=dict(\n",
" showscale=False,\n",
" color='rgb(150,150,150)',\n",
" size=10,\n",
" line=dict(width=2, color='rgb(255,255,255)')))\n",
"\n",
"layout = go.Layout(\n",
" showlegend=False,\n",
" hovermode='closest',\n",
" margin=dict(b=20, l=5, r=5, t=40),\n",
" xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
" yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))\n",
"\n",
"fig = go.Figure(data=[edge_trace, node_trace], layout=layout)\n",
"fig.show()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Find Shortest path from one food to other food where similarities score is reverse (low similarities score = high similarity)\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Cheese twist', 'Scottish All Butter Shortbread Assortment', 'all butter Scottish shortbread biscuits with toffee pieces', 'Cookies stem ginger', 'Jam Sandwich Creams', 'Golden crunchy creams', 'Pepperidge farm cookies']\n"
]
}
],
"source": [
"# ! Shortest Path\n",
"\n",
"G = nx.Graph()\n",
"G.add_nodes_from(df['product_name'])\n",
"\n",
"# ! Add edges to the graph with weight as the modified similarity score(Reverse similarities score)\n",
"for i in range(len(similarity_df)):\n",
" for j in range(i+1, len(similarity_df)):\n",
" if similarity_df.iloc[i,j] > 0:\n",
" similarity_score = similarity_df.iloc[i,j]\n",
" similarity_weight = 1 - similarity_score\n",
" G.add_edge(similarity_df.index[i], similarity_df.columns[j], weight=similarity_weight)\n",
"\n",
"# * Example\n",
"start_node = 'Cheese twist'\n",
"end_node = 'Pepperidge farm cookies'\n",
"shortest_path = nx.shortest_path(G, start_node, end_node, weight='weight')\n",
"\n",
"print(shortest_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}