Defining Zhang's metric
In general, when we want to perform a task many times, we'll write a function, rather than coding up each individual instance. In this exercise, we'll define a function for Zhang's metric that takes an antecedent and consequent and outputs the metric itself. When the problems we solve become increasingly complicated in the following chapter, having a convenient means of computing a metric will greatly simplify things.
Note that numpy
has been imported as np
and pandas
has been imported as pd
. Additionally, recall that the expression for Zhang's metric in terms of support calculations is the following:
$$Zhang(A \rightarrow B) = $$ $$\frac{Support(A \& B) - Support(A) Support(B)}{ max[Support(AB) (1-Support(A)), Support(A)(Support(B)-Support(AB))]}$$
This is a part of the course
“Market Basket Analysis in Python”
Exercise instructions
- Define the support values of the antecedent and consequent individually.
- Define the support of {antecedent, consequent}.
- Complete the expressions for the numerator and denominator.
- Complete the expression for Zhang's metric.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define a function to compute Zhang's metric
def zhang(antecedent, consequent):
# Compute the support of each book
supportA = antecedent.____
supportC = consequent.____
# Compute the support of both books
supportAC = np.____(antecedent, consequent).____
# Complete the expressions for the numerator and denominator
numerator = supportAC - supportA*supportC
denominator = ___(supportAC*(1-supportA), supportA*(supportC-supportAC))
# Return Zhang's metric
return numerator / denominator