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 - Selecting the Model - Cross-Validation - SGDClassifier

Let us import some libraries and define a function (display_scores()) which we will be using for cross-validation.

We will be performing k-fold cross-validation with 3 folds (cv=3) on the SGDClassifier model, and calculating the mean accuracy, precision, recall and F1 score values for the same.

INSTRUCTIONS

Please follow the below steps:

Define a function called display_scores() which should print the score value which is passed to it as argument, and also calculate and print the 'mean' and 'standard deviation' of this score.

def display_scores(scores):
      <<your code comes here>>

Please import the following libraries from SKLearn's model_selection package:

from <<your code comes here>> import cross_val_score
from <<your code comes here>> import cross_val_predict

Please import the following libraries from SKLearn's metrics package: from <> import confusion_matrix from <> import precision_score, recall_score from <> import f1_score

Please create an instance of SGDClassifier called 'sgd_clf' by passing to it the parameter random_state=42

sgd_clf = SGDClassifier(<<your code comes here>>)

Please call cross_val_score() function by passing following parameters to it - the model (sgd_clf), the scaled training dataset (X_train_scaled), y_train, cv=3 and scoring="accuracy" - and save the returned value in a variable called sgd_cv_scores.

Call display_scores() function, by passing to it the sgd_cv_scores variable, to calculate and display(print) the 'accuracy' score, the mean of the 'accuracy' score and the 'standard deviation' of the 'accuracy' score.

sgd_cv_scores = cross_val_score(<<your code comes here>>) 
display_scores(sgd_cv_scores)

Call mean() method on sgd_cv_scores object to get the mean accuracy score and store this mean accuracy score in a variable sgd_cv_accuracy.

sgd_cv_accuracy = sgd_cv_scores.<<your code comes here>>

Please call cross_val_predict() function by passing following parameters to it - the model (sgd_clf), the scaled training dataset (X_train_scaled), y_train, cv=3 - and save the returned value in a variable called y_train_pred.

y_train_pred = cross_val_predict(<<your code comes here>>)

Note: The cross-validation might take approx. 20 minutes

Compute the confusion matrix by using confusion_matrix() function

confusion_matrix(y_train, <<your code comes here>>)

Calculate the precision score by the using the precision_score() function

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

Calculate the recall score by the using the recall_score() function

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

Calculate the F1 score by the using the f1_score() function

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

Print the above calculated values of sgd_cv_accuracy, sgd_cv_precision, sgd_cv_recall , sgd_cv_f1_score


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