simple implementation of unsupervised machine learning algorithm
By: Adedamola Adejokun
Tags: Unsupervised Learning, Clustering Algorithm, Data Analytics, Data Science, Machine Learning

A Simple Implementation of Unsupervised Machine Learning

Canadian University Clusters

Photo by Agence Olloweb on Unsplash

Unlike supervised machine learning which fits a model to a dataset with reference to a target label, unsupervised machine learning algorithms are allowed to determine patterns in the dataset without recourse to a target label. A label may refer to whether a customer defaults on a bank loan, or which drug is effective against a particular ailment amongst a number of patients. Clustering and Anomaly Detection are common applications of unsupervised machine learning. My aim is to delineate the exciting capabilities of Clustering by presenting a simple implementation with a relatable use case.

Introduction

Thousands of foreign nationals troop into Canada every year in search of University education. This is due largely to Canada’s pro-immigration policy, quality education, comparative lower tuition, and consistently high ranking for overall quality of life. Prospective students are presented with a deluge of options from which to make a choice, as Canada has a plethora of reputable Universities across its provinces and territories.

In order to optimize the search experience of prospective students, Canadian Universities are organized into clusters predicated upon pre-selected features (or attributes). The clusters are derived from the implementation of the k-means Clustering Machine Learning algorithm from Python’s scikit-learn library, on the defined feature set. Each cluster comprises Universities having a unique combination of features, which in turn describe the clusters. Prospective students can then identify the cluster into which to focus their search based on the accompanying cluster description. Thus, the search experience is refined, effective, and less time-consuming.

Data

The dataset used for this study was assembled from scratch. The sources of the components of the final dataset are given below:

Canadian University List : Scrapped from the List of Universities in Canada[1] using the BeautifulSoup Python library.

: Scrapped from the List of Universities in Canada[1] using the BeautifulSoup Python library. Canadian University Ranking : Scrapped from the Canadian University Ranking[2] using the BeautifulSoup Python library.

: Scrapped from the Canadian University Ranking[2] using the BeautifulSoup Python library. Provincial Rent Rates : Sourced from rentals.ca Provincial Median Rent Rates[3].

: Sourced from rentals.ca Provincial Median Rent Rates[3]. Geographical Coordinates of Universities : The geopy Python library was engaged. The University names were passed as arguments and the respective coordinates were returned.

: The geopy Python library was engaged. The University names were passed as arguments and the respective coordinates were returned. Location Data (Recreational Index): Data for venues within 500 meters of each University was obtained as location data with the Foursquare API. The Recreational Index for each University was then derived from the unique venue categories.

Methodology

Data Gathering

As detailed in the preceding section, the dataset used in this project was put together from different sources to fulfill the objective. The BeautifulSoup Python library was used to scrape the Canadian University List and Rankings[1][2]. The data for the Provincial Rent Rates was sourced from the Rentals.ca May 2020 Rent Report[3]. University coordinates were generated with the geopy library. Lastly, the Recreational Index, which is a measure of the availability of ‘fun’ spots within 500 meters of the institution, was obtained as location data using the Foursquare API. A series of cleaning, merge and join operations were involved in building the final dataset.

The code used to scrape the Canadian University list is given below:

Resulting Dataframe was cleaned with the following code:

The series of code below describes the formulation of the Recreational Index, beginning with location data from Foursquare API. First, the top 20 venues within 500 meters of the University were obtained in a Dataframe:

Next, the unique venue categories were obtained and ‘counted’ to give the Recreational Index.

Feature Set Categorization

The selection of the feature set preceded the implementation of the machine learning algorithm. The feature set included the following columns: ‘Rankings’, ‘Rental Rates’, and ‘Recreational Index’. These features were then converted to categorical variables upon which one-hot encoding was subsequently performed to facilitate the analysis of machine learning algorithm output.

Categorization was effected with the following code:

Feature Categories

The above categorization was informed by exploratory data analysis performed on the feature set:

Histogram of Feature Set

Deriving University Clusters

After the dataset had been assembled, one hot encoding was implemented on the feature set. This ensured the data was transformed into a structure better suited for the implementation of the machine learning algorithm. Next, the k-means clustering machine learning algorithm was implemented on the encoded dataset.

k-means clustering was implemented with the following code:

Assumptions/Limitations

Only publicly run Universities were considered in the study. An arbitrary ranking of 50 was assigned to Universities without an official ranking to facilitate numeric data handling. The Provincial rent rates[3] employed are representative of the median monthly rates within the specified Province. Thus, actual rent rates across cities within the Province may differ from those stated.

Result and Discussion

The output from the clustering algorithm was examined. Unique descriptions were then assigned to each cluster. These descriptions were predicated upon the combination of features prevalent within each cluster and served as markers to prospective students as to which cluster to ‘smartly select’ for further exploration.

The table below shows the clusters and their respective descriptions:

Unique Clusters and Descriptions

Further examination of the clusters revealed the following:

Universities in Provinces with ‘cheap’ median rent do not have an accompanying ‘exciting’ recreation (i.e. the recreational index is either ‘fun’ or ‘sparse’).

Universities in Provinces with ‘expensive’ and ‘luxury’ median rent have the full spectrum for rankings and recreation as options.

None of the low ranking or unranked Universities has accompanying ‘exciting’ recreation.

Every high ranking University is situated in a Province with either an ‘expensive’ or ‘luxury’ median rent.

The following charts provide more insight into the clusters. First, the function defined to generate each chart is presented:

University Distribution in Clusters Based on Ranking Feature

Gleaning insight from the above chart, students looking to study in high ranking Universities would directly explore the options in Cluster 8.

University Distribution in Clusters Based on Rental Rates Feature

Prospective students whose primary consideration is ‘cheap’ rent would readily explore Universities in Cluster 0, based on the above chart.

Furthermore, the figure below shows that for prospective students who consider recreation as an absolute imperative, Clusters 2 and 7 offer exclusively ‘exciting’ Universities. Clusters 4, 5, and 8 also present further options.

University Distribution in Clusters Based on Recreational Index Feature

It should however be noted that no single chart would be effective in isolation, as prospective students would usually have at least two features they consider imperative to their University education experience. Consequently, the charts should be used in combination for the best results. Review the clusters and their respective composition here. Click here to view the code on GitHub.

Conclusion

Canadian Universities have been clustered based on defined features: University ranking, Provincial rent rates, and Recreational Index. The clusters were obtained using the popular k-means machine learning clustering algorithm, while the location data upon which the novel Recreational Index was derived, was generated engaging the Foursquare API. The clusters have been uniquely characterized, each distinctly comprising Universities having a combination of the defined features. Prospective students can now confidently rely on the clusters to streamline their search for their dream Canadian University.

References

[1] List of Universities in Canada (12 May 2020). Wikipedia, the Free Encyclopedia. Retrieved 18 May 2020, from https://en.wikipedia.org/wiki/List_of_universities_in_Canada#mw-head

[2] Best Global Universities in Canada (21 Oct 2019). U.S. News and World Report LP

[3] May 2020 Rent Report (n.p.). Rentals.ca. Retrieved 18, May 2020, from https://rentals.ca/national-rent-report#provincial-rental-rates