Get startedGet started for free

Applying Zhang's metric

The founder of the ebook start-up has returned for additional consulting services. She has sent you a list of itemsets she's investigating and has asked you to determine whether any of them contain items that are dissociated. When you're finished, she has asked that you add the metric you use to a column in the rules DataFrame, which is available to you, and currently contains columns for antecedents and consequents.

The itemsets are available as a list of lists called itemsets. Each list contains the antecedent first and the consequent second. You also have access to the books DataFrame from previous exercises. Note that Zhang's metric has been defined for you and is available as zhang(). Additionally, pandas is available as pd and numpy as np.

This exercise is part of the course

Market Basket Analysis in Python

View Course

Exercise instructions

  • Loop over each itemset in itemsets.
  • Extract the antecedent and consequent columns from books for each itemset.
  • Complete the statement and append it to the zhangs_metric list.
  • Print the metric for each itemset.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define an empty list for Zhang's metric
zhangs_metric = []

# Loop over lists in itemsets
for itemset in ____:
    # Extract the antecedent and consequent columns
	antecedent = books[itemset[0]]
	consequent = ____[itemset[1]]
    
    # Complete Zhang's metric and append it to the list
	zhangs_metric.append(zhang(____, ____))
    
# Print results
rules['zhang'] = zhangs_metric
print(rules)
Edit and Run Code