finished assignment 6

This commit is contained in:
Ziad Arafat committed 2023-11-15 21:18:50 -07:00
1 parent 895979b42f
commit 4e61323066
3 files changed
+542283

No files matched your search

@@ -0,0 +1,373 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mWARNING: Ignoring invalid distribution -cikit-learn (/home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: pandas in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (2.1.0)\n",
"Requirement already satisfied: numpy in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (1.26.0)\n",
"Requirement already satisfied: matplotlib in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (3.8.0)\n",
"Requirement already satisfied: seaborn in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (0.12.2)\n",
"Requirement already satisfied: scikit-learn==1.2.2 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (1.2.2)\n",
"Requirement already satisfied: mlxtend in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (0.23.0)\n",
"Requirement already satisfied: scipy>=1.3.2 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from scikit-learn==1.2.2) (1.11.2)\n",
"Requirement already satisfied: joblib>=1.1.1 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from scikit-learn==1.2.2) (1.3.2)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from scikit-learn==1.2.2) (3.2.0)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from pandas) (2.8.2)\n",
"Requirement already satisfied: pytz>=2020.1 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from pandas) (2023.3.post1)\n",
"Requirement already satisfied: tzdata>=2022.1 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from pandas) (2023.3)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from matplotlib) (1.1.1)\n",
"Requirement already satisfied: cycler>=0.10 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from matplotlib) (0.11.0)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from matplotlib) (4.42.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from matplotlib) (1.4.5)\n",
"Requirement already satisfied: packaging>=20.0 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from matplotlib) (23.1)\n",
"Requirement already satisfied: pillow>=6.2.0 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from matplotlib) (10.0.1)\n",
"Requirement already satisfied: pyparsing>=2.3.1 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from matplotlib) (3.1.1)\n",
"Requirement already satisfied: six>=1.5 in /home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
"\u001b[33mWARNING: Ignoring invalid distribution -cikit-learn (/home/ugrad10/zarafat/src/data-mining/.venv/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n",
"\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"!pip install pandas numpy matplotlib seaborn scikit-learn==1.2.2 mlxtend\n",
"\n",
"\n",
"# This is to disable annoying warning messages from sklearn 1.2.2\n",
"def warn(*args, **kwargs):\n",
" pass\n",
"import warnings\n",
"warnings.warn = warn"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt \n",
"import seaborn as sns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 6\n",
"## Ziad Arafat\n",
"\n",
"### Reading in the data\n",
"\n",
"1. We read in the CSV using the pandas library and store it in a dataframe. \n",
"2. We print the data in the first two rows using the `head()` method"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' \\nWe will use a dataset called Online Retail Data Set from the UCI repository\\n(https://archive.ics.uci.edu/ml/datasets/Online+Retail). Its csv file (Online Retail.csv) can be\\ndownloaded from Canvas. This is a transactional data set that contains all the transactions\\noccurring between 01/12/2010 and 09/12/2011 for a UK-based and registered non-store online\\nretail. Note that the InvoiceNo column contains the id of the transaction and the column StockCode\\ncontains the ids of the items.\\nBased on association rules generation, what are the top 10 association rules (please use\\nconfidence as the rule evaluation metric) that you can find based on this dataset?\\nFor Association Rules Mining, you can consider using frequent_patterns package in MLxtend.\\nPlease check http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/association_rules/ for how\\nto generate the association rules using MLxtend.\\n'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\" \n",
"We will use a dataset called Online Retail Data Set from the UCI repository\n",
"(https://archive.ics.uci.edu/ml/datasets/Online+Retail). Its csv file (Online Retail.csv) can be\n",
"downloaded from Canvas. This is a transactional data set that contains all the transactions\n",
"occurring between 01/12/2010 and 09/12/2011 for a UK-based and registered non-store online\n",
"retail. Note that the InvoiceNo column contains the id of the transaction and the column StockCode\n",
"contains the ids of the items.\n",
"Based on association rules generation, what are the top 10 association rules (please use\n",
"confidence as the rule evaluation metric) that you can find based on this dataset?\n",
"For Association Rules Mining, you can consider using frequent_patterns package in MLxtend.\n",
"Please check http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/association_rules/ for how\n",
"to generate the association rules using MLxtend.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" InvoiceNo StockCode Description Quantity \\\n",
"0 536365 85123A WHITE HANGING HEART T-LIGHT HOLDER 6 \n",
"1 536365 71053 WHITE METAL LANTERN 6 \n",
"2 536365 84406B CREAM CUPID HEARTS COAT HANGER 8 \n",
"3 536365 84029G KNITTED UNION FLAG HOT WATER BOTTLE 6 \n",
"4 536365 84029E RED WOOLLY HOTTIE WHITE HEART. 6 \n",
"\n",
" InvoiceDate UnitPrice CustomerID Country \n",
"0 12/1/2010 8:26 2.55 17850.0 United Kingdom \n",
"1 12/1/2010 8:26 3.39 17850.0 United Kingdom \n",
"2 12/1/2010 8:26 2.75 17850.0 United Kingdom \n",
"3 12/1/2010 8:26 3.39 17850.0 United Kingdom \n",
"4 12/1/2010 8:26 3.39 17850.0 United Kingdom \n"
]
}
],
"source": [
"df_online_retail = pd.read_csv(\"Online Retail.csv\")\n",
"print(df_online_retail.head(n=5))\n",
"\n",
"# columns: InvoiceNo, StockCode, Description, Quantity, InvoiceDate, UnitPrice, CustomerID, Country"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sample StockCode values from the DataFrame:\n",
"0 85123A\n",
"1 71053\n",
"2 84406B\n",
"3 84029G\n",
"4 84029E\n",
"Name: StockCode, dtype: object\n"
]
}
],
"source": [
"# Step 1: Verify the format of StockCode values\n",
"print(\"Sample StockCode values from the DataFrame:\")\n",
"print(df_online_retail['StockCode'].head())\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Preprocessing\n",
"#### Scaling\n",
"1. We use minmax to normalize the data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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></th>\n",
" <th>antecedents</th>\n",
" <th>consequents</th>\n",
" <th>confidence</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>71</th>\n",
" <td>[REGENCY CAKESTAND 3 TIER, PINK REGENCY TEACUP AND SAUCER, ROSES REGENCY TEACUP AND SAUCER ]</td>\n",
" <td>[GREEN REGENCY TEACUP AND SAUCER]</td>\n",
" <td>0.899110</td>\n",
" </tr>\n",
" <tr>\n",
" <th>82</th>\n",
" <td>[REGENCY TEA PLATE PINK]</td>\n",
" <td>[REGENCY TEA PLATE GREEN ]</td>\n",
" <td>0.898089</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>[SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETROSPOT PAPER NAPKINS ]</td>\n",
" <td>[SET/6 RED SPOTTY PAPER PLATES]</td>\n",
" <td>0.895270</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75</th>\n",
" <td>[PINK REGENCY TEACUP AND SAUCER, ROSES REGENCY TEACUP AND SAUCER ]</td>\n",
" <td>[GREEN REGENCY TEACUP AND SAUCER]</td>\n",
" <td>0.894137</td>\n",
" </tr>\n",
" <tr>\n",
" <th>69</th>\n",
" <td>[GREEN REGENCY TEACUP AND SAUCER, PINK REGENCY TEACUP AND SAUCER, REGENCY CAKESTAND 3 TIER]</td>\n",
" <td>[ROSES REGENCY TEACUP AND SAUCER ]</td>\n",
" <td>0.875723</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>[JUMBO STORAGE BAG SUKI, JUMBO BAG PINK POLKADOT, JUMBO SHOPPER VINTAGE RED PAISLEY]</td>\n",
" <td>[JUMBO BAG RED RETROSPOT]</td>\n",
" <td>0.867742</td>\n",
" </tr>\n",
" <tr>\n",
" <th>83</th>\n",
" <td>[REGENCY TEA PLATE PINK]</td>\n",
" <td>[REGENCY TEA PLATE ROSES ]</td>\n",
" <td>0.866242</td>\n",
" </tr>\n",
" <tr>\n",
" <th>67</th>\n",
" <td>[REGENCY CAKESTAND 3 TIER, PINK REGENCY TEACUP AND SAUCER]</td>\n",
" <td>[GREEN REGENCY TEACUP AND SAUCER]</td>\n",
" <td>0.860697</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>[CHARLOTTE BAG PINK POLKADOT, STRAWBERRY CHARLOTTE BAG]</td>\n",
" <td>[RED RETROSPOT CHARLOTTE BAG]</td>\n",
" <td>0.858639</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>[CHARLOTTE BAG SUKI DESIGN, WOODLAND CHARLOTTE BAG, STRAWBERRY CHARLOTTE BAG]</td>\n",
" <td>[RED RETROSPOT CHARLOTTE BAG]</td>\n",
" <td>0.858553</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" antecedents \\\n",
"71 [REGENCY CAKESTAND 3 TIER, PINK REGENCY TEACUP AND SAUCER, ROSES REGENCY TEACUP AND SAUCER ] \n",
"82 [REGENCY TEA PLATE PINK] \n",
"26 [SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETROSPOT PAPER NAPKINS ] \n",
"75 [PINK REGENCY TEACUP AND SAUCER, ROSES REGENCY TEACUP AND SAUCER ] \n",
"69 [GREEN REGENCY TEACUP AND SAUCER, PINK REGENCY TEACUP AND SAUCER, REGENCY CAKESTAND 3 TIER] \n",
"22 [JUMBO STORAGE BAG SUKI, JUMBO BAG PINK POLKADOT, JUMBO SHOPPER VINTAGE RED PAISLEY] \n",
"83 [REGENCY TEA PLATE PINK] \n",
"67 [REGENCY CAKESTAND 3 TIER, PINK REGENCY TEACUP AND SAUCER] \n",
"10 [CHARLOTTE BAG PINK POLKADOT, STRAWBERRY CHARLOTTE BAG] \n",
"17 [CHARLOTTE BAG SUKI DESIGN, WOODLAND CHARLOTTE BAG, STRAWBERRY CHARLOTTE BAG] \n",
"\n",
" consequents confidence \n",
"71 [GREEN REGENCY TEACUP AND SAUCER] 0.899110 \n",
"82 [REGENCY TEA PLATE GREEN ] 0.898089 \n",
"26 [SET/6 RED SPOTTY PAPER PLATES] 0.895270 \n",
"75 [GREEN REGENCY TEACUP AND SAUCER] 0.894137 \n",
"69 [ROSES REGENCY TEACUP AND SAUCER ] 0.875723 \n",
"22 [JUMBO BAG RED RETROSPOT] 0.867742 \n",
"83 [REGENCY TEA PLATE ROSES ] 0.866242 \n",
"67 [GREEN REGENCY TEACUP AND SAUCER] 0.860697 \n",
"10 [RED RETROSPOT CHARLOTTE BAG] 0.858639 \n",
"17 [RED RETROSPOT CHARLOTTE BAG] 0.858553 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from mlxtend.preprocessing import TransactionEncoder\n",
"from mlxtend.frequent_patterns import fpgrowth, association_rules\n",
"\n",
"# Create the stockcode to description mapping dictionary\n",
"# This is to map the StockCode values to the actual description at the end\n",
"stockcode_description_mapping = pd.Series(\n",
" df_online_retail.Description.values,\n",
" index=df_online_retail.StockCode\n",
").to_dict()\n",
"\n",
"# Prepare the transactions data\n",
"transactions = df_online_retail.groupby('InvoiceNo')['StockCode'].apply(list)\n",
"\n",
"# Transaction Encoder using the actual StockCode values\n",
"te = TransactionEncoder()\n",
"te_ary = te.fit(transactions).transform(transactions)\n",
"df_transaction_encoder = pd.DataFrame(te_ary, columns=te.columns_)\n",
"\n",
"# Generate frequent itemsets and association rules\n",
"frequent_itemsets = fpgrowth(\n",
" df_transaction_encoder,\n",
" min_support=0.01,\n",
" use_colnames=True\n",
")\n",
"rules = association_rules(\n",
" frequent_itemsets,\n",
" metric=\"confidence\",\n",
" min_threshold=0.7\n",
")\n",
"\n",
"# Map antecedents and consequents to descriptions\n",
"rules['antecedents'] = rules['antecedents'].apply(\n",
" lambda x: [stockcode_description_mapping[item] for item in x]\n",
")\n",
"rules['consequents'] = rules['consequents'].apply(\n",
" lambda x: [stockcode_description_mapping[item] for item in x]\n",
")\n",
"\n",
"# sort the rules by confidence\n",
"rules = rules.sort_values(by='confidence', ascending=False)\n",
"\n",
"# Print the top 10 association rules \n",
"# only the antecedents, consequents and confidence \n",
"# Without truncating the columns\n",
"with pd.option_context('display.max_colwidth', None):\n",
" display(rules[['antecedents', 'consequents', 'confidence']].head(n=10))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.10.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file not shown.
File diff suppressed because it is too large. Load diff