From f6bae9c70a77aea8f6f219e2678b822791f8bf98 Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Fri, 12 May 2023 19:45:24 +0700 Subject: [PATCH] Change file format of network graph file --- Essential/networkgraphprob.ipynb | 2028 ++++++++++++++++++++++++++++++ Essential/networkgraphvisual.py | 110 -- 2 files changed, 2028 insertions(+), 110 deletions(-) create mode 100644 Essential/networkgraphprob.ipynb delete mode 100644 Essential/networkgraphvisual.py diff --git a/Essential/networkgraphprob.ipynb b/Essential/networkgraphprob.ipynb new file mode 100644 index 0000000..cf832de --- /dev/null +++ b/Essential/networkgraphprob.ipynb @@ -0,0 +1,2028 @@ +{ + "cells": [ + { + "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": [ + "
\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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
product_nameNitroglycerinCheese twistPepperidge farm cookiesAsian home gourmet, spice paste for indian butter chicken, mildCrunchy Granola BarsNature Valley Crunchy Oats 'n Dark Chocolate Granola BarIced Party RingsGolden crunchy creamsGuacamole DipCoconut oil...คุกกี้สเปลท์เนยสดผสมข้าวกล้องงอกกระเทียบปลอดเปลืองมะนาวมะเขือเทศราชินีกรีนโอ๊คฟิลเลย์ไอช์เบิร์ทส้มสายน้ำผึ้งlactasoyท๊อฟฟี่เค้กBavarian bread
product_name
Nitroglycerin0.00.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00.000000...0.00.00.00.00.00.00.00.00.0000000.0
Cheese twist0.01.0000000.9940180.9958400.9965310.9590940.9913350.9962050.00.991655...0.00.00.00.00.00.00.00.00.9975570.0
Pepperidge farm cookies0.00.9940181.0000000.9883830.9992000.9794320.9996490.9996090.00.977958...0.00.00.00.00.00.00.00.00.9976380.0
Asian home gourmet, spice paste for indian butter chicken, mild0.00.9958400.9883831.0000000.9912820.9448710.9851610.9915370.00.991367...0.00.00.00.00.00.00.00.00.9954060.0
Crunchy Granola Bars0.00.9965310.9992000.9912821.0000000.9755560.9984780.9993250.00.980052...0.00.00.00.00.00.00.00.00.9986500.0
..................................................................
ฟิลเลย์ไอช์เบิร์ท0.00.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00.000000...0.00.00.00.00.00.00.00.00.0000000.0
ส้มสายน้ำผึ้ง0.00.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00.000000...0.00.00.00.00.00.00.00.00.0000000.0
lactasoy0.00.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00.000000...0.00.00.00.00.00.00.00.00.0000000.0
ท๊อฟฟี่เค้ก0.00.9975570.9976380.9954060.9986500.9679320.9961870.9989440.00.986198...0.00.00.00.00.00.00.00.01.0000000.0
Bavarian bread0.00.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00.000000...0.00.00.00.00.00.00.00.00.0000000.0
\n", + "

100 rows × 100 columns

\n", + "
" + ], + "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 +} diff --git a/Essential/networkgraphvisual.py b/Essential/networkgraphvisual.py deleted file mode 100644 index 9d986be..0000000 --- a/Essential/networkgraphvisual.py +++ /dev/null @@ -1,110 +0,0 @@ -import sqlite3 -import pandas as pd -import numpy as np -from sklearn.metrics.pairwise import cosine_similarity -import networkx as nx -import plotly.graph_objects as go -import os.path - -current_dir = os.path.dirname(os.path.abspath(__file__)) -db_path = (current_dir + r"\data\food_data.db") - -conn = sqlite3.connect(db_path) -query = "SELECT * FROM food_data LIMIT 100" -df = pd.read_sql_query(query, conn) - -df= df.fillna(0) - -nutrient_columns = [ - 'energy-kcal_100g', 'fat_100g', 'saturated-fat_100g', 'unsaturated-fat_100g', - 'omega-3-fat_100g', 'omega-6-fat_100g', 'omega-9-fat_100g', 'trans-fat_100g', - 'cholesterol_100g', 'carbohydrates_100g', 'sugars_100g', 'sucrose_100g', - 'glucose_100g', 'fructose_100g', 'lactose_100g', 'maltose_100g', 'fiber_100g', - 'soluble-fiber_100g', 'insoluble-fiber_100g', 'proteins_100g', 'salt_100g', - 'added-salt_100g', 'sodium_100g', 'alcohol_100g', 'vitamin-a_100g', - 'beta-carotene_100g', 'vitamin-d_100g', 'vitamin-e_100g', 'vitamin-k_100g', - 'vitamin-c_100g', 'vitamin-b1_100g', 'vitamin-b2_100g', 'vitamin-pp_100g', - 'vitamin-b6_100g', 'vitamin-b9_100g', 'vitamin-b12_100g', 'bicarbonate_100g', - 'potassium_100g', 'chloride_100g', 'calcium_100g', 'phosphorus_100g', 'iron_100g', - 'magnesium_100g', 'zinc_100g', 'copper_100g', 'manganese_100g', 'fluoride_100g', - 'selenium_100g', 'chromium_100g', 'molybdenum_100g', 'iodine_100g', - 'caffeine_100g', 'cocoa_100g', 'carbon-footprint_100g' -] - -conn.close() -chunk_size = 1000 - -similarity_df = pd.DataFrame(index=df['product_name'], columns=df['product_name']) -for i in range(0, len(df), chunk_size): - chunk = df.iloc[i:i+chunk_size] - chunk_similarity_matrix = cosine_similarity(chunk[nutrient_columns]) - - chunk_similarity_df = pd.DataFrame(chunk_similarity_matrix, index=chunk['product_name'], columns=chunk['product_name']) - similarity_df.update(chunk_similarity_df) -similarity_df = similarity_df.fillna(1.0) - -# * Similarities calculate by cosine similarities - -graph = nx.from_pandas_adjacency(similarity_df) -node_degrees = graph.degree() -sorted_nodes = sorted(node_degrees, key=lambda x: x[1], reverse=True) -top_nodes = [node[0] for node in sorted_nodes[:10]] -subgraph = graph.subgraph(top_nodes) -pos = nx.spring_layout(subgraph) -x, y = zip(*pos.values()) -edge_trace = go.Scatter( - x=[], - y=[], - line=dict(width=0.5, color='#888'), - hoverinfo='none', - mode='lines') - -for edge in subgraph.edges(): - x0, y0 = pos[edge[0]] - x1, y1 = pos[edge[1]] - edge_trace['x'] += tuple([x0, x1, None]) - edge_trace['y'] += tuple([y0, y1, None]) - -node_trace = go.Scatter( - x=x, - y=y, - mode='markers+text', - hoverinfo='text', - text=list(subgraph.nodes()), - textposition='top center', - marker=dict( - showscale=False, - color='rgb(150,150,150)', - size=10, - line=dict(width=2, color='rgb(255,255,255)'))) - -layout = go.Layout( - showlegend=False, - hovermode='closest', - margin=dict(b=20, l=5, r=5, t=40), - xaxis=dict(showgrid=False, zeroline=False, showticklabels=False), - yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)) - -fig = go.Figure(data=[edge_trace, node_trace], layout=layout) -fig.show() - - -# ! Shortest Path - -G = nx.Graph() -G.add_nodes_from(df['product_name']) - -# ! Add edges to the graph with weight as the modified similarity score(Reverse similarities score) -for i in range(len(similarity_df)): - for j in range(i+1, len(similarity_df)): - if similarity_df.iloc[i,j] > 0: - similarity_score = similarity_df.iloc[i,j] - similarity_weight = 1 - similarity_score - G.add_edge(similarity_df.index[i], similarity_df.columns[j], weight=similarity_weight) - -# * Example -start_node = 'Cheese twist' -end_node = 'Pepperidge farm cookies' -shortest_path = nx.shortest_path(G, start_node, end_node, weight='weight') - -print(shortest_path) \ No newline at end of file