Computing conviction
After hearing about the useful advice you provided to the library, the founder of a small ebook selling start-up approaches you for consulting services. As a test of your abilities, she asks you if you are able to compute conviction for the rule {Potter} \(\rightarrow\) {Hunger}, so she can decide whether to place the books next to each other on the company's website. Fortunately, you still have access to the goodreads-10k data, which is available as books
. Additionally, pandas
has been imported as pd
and numpy
as np
.
This exercise is part of the course
Market Basket Analysis in Python
Exercise instructions
- Compute the support for {Potter} and assign it to
supportP
. - Compute the support for NOT {Hunger}.
- Compute the support for {Potter} and NOT {Hunger}.
- Complete the expression for the conviction metric in the return statement.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Compute support for Potter AND Hunger
supportPH = np.logical_and(books['Potter'], books['Hunger']).mean()
# Compute support for Potter
supportP = ____.mean()
# Compute support for NOT Hunger
supportnH = 1.0 - books['____'].mean()
# Compute support for Potter and NOT Hunger
supportPnH = ____ - supportPH
# Compute and print conviction for Potter -> Hunger
conviction = ____ * supportnH / supportPnH
print("Conviction: %.2f" % conviction)