PRACTICAL 12: FP-TREE ASSOCIATION

Dataset: disney_movies.csv

STEPS:
1. Install the library:  pip install pyfpgrowth
2. Read disney_movies.csv with the csv module (utf8 encoding) and keep every row as a list.
3. Remove the empty values from each row and remove the header row.
4. Print some records (first 10) to check the data.
5. Find the frequent patterns with find_frequent_patterns (minimum support count = 2).
6. Generate the association rules from the patterns with generate_association_rules (minimum confidence = 0.7).
7. Print the patterns and the rules.

CODE:
import pyfpgrowth
import csv
with open('disney_movies.csv',encoding="utf8",newline='') as f:
    reader=csv.reader(f)
    data=list(reader)
for i in range(len(data))    :
    if '' in data[i]:
        data[i]=[x for x in data[i] if x]
data.pop(0)
print("some records",data[0:10])
patterns=pyfpgrowth.find_frequent_patterns(data,2)
print("pattern",patterns)
print()
rules=pyfpgrowth.generate_association_rules(patterns,0.7)
print("rules output",rules)

EXPLANATION:
- FP-Growth is a faster alternative to Apriori: it builds a compact FP-tree of the transactions and does not generate candidate itemsets.
- Each row of the csv (one movie with its details) is treated as one transaction after the empty cells are removed.
- find_frequent_patterns(data, 2) returns the item patterns that appear in at least 2 transactions.
- generate_association_rules(patterns, 0.7) keeps only the rules whose confidence is 70% or more.
- The output is a dictionary of patterns with their counts and the rules in the form: antecedent -> (consequent, confidence).
