Ask the Forest

Machine Learning in CodeEasy+10 XP+10

Ask the Forest

A garden centre tests the soil under each plant as [pH, moisture %] and labels the plant 1 if it thrived and 0 if it struggled.

Write forest_predict(rows, thrived, new_rows, trees).

  • Fit RandomForestClassifier(n_estimators=trees, random_state=0).
  • Predict every row in new_rows, in order.

Return the predictions as a list of ints.

Examples

rows = [[6.5, 42.8], [6.6, 34.6], [6.4, 38.2], [6.2, 32.4], [6.4, 34.8], [6.2, 32.6], [6.5, 39.1], [6.9, 34.9], [6.4, 41.1], [6.3, 40.6], [6.6, 39.3], [6.6, 29.9], [6.5, 37.8], [6.2, 39.8], [6.5, 40.5], [4.5, 15.8], [4.9, 25.4], [4.7, 22.5], [4.8, 19.4], [5.3, 30.0], [4.8, 25.0], [5.0, 17.2], [5.3, 22.3], [4.8, 24.3], [5.0, 21.2], [5.0, 24.7], [5.0, 21.7], [4.6, 24.7], [5.0, 27.8], [5.4, 19.3]]
thrived = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
new_rows = [[6.4, 38.0], [5.1, 25.0]]
trees = 25
returns [1, 0]
rows = [[6.5, 42.8], [6.6, 34.6], [6.4, 38.2], [6.2, 32.4], [6.4, 34.8], [6.2, 32.6], [6.5, 39.1], [6.9, 34.9], [6.4, 41.1], [6.3, 40.6], [6.6, 39.3], [6.6, 29.9], [6.5, 37.8], [6.2, 39.8], [6.5, 40.5], [4.5, 15.8], [4.9, 25.4], [4.7, 22.5], [4.8, 19.4], [5.3, 30.0], [4.8, 25.0], [5.0, 17.2], [5.3, 22.3], [4.8, 24.3], [5.0, 21.2], [5.0, 24.7], [5.0, 21.7], [4.6, 24.7], [5.0, 27.8], [5.4, 19.3]]
thrived = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
new_rows = [[6.7, 36.0], [4.7, 20.0]]
trees = 10
returns [1, 0]

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