{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"TEAM MEMBERS:
\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"PART 1: Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Our team put a lot of effort into generalizing the solution for the given problem, but we failed. We expected that if the transactions contained n items, then the algorithm would generate all possible association rules for k-items sets, k = 1,2,3,...,n. However, the running time was excessively large, and we could not execute the algorithm within an acceptable timeframe. Therefore, we decided to narrow the scope of the problem to make our algorithm feasible. Specifically, we decided not to find association rules for \"any\" item set. Instead, we transformed the problem into finding all possible association rules for a \"specific\" item set.\n",
"
\n",
"
\n",
"In the following, we will input a \"specific\" item set (that we call it sample). Then, we expect that the algorithm will generate all possible corresponding rules for that item set. Notice that if the input item set has a size of k, then every possible rule contains exactly k items - no more, no less."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"PART 2: Apriori Algorithm Usage"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import AprioriAlgorithm as apr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load and Clean data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The following are the first 10 transactions:\n",
"[39, 48, 130, 147, 475, 2998, 4792, 5478, 7146, 7160, 10410]\n",
"[32, 48]\n",
"[48, 79, 232, 340, 396, 649, 911, 1016, 1020, 1172, 1874, 2115, 2208, 2792, 3106, 3964, 4070, 4146, 4764, 5432, 6446, 9082, 9231, 10579, 14029, 14350]\n",
"[32, 48, 65, 130, 772, 816, 1344, 3220, 4445, 6536, 13083]\n",
"[39, 175, 421, 806, 1025, 1629, 2424, 3151]\n",
"[31, 39, 65, 107, 179, 301, 441, 533, 548, 878, 961, 1024, 1319, 1404, 1476, 1486, 1897, 1987, 2052, 2083, 2625, 4064, 4753, 4953, 5051, 5347, 6567, 7017, 9271, 10074, 13062, 13695]\n",
"[39, 232, 495, 2008, 7378, 10650]\n",
"[39, 5994]\n",
"[55]\n",
"[664, 1052, 4383, 11702, 12285, 12469]\n"
]
}
],
"source": [
"# Read data from csv file\n",
"path = 'SuperCenterDataNew.csv'\n",
"data = apr.load_and_clean_data(path)\n",
"\n",
"# Show first 10 rows of data\n",
"print('The following are the first 10 transactions:')\n",
"for i in range(10):\n",
" print(data[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we test Apriori Algorithm with the following parameters:\n",
"- sample size = 6\n",
"- minimum confidence = 0.1\n",
"- minimum support is between 3 and 10"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"sample = [39, 48, 65, 89, 225, 237]\n",
"min_confidence = 0.1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 225} -----> {48, 237, 39} with confidence = 0.25\n",
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{65, 225, 237} -----> {48, 89, 39} with confidence = 0.2222222222222222\n",
"{89, 225, 237} -----> {48, 65, 39} with confidence = 0.4\n",
"{89, 65, 225, 39} -----> {48, 237} with confidence = 0.3333333333333333\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{65, 225, 237, 39} -----> {48, 89} with confidence = 0.3333333333333333\n",
"{89, 225, 237, 39} -----> {48, 65} with confidence = 0.5\n",
"{48, 65, 225, 89} -----> {237, 39} with confidence = 0.4\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n",
"{48, 65, 225, 237} -----> {89, 39} with confidence = 0.2222222222222222\n",
"{48, 89, 225, 237} -----> {65, 39} with confidence = 0.4\n",
"{89, 65, 225, 237} -----> {48, 39} with confidence = 0.6666666666666666\n",
"{65, 225, 39, 48, 89} -----> {237} with confidence = 0.5\n",
"{65, 39, 237, 48, 89} -----> {225} with confidence = 0.25\n",
"{65, 225, 39, 237, 48} -----> {89} with confidence = 0.3333333333333333\n",
"{225, 39, 237, 48, 89} -----> {65} with confidence = 0.5\n",
"{65, 225, 237, 48, 89} -----> {39} with confidence = 0.6666666666666666\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=3, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 4"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 225} -----> {48, 237, 39} with confidence = 0.25\n",
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{65, 225, 237} -----> {48, 89, 39} with confidence = 0.2222222222222222\n",
"{89, 225, 237} -----> {48, 65, 39} with confidence = 0.4\n",
"{89, 65, 225, 39} -----> {48, 237} with confidence = 0.3333333333333333\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{65, 225, 237, 39} -----> {48, 89} with confidence = 0.3333333333333333\n",
"{89, 225, 237, 39} -----> {48, 65} with confidence = 0.5\n",
"{48, 65, 225, 89} -----> {237, 39} with confidence = 0.4\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n",
"{48, 65, 225, 237} -----> {89, 39} with confidence = 0.2222222222222222\n",
"{48, 89, 225, 237} -----> {65, 39} with confidence = 0.4\n",
"{65, 225, 39, 48, 89} -----> {237} with confidence = 0.5\n",
"{65, 39, 237, 48, 89} -----> {225} with confidence = 0.25\n",
"{65, 225, 39, 237, 48} -----> {89} with confidence = 0.3333333333333333\n",
"{225, 39, 237, 48, 89} -----> {65} with confidence = 0.5\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=4, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 5"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 225} -----> {48, 237, 39} with confidence = 0.25\n",
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{65, 225, 237} -----> {48, 89, 39} with confidence = 0.2222222222222222\n",
"{89, 225, 237} -----> {48, 65, 39} with confidence = 0.4\n",
"{89, 65, 225, 39} -----> {48, 237} with confidence = 0.3333333333333333\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{65, 225, 237, 39} -----> {48, 89} with confidence = 0.3333333333333333\n",
"{48, 65, 225, 89} -----> {237, 39} with confidence = 0.4\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n",
"{48, 65, 225, 237} -----> {89, 39} with confidence = 0.2222222222222222\n",
"{48, 89, 225, 237} -----> {65, 39} with confidence = 0.4\n",
"{65, 39, 237, 48, 89} -----> {225} with confidence = 0.25\n",
"{65, 225, 39, 237, 48} -----> {89} with confidence = 0.3333333333333333\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=5, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 6"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 225} -----> {48, 237, 39} with confidence = 0.25\n",
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{65, 225, 237} -----> {48, 89, 39} with confidence = 0.2222222222222222\n",
"{89, 65, 225, 39} -----> {48, 237} with confidence = 0.3333333333333333\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{65, 225, 237, 39} -----> {48, 89} with confidence = 0.3333333333333333\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n",
"{48, 65, 225, 237} -----> {89, 39} with confidence = 0.2222222222222222\n",
"{65, 39, 237, 48, 89} -----> {225} with confidence = 0.25\n",
"{65, 225, 39, 237, 48} -----> {89} with confidence = 0.3333333333333333\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=6, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 7"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 225} -----> {48, 237, 39} with confidence = 0.25\n",
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{65, 225, 237} -----> {48, 89, 39} with confidence = 0.2222222222222222\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n",
"{48, 65, 225, 237} -----> {89, 39} with confidence = 0.2222222222222222\n",
"{65, 39, 237, 48, 89} -----> {225} with confidence = 0.25\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=7, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 8"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 225} -----> {48, 237, 39} with confidence = 0.25\n",
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{65, 225, 237} -----> {48, 89, 39} with confidence = 0.2222222222222222\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n",
"{48, 65, 225, 237} -----> {89, 39} with confidence = 0.2222222222222222\n",
"{65, 39, 237, 48, 89} -----> {225} with confidence = 0.25\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=8, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 9"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{65, 225, 237} -----> {48, 89, 39} with confidence = 0.2222222222222222\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n",
"{48, 65, 225, 237} -----> {89, 39} with confidence = 0.2222222222222222\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=9, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Minimum Support = 10"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{89, 65, 237} -----> {48, 225, 39} with confidence = 0.15384615384615385\n",
"{89, 65, 237, 39} -----> {48, 225} with confidence = 0.2\n",
"{48, 65, 237, 89} -----> {225, 39} with confidence = 0.18181818181818182\n"
]
}
],
"source": [
"antecedents, consequents, confidences = apr.Apriori(dataset=data, sample=sample, min_support=10, min_confidence=min_confidence)\n",
"\n",
"# Print possible rules with confidences\n",
"if len(antecedents) == 0:\n",
" print('Not Found Any Rule Passing The Given Conditions')\n",
"else:\n",
" for i in range(len(antecedents)):\n",
" print(f'{set(antecedents[i])} -----> {set(consequents[i])} with confidence = {confidences[i]}')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}