Get startedGet started for free

A second toy model

In this exercise, you will build another classifier using logistic regression on a dataset of images. Each image represents a number 0 through 9. The goal is to classify each imagine as a number - for example, a 7 or a 9. The features used are specific pixel values ranging from 0-16 that make up the image. Instead of evaluating accuracy by hand, you will evaluate the accuracy of the model using accuracy_score() from sklearn.

Sample image data is loaded as image_data along with sklearn and pandas as pd. LogisticRegression is available via sklearn.linear_model.

This exercise is part of the course

Predicting CTR with Machine Learning in Python

View Course

Exercise instructions

  • Get the row count of X to determine the index at which to split the training and testing data.
  • Create a logistic regression classifier.
  • Create predictions using the classifier and evaluate the accuracy using accuracy_score() from sklearn.metrics.

Hands-on interactive exercise

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

# Define X and y 
X = image_data.data
y = image_data.target

# Define training and testing
split = int(0.7 * ____(X))
X_train, X_test, y_train, y_test = \
	X[:split], X[split:], y[:split], y[split:] 

# Create logistic regression classifier
clf = ____()

# Train classifier - predict label and evaluate accuracy
y_pred = clf.fit(X_train, y_train).____(X_test) 
print(____(y_test, y_pred))
Edit and Run Code