Start refactoring the README (let's see how it looks!).
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
# mlpack in R quickstart guide
|
||||
|
||||
This page describes how you can quickly get started using mlpack from R and
|
||||
gives a few examples of usage, and pointers to deeper documentation.
|
||||
|
||||
This quickstart guide is also available for [Python]( ), [Julia]( ),
|
||||
[the command line]( ), and [Go]( ).
|
||||
|
||||
## Installing mlpack
|
||||
|
||||
Installing the mlpack bindings for R is straightforward; you can just use
|
||||
CRAN:
|
||||
|
||||
```r
|
||||
install.packages('mlpack')
|
||||
```
|
||||
|
||||
Building the R bindings from scratch is a little more in-depth, though. For
|
||||
information on that, follow the instructions in the [main README]( ).
|
||||
|
||||
## Simple mlpack quickstart example
|
||||
|
||||
As a really simple example of how to use mlpack from R, 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 R to run it.
|
||||
|
||||
```r
|
||||
if(!requireNamespace("data.table", quietly = TRUE)) { install.packages("data.table") }
|
||||
suppressMessages({
|
||||
library("mlpack")
|
||||
library("data.table")
|
||||
})
|
||||
|
||||
# Load the dataset from an online URL. Replace with 'covertype.csv.gz' if you
|
||||
# want to use on the full dataset.
|
||||
df <- fread("https://www.mlpack.org/datasets/covertype-small.csv.gz")
|
||||
|
||||
# Split the labels.
|
||||
labels <- df[, .(label)]
|
||||
dataset <- df[, label:=NULL]
|
||||
|
||||
# Split the dataset using mlpack.
|
||||
prepdata <- preprocess_split(input = dataset,
|
||||
input_labels = labels,
|
||||
test_ratio = 0.3,
|
||||
verbose = TRUE)
|
||||
|
||||
# Train a random forest.
|
||||
output <- random_forest(training = prepdata$training,
|
||||
labels = prepdata$training_labels,
|
||||
print_training_accuracy = TRUE,
|
||||
num_trees = 10,
|
||||
minimum_leaf_size = 3,
|
||||
verbose = TRUE)
|
||||
rf_model <- output$output_model
|
||||
|
||||
# Predict the labels of the test points.
|
||||
output <- random_forest(input_model = rf_model,
|
||||
test = prepdata$test,
|
||||
verbose = TRUE)
|
||||
|
||||
# Now print the accuracy. The third return value ('probabilities'), which we
|
||||
# ignored here, could also be used to generate an ROC curve.
|
||||
correct <- sum(output$predictions == prepdata$test_labels)
|
||||
cat(correct, "out of", length(prepdata$test_labels), "test points correct",
|
||||
correct / length(prepdata$test_labels) * 100.0, "%\n")
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## Using mlpack for movie recommendations
|
||||
|
||||
In this example, we'll train a collaborative filtering model using mlpack's
|
||||
[`cf()`](https://www.mlpack.org/doc/stable/r_documentation.html#cf) method.
|
||||
We'll train this on the
|
||||
[MovieLens dataset](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 R to run it.
|
||||
|
||||
```r
|
||||
if(!requireNamespace("data.table", quietly = TRUE)) { install.packages("data.table") }
|
||||
suppressMessages({
|
||||
library("mlpack")
|
||||
library("data.table")
|
||||
})
|
||||
|
||||
# First, load the MovieLens dataset. This is taken from files.grouplens.org/
|
||||
# but reposted on mlpack.org as unpacked and slightly preprocessed data.
|
||||
ratings <- fread("http://www.mlpack.org/datasets/ml-20m/ratings-only.csv.gz")
|
||||
movies <- fread("http://www.mlpack.org/datasets/ml-20m/movies.csv.gz")
|
||||
|
||||
# Hold out 10% of the dataset into a test set so we can evaluate performance.
|
||||
predata <- preprocess_split(input = ratings,
|
||||
test_ratio = 0.1,
|
||||
verbose = TRUE)
|
||||
|
||||
# Train the model. Change the rank to increase/decrease the complexity of the
|
||||
# model.
|
||||
output <- cf(training = predata$training,
|
||||
test = predata$test,
|
||||
rank = 10,
|
||||
verbose = TRUE,
|
||||
max_iteration=2,
|
||||
algorithm = "RegSVD")
|
||||
cf_model <- output$output_model
|
||||
|
||||
# Now query the 5 top movies for user 1.
|
||||
output <- cf(input_model = cf_model,
|
||||
query = matrix(1),
|
||||
recommendations = 10,
|
||||
verbose = TRUE)
|
||||
|
||||
# Get the names of the movies for user 1.
|
||||
cat("Recommendations for user 1:\n")
|
||||
for (i in 1:10) {
|
||||
cat(" ", i, ":", as.character(movies[output$output[i], 3]), "\n")
|
||||
}
|
||||
```
|
||||
|
||||
Here is some example output, showing that user 1 seems to have good taste in
|
||||
movies:
|
||||
|
||||
```
|
||||
Recommendations for user 1:
|
||||
0: Casablanca (1942)
|
||||
1: Pan's Labyrinth (Laberinto del fauno, El) (2006)
|
||||
2: Godfather, The (1972)
|
||||
3: Answer This! (2010)
|
||||
4: Life Is Beautiful (La Vita è bella) (1997)
|
||||
5: Adventures of Tintin, The (2011)
|
||||
6: Dark Knight, The (2008)
|
||||
7: Out for Justice (1991)
|
||||
8: Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)
|
||||
9: Schindler's List (1993)
|
||||
```
|
||||
|
||||
## Next steps with mlpack
|
||||
|
||||
After working through this overview to `mlpack`'s R package, we hope you are
|
||||
inspired to use `mlpack`' in your data science workflow. However, the two
|
||||
examples above have only shown a little bit of the functionality of mlpack.
|
||||
Lots of other functions are available with different functionality. A full list
|
||||
of each of these functions and full documentation can be found on the following
|
||||
page:
|
||||
|
||||
- [R documentation](https://www.mlpack.org/doc/stable/r_documentation.html)
|
||||
|
||||
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 Rcpp). To get started learning about mlpack in C++, a
|
||||
good starting point is the [C++ quickstart guide]( ).
|
||||
@@ -0,0 +1,183 @@
|
||||
# mlpack command-line quickstart guide
|
||||
|
||||
This page describes how you can quickly get started using mlpack from the
|
||||
command-line and gives a few examples of usage, and pointers to deeper
|
||||
documentation.
|
||||
|
||||
This quickstart guide is also available for [Python]( ), [R]( ), [Julia]( ), and
|
||||
[Go]( ).
|
||||
|
||||
## Installing mlpack
|
||||
|
||||
Installing mlpack is straightforward and can be done with your system's package
|
||||
manager. For instance, for Ubuntu or Debian the command is simply
|
||||
|
||||
```sh
|
||||
sudo apt-get install mlpack-bin
|
||||
```
|
||||
|
||||
On Fedora or Red Hat:
|
||||
|
||||
```sh
|
||||
sudo dnf install mlpack
|
||||
```
|
||||
|
||||
If you use a different distribution, mlpack may be packaged under a different
|
||||
name. And if it is not packaged, you can use a Docker image from Dockerhub:
|
||||
|
||||
```sh
|
||||
docker run -it mlpack/mlpack /bin/bash
|
||||
```
|
||||
|
||||
This Docker image has mlpack's command-line bindings already built and
|
||||
installed.
|
||||
|
||||
If you prefer to build mlpack from scratch, see the [main README]( ).
|
||||
|
||||
## Simple quickstart example
|
||||
|
||||
As a really simple example of how to use mlpack from the command-line, 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 your shell to run it.
|
||||
|
||||
```sh
|
||||
# Get the dataset and unpack it.
|
||||
wget https://www.mlpack.org/datasets/covertype-small.data.csv.gz
|
||||
wget https://www.mlpack.org/datasets/covertype-small.labels.csv.gz
|
||||
gunzip covertype-small.data.csv.gz covertype-small.labels.csv.gz
|
||||
|
||||
# Split the dataset; 70% into a training set and 30% into a test set.
|
||||
# Each of these options has a shorthand single-character option but here we type
|
||||
# it all out for clarity.
|
||||
mlpack_preprocess_split \
|
||||
--input_file covertype-small.data.csv \
|
||||
--input_labels_file covertype-small.labels.csv \
|
||||
--training_file covertype-small.train.csv \
|
||||
--training_labels_file covertype-small.train.labels.csv \
|
||||
--test_file covertype-small.test.csv \
|
||||
--test_labels_file covertype-small.test.labels.csv \
|
||||
--test_ratio 0.3 \
|
||||
--verbose
|
||||
|
||||
# Train a random forest.
|
||||
mlpack_random_forest \
|
||||
--training_file covertype-small.train.csv \
|
||||
--labels_file covertype-small.train.labels.csv \
|
||||
--num_trees 10 \
|
||||
--minimum_leaf_size 3 \
|
||||
--print_training_accuracy \
|
||||
--output_model_file rf-model.bin \
|
||||
--verbose
|
||||
|
||||
# Now predict the labels of the test points and print the accuracy.
|
||||
# Also, save the test set predictions to the file 'predictions.csv'.
|
||||
mlpack_random_forest \
|
||||
--input_model_file rf-model.bin \
|
||||
--test_file covertype-small.test.csv \
|
||||
--test_labels_file covertype-small.test.labels.csv \
|
||||
--predictions_file predictions.csv \
|
||||
--verbose
|
||||
```
|
||||
|
||||
We can see by looking at the output that we achieve reasonably good accuracy on
|
||||
the test dataset (80%+). The file `predictions.csv` could also be used by
|
||||
other tools; for instance, we can easily calculate the number of points that
|
||||
were predicted incorrectly:
|
||||
|
||||
```sh
|
||||
$ diff -U 0 predictions.csv covertype-small.test.labels.csv | grep '^@@' | wc -l
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## Using mlpack for movie recommendations
|
||||
|
||||
In this example, we'll train a collaborative filtering model using mlpack's
|
||||
`mlpack_cf` program. We'll train this on the
|
||||
[MovieLens dataset](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 the command line to run it.
|
||||
|
||||
```sh
|
||||
wget https://www.mlpack.org/datasets/ml-20m/ratings-only.csv.gz
|
||||
wget https://www.mlpack.org/datasets/ml-20m/movies.csv.gz
|
||||
gunzip ratings-only.csv.gz
|
||||
gunzip movies.csv.gz
|
||||
|
||||
# Hold out 10% of the dataset into a test set so we can evaluate performance.
|
||||
mlpack_preprocess_split \
|
||||
--input_file ratings-only.csv \
|
||||
--training_file ratings-train.csv \
|
||||
--test_file ratings-test.csv \
|
||||
--test_ratio 0.1 \
|
||||
--verbose
|
||||
|
||||
# Train the model. Change the rank to increase/decrease the complexity of the
|
||||
# model.
|
||||
mlpack_cf \
|
||||
--training_file ratings-train.csv \
|
||||
--test_file ratings-test.csv \
|
||||
--rank 10 \
|
||||
--algorithm RegSVD \
|
||||
--output_model_file cf-model.bin \
|
||||
--verbose
|
||||
|
||||
# Now query the 5 top movies for user 1.
|
||||
echo "1" > query.csv;
|
||||
mlpack_cf \
|
||||
--input_model_file cf-model.bin \
|
||||
--query_file query.csv \
|
||||
--recommendations 10 \
|
||||
--output_file recommendations.csv \
|
||||
--verbose
|
||||
|
||||
# Get the names of the movies for user 1.
|
||||
echo "Recommendations for user 1:"
|
||||
for i in `seq 1 10`; do
|
||||
item=`cat recommendations.csv | awk -F',' '{ print $'$i' }'`;
|
||||
head -n $(($item + 2)) movies.csv | tail -1 | \
|
||||
sed 's/^[^,]*,[^,]*,//' | \
|
||||
sed 's/\(.*\),.*$/\1/' | sed 's/"//g';
|
||||
done
|
||||
```
|
||||
|
||||
Here is some example output, showing that user 1 seems to have good taste in
|
||||
movies:
|
||||
|
||||
```
|
||||
Recommendations for user 1:
|
||||
Casablanca (1942)
|
||||
Pan's Labyrinth (Laberinto del fauno, El) (2006)
|
||||
Godfather, The (1972)
|
||||
Answer This! (2010)
|
||||
Life Is Beautiful (La Vita è bella) (1997)
|
||||
Adventures of Tintin, The (2011)
|
||||
Dark Knight, The (2008)
|
||||
Out for Justice (1991)
|
||||
Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)
|
||||
Schindler's List (1993)
|
||||
```
|
||||
## Next steps wtih mlpack
|
||||
|
||||
For more information on what mlpack does, see the [mlpack
|
||||
homepage](https://www.mlpack.org). Next, let's go through another example for
|
||||
providing movie recommendations with mlpack.
|
||||
|
||||
Now that you have done some simple work with mlpack, you have seen how it can
|
||||
easily plug into a data science production workflow for the command line. But
|
||||
these two examples have only shown a little bit of the functionality of mlpack.
|
||||
Lots of other commands are available with different functionality. A full list
|
||||
of commands and full documentation for each can be found on the following page:
|
||||
|
||||
- [CLI program documentation](https://www.mlpack.org/doc/stable/cli_documentation.html)
|
||||
|
||||
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++. To get started learning about mlpack in C++, the [C++ quickstart]( )
|
||||
is a good place to start.
|
||||
@@ -0,0 +1,200 @@
|
||||
# mlpack in Go quickstart guide
|
||||
|
||||
This page describes how you can quickly get started using mlpack from Go and
|
||||
gives a few examples of usage, and pointers to deeper documentation.
|
||||
|
||||
This quickstart guide is also available for [Python]( ), [Julia]( ),
|
||||
[the command line]( ), and [R]( ).
|
||||
|
||||
## Installing mlpack
|
||||
|
||||
Installing the mlpack bindings for Go is somewhat time-consuming as the library
|
||||
must be built; you can run the following code:
|
||||
|
||||
```sh
|
||||
go get -u -d mlpack.org/v1/mlpack
|
||||
cd ${GOPATH}/src/mlpack.org/v1/mlpack
|
||||
make install
|
||||
```
|
||||
Building the Go bindings from scratch is a little more in-depth, though. For
|
||||
information on that, follow the instructions in the [main README]( ).
|
||||
|
||||
## Simple mlpack quickstart example
|
||||
|
||||
As a really simple example of how to use mlpack from Go, 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 main.go to run it.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"mlpack.org/v1/mlpack"
|
||||
"fmt"
|
||||
)
|
||||
func main() {
|
||||
|
||||
// Download dataset.
|
||||
mlpack.DownloadFile("https://www.mlpack.org/datasets/covertype-small.data.csv.gz",
|
||||
"data.csv.gz")
|
||||
mlpack.DownloadFile("https://www.mlpack.org/datasets/covertype-small.labels.csv.gz",
|
||||
"labels.csv.gz")
|
||||
|
||||
// Extract/Unzip the dataset.
|
||||
mlpack.UnZip("data.csv.gz", "data.csv")
|
||||
dataset, _ := mlpack.Load("data.csv")
|
||||
|
||||
mlpack.UnZip("labels.csv.gz", "labels.csv")
|
||||
labels, _ := mlpack.Load("labels.csv")
|
||||
|
||||
// Split the dataset using mlpack.
|
||||
params := mlpack.PreprocessSplitOptions()
|
||||
params.InputLabels = labels
|
||||
params.TestRatio = 0.3
|
||||
params.Verbose = true
|
||||
test, test_labels, train, train_labels :=
|
||||
mlpack.PreprocessSplit(dataset, params)
|
||||
|
||||
// Train a random forest.
|
||||
rf_params := mlpack.RandomForestOptions()
|
||||
rf_params.NumTrees = 10
|
||||
rf_params.MinimumLeafSize = 3
|
||||
rf_params.PrintTrainingAccuracy = true
|
||||
rf_params.Training = train
|
||||
rf_params.Labels = train_labels
|
||||
rf_params.Verbose = true
|
||||
rf_model, _, _ := mlpack.RandomForest(rf_params)
|
||||
|
||||
// Predict the labels of the test points.
|
||||
rf_params_2 := mlpack.RandomForestOptions()
|
||||
rf_params_2.Test = test
|
||||
rf_params_2.InputModel = &rf_model
|
||||
rf_params_2.Verbose = true
|
||||
_, predictions, _ := mlpack.RandomForest(rf_params_2)
|
||||
|
||||
// Now print the accuracy.
|
||||
rows, _ := predictions.Dims()
|
||||
var sum int = 0
|
||||
for i := 0; i < rows; i++ {
|
||||
if (predictions.At(i, 0) == test_labels.At(i, 0)) {
|
||||
sum = sum + 1
|
||||
}
|
||||
}
|
||||
fmt.Print(sum, " correct out of ", rows, " (",
|
||||
(float64(sum) / float64(rows)) * 100, "%).\n")
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## Using mlpack for movie recommendations
|
||||
|
||||
In this example, we'll train a collaborative filtering model using mlpack's
|
||||
[`cf()`](https://www.mlpack.org/doc/stable/go_documentation.html#cf) method.
|
||||
We'll train this on the
|
||||
[MovieLens dataset](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 main.go to run it.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/frictionlessdata/tableschema-go/csv"
|
||||
"mlpack.org/v1/mlpack"
|
||||
"gonum.org/v1/gonum/mat"
|
||||
"fmt"
|
||||
)
|
||||
func main() {
|
||||
|
||||
// Download dataset.
|
||||
mlpack.DownloadFile("https://www.mlpack.org/datasets/ml-20m/ratings-only.csv.gz",
|
||||
"ratings-only.csv.gz")
|
||||
mlpack.DownloadFile("https://www.mlpack.org/datasets/ml-20m/movies.csv.gz",
|
||||
"movies.csv.gz")
|
||||
|
||||
// Extract dataset.
|
||||
mlpack.UnZip("ratings-only.csv.gz", "ratings-only.csv")
|
||||
ratings, _ := mlpack.Load("ratings-only.csv")
|
||||
|
||||
mlpack.UnZip("movies.csv.gz", "movies.csv")
|
||||
table, _ := csv.NewTable(csv.FromFile("movies.csv"), csv.LoadHeaders())
|
||||
movies, _ := table.ReadColumn("title")
|
||||
|
||||
// Split the dataset using mlpack.
|
||||
params := mlpack.PreprocessSplitOptions()
|
||||
params.TestRatio = 0.1
|
||||
params.Verbose = true
|
||||
ratings_test, _, ratings_train, _ := mlpack.PreprocessSplit(ratings, params)
|
||||
|
||||
// Train the model. Change the rank to increase/decrease the complexity of the
|
||||
// model.
|
||||
cf_params := mlpack.CfOptions()
|
||||
cf_params.Training = ratings_train
|
||||
cf_params.Test = ratings_test
|
||||
cf_params.Rank = 10
|
||||
cf_params.Verbose = true
|
||||
cf_params.Algorithm = "RegSVD"
|
||||
_, cf_model := mlpack.Cf(cf_params)
|
||||
|
||||
// Now query the 5 top movies for user 1.
|
||||
cf_params_2 := mlpack.CfOptions()
|
||||
cf_params_2.InputModel = &cf_model
|
||||
cf_params_2.Recommendations = 10
|
||||
cf_params_2.Query = mat.NewDense(1, 1, []float64{1})
|
||||
cf_params_2.Verbose = true
|
||||
cf_params_2.MaxIterations = 10
|
||||
output, _ := mlpack.Cf(cf_params_2)
|
||||
|
||||
// Get the names of the movies for user 1.
|
||||
fmt.Println("Recommendations for user 1")
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println(i, ":", movies[int(output.At(0 , i))])
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Here is some example output, showing that user 1 seems to have good taste in
|
||||
movies:
|
||||
|
||||
```
|
||||
Recommendations for user 1:
|
||||
0: Casablanca (1942)
|
||||
1: Pan's Labyrinth (Laberinto del fauno, El) (2006)
|
||||
2: Godfather, The (1972)
|
||||
3: Answer This! (2010)
|
||||
4: Life Is Beautiful (La Vita è bella) (1997)
|
||||
5: Adventures of Tintin, The (2011)
|
||||
6: Dark Knight, The (2008)
|
||||
7: Out for Justice (1991)
|
||||
8: Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)
|
||||
9: Schindler's List (1993)
|
||||
```
|
||||
|
||||
## 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 Go. But the two examples above have
|
||||
only shown a little bit of the functionality of mlpack. Lots of other methods
|
||||
are available with different functionality. A full list of each of these
|
||||
methods and full documentation can be found on the following page:
|
||||
|
||||
- [mlpack Go binding documentation](https://www.mlpack.org/doc/stable/go_documentation.html)
|
||||
|
||||
You can also use GoDoc to explore the `mlpack` module and its functions; every
|
||||
function comes with comprehensive documentation.
|
||||
|
||||
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++. To get started learning about mlpack in C++, the [C++ quickstart]( )
|
||||
is a good resource to visit next.
|
||||
@@ -0,0 +1,161 @@
|
||||
# mlpack in Julia quickstart guide
|
||||
|
||||
This page describes how you can quickly get started using mlpack from Julia and
|
||||
gives a few examples of usage, and pointers to deeper documentation.
|
||||
|
||||
This quickstart guide is also available for [Python]( ), [the command line]( ),
|
||||
[R]( ), and [Go]( ).
|
||||
|
||||
## Installing mlpack
|
||||
|
||||
Installing the mlpack bindings for Julia is straightforward; you can just use
|
||||
`Pkg`:
|
||||
|
||||
```julia
|
||||
using Pkg
|
||||
Pkg.add("mlpack")
|
||||
```
|
||||
|
||||
Building the Julia bindings from scratch is a little more in-depth, though. For
|
||||
information on that, follow the instructions in the [main README]( ).
|
||||
|
||||
## Simple quickstart example
|
||||
|
||||
As a really simple example of how to use mlpack from Julia, 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 Julia to run it. You may need to add
|
||||
some extra packages with, e.g., `using Pkg; Pkg.add("CSV");
|
||||
Pkg.add("DataFrames"); Pkg.add("Libz")`.
|
||||
|
||||
```julia
|
||||
using CSV
|
||||
using DataFrames
|
||||
using Libz
|
||||
using mlpack
|
||||
|
||||
# Load the dataset from an online URL. Replace with 'covertype.csv.gz' if you
|
||||
# want to use on the full dataset.
|
||||
df = CSV.read(ZlibInflateInputStream(open(download(
|
||||
"http://www.mlpack.org/datasets/covertype-small.csv.gz"))))
|
||||
|
||||
# Split the labels.
|
||||
labels = df[!, :label][:]
|
||||
dataset = select!(df, Not(:label))
|
||||
|
||||
# Split the dataset using mlpack.
|
||||
test, test_labels, train, train_labels = mlpack.preprocess_split(
|
||||
dataset,
|
||||
input_labels=labels,
|
||||
test_ratio=0.3)
|
||||
|
||||
# Train a random forest.
|
||||
rf_model, _, _ = mlpack.random_forest(training=train,
|
||||
labels=train_labels,
|
||||
print_training_accuracy=true,
|
||||
num_trees=10,
|
||||
minimum_leaf_size=3)
|
||||
|
||||
# Predict the labels of the test points.
|
||||
_, predictions, _ = mlpack.random_forest(input_model=rf_model,
|
||||
test=test)
|
||||
|
||||
# Now print the accuracy. The third return value ('probabilities'), which we
|
||||
# ignored here, could also be used to generate an ROC curve.
|
||||
correct = sum(predictions .== test_labels)
|
||||
print("$(correct) out of $(length(test_labels)) test points correct " *
|
||||
"($(correct / length(test_labels) * 100.0)%).\n")
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## Using mlpack for movie recommendations
|
||||
|
||||
In this example, we'll train a collaborative filtering model using mlpack's
|
||||
[`cf()`](https://www.mlpack.org/doc/stable/julia_documentation.html#cf) method.
|
||||
We'll train this on the
|
||||
[MovieLens dataset](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 Julia to run it.
|
||||
|
||||
```julia
|
||||
using CSV
|
||||
using mlpack
|
||||
using Libz
|
||||
using DataFrames
|
||||
|
||||
# Load the dataset from an online URL. Replace with 'covertype.csv.gz' if you
|
||||
# want to use on the full dataset.
|
||||
ratings = CSV.read(ZlibInflateInputStream(open(download(
|
||||
"http://www.mlpack.org/datasets/ml-20m/ratings-only.csv.gz"))))
|
||||
movies = CSV.read(ZlibInflateInputStream(open(download(
|
||||
"http://www.mlpack.org/datasets/ml-20m/movies.csv.gz"))))
|
||||
|
||||
# Hold out 10% of the dataset into a test set so we can evaluate performance.
|
||||
ratings_test, _, ratings_train, _ = mlpack.preprocess_split(ratings;
|
||||
test_ratio=0.1, verbose=true)
|
||||
|
||||
# Train the model. Change the rank to increase/decrease the complexity of the
|
||||
# model.
|
||||
_, cf_model = mlpack.cf(training=ratings_train,
|
||||
test=ratings_test,
|
||||
rank=10,
|
||||
verbose=true,
|
||||
algorithm="RegSVD")
|
||||
|
||||
# Now query the 5 top movies for user 1.
|
||||
output, _ = mlpack.cf(input_model=cf_model,
|
||||
query=[1],
|
||||
recommendations=10,
|
||||
verbose=true,
|
||||
max_iterations=10)
|
||||
|
||||
print("Recommendations for user 1:\n")
|
||||
for i in 1:10
|
||||
print(" $(i): $(movies[output[i], :][3])\n")
|
||||
end
|
||||
```
|
||||
|
||||
Here is some example output, showing that user 1 seems to have good taste in
|
||||
movies:
|
||||
|
||||
```
|
||||
Recommendations for user 1:
|
||||
0: Casablanca (1942)
|
||||
1: Pan's Labyrinth (Laberinto del fauno, El) (2006)
|
||||
2: Godfather, The (1972)
|
||||
3: Answer This! (2010)
|
||||
4: Life Is Beautiful (La Vita è bella) (1997)
|
||||
5: Adventures of Tintin, The (2011)
|
||||
6: Dark Knight, The (2008)
|
||||
7: Out for Justice (1991)
|
||||
8: Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)
|
||||
9: Schindler's List (1993)
|
||||
```
|
||||
|
||||
## 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 Julia. But the two examples above
|
||||
have only shown a little bit of the functionality of mlpack. Lots of other
|
||||
functions are available with different functionality. A full list of each of
|
||||
these commands and full documentation can be found on the following page:
|
||||
|
||||
- [Julia documentation](https://www.mlpack.org/doc/stable/julia_documentation.html)
|
||||
|
||||
You can also use the Julia REPL to explore the `mlpack` module and its
|
||||
functions; every function comes with comprehensive documentation.
|
||||
|
||||
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 CxxWrap.jl). To get started learning about mlpack in C++,
|
||||
the [C++ quickstart]( ) would be a good place to start.
|
||||
@@ -0,0 +1,169 @@
|
||||
# mlpack in Python quickstart guide
|
||||
|
||||
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]( ), [Julia]( ),
|
||||
[R]( ), and [Go]( ).
|
||||
|
||||
## Installing mlpack
|
||||
|
||||
Installing the mlpack bindings for Python is straightforward. It's easy to use
|
||||
`conda` or `pip` to do this:
|
||||
|
||||
```sh
|
||||
pip install mlpack
|
||||
```
|
||||
|
||||
```sh
|
||||
conda install -c conda-forge mlpack
|
||||
```
|
||||
|
||||
You can also use the mlpack Docker image on Dockerhub, which has all of the
|
||||
Python bindings pre-installed:
|
||||
|
||||
```sh
|
||||
docker run -it mlpack/mlpack /bin/bash
|
||||
```
|
||||
|
||||
Otherwise, you can build the Python bindings from scratch using the
|
||||
documentation in the [main README]( ).
|
||||
|
||||
## 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.
|
||||
|
||||
```py
|
||||
import mlpack
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
# Load the dataset from an online URL. Replace with 'covertype.csv.gz' if you
|
||||
# want to use on the full dataset.
|
||||
df = pd.read_csv('http://www.mlpack.org/datasets/covertype-small.csv.gz')
|
||||
|
||||
# Split the labels.
|
||||
labels = df['label']
|
||||
dataset = df.drop('label', 1)
|
||||
|
||||
# Split the dataset using mlpack. The output comes back as a dictionary,
|
||||
# which we'll unpack for clarity of code.
|
||||
output = mlpack.preprocess_split(input=dataset,
|
||||
input_labels=labels,
|
||||
test_ratio=0.3)
|
||||
training_set = output['training']
|
||||
training_labels = output['training_labels']
|
||||
test_set = output['test']
|
||||
test_labels = output['test_labels']
|
||||
|
||||
# Train a random forest.
|
||||
output = mlpack.random_forest(training=training_set,
|
||||
labels=training_labels,
|
||||
print_training_accuracy=True,
|
||||
num_trees=10,
|
||||
minimum_leaf_size=3)
|
||||
random_forest = output['output_model']
|
||||
|
||||
# Predict the labels of the test points.
|
||||
output = mlpack.random_forest(input_model=random_forest,
|
||||
test=test_set)
|
||||
|
||||
# Now print the accuracy. The 'probabilities' output could also be used
|
||||
# to generate an ROC curve.
|
||||
correct = np.sum(
|
||||
output['predictions'] == np.reshape(test_labels, (test_labels.shape[0],)))
|
||||
print(str(correct) + ' correct out of ' + str(len(test_labels)) + ' (' +
|
||||
str(100 * float(correct) / float(len(test_labels))) + '%).')
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## Using mlpack for movie recommendations
|
||||
|
||||
In this example, we'll train a collaborative filtering model using mlpack's
|
||||
[`cf()`](https://www.mlpack.org/doc/stable/python_documentation.html#cf) method.
|
||||
We'll train this on the
|
||||
[MovieLens dataset](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.
|
||||
|
||||
```py
|
||||
import mlpack
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
# First, load the MovieLens dataset. This is taken from files.grouplens.org/
|
||||
# but reposted on mlpack.org as unpacked and slightly preprocessed data.
|
||||
ratings = pd.read_csv('http://www.mlpack.org/datasets/ml-20m/ratings-only.csv.gz')
|
||||
movies = pd.read_csv('http://www.mlpack.org/datasets/ml-20m/movies.csv.gz')
|
||||
|
||||
# Hold out 10% of the dataset into a test set so we can evaluate performance.
|
||||
output = mlpack.preprocess_split(input=ratings, test_ratio=0.1, verbose=True)
|
||||
ratings_train = output['training']
|
||||
ratings_test = output['test']
|
||||
|
||||
# Train the model. Change the rank to increase/decrease the complexity of the
|
||||
# model.
|
||||
output = mlpack.cf(training=ratings_train,
|
||||
test=ratings_test,
|
||||
rank=10,
|
||||
verbose=True,
|
||||
algorithm='RegSVD')
|
||||
cf_model = output['output_model']
|
||||
|
||||
# Now query the 5 top movies for user 1.
|
||||
output = mlpack.cf(input_model=cf_model,
|
||||
query=[[1]],
|
||||
recommendations=10,
|
||||
verbose=True)
|
||||
|
||||
# Get the names of the movies for user 1.
|
||||
print("Recommendations for user 1:")
|
||||
for i in range(10):
|
||||
print(" " + str(i) + ": " + str(movies.loc[movies['movieId'] ==
|
||||
output['output'][0, i]].iloc[0]['title']))
|
||||
```
|
||||
|
||||
Here is some example output, showing that user 1 seems to have good taste in
|
||||
movies:
|
||||
|
||||
```
|
||||
Recommendations for user 1:
|
||||
0: Casablanca (1942)
|
||||
1: Pan's Labyrinth (Laberinto del fauno, El) (2006)
|
||||
2: Godfather, The (1972)
|
||||
3: Answer This! (2010)
|
||||
4: Life Is Beautiful (La Vita è bella) (1997)
|
||||
5: Adventures of Tintin, The (2011)
|
||||
6: Dark Knight, The (2008)
|
||||
7: Out for Justice (1991)
|
||||
8: Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)
|
||||
9: Schindler's List (1993)
|
||||
```
|
||||
|
||||
## 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. But the two examples above
|
||||
have 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:
|
||||
|
||||
- [Python documentation](https://www.mlpack.org/doc/stable/python_documentation.html)
|
||||
|
||||
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
|
||||
[C++ quickstart]( ) would be a good place to go.
|
||||
Reference in New Issue
Block a user