From Numbers to Names

Machine Learning in CodeEasy+10 XP+10

From Numbers to Names

A gardener has measured some iris flowers and wants species names, not numbers.

Task: write species_names(flowers).

  • flowers is a list of flowers. Each is a list of 4 numbers in the same order as the iris columns: sepal length, sepal width, petal length, petal width.
  • Load iris with load_iris(as_frame=True) and split iris.data and iris.target with train_test_split(..., test_size=0.25, random_state=0).
  • Train a KNeighborsClassifier(n_neighbors=5) on the training rows.
  • Predict each flower and return the species names as a list of strings, in the same order as flowers.
  • Put the flowers in a DataFrame with the same column names as the iris data, so scikit-learn doesn't warn about missing names.

Examples

flowers = [[5.0, 3.4, 1.5, 0.2], [6.7, 3.1, 5.6, 2.4]]
returns ['setosa', 'virginica']
flowers = [[5.9, 2.8, 4.2, 1.3]]
returns ['versicolor']

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