- Home
- Assessment

20 / 30

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 Softmax Regression model, and calculating the mean accuracy, precision, recall and F1 score values for the same.

Please follow the below steps:

Import the module `cross_val_score`

and `cross_val_predict`

from `sklearn.model_selection`

```
from sklearn.model_selection import << your code comes here >>
```

Import the module `confusion_matrix`

from `sklearn.metrics`

.

```
from sklearn.metrics import << your code comes here >>
```

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 create an instance of LogisticRegression called log_clf by passing to it the parameters - multi_class="multinomial", solver="lbfgs", C=10 and random_state=42

```
log_clf = LogisticRegression(<<your code comes here>>)
```

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

Call display_scores() function, by passing to it the log_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.

```
log_cv_scores = cross_val_score(<<your code comes here>>)
display_scores(log_cv_scores)
```

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

```
log_cv_accuracy = log_cv_scores.<<your code comes here>>
```

Please call cross_val_predict() function by passing following parameters to it - the model (log_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>>)
```

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

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

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

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

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

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

Print the above calculated values of log_cv_accuracy, log_cv_precision, log_cv_recall , log_cv_f1_score

XP

Taking you to the next exercise in seconds...

Want to create exercises like this yourself? Click here.

Checking Please wait.

Success

Error

Fetching hint, please wait...

Error

Answer is not availble for this assesment

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

## Loading comments...