Start learning free

LESSON 120 OF 165 · MACHINE LEARNING: RECOMMENDERS

Is It Any Good? Hold One Out in Python

Practice

Your recommender gives every customer 3 products, but are they good ones? You can't wait for customers to come back and shop. The trick is hold one out: for each customer, hide one product they really bought, recommend 3 from what's left, and see whether the hidden product comes back.

Hiding a product: np.random.default_rng(0) makes a random generator, and its choice(a_list) gives back one item of the list at random; the seed 0 makes it the same item on every run. You hide the pick in a copy of the table, train, by setting its cell to 0 with .loc, while matrix keeps the truth. On other values:

rng = np.random.default_rng(0)
pick = rng.choice(["Mug", "Pen", "Lamp"])   # one of the three, the same on every run
copy_of_table.loc[7, pick] = 0              # customer 7 no longer has it in the copy

Only customers with 2 or more products are tested: hiding a lone product would leave nothing to recommend from. The similarities and the popularity counts are built from train, so the recommender never sees a hidden product. A hit is a customer whose hidden product is among their 3 recommendations; the hit rate is hits divided by the customers tested.

Your script prints the first three lines below, and the check adds the last three: no hidden product is left in train, 3 random picks would score 0.568 on average, and similarities built from matrix, which saw the hidden products, would score a too-good 0.828.

Customers tested: 256
Hit rate, similar products: 0.801
Hit rate, most popular: 0.598
Hidden products still in train: 0
Hit rate, 3 products picked at random (on average): 0.568
Hit rate, similarities that saw the hidden products: 0.828

Your task: the data, recommend(), the most-popular baseline and the loops are already written. Inside the hiding loop, pick a product with rng.choice, set its cell of train to 0 and remember it in hidden. In the last loop, add 1 to hits when the hidden product is in recommend(owned), then print the hit rate of similar products.


Assignment
Follow the Coding Tutorial to hide one product per customer, count the hits of the similar-products recommender and print its hit rate.


Hint
Hide in train, never in matrix: the recommender learns from train. hidden[customer_id] in recommend(owned) is True for a hit, and the rate is round(hits / len(hidden), 3).


STEP 1 OF 6
    Ctrl + Enter to check

    Rec Evaluate in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free