{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyMJAMTJ2UxcPpBAc/ntokzp", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "iU_l3SQ9A65p" }, "outputs": [], "source": [] }, { "source": [ "#23_MarketBasketAnalysisusingAPIRIORI\n", "### Importing the basic libraries\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "### Load Dataset from Local Directory\n", "from google.colab import files\n", "uploaded = files.upload()\n", "### Importing the dataset\n", "dataset = pd.read_csv('dataset.csv')\n", "print(dataset.shape)\n", "print(dataset.head(5))\n", "### Data Pre-Processing using vectorized operations\n", "# Extract the relevant columns (assuming first 20)\n", "transactions_df = dataset.iloc[:, :20]\n", "# Convert all values to strings\n", "transactions_df = transactions_df.astype(str)\n", "# Convert DataFrame to list of lists\n", "transactions = transactions_df.values.tolist()\n", "### Training APRIORI\n", "!pip install apyori\n", "from apyori import apriori\n", "rules = apriori(transactions = transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2, max_length = 2)\n", "### Result\n", "results = list(rules)\n", "results\n", "### Results in Dataframe\n", "lhs = [tuple(result[2][0][0])[0] for result in results]\n", "rhs = [tuple(result[2][0][1])[0] for result in results]\n", "supports = [result[1] for result in results]\n", "confidences = [result[2][0][2] for result in results]\n", "lifts = [result[2][0][3] for result in results]\n", "resultsinDataFrame = pd.DataFrame(zip(lhs, rhs, supports, confidences, lifts), columns = ['Left Hand Side', 'Right Hand Side', 'Support', 'Confidence', 'Lift'])\n", "resultsinDataFrame" ], "cell_type": "code", "metadata": { "id": "lFqrYPxIAk31", "colab": { "base_uri": "https://localhost:8080/", "height": 1291 }, "outputId": "dfb99183-e856-45ba-8a3d-9d4e58d67a90" }, "execution_count": 1, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "\n", " \n", " \n", " Upload widget is only available when the cell has been executed in the\n", " current browser session. Please rerun this cell to enable.\n", " \n", " " ] }, "metadata": {} }, { "output_type": "stream", "name": "stdout", "text": [ "Saving dataset.csv to dataset.csv\n", "(7500, 20)\n", " shrimp almonds avocado vegetables mix green grapes \\\n", "0 burgers meatballs eggs NaN NaN \n", "1 chutney NaN NaN NaN NaN \n", "2 turkey avocado NaN NaN NaN \n", "3 mineral water milk energy bar whole wheat rice green tea \n", "4 low fat yogurt NaN NaN NaN NaN \n", "\n", " whole weat flour yams cottage cheese energy drink tomato juice \\\n", "0 NaN NaN NaN NaN NaN \n", "1 NaN NaN NaN NaN NaN \n", "2 NaN NaN NaN NaN NaN \n", "3 NaN NaN NaN NaN NaN \n", "4 NaN NaN NaN NaN NaN \n", "\n", " low fat yogurt green tea honey salad mineral water salmon antioxydant juice \\\n", "0 NaN NaN NaN NaN NaN NaN NaN \n", "1 NaN NaN NaN NaN NaN NaN NaN \n", "2 NaN NaN NaN NaN NaN NaN NaN \n", "3 NaN NaN NaN NaN NaN NaN NaN \n", "4 NaN NaN NaN NaN NaN NaN NaN \n", "\n", " frozen smoothie spinach olive oil \n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 NaN NaN NaN \n", "3 NaN NaN NaN \n", "4 NaN NaN NaN \n", "Collecting apyori\n", " Downloading apyori-1.1.2.tar.gz (8.6 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Building wheels for collected packages: apyori\n", " Building wheel for apyori (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for apyori: filename=apyori-1.1.2-py3-none-any.whl size=5954 sha256=6f460bf2d21945572fd7d1a90d9524bf5c416704c59847856c42f2fb0d60d484\n", " Stored in directory: /root/.cache/pip/wheels/77/3d/a6/d317a6fb32be58a602b1e8c6b5d6f31f79322da554cad2a5ea\n", "Successfully built apyori\n", "Installing collected packages: apyori\n", "Successfully installed apyori-1.1.2\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ " Left Hand Side Right Hand Side Support Confidence Lift\n", "0 light cream chicken 0.004533 0.290598 4.843305\n", "1 mushroom cream sauce escalope 0.005733 0.300699 3.790327\n", "2 pasta escalope 0.005867 0.372881 4.700185\n", "3 fromage blanc honey 0.003333 0.245098 5.178128\n", "4 herb & pepper ground beef 0.016000 0.323450 3.291555\n", "5 tomato sauce ground beef 0.005333 0.377358 3.840147\n", "6 light cream olive oil 0.003200 0.205128 3.120612\n", "7 whole wheat pasta olive oil 0.008000 0.271493 4.130221\n", "8 pasta shrimp 0.005067 0.322034 4.514494" ], "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", "
Left Hand SideRight Hand SideSupportConfidenceLift
0light creamchicken0.0045330.2905984.843305
1mushroom cream sauceescalope0.0057330.3006993.790327
2pastaescalope0.0058670.3728814.700185
3fromage blanchoney0.0033330.2450985.178128
4herb & pepperground beef0.0160000.3234503.291555
5tomato sauceground beef0.0053330.3773583.840147
6light creamolive oil0.0032000.2051283.120612
7whole wheat pastaolive oil0.0080000.2714934.130221
8pastashrimp0.0050670.3220344.514494
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "resultsinDataFrame", "summary": "{\n \"name\": \"resultsinDataFrame\",\n \"rows\": 9,\n \"fields\": [\n {\n \"column\": \"Left Hand Side\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 7,\n \"samples\": [\n \"light cream\",\n \"mushroom cream sauce\",\n \"tomato sauce\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Right Hand Side\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 6,\n \"samples\": [\n \"chicken\",\n \"escalope\",\n \"shrimp\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Support\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.003896500487537852,\n \"min\": 0.0032,\n \"max\": 0.016,\n \"num_unique_values\": 9,\n \"samples\": [\n 0.008,\n 0.005733333333333333,\n 0.005333333333333333\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Confidence\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.056168653549259547,\n \"min\": 0.20512820512820515,\n \"max\": 0.37735849056603776,\n \"num_unique_values\": 9,\n \"samples\": [\n 0.2714932126696833,\n 0.30069930069930073,\n 0.37735849056603776\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Lift\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.7066844698506499,\n \"min\": 3.120611639881417,\n \"max\": 5.178127589063795,\n \"num_unique_values\": 9,\n \"samples\": [\n 4.130221288078346,\n 3.7903273197390845,\n 3.840147461662528\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 1 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "rj5duez_ERkd" }, "execution_count": null, "outputs": [] } ] }