Project - Classify Clothes from Fashion MNIST Dataset using Machine Learning Techniques

16 / 30

End to End ML Project - Fashion MNIST - Training the Model - RandomForestClassifier

Let us now train the RandomForestClassifier. We will be doing the following as part of this exercise:

  1. We will be first training the RandomForestClassifier on the training dataset
  2. Using the trained model, make the prediction on a sample instance and compare the prediction with the actual value.
  3. Using the trained model, make the prediction on the whole training dataset
  4. Calculate - accuracy, precision, recall and F1 Score for RandomForestClassifier.

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'.

Note: Please ensure that you are putting these hyperparameters correctly, otherwise your model will not give correct results.

rnd_clf = RandomForestClassifier(n_estimators=<<your code comes here>>, max_depth=10, random_state=<<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.




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

See Answer

No hints are availble for this assesment

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...