Getting Started with GraphLab for Machine Learning in Python
Introduction
Machine learning is one of the most powerful and widely applicable techniques in data science today. Python has become the language of choice for many data scientists, in large part due to the incredible ecosystem of open source tools and libraries for data analysis and machine learning. In recent years, a library called GraphLab Create has emerged as an exceptionally useful tool for applying machine learning in Python.
In this post, we‘ll take a detailed look at what GraphLab Create is, how it got started, and what makes it so beneficial for machine learning tasks. We‘ll walk through the process of installing GraphLab and getting started by loading and exploring a dataset. Then we‘ll see how to use GraphLab to build, evaluate, and apply machine learning models in just a few lines of code. Finally, we‘ll touch on some of the other powerful capabilities GraphLab provides beyond just machine learning.
Whether you‘re an experienced data scientist looking to add a new tool to your belt, or relatively new to machine learning and looking for a way to get started, GraphLab Create is well worth checking out. Let‘s dive in!
What is GraphLab Create?
GraphLab Create, also known as Turi Create after Apple acquired the company in 2016, is a machine learning framework designed to make it simpler to build intelligent applications at scale. It was originally created as an academic project at Carnegie Mellon University in 2009 by Prof. Carlos Guestrin and his PhD students.
Their goal was to build a better platform for machine learning that could handle the challenges of real-world, large-scale data. Existing tools at the time, like Apache Mahout, relied on Hadoop MapReduce which made machine learning pipelines slow and cumbersome. The GraphLab researchers took a different approach, building a new framework in C++ designed for fast, distributed computation.
A key realization was that many machine learning algorithms could be expressed in terms of vertex programming on a graph. By unifying diverse ML tasks within a graph-based framework, GraphLab aimed to make ML pipelines faster, more scalable, and easier to work with. This graph-centric design is reflected in the GraphLab name.
Over time, GraphLab evolved from a research project into a company focused on commercializing its technology. In 2013, GraphLab Inc launched GraphLab Create, a Python package that exposes high-level APIs for common ML tasks on top of the lower-level GraphLab graph computation engine.
Benefits of GraphLab Create
So what are the key benefits of using GraphLab Create for machine learning? There are several aspects that make it an appealing choice:
Scalability – GraphLab Create is designed to make it easy to scale ML to very large datasets. It provides data structures like SFrames and SGraphs that are not limited by memory, unlike Pandas DataFrames which are in-memory only. This allows GraphLab to handle terabyte-scale data, even on a single machine.
Performance – Built on a C++ backend optimized for fast parallel computation, GraphLab Create is able to achieve high performance on ML tasks. It utilizes your CPU cores to parallelize computations across threads.
Ease of Use – GraphLab Create aims to make machine learning more accessible by providing a high-level, user-friendly API. It includes modules and functions for common tasks like data loading, feature engineering, model building, and evaluation that can be accomplished with just a few lines of code. This allows you to focus on the ML process without getting bogged down in the low-level details.
Breadth of Features – GraphLab Create goes beyond just machine learning to provide tools for data manipulation, feature engineering, graph analytics, recommender systems, and even deep learning. Having such a wide range of features available in one unified platform makes it an appealing choice.
Visualization – A very handy feature of GraphLab Create is the ability to quickly explore and visualize your data using a browser-based GUI called Canvas. Canvas allows you to see distributions, summary statistics, and create charts and graphs with just a single command. This makes the process of getting to know a new dataset much simpler compared to matplotlib or other viz libraries.
Of course, no tool is perfect, and GraphLab Create does have a few potential drawbacks to consider. One is that it is a commercial product, released under a proprietary license. After a free 30 day trial, you‘ll need to purchase a license for commercial use (though it is free for 1 year for academic use). Additionally, since it is less widely used than tools like scikit-learn, the community and resources built around it are smaller.
Overall though, GraphLab Create‘s advantages make it a compelling option in many scenarios. Now let‘s look at how to get started using it.
Installing GraphLab Create
The first step to using GraphLab Create is to get it installed on your machine. GraphLab is shipped as a Python package, but under the hood it relies on a C++ backend, so some extra installation steps are required beyond a simple pip install.
GraphLab officially supports:
- MacOS 10.8+
- Linux (requires GCC 4.8 and above)
- Windows 7 and above
Your machine should have at least 4GB of RAM, though 8GB+ is recommended for larger datasets. If your machine doesn‘t meet the requirements, GraphLab can also be used on a cloud platform like Amazon EC2.
To install on Mac or Linux, the easiest route is to first install the Anaconda Python distribution. Then you can run:
conda update conda
conda install conda=4.0.4
conda create -n gl-env python=2.7
source activate gl-env
pip install --upgrade --no-cache-dir https://get.graphlab.com/GraphLab-Create/2.1/your-registered-email-address/your-product-key/GraphLab-Create-License.tar.gz
On Windows, it‘s recommended to use the GraphLab Create Launcher which will guide you through the installation.
Getting Started with GraphLab
Now that we have GraphLab installed, let‘s load up a dataset and start to explore how we can use it. We‘ll use the Black Friday sales dataset from a past Analytics Vidhya hackathon.
First, we can import GraphLab and load the CSV data into an SFrame:
import graphlab as gl
train_sf = gl.SFrame(‘black_friday_train.csv‘)
test_sf = gl.SFrame(‘black_friday_test.csv‘)
We can immediately start exploring the data using GraphLab‘s Canvas visualization tool:
train_sf.show()
This will open an interactive view in your browser where you can see high level statistics and distributions for each column.
For example, we can see that the dataset has both numeric and categorical columns:

We can also dive into an individual column and see more details. Here‘s the categorical distribution for the ‘Age‘ column:

Being able to quickly visualize the data like this is a big time saver when starting to work with a new dataset. The Canvas tool also supports more complex plot types like heatmaps and box plots.
In addition to visualization, GraphLab provides concise methods for data manipulation. For example, we can easily add columns or change data within an SFrame:
train_sf[‘Age_Squared‘] = train_sf[‘Age‘]**2
def bucketize_age(age):
if age == ‘0-17‘:
return ‘Young‘
elif age == ‘18-25‘ or age == ‘26-35‘:
return ‘Young Adult‘
elif age == ‘36-45‘:
return ‘Adult‘
else:
return ‘Senior‘
train_sf[‘Age_Bucket‘] = train_sf[‘Age‘].apply(bucketize_age)
Feature Engineering
Another area where GraphLab shines is feature engineering. It includes a variety of tools for transforming and creating new features from your raw data.
Some of the key feature engineering capabilities:
- Handling missing values via imputation
- Encoding categorical variables
- Scaling and normalizing numeric features
- Generating new features via binning, quadratic expansion, etc.
- Text analytics like count vectorizing, TF-IDF, etc.
Rather than having to code all of these transformations from scratch, we can utilize GraphLab‘s built-in functions:
import graphlab.feature_engineering as fe
cat_features = [‘Age‘, ‘City_Category‘, ‘Stay_In_Current_City_Years‘]
cat_imputer = gl.feature_engineering.CategoricalImputer()
train_sf = cat_imputer.fit_transform(train_sf, cat_features)
ohe = gl.feature_engineering.OneHotEncoder(features = cat_features)
train_encoded = ohe.fit_transform(train_sf)
test_encoded = ohe.transform(test_sf)
Modeling and Evaluation
With our data loaded and features prepared, we‘re ready to build some models. GraphLab includes a variety of toolkits for different ML tasks – classification, regression, clustering, recommender systems, graph analytics, and even deep learning.
For our sales prediction problem, we can use GraphLab‘s regression module. It supports linear regression, decision trees, random forests, and gradient boosted trees. We can automatically create a model tuned for our data:
model = gl.regression.create(train_encoded, target=‘Purchase‘,
features = cat_features + [‘User_ID‘, ‘Product_ID‘, ‘Gender‘, ‘Occupation‘, ‘Product_Category_1‘])
And that‘s it! With a single line of code, GraphLab will create an optimized model. We can inspect the model to understand what it did:
Class : BoostedTreesRegression
Loss : squared_loss
Number of examples : 233599
Number of feature columns : 534
Number of unpacked features : 534
Number of trees : 14
Max tree depth : 6
By default, it chose a boosted trees model. Now we can evaluate performance on a test set:
eval_results = model.evaluate(test_encoded)
print eval_results
{‘max_error‘: 12668.55, ‘rmse‘: 2549.41}
To make predictions and create a submission file:
preds = model.predict(test_encoded)
sub_df = gl.SFrame({‘User_ID‘: test_sf[‘User_ID‘], ‘Product_ID‘: test_sf[‘Product_ID‘], ‘Purchase‘: preds})
sub_df.save(‘submission.csv‘, format=‘csv‘)
And just like that, we‘ve created a submission using a gradient boosted trees model! Of course, to maximize performance you‘ll want to iterate and fine tune the process, but GraphLab makes it easy to get a first cut model built quickly.
Taking it Further
We‘ve only scratched the surface of what‘s possible with GraphLab Create. Some of the other key areas to explore:
Graph Analytics – GraphLab provides a SGraph data structure and many algorithms for analyzing graph properties and doing link prediction.
Recommender Systems – GraphLab has tools for building and evaluating recommender engines, useful for scenarios like product recommendations or content personalization.
Text Analytics – Classify or extract meaning from text using GraphLab‘s text processing and analysis capabilities.
Deployment – GraphLab‘s APIs make it straightforward to generate predictions from your trained models in production.
Conclusion
GraphLab Create is a powerful and user-friendly tool for doing machine learning in Python. It provides high-performance, scalable implementations of many common ML algorithms, as well as capabilities for data manipulation, feature engineering, and visualization.
While it requires an up-front investment of time to learn its API and a monetary investment for a commercial license, the productivity gains can make this well worthwhile. If you‘re working on a complex, large-scale machine learning problem, I‘d definitely recommend giving GraphLab Create a look.
You can find more details in the official User Guide. Happy modeling!