Yelp Graph: Business Clustering Based on Check-In Data
Join the DZone community and get the full member experience.Join For Free
recently, yelp made available a sample dataset from the greater phoenix metropolitan area including around 11,000 businesses, 8,000 check-in sets, 43,000 users and 230,000 user reviews. with the help of this data, data scientists can execute real-life experiments with various data mining/machine learning algorithms. in our case, we are interested in finding out whether it is possible to visually cluster businesses by category, based purely on their check-in data. the check-in data itself is available on a day-hour level: for each business, it is possible to retrieve the number of check-ins on a sunday afternoon between 3 and 4. so, with only this data in mind, are we able to cluster businesses as being restaurants or fashion stores, based purely on the correlations calculated amongst their check-in data? for this experiment, we use the neo4j graph database for storing our check-in-based correlation graph and employ the gephi graph visualization platform for interpreting the identified business communities/clusters. as always, the full source code of this article can be found on the datablend public github repository (although you will need to acquire the dataset yourself through the yelp dataset challenge portal).
1. building the neo4j check-in correlation graph
we start by parsing both the business and check-in json-files from the yelp dataset challenge. unfortunately, check-in data is available for only 8,282 out of the 11,537 supplied businesses. in addition, many of these have only a limited set of associated check-ins. hence, in order to make sure that only relevant correlations are calculated, we ignore the ones that have less than a 100 check-ins, resulting in around 1,920 remaining businesses.
next, we try to identify the correlation between two businesses by using the pearson correlation coefficient (read this site for a nice introduction). simply put, we try to identify whether a linear association exists between the check-ins of two individual businesses. in our case, the calculation is based upon 168 data points (24 hours x 7 days), the idea being that two breakfast restaurants will get most of their check-ins from the morning till noon, while two bars will get most of their check-ins during the evening and at night. hence, we expect the correlation between two businesses of the same type to be quite high, while different types of businesses (i.e. a breakfast restaurant and a bar) will result in little or no correlation.
time to get our hands dirty. after parsing the data files, we use the existing apache.commons.math pearsonscorrelation -implementation to calculate the pairwise correlation between the check-in datasets of the 1920 businesses. if the resulting coefficient is 0.8 or higher, we consider both businesses to be correlated. we create a unique node for each business within the neo4j graph and combine them via a "correlated"-relationship.[gist id=7704584]
the generated graph contains 606 unique nodes (i.e. businesses that are correlated to at least one other business) and 2585 edges (i.e. actual correlations).
2. gephi interpretation
our next task is to observe whether groups of businesses exist that are highly correlated (i.e. highly interconnected) and identify whether these correlations makes sense. in order to do so, we import our neo4j correlation graph in gephi through the gephi neo4j plugin . once loaded, we run the modularity -function to identify meaningful communities. these computed communities are then used to partition (i.e. color) the nodes (and their related edges) so that clusters can easily be observed. next, we apply k-core filtering, in our case 3-core, to keep the subgraph from which all nodes have a degree of at least 3 (i.e. 3 relationships with other nodes). the size of the nodes (and their associated labels) is configured to be proportional with their degree. finally, we apply fruchterman-reingold lay-outing in order to clearly visualise the various clusters.
we can easily observe 8 communities, but are these clusters meaningful? the pink cluster on the right-end side is highly interconnected (i.e. all nodes of the cluster have mutual correlations). most of them can be identified as being breakfast diners (ex. the good egg , the breakfast joynt and orange table ). cool. this certainly make sense, as most of these business have check-ins early morning until early afternoon. the yellow cluster on the top contains various department stores (including costco , nordstrom rack and ikea ). again meaningful, as most of them open their doors somewhere around 10am and close around 7pm. at first sight, it seems strange that the coffee places are correlated into two separate groups (yellow cluster at the bottom and pink cluster on the top). the reason however is simple: some of them close late afternoon while others are open until midnight.
the neo4j/gephi solution works remarkably well to visually identify the various business clusters from the yelp dataset. in the next blog article, we will show how to use the k-nearest neighbours algorithm to automatically predict the type of business based upon solely the check-in information.
Published at DZone with permission of Davy Suvee, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.