Before the Evidence

Machine Learning in CodeEasy+10 XP+10

Before the Evidence

A fish farm measures each fish as [length in cm, weight in kg] and labels it "trout" or "salmon".

Write start_and_guess(rows, kinds, new_rows).

  • Fit a GaussianNB model.
  • Read its starting belief, class_prior_: how common each class was in the training rows. The order matches the model's classes_.
  • Predict the kind of every fish in new_rows, in order.

Return a dict:

  • "priors": a dict mapping each class name to its prior, rounded to 3 decimal places.
  • "predictions": the list of predicted class names, as strings.

Examples

rows = [[60, 2.1], [65, 2.6], [70, 3.0], [58, 1.9], [62, 2.3], [80, 4.2], [85, 4.9], [90, 5.5], [78, 4.0], [88, 5.1], [83, 4.5], [67, 2.8], [61, 2.2]]
kinds = ['trout', 'trout', 'trout', 'trout', 'trout', 'salmon', 'salmon', 'salmon', 'salmon', 'salmon', 'salmon', 'trout', 'trout']
new_rows = [[64, 2.5], [86, 4.7]]
returns {'priors': {'salmon': 0.462, 'trout': 0.538}, 'predictions': ['trout', 'salmon']}
rows = [[1.0, 5.0], [1.2, 4.8], [3.0, 1.0], [3.2, 1.3], [2.9, 0.8]]
kinds = ['a', 'a', 'b', 'b', 'b']
new_rows = [[1.1, 5.1]]
returns {'priors': {'a': 0.4, 'b': 0.6}, 'predictions': ['a']}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run