mlpack
3.0.0
|
Introduction
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.
Installing mlpack
(This section will be simplified when mlpack is available in PyPI or conda.)
Installing the mlpack bindings for Python is straightforward. 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.
You can also use the mlpack Docker image on Dockerhub, which has all the Python bindings pre-installed:
Simple mlpack quickstart example
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.
What else does mlpack implement?
The example above has only shown a little bit of the functionality of mlpack. Lots of other commands are available with different functionality. Below is a list of all the mlpack functionality offered through Python, split into some categories.
- Classification techniques:
adaboost()
,decision_stump()
,decision_tree()
,hmm_train()
,hmm_generate()
,hmm_loglik()
,hmm_viterbi()
,hoeffding_tree()
,logistic_regression()
,nbc()
,perceptron()
,random_forest()
,softmax_regression()
,cf()
- Distance-based problems:
approx_kfn()
,emst()
,fastmks()
,kfn()
,knn()
,krann()
,lsh()
,det()
- Clustering:
kmeans()
,mean_shift()
,gmm_train()
,gmm_generate()
,gmm_probability()
- Transformations:
pca()
,radical()
,local_coordinate_coding()
,sparse_coding()
,nca()
,kernel_pca()
- Regression:
linear_regression()
,lars()
- Preprocessing/other:
preprocess_binarize()
,preprocess_split()
,preprocess_describe()
,nmf()
For more information on what mlpack does, see http://www.mlpack.org/about.html. Next, let's go through another example for providing movie recommendations with mlpack.
Using mlpack for movie recommendations
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:
Next steps with mlpack
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:
Generated by
