\section{Introduction}\label{python_quickstart_python_quickstart_intro}
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 \doxyref{the command-\/line}{p.}{cli_quickstart} and \doxyref{Julia}{p.}{julia_quickstart}.\section{Installing mlpack}\label{python_quickstart_python_quickstart_install}
Installing the mlpack bindings for Python is straightforward. It\textquotesingle{}s easy to use conda or pip to do this\+:


\begin{DoxyCode}
pip install mlpack
\end{DoxyCode}



\begin{DoxyCode}
conda install -c conda-forge mlpack
\end{DoxyCode}


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.


\begin{DoxyCode}
sudo apt-get install libboost-all-dev g++ cmake libarmadillo-dev python-pip wget
sudo pip install cython setuptools distutils numpy pandas
wget https://www.mlpack.org/files/mlpack-3.2.2.tar.gz
tar -xvzpf mlpack-3.2.2.tar.gz
mkdir -p mlpack-3.2.2/build/ && cd mlpack-3.2.2/build/
cmake ../ && make -j4 && sudo make install
\end{DoxyCode}


More information on the build process and details can be found on the \doxyref{Building mlpack From Source}{p.}{build} page. You may also need to set the environment variable {\ttfamily L\+D\+\_\+\+L\+I\+B\+R\+A\+R\+Y\+\_\+\+P\+A\+TH} to include {\ttfamily /usr/local/lib/} on most Linux systems.


\begin{DoxyCode}
export LD\_LIBRARY\_PATH=/usr/local/lib/
\end{DoxyCode}


You can also use the mlpack Docker image on Dockerhub, which has all of the Python bindings pre-\/installed\+:


\begin{DoxyCode}
docker run -it mlpack/mlpack /bin/bash
\end{DoxyCode}
\section{Simple mlpack quickstart example}\label{python_quickstart_python_quickstart_example}
As a really simple example of how to use mlpack from Python, let\textquotesingle{}s do some simple classification on a subset of the standard machine learning {\ttfamily covertype} dataset. We\textquotesingle{}ll first split the dataset into a training set and a testing set, then we\textquotesingle{}ll train an mlpack random forest on the training data, and finally we\textquotesingle{}ll print the accuracy of the random forest on the test dataset.

You can copy-\/paste this code directly into Python to run it.


\begin{DoxyCode}
\textcolor{keyword}{import} mlpack
\textcolor{keyword}{import} pandas \textcolor{keyword}{as} pd
\textcolor{keyword}{import} numpy \textcolor{keyword}{as} np

\textcolor{comment}{# Load the dataset from an online URL.  Replace with 'covertype.csv.gz' if you}
\textcolor{comment}{# want to use on the full dataset.}
df = pd.read\_csv(\textcolor{stringliteral}{'http://www.mlpack.org/datasets/covertype-small.csv.gz'})

\textcolor{comment}{# Split the labels.}
labels = df[\textcolor{stringliteral}{'label'}]
dataset = df.drop(\textcolor{stringliteral}{'label'}, 1)

\textcolor{comment}{# Split the dataset using mlpack.  The output comes back as a dictionary,}
\textcolor{comment}{# which we'll unpack for clarity of code.}
output = mlpack.preprocess\_split(input=dataset,
                                 input\_labels=labels,
                                 test\_ratio=0.3)
training\_set = output[\textcolor{stringliteral}{'training'}]
training\_labels = output[\textcolor{stringliteral}{'training\_labels'}]
test\_set = output[\textcolor{stringliteral}{'test'}]
test\_labels = output[\textcolor{stringliteral}{'test\_labels'}]

\textcolor{comment}{# Train a random forest.}
output = mlpack.random\_forest(training=training\_set,
                              labels=training\_labels,
                              print\_training\_accuracy=\textcolor{keyword}{True},
                              num\_trees=10,
                              minimum\_leaf\_size=3)
random\_forest = output[\textcolor{stringliteral}{'output\_model'}]

\textcolor{comment}{# Predict the labels of the test points.}
output = mlpack.random\_forest(input\_model=random\_forest,
                              test=test\_set)

\textcolor{comment}{# Now print the accuracy.  The 'probabilities' output could also be used}
\textcolor{comment}{# to generate an ROC curve.}
correct = np.sum(
    output[\textcolor{stringliteral}{'predictions'}] == np.reshape(test\_labels, (test\_labels.shape[0],)))
print(str(correct) + \textcolor{stringliteral}{' correct out of '} + str(len(test\_labels)) + \textcolor{stringliteral}{' ('} +
    str(100 * float(correct) / float(len(test\_labels))) + \textcolor{stringliteral}{'%).'})
\end{DoxyCode}


We can see that we achieve reasonably good accuracy on the test dataset (80\%+); if we use the full {\ttfamily covertype.\+csv.\+gz}, the accuracy should increase significantly (but training will take longer).

It\textquotesingle{}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.\section{What else does mlpack implement?}\label{python_quickstart_python_quickstart_whatelse}
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\+:


\begin{DoxyItemize}
\item {\tt Python documentation}
\end{DoxyItemize}

For more information on what mlpack does, see {\tt https\+://www.\+mlpack.\+org/}. Next, let\textquotesingle{}s go through another example for providing movie recommendations with mlpack.\section{Using mlpack for movie recommendations}\label{python_quickstart_python_quickstart_movierecs}
In this example, we\textquotesingle{}ll train a collaborative filtering model using mlpack\textquotesingle{}s {\ttfamily {\tt cf()}} method. We\textquotesingle{}ll train this on the Movie\+Lens dataset from {\tt https\+://grouplens.\+org/datasets/movielens/,} and then we\textquotesingle{}ll use the model that we train to give recommendations.

You can copy-\/paste this code directly into Python to run it.


\begin{DoxyCode}
\textcolor{keyword}{import} mlpack
\textcolor{keyword}{import} pandas \textcolor{keyword}{as} pd
\textcolor{keyword}{import} numpy \textcolor{keyword}{as} np

\textcolor{comment}{# First, load the MovieLens dataset.  This is taken from files.grouplens.org/}
\textcolor{comment}{# but reposted on mlpack.org as unpacked and slightly preprocessed data.}
ratings = pd.read\_csv(\textcolor{stringliteral}{'http://www.mlpack.org/datasets/ml-20m/ratings-only.csv.gz'})
movies = pd.read\_csv(\textcolor{stringliteral}{'http://www.mlpack.org/datasets/ml-20m/movies.csv.gz'})

\textcolor{comment}{# 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=\textcolor{keyword}{True})
ratings\_train = output[\textcolor{stringliteral}{'training'}]
ratings\_test = output[\textcolor{stringliteral}{'test'}]

\textcolor{comment}{# Train the model.  Change the rank to increase/decrease the complexity of the}
\textcolor{comment}{# model.}
output = mlpack.cf(training=ratings\_train,
                   test=ratings\_test,
                   rank=10,
                   verbose=\textcolor{keyword}{True},
                   algorithm=\textcolor{stringliteral}{'RegSVD'})
cf\_model = output[\textcolor{stringliteral}{'output\_model'}]

\textcolor{comment}{# Now query the 5 top movies for user 1.}
output = mlpack.cf(input\_model=cf\_model,
                   query=[[1]],
                   recommendations=10,
                   verbose=\textcolor{keyword}{True})

\textcolor{comment}{# Get the names of the movies for user 1.}
print(\textcolor{stringliteral}{"Recommendations for user 1:"})
\textcolor{keywordflow}{for} i \textcolor{keywordflow}{in} range(10):
  print(\textcolor{stringliteral}{"  "} + str(i) + \textcolor{stringliteral}{": "} + str(movies.loc[movies[\textcolor{stringliteral}{'movieId'}] ==
      output[\textcolor{stringliteral}{'output'}][0, i]].iloc[0][\textcolor{stringliteral}{'title'}]))
\end{DoxyCode}


Here is some example output, showing that user 1 seems to have good taste in movies\+:


\begin{DoxyCode}
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)
\end{DoxyCode}
\section{Next steps with mlpack}\label{python_quickstart_python_quickstart_nextsteps}
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\+:


\begin{DoxyItemize}
\item {\tt Python mlpack binding documentation}
\end{DoxyItemize}

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\+:


\begin{DoxyItemize}
\item {\tt mlpack C++ tutorials}
\item {\tt mlpack build and installation guide}
\item {\tt Simple sample C++ mlpack programs}
\item {\tt mlpack Doxygen documentation homepage} 
\end{DoxyItemize}