Let us now train the RandomForestClassifier. We will be doing the following as part of this exercise:
Please follow the below steps:
Import RandomForestClassifier from SKLearn
from <<your code comes here>> import RandomForestClassifier
Create an instance of RandomForestClassifier by passing parameters - n_estimators=20, max_depth=10, random_state=42, and store this created instance in a variable called 'rnd_clf'.
rnd_clf = RandomForestClassifier(<<your code comes here>>)
# Scaling is not needed for Decision Tree based algorithms like Random Forest and XGBoost
Now, train the model on training dataset
rnd_clf.<<your code comes here>>(X_train, <<your code comes here>>)
Note: Please note that the training might take upto 2-3 minutes.
Make prediction on an instance from the training dataset (say instance at index '0' i.e. X_train[0]) using the above trained model 'rnd_clf', and store the predicted value in a variable called y_train_predict
y_train_predict = rnd_clf.<<your code comes here>>(X_train[0].reshape(1, -1))
Let us compare the actual value (digit) to the predicted value (digit). You can use showImage() function to see the image.
y_train[0]
y_train_predict[0]
showImage(X_train[0])
Make the predictions on the complete training dataset X_train using the above trained model 'rnd_clf' and save the result in variable 'y_train_predict'
y_train_predict = rnd_clf.<<your code comes here>>(X_train)
Calculate the various metrics scores like - accuracy, precision, recall, F1 score - using the actual and the predicted values and relevant functions, - and store them in respective variables - rnd_accuracy, rnd_precision, rnd_recall and rnd_f1_score.
rnd_accuracy = <<your code comes here>>(y_train, <<your code comes here>>)
rnd_precision = <<your code comes here>>(y_train, <<your code comes here>>, average='weighted')
rnd_recall = <<your code comes here>>(y_train, <<your code comes here>>, average='weighted')
rnd_f1_score = <<your code comes here>>(y_train, <<your code comes here>>, average='weighted')
You can print the above metrics values (accuracy, etc.) using the print() function
No hints are availble for this assesment
Answer is not availble for this assesment
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...