Get Started

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”

View Course

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

This exercise is part of the course

Market Basket Analysis in Python

IntermediateSkill Level
4.8+
5 reviews

Explore association rules in market basket analysis with Python by bookstore data and creating movie recommendations.

Association rules tell us that two or more items are related. Metrics allow us to quantify the usefulness of those relationships. In this chapter, you’ll apply six metrics to evaluate association rules: supply, confidence, lift, conviction, leverage, and Zhang's metric. You’ll then use association rules and metrics to assist a library and an e-book seller.

Exercise 1: Confidence and liftExercise 2: Recommending books with supportExercise 3: Refining support with confidenceExercise 4: Further refinement with liftExercise 5: Leverage and convictionExercise 6: Lift versus leverageExercise 7: Computing convictionExercise 8: Computing conviction with a functionExercise 9: Promoting ebooks with convictionExercise 10: Association and dissociationExercise 11: Computing association and dissociationExercise 12: Defining Zhang's metric
Exercise 13: Applying Zhang's metricExercise 14: Advanced rulesExercise 15: Filtering with support and convictionExercise 16: Using multi-metric filtering to cross-promote books

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free