Get startedGet started for free

Fitting multi-class logistic regression

In this exercise, you'll fit the two types of multi-class logistic regression, one-vs-rest and softmax/multinomial, on the handwritten digits data set and compare the results. The handwritten digits dataset is already loaded and split into X_train, y_train, X_test, and y_test.

This exercise is part of the course

Linear Classifiers in Python

View Course

Exercise instructions

  • Fit a one-vs-rest logistic regression classifier by setting the multi_class parameter and report the results.
  • Fit a multinomial logistic regression classifier by setting the multi_class parameter and report the results.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Fit one-vs-rest logistic regression classifier
lr_ovr = ____
lr_ovr.fit(X_train, y_train)

print("OVR training accuracy:", lr_ovr.score(X_train, y_train))
print("OVR test accuracy    :", lr_ovr.score(X_test, y_test))

# Fit softmax classifier
lr_mn = ____
lr_mn.fit(X_train, y_train)

print("Softmax training accuracy:", lr_mn.score(X_train, y_train))
print("Softmax test accuracy    :", lr_mn.score(X_test, y_test))
Edit and Run Code