This page describes how you can quickly get started using mlpack from Python and gives a few examples of usage, and pointers to deeper documentation.
This quickstart guide is also available for the command-line and Julia.
Installing the mlpack bindings for Python is straightforward. It's easy to use conda or pip to do this:
Otherwise, we can build the Python bindings from scratch, as follows. First we have to install the dependencies (the code below is for Ubuntu), then we can build and install mlpack. You can copy-paste the commands into your shell.
More information on the build process and details can be found on the Building mlpack From Source page. You may also need to set the environment variable LD_LIBRARY_PATH
to include /usr/local/lib/
on most Linux systems.
You can also use the mlpack Docker image on Dockerhub, which has all of the Python bindings pre-installed:
As a really simple example of how to use mlpack from Python, let's do some simple classification on a subset of the standard machine learning covertype
dataset. We'll first split the dataset into a training set and a testing set, then we'll train an mlpack random forest on the training data, and finally we'll print the accuracy of the random forest on the test dataset.
You can copy-paste this code directly into Python to run it.
We can see that we achieve reasonably good accuracy on the test dataset (80%+); if we use the full covertype.csv.gz
, the accuracy should increase significantly (but training will take longer).
It's easy to modify the code above to do more complex things, or to use different mlpack learners, or to interface with other machine learning toolkits.
The example above has only shown a little bit of the functionality of mlpack. Lots of other commands are available with different functionality. A full list of each of these commands and full documentation can be found on the following page:
For more information on what mlpack does, see https://www.mlpack.org/. Next, let's go through another example for providing movie recommendations with mlpack.
In this example, we'll train a collaborative filtering model using mlpack's cf()
method. We'll train this on the MovieLens dataset from https://grouplens.org/datasets/movielens/, and then we'll use the model that we train to give recommendations.
You can copy-paste this code directly into Python to run it.
Here is some example output, showing that user 1 seems to have good taste in movies:
Now that you have done some simple work with mlpack, you have seen how it can easily plug into a data science workflow in Python. A great thing to do next would be to look at more documentation for the Python mlpack bindings:
Also, mlpack is much more flexible from C++ and allows much greater functionality. So, more complicated tasks are possible if you are willing to write C++ (or perhaps Cython). To get started learning about mlpack in C++, the following resources might be helpful: