Kigali, The 2019 Happiness Capital

Herman Wandabwa

Africa over time has been riddled by damaging connotations. I’m in no place to defend or assist such assertions as generally, such views I imagine are solely within the eyes of the proponents. That apart, I selected to take a knowledge perspective in wanting on the positives within the African continent extra so on the general citizenry happiness. Earlier than, I delve deep within the analyses, please take someday and browse via the the highest 5 2019 optimistic tales from the continent as per BBC. Instructor Tabichi needed to be there, could also be I’m simply being too Kenyan. The African gods received’t be glad if I fail mentioning my favourite choir’s efficiency on AGT, the Ndlovu Youth Choir in addition to the crowning of Springboks as World Rugby champions.

In my quest to unravel this happiness concept, I penned down just a few “analysis questions” that I believed would have led me to the top end result:-

1. Is it potential to get vital information about African international locations to find out their happiness?
2. What’s the finest computational methodology for a similar?
three. Are the outcomes verifiable by any means? Any validation units?

Knowledge about happiness index exists for many international locations, however often entails a mixture of a number of metrics and I’m not keen to go that route not less than for now. Hopefully, that may kind the validation set in future, fingers crossed. I’ve educated many analytics fashions so the methodology wasn’t arduous with the appropriate information.

Knowledge from Largest Cities by inhabitants

I ended up getting randomized tweets from completely different cities in Africa primarily based on their inhabitants and private preferences. Due to this fact, anticipate a bit of little bit of skewness within the information and private preferences however hey, outcomes are nonetheless definitive. Probably the most populous cities on this occasion are listed right here . I ended up mixing in just a few East African cities regardless of their inhabitants sizes. Abidjan, Addis Ababa, Bujumbura, Cairo, Dar Es Salaam, Johannesburg, Kampala, Kigali, Kinshasa, Lagos, Luanda, Mogadishu and Nairobi had been of curiosity within the evaluation.

Tweets Assortment

Geolocation is important within the assortment of tweets disseminated from these cities. Twitter has a sophisticated search characteristic thus this assortment is feasible to utilizing the key phrase “close to”. Check out Jefferson’s GetOldTweets repo for an in-depth search and assortment of previous tweets. You’ll thank me later. Amassing tweets say disseminated from Nairobi might be so simple as the beneath Python command.

With the above command, you’ll have the ability to accumulate as much as 1M tweets disseminated in 2019 from/close to Nairobi,Kenya.

I used to be serious about a smaller set of tweets from the cities thus simply ended up gathering 16942 random and distinctive tweets. The problem was that the tweets had been in several languages opposite to the necessities of my coaching information and methodology of selection.

Tweets Pre-processing and Translation

Tweets not like standard English texts are distinctive as they’re shortened to suit the character limits and the language of expression is unconventional generally. Due to this fact, pre-processing the identical is completely different. I used Pandas to transform the CSV to a dataframe for simpler manipulation.

The “textual content” column which is the tweet itself was of curiosity. Due to this fact, I dropped all different columns, eliminated duplicates, cease phrases in addition to empty tweets. This occurs quite a bit with tweets. The beneath code will handle all these processes.

Sadly this course of will depart a number of quick tweets empty as soon as once more as proven beneath.

Work round is to drop nulls as soon as once more as beneath as a result of will probably be a waste of assets coaching a mannequin on empty fields.

Output of the above course of is beneath.

This is smart. Sadly, our mannequin was educated on English texts, thus the necessity to translate these tweets to English, extra so from Francophone cities like Abidjan and Kinshasa. I made use of the GoogleTranslate API key to translate all tweets to English. It takes someday however will get the job accomplished utilizing the beneath code:

Output was as beneath after concatenation with the cities discipline.

BERT in Coaching our mannequin

Bidirectional Encoder Representations from Transformers (BERT) is a Pure Language Processing (NLP) approach developed by Google primarily based on transformers. From Google’s weblog , “this breakthrough was the results of Google analysis on transformers: fashions that course of phrases in relation to all the opposite phrases in a sentence, slightly than one-by-one so as. BERT fashions can subsequently think about the total context of a phrase by wanting on the phrases that come earlier than and after it — significantly helpful for understanding the intent behind search queries.”

Since BERT fashions are already educated on tens of millions of sentences and options, they carry out fairly properly on many out-of-the field NLP duties. I used Quick-Bert, a wonderful easy wrapper for the PyTorch BERT module to coach the mannequin on a dataset of tweets , with textual content because the characteristic and sentiment class (zero to four) because the label. The mannequin was then examined on our translated set of tweets.


Sadly BERT fashions are big, with tens of millions of options thus computational energy to run the fashions was an enormous subject for me. I went for a number of cloud choices and beneath are my ideas on the few that I attempted.

  1. Kaggle — Free platform with GPU assist however reminiscence allocation labored towards me. Coaching course of was killed due to reminiscence associated points.
  2. Google’s Colab — Labored properly nevertheless it too shut down severally after a many hours of coaching although was higher for this activity in comparison with Kaggle’s platform. Didn’t attempt the TPU capabilities as Pytorch shouldn’t be supported but.
  3. Google Cloud Platform — I ended up deciding on this as I used to be ready to make use of the $300 free credit score they provide. I used a small fraction of the identical so that is the very best platform for anybody serious about coaching such a mannequin. For a begin, don’t pay for GCP. Because you’ll be utilizing Digital Machines (VMs), simply be content material with ones excessive on CPUs and reminiscence. Don’t go for GPU with the paid ones. I attempted this and ended up coaching nothing along with paying $68. Simply comply with this article to setup your notebooks on GCP. You’ll save a number of time.

Further Instruments:

Python three

PyTorch is required for Quick-Bert to coach the mannequin
apex is a PyTorch extension used for distributed coaching utilized by Quick-Bert. Ignore this if utilizing CPU.

I selected DistilBERT , a smaller, quicker and cheaper mannequin to coach and arrange the CPU model as I had no entry to GPU with Google credit. The beneath code imported all vital packages for mannequin coaching along with segmenting the coaching and validation units.

The Databunch

The primary modeling step is to create the databunch. That is merely an object that takes coaching, validation and take a look at CSV recordsdata and converts them into inside illustration for BERT and its variants like DistilBERT. It additionally instantiates the proper data-loaders primarily based on the system profile, batch_size and max_sequence_length.

The learner

The educational step follows with the databunch and associated parameters as enter. Beneath is a illustration of the identical in Python the place the learner encapsulates the important thing logic for the lifecycle of the mannequin resembling coaching, validation and inference.

The mannequin is then educated as beneath.

Factoring in coaching time and assets, I simply set the variety of epochs to 1. You possibly can change this quantity for higher accuracy (not assured) when you have the assets. With 16 vCPUs and 64GB reminiscence, coaching took about 19 hours. Saving the mannequin is suggested in order that retraining shouldn’t be accomplished. The predictor object as beneath takes the mannequin and labels as enter.

Performing Sentiment Evaluation on the Check Set

Batch sentiment prediction was of selection utilizing the beneath code.

Output as proven beneath would be the label and chance of the tweet falling within the sentiment class. The sum of chances for every tweet’s sentiment output ought to be equal to 1.

Due to this fact, the label with the best chance was the expected label of selection. Based mostly on the output above, the primary tweet sentiment predicted label is zero with a 25% chance. To pick the labels with the best chance within the listing, the primary line within the code beneath labored. The output was then transformed to a dataframe within the second line.

Concatenation of the unique take a look at set and the predictions per tweet resulted within the beneath:-

The bitter-sweet second

By now, you should be having a notion of what happiness entails within the output. A sentiment label four reveals contentment to a bigger extent in comparison with the remainder. Label zero alternatively reveals dissatisfaction thus interpreted to be a tragic feeling. Due to this fact, getting the imply of sentiment scores, grouped by cities is a straightforward however believable reply to computation of the general happiness of a metropolis. The beneath code nails that.

Kigali, Rwanda takes the title of the happiest metropolis of the 13. Surprisingly, Mogadishu, Somalia is available in second regardless of the damaging tales we examine Somalia as a rustic. Nairobi, Kenya my capital is the second most sad metropolis in East Africa after Kampala, Uganda.


The above outcomes to some extent are an eye fixed openner in relation to utilizing information pushed options in deriving the happiness of individuals in sure international locations or cities. Just a few factors as I conclude:-

  1. The selection of the cities was purely primarily based on inhabitants and private preferences particularly with the selection of East African cities. It’s the very best/solely manner I can inform the African story. With time and extra assets, I’ll have the ability to get extra tweets from most capitals or international locations in Africa and make higher comparisons.
  2. The coaching set of 200Ok random tweets (although balanced in courses) is a bit of on the decrease aspect. DistilBERT mannequin too has fewer options(6M) which to a small extent compromises on the accuracy. I plan on implementing the identical with a multilingual full mannequin (110M options) with the whole 1.6M tweets coaching set. Hopefully, there might be some notable distinction.
  3. Translation particularly for shortened and contextually difficult phrases in tweets is a problem. The unique context could also be misplaced however the translator labored fairly properly on the few samples that human evaluators checked out.
  4. Validating our outcomes. It is a tough half. We primarily based our information on social information, extra so tweets. Tweets depict each day chatter in respective cities thus are finest positioned to measure total happiness for the disseminators. Nonetheless, the methodologies at present in place consider a number of different elements in figuring out this happiness. For instance, the UN world happiness report is one among such thus utilizing it as floor reality might be legitimate if different elements are integrated within the mannequin. That is open for dialogue.

Leave a Reply

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