Get startedGet started for free

Logistic regression using Twitter data

In this exercise, you will build a logistic regression model using the tweets dataset. The target is given by the airline_sentiment, which is 0 for negative tweets, 1 for neutral, and 2 for positive ones. So, in this case, you are given a multi-class classification task. Everything we learned about binary problems applies to multi-class classification problems as well.

You will evaluate the accuracy of the model using the two different approaches from the slides.

The logistic regression function and accuracy score have been imported for you.

This exercise is part of the course

Sentiment Analysis in Python

View Course

Exercise instructions

  • Build and fit a logistic regression model using the defined X and y as arguments.
  • Calculate the accuracy of the logistic regression model.
  • Predict the labels.
  • Calculate the accuracy score using the predicted and true labels.

Hands-on interactive exercise

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

# Define the vector of targets and matrix of features
y = tweets.airline_sentiment
X = tweets.drop('airline_sentiment', axis=1)

# Build a logistic regression model and calculate the accuracy
log_reg = ____.____(X, y)
print('Accuracy of logistic regression: ', log_reg.____)

# Create an array of prediction
y_predict = log_reg.____

# Print the accuracy using accuracy score
print('Accuracy of logistic regression: ', ____(___, ____))
Edit and Run Code