Your random forest scores 0.76 on the 75 test customers. But which of its 5 features did the work? Every tree sorts customers with questions about one feature at a time. After fit, forest.feature_importances_ gives back one number per feature, in the order of your column list: that feature's share of all the sorting. The shares add up to 1. They have no sign: they say how much a feature helped, not in which direction.
To read them, give each number its name with a pandas Series: pd.Series(values, index=labels) gives back a column of numbers with a label on each. sort_values(ascending=False) gives back the Series biggest first, and idxmin() gives back the label of the smallest value, not the value itself:
prices = pd.Series([4.5, 2.0, 7.25], index=["pen", "mug", "book"])
print(prices.idxmin()) # mug
print(prices.min()) # 2.0
A feature that did almost none of the work is a candidate to drop. table.drop(columns=name) gives back a copy of the table without that column; the table itself stays as it was. Train a second forest with the same settings on the smaller training rows. Score it on test rows without that column too: a model is scored on the same columns it learned from.
Your program must print this. The last two lines come from the lesson's check, which runs after your code:
All 5 features: 0.76 visits 0.279 days_since_last 0.275 spent 0.243 orders 0.182 returned_item 0.022 Without returned_item: 0.733 Importances add up to: 1.0 Both forests give the same answer for 71 of 75 test customers
Your task: the data, the split and the 5-feature forest are ready. Put its importances in a Series named importances and print them sorted, keep the weakest feature's name in weakest, then train a second forest, smaller, without that column and print its test score.
Assignment
Follow the Coding Tutorial to rank the forest's features and retrain it without the weakest one.
Hint
The importances come in the order of features, so that list is the Series' index. Drop weakest from X_test as well as from X_train.
Free plan: 30 tutor messages. PRO is unlimited.
