How Sure Is It?

Machine Learning in CodeMedium+10 XP+10

How Sure Is It?

A prediction on its own hides how close the vote was. You want the model's confidence too.

Task: write top_confidence(flowers, k).

  • flowers is a list of iris flowers, each 4 numbers in the iris column order.
  • Load iris with load_iris(as_frame=True) and split with train_test_split(..., test_size=0.25, random_state=0).
  • Train a KNeighborsClassifier(n_neighbors=k) on the training rows.
  • For each flower, find the highest of its predicted probabilities, which is the probability of the species the model would predict.
  • Return those values as a list, each rounded to 2 decimal places, in the same order as flowers.
  • Pass the flowers as a DataFrame with the iris column names.

Examples

flowers = [[5.0, 3.4, 1.5, 0.2], [6.3, 2.8, 5.0, 1.6]]
k = 5
returns [1.0, 0.8]
flowers = [[5.9, 3.0, 5.1, 1.8]]
k = 5
returns [0.8]

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