Playing with happiness on Twitter in Python

Let's play a bit with Twitter data.

There is a Spanish saying that says "La felicidad va por barrios" which is used like "every dog has its day" but close-to-literally translates into "happiness goes to some neighbours". Me and some friends wanted to check if happiness in Twitter depends on your neighbourhood too. Yes, I know it is a weak justification for writing software but... ¯_(ツ)_/¯

This is my first python code so don't be hard on me.

Gathering data

The first thing we did was writing a small python script to fetch data from Twitter.

The relevant lines are like the following

I focused on twits in English for reasons we will see later. We will be dumping this data into a CSV file. Even if it is not completely optimal we will be flushing on every twit write since I want to be able to download periodically the csv to play with it meanwhile I connect to the stream.

Twitter stream allows you to filter by location and that is what I did. I was studying Spain and Germany so I defined bounding boxes for them. The bounding boxes are far from perfect but that is enough for the moment. The relevant piece of code looks like:

If you want to calculate your own bounding boxes you can use this web. The twitter API needs the array to be flat. Since I wanted to keep my locations as readable as I could I just create a function to flat the array and passed that to the api call.

Plotting the data to a map

I was using Folium to draw a map of the data I got. I did that because it was super super simple and I wanted something super super simple 😀 😀 😀

Once I got my first map I started just platting markers on the appropriate places.

So with this tiny magic spell we can show something already. For example:

We can see here that the most populated cities in Spain get the most twits as expected. Will they be happy or sad?

Sentiment analysis

Since I am not very good at writing classifiers I will just use scikit-learn as a quick solution to get our sentiment analysis in place. We can create a linear classifier really easily.

I broke the classifier into a training script and another one to apply the analysis since I will be changing my CSV often but I won't want to retrain the classifier every time.

To be honest I still need to tweak the vectorizer since I don't fully get all the parameters. I used the corpus at Thinknook to train the classifier. At the end I got a 79% of prediction which is not good at all but it is enough for me to have some fun. I will try to improve this in a further blog post.

Once the classifier is trained we can use it with:

So using pandas we can easily generate another file 'data/twits_classified.csv' with an extra column containing either positive or negative depending on the classification of the tweet.

Plotting the results

Now we have all our data in place. We just need to modify a little bit the code block that does the plot:

And there we are:

Ready to take a look around to see happy people everywhere.

Conclusions

With this obviously unfinished we can see some things

  • People tweets more happy feelings than sad. That great news. Maybe because now it is summer! Probably just displaying in a map won't give a clear idea on the proportion between happy and sad tweets in an area. I will try a heat map in a following post.
  • People in Spain don't tweet a lot in English 😀 😀 😀 but I will analyze that in a different visualization.
  • Barcelona and Madrid are the places that send the most tweets in English. Germany's distribution is much much equally distributed.
  • After reading all the data I had to filter out a lot of automatic tweets. There were some bots that tweeted the weather, a lot of Endomodo tweets, some people just posting the location... Cleaning the data a bit was not hard though.
  • I got no clue on both machine learning and data visualization. I need to work a lot more on this. 😀
  • I need to be a lot more tidy while exploring the data I retrieved. Next time I will try different visualizations. I am learning though 🙂
  • This was really fun exercise anyway. You should try on your own!

Have fun!!

Leave a Reply

Your email address will not be published. Required fields are marked *