0
0
ML Pythonml~10 mins

Random forest in depth in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the RandomForestClassifier from scikit-learn.

ML Python
from sklearn.ensemble import [1]
Drag options to blanks, or click blank then click option'
ASVC
BDecisionTreeClassifier
CKNeighborsClassifier
DRandomForestClassifier
Attempts:
3 left
💡 Hint
Common Mistakes
Importing DecisionTreeClassifier instead of RandomForestClassifier
Importing unrelated classifiers like KNeighborsClassifier or SVC
2fill in blank
medium

Complete the code to create a random forest model with 100 trees.

ML Python
model = RandomForestClassifier(n_estimators=[1])
Drag options to blanks, or click blank then click option'
A100
B10
C1000
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using too few trees like 1 or 10 which may underfit
Using too many trees like 1000 which can be slow
3fill in blank
hard

Fix the error in the code to fit the model on training data X_train and y_train.

ML Python
model.[1](X_train, y_train)
Drag options to blanks, or click blank then click option'
Afit
Bpredict
Ctransform
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using predict instead of fit to train the model
Using transform which is for data preprocessing
4fill in blank
hard

Fill both blanks to create a dictionary of feature importances and sort it by importance descending.

ML Python
importances = dict(enumerate(model.[1]))
sorted_importances = dict(sorted(importances.items(), key=lambda item: item[[2]], reverse=True))
Drag options to blanks, or click blank then click option'
Afeature_importances_
B0
C1
Dfeatures_
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'features_' which does not exist
Sorting by item[0] which sorts by feature names, not importance
5fill in blank
hard

Fill all three blanks to predict on test data X_test, calculate accuracy, and print it.

ML Python
predictions = model.[1](X_test)
accuracy = [2](y_test, predictions)
print('Accuracy:', [3])
Drag options to blanks, or click blank then click option'
Apredict
Baccuracy_score
Caccuracy
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using model.score instead of accuracy_score function
Printing the function name instead of the accuracy variable