ComeçarComece de graça

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))]}$$

Este exercício faz parte do curso

Market Basket Analysis in Python

Ver curso

Instruções do exercício

  • 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.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# 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
Editar e executar o código