Halloween Sale: Flat 70% + Addl. 25% Off + 30 Days Extra Lab on all Courses | Use Coupon HS25 in Checkout | Offer Expires In

  Enroll Now

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

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

  1. We will be first training the VotingClassifier 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 VotingClassifier.
INSTRUCTIONS

Please follow the below steps:

Import VotingClassifier from SKLearn

from <<your code comes here>> import VotingClassifier

As we are going to use Ensemble of Softmax Regression and RandomForest ML algorithms, let us create their instances, as below:

Create an instance of LogisticRegression by passing following parameters - multi_class="multinomial", solver="lbfgs", C=10, and random_state=42. Store this created instance in a variable called 'log_clf_ens'.

log_clf_ens = LogisticRegression(<<your code comes here>>)

Create an instance of RandomForestClassifier by passing following parameters - n_estimators=100, max_depth=50 and random_state=42. Store this created instance in a variable called 'rnd_clf_ens'.

rnd_clf_ens = RandomForestClassifier(<<your code comes here>>)

Create an instance of VotingClassifier by passing following parameters - estimators, and voting='soft'. Store this created instance in a variable called 'voting_clf'. estimators are the above created LogisticRegression (log_clf_ens) and RandomForestClassifier (rnd_clf_ens) models.

voting_clf = VotingClassifier(
    estimators=[('lr', <<your code comes here>>), ('rf', <<your code comes here>>)],
    voting='soft')

Now, train the model on 'scaled' training dataset

voting_clf.<<your code comes here>>(X_train_scaled, <<your code comes here>>)

Note: Please note that the training might time approximately 5-7 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 'voting_clf', and store the predicted value in a variable called y_train_predict

y_train_predict = voting_clf.<<your code comes here>>(X_train[0].reshape(1, -1))

Let us compare the actual value to the predicted value of the label. 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_scaled using the above trained model 'voting_clf' and save the result in variable 'y_train_predict'

y_train_predict = voting_clf.<<your code comes here>>(X_train_scaled)

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 - voting_accuracy, voting_precision, voting_recall and voting_f1_score.

voting_accuracy = <<your code comes here>>(y_train, <<your code comes here>>)

voting_precision = <<your code comes here>>(y_train, <<your code comes here>>, average='weighted')

voting_recall = <<your code comes here>>(y_train, <<your code comes here>>, average='weighted')

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