CommencerCommencer gratuitement

Computing conviction with a function

After successful completion of her trial project, the ebook start-up's founder decides to hire you for a much bigger project. She asks you if you are able to compute conviction for every pair of books in the goodreads-10k dataset, so she can use that information to decide which books to locate closer together on the website.

You agree to take the job, but realize that you need more a efficient way to compute conviction, since you will need to compute it many times. You decide to write a function that computes it. It will take two columns of a pandas DataFrame as an input, one antecedent and one consequent, and output the conviction metric. Note that pandas is available as pd and numpy is available as np.

Cet exercice fait partie du cours

Market Basket Analysis in Python

Afficher le cours

Instructions

  • Compute the support for the antecedent and assign it to supportA.
  • Compute the support for NOT consequent.
  • Compute the support for antecedent and NOT consequent.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

def conviction(antecedent, consequent):
	# Compute support for antecedent AND consequent
	supportAC = np.logical_and(antecedent, consequent).mean()

	# Compute support for antecedent
	supportA = ____.____()

	# Compute support for NOT consequent
	supportnC = 1.0 - ____.____()

	# Compute support for antecedent and NOT consequent
	supportAnC = ____ - supportAC

    # Return conviction
	return supportA * supportnC / supportAnC
Modifier et exécuter le code