+91 81900 98289

Grid Search

This Grid Search lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Grid search tries a list of settings and keeps the one with the best score on held-out rows.

The example scores two thresholds and keeps the higher accuracy.

Example 1: Start here

Score each setting on the validation rows, not on the final test rows.

actual = [1, 0, 1, 0] # a list; the values stay in this order probs = [0.9, 0.2, 0.4, 0.7] # a list; the values stay in this order best = None # no value yet for threshold in (0.3, 0.6): # repeat the indented lines once per value pred = [1 if p >= threshold else 0 for p in probs] # a list; the values stay in this order hits = sum(a == p for a, p in zip(actual, pred)) # the total if best is None or hits > best[0]: # run the indented lines only when this is true best = (hits, threshold) # a tuple; a fixed group of values print(best)

Example 2: Add two rows

Grid Search applies one operation to every number. zip pairs the rows, and the list keeps each sum.

left = [1, 2, 20] # a list; the values stay in this order right = [10, 20, 30] # a list; the values stay in this order print([a + b for a, b in zip(left, right)]) # pair the lists, then combine each pair

Example 3: Average a column

The average is the total divided by how many numbers you have. That is the same summary a statistics page starts with.

scores = [70, 20, 90] # a list; the values stay in this order print(sum(scores) / len(scores)) # len counts the items

Example 4: Keep values above a line

A cutoff drops the low values. Change 80 and run the example again to see who remains.

scores = [70, 20, 90] # a list; the values stay in this order print([score for score in scores if score >= 80]) # keep the items that pass the test
Score each setting on the validation rows, not on the final test rows.
Call Now 81900 98289