The “cluster of six”

Unsupervised machine learning

Hansard reports what’s said in the UK Parliament, sets out details of divisions, and records decisions taken during a sitting. The hansard R package provides functions to import its data.

Using the Hansard API (Application Programming Interface), we’ll apply unsupervised machine learning to analyze the voting patterns of 219 Labour Members of Parliament (MPs). We’ll consider all divisions (results of the votes) in the UK House of Commons since the 2017 general election.

Supervised machine learning makes predictions from labeled training data. The unsupervised flavour looks for hidden structure in “unlabeled” data, i.e. a classification or categorisation not included in the observations. Hierarchical clustering will identify a cluster of six MPs as the most “distant” from the wider party.

The full methodology, including the code, is published here. This extended narrative confirms the suitability of the data for clustering; reviews eight clustering methods for optimal fit; plots the full dendrogram of 219 Labour MPs; and rationalises the outcome in more detail, for example, using Cook’s Distance.

We’ll set a vote of “aye” to 1, and “no” to -1. And we’ll treat non-votes as 0. Voting the opposite way to the majority of the party, as well as non-votes, will be of interest when assessing which MPs are “most distant” from the majority.

The “cluster of six”

We’ll apply a “bottom up” clustering approach. Each MP starts in their own cluster, and pairs of clusters are progressively combined until none remains.

What we find is the above cluster of six MPs who, based on their voting patterns, are the last to merge with the wider party.

Does this cluster make sense?

Can we rationalize why machine learning has isolated this cluster? If we inspect the ten MPs recording the fewest votes since June 8th, 2017, it does include all six.

Nonetheless, non-voting will not be the only influencing factor. A small minority of MPs voting the opposite way to the overwhelming majority will influence the “distant cluster”.

Cook’s Distance visualizes these influential outliers. It shows the voting of three MPs, all on the European Union Withdrawal Bill readings, to be particular outliers. All three MPs are in the “cluster of six”.


So, in summary, we established that the data is suitable for hierarchical clustering and selected the clustering method that best fits the data. We identified a “cluster of six” MPs who are the last to combine with the rest of the party. And, by inspecting the non-votes and most influential outliers, we can rationalize why unsupervised machine learning merged these MPs last.

R toolkit

purrrmap_df; pmap; map_lgl; transpose; safely
hansardcommons_members; mp_vote_record
dplyrmutate; select; if_else; left_join; everything; summarise; group_by; arrange
tidyrspread; gather
stringrstr_replace; str_c
lubridatemonth; day; year
tibbleas_data_frame; data_frame
ggplot2geom_col; geom_text; ggtitle; coord_flip; geom_text_repel; geom_jitter
ggthemestheme_economist; economist_pal
factoextraget_clust_tendency; fviz_dist
dendextenddendlist; color_branches; assign_values_to_leaves_nodePar

View the code here.

Citations / Attributions

R Development Core Team (2008). R: A language and environment for
statistical computing. R Foundation for Statistical Computing,
Vienna, Austria. ISBN 3-900051-07-0, URL

Tal Galili (2015). dendextend: an R package for visualizing, adjusting, and comparing trees of hierarchical clustering. Bioinformatics  <doi:10.1093/bioinformratics/btv428>

Odell E (2017). hansard: Provides Easy Downloading Capabilities for the UK Parliament API. doi: 10.5281/zenodo.591264, R package version 0.5.5,

2 Replies to “The “cluster of six””

  1. I like the “R Toolkit” image. Did you create this manually or is there a function to details the functions used from different packages? I had always thought it useful to identify packages no longer required as you decided to swap a function out for an alternative.

    1. Thanks. I create the table container using the WordPress plugin TablePress. The icons are adapted from Font Awesome. The content though is manual; a function to do that would be good for the reason you describe. I like creating the table to see what new functions I’ve newly introduced in an article that I haven’t used in any prior article.

Leave a Reply

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