Azure Cognitive Services : Text Analytics API

In this notebook, we will use the Azure Cognitive Services Text Analytics API - Sentiment Analysis to predict the sentiment of the tweets.

We will compare this pre-trained cloud model to the baseline model from 1_baseline.ipynb.

Load project modules and data

We will use basic python packages, and the azure package to access the Azure Cognitive Services Text Analytics API. The secrets are stored in the .env file.

Classification Models

Now we can measure the performance of our model defined in custom_azure_text_analysis_classifier.py. We are going to use the same metrics as our baseline model defined in 1_baseline.ipynb.

Basic model from Azure Cognitive Service for Language

In this model, we will use Azure's API results to predict the sentiment of the tweets. The API returns 3 possible sentiment values: positive, negative, and neutral. The neutral value considered as half positive and half negative.

The performances on the dataset are slightly better than our baseline model :

This model is also biased towards the positive class, but less than our baseline model : it predicted 26% (baseline = 35% , -26%) more POSITIVE (1115) messages than NEGATIVE (885).

Logistic Regression model based on Azure Cognitive Service for Language

In the previous model, we did not fully use the neutral value (it was just considered as half positive and half negative). In order to use this information and try to remmove the bias from the basic model, we will train a logistic regression model on the sentiment values from the API.

The performances on the train and test datasets are identical, so we know our model is well trained (no over/under-fitting).

The new model has the same performance as our basic model.

Our model is even more biased towards the POSITIVE class than the basic model : it predicted 54% (basic model = 26% , +107%) more POSITIVE (1214) messages than NEGATIVE (786).

Let's observe some classification errors.

On this false-positive example, we can see that the model is not able to predict the NEGATIVE sentiment of the message. It predicts a strong POSITIVE sentiment even though the sentiment is rather mixed (the person seems to have a lot of work to do).

On this false-negative example, we can see that the model is not able to predict the POSITIVE sentiment of the message. But again in this case, the true sentiment is not obvious (the person is not happy to go to work).

Models comparison

Training a logistic regression model on the sentiment values from the API has not improved the classification metrics and has worsen the bias towards the POSITIVE class. We can see that the model is not able to predict the NEGATIVE sentiment of the message.