Scikit-learn Installation (Linux Mint)

Scikit-learn is a free software machine learning library for the Python programming language. It features various classification, regression and clustering algorithms including support vector machines, random forests, gradient boosting, k-means and DBSCAN, and is designed to interoperate with the Python numerical and scientific libraries NumPy and SciPy.

The installation process on Linux Mint and other Debian derivated systems is very simple, but there is a final step that is ambiguous which is the re-configuration of the Python interpreter to load the provided Anaconda libraries which includes the "scikit-learn" package.

Installing Anaconda is as easy as going to the download page, choosing the appropriate version for your python installation, following the instructions step-by-step, and letting the installer do its thing. In my case, in order to keep things simple, I decided to change the target directory to /opt/anaconda/ and at the end of the installation process the script suggests to include the new binary path into the global PATH environment variable.

export PATH="/opt/anaconda/bin:$PATH"

After this you have to restart your current Terminal or execute source ~/.bashrc to load the new configuration. At this point we expect to have all the Anaconda-related programs loaded system wide, we can check this executing conda --version.

And here is where it gets tricky...

From the Scikit-learn website we can read that the latest version of Canopy and Anaconda both ship a recent version of scikit-learn, in addition to a large set of scientific python library. We can verify this by requesting an update of the repository registry and trying to install the "sciki-learn" package, I say "try" because it is supposed to be already installed.

$ ls /opt/anaconda/pkgs/
$ conda update conda
$ conda install scikit-learn

However, trying to load the "sklearn" module fails:

$ python -c "import sklearn"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'sklearn'

This is why I decided to write this article, maybe someone will get stuck in this step the same way I was. Here is what happened... Before you installed Anaconda you were provided two options for the installer, one for Python3.x and one for Python2.x, this is important because after the installation a new set of aliases are installed in /opt/anaconda/bin/python* which are supposed to replace the interpreter shipped by default with your operating system.

If we execute the aliases inside the Anaconda directory the module will load:

$ /opt/anaconda/bin/python -c "import sklearn"
$ if [[ "$?" -eq 0 ]]; then echo "Success"; else echo "Failure"; fi

So in order to make this alias the default entry point for all our future Machine Learning projects we will have to force the system to load the interpreter inside the Anaconda directory system wide. A simple re-linking of /usr/bin/python should work but fortunately Linux Mint (and other Debian derived systems) come with a tool named update-alternatives:

$ sudo -s # Root privileges are required
$ update-alternatives --install /usr/bin/python python /opt/anaconda/bin/python 1
$ python --version
Python 2.7.12 :: Anaconda 4.1.1 (64-bit)

Now, we are all set... At least with the scikit-learn part. With the modification of the default binary that acts as the Python interpreter we just broke all the rest of the system LOL ¯_(ツ)_/¯ but there is an easy fix for that, we will load the default Python module directories into PYTHONPATH using .bashrc:

export PATH="$PATH:/opt/anaconda/bin"
export PYTHONPATH="$PYTHONPATH:/usr/lib/pymodules/python2.7"
export PYTHONPATH="$PYTHONPATH:/usr/share/pyshared"
export PYTHONPATH="$PYTHONPATH:/opt/anaconda/pkgs"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/plat-x86_64-linux-gnu"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/lib-tk"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/lib-old"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/lib-dynload"
export PYTHONPATH="$PYTHONPATH:/usr/local/lib/python2.7/dist-packages"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/dist-packages"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/dist-packages/PILcompat"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/dist-packages/gst-0.10"
export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/dist-packages/gtk-2.0"

Check if the directories above are actually being loaded:

$ python -c "import sys, pprint; pprint.pprint(sys.path)"

And now we are all set, you can start hitting your head against the wall while learning about Machine Learning with Scikit-learn here are some videos that act as an introductory tutorial into this amazing world of data science:

3 months ago
  • 8da57acFix minor bugs found by the code static ana…
3 months ago
  • caa417fAdd option to configure the malware scanner…
3 months ago
  • 9c86744Modify default value for some of the alert …
3 months ago
  • 84dd39dAdd option to stop sending the failed login…
3 months ago
  • eb05935Add pre-checks for every plugin page for si…
3 months ago
  • d21a062Modify mechanism to ignore files from integ…
3 months ago
  • b1a9169Add developer option to disable failed pass…
3 months ago
  • 4e3ef13Add support for other English and Spanish b…
3 months ago
  • 2d07b4eFix error interception for Firewall API err…
3 months ago
pushed to master at cixtor/slackapi
  • 4a2c1c8Modify data type for methods related to cha…
  • 6716199Add CLI handler for the users.identity API …
  • f9c448dAdd CLI handler for the mpim.open API endpo…
  • 305d1c4Add CLI handler for the mpim.mark API endpo…
  • 8bb89afAdd CLI handler for the mpim.close API endp…
  • 202a017Add CLI handler for the dnd.teamInfo API en…
  • 68819e9Add CLI handler for the dnd.info API endpoi…
  • 9a2b29aAdd CLI handler for the dnd.endSnooze API e…
  • e7dc86aAdd CLI handler for the dnd.setSnooze API e…
  • 111c53fAdd CLI handler for the dnd.endDnd API endp…
  • cdb620dFix token usage when there are no extra par…
  • View comparison for these 11 commits
3 months ago
pushed to master at cixtor/slackapi
3 months ago
  • 6497e80Remove unnecessary automatic blacklisting o…
3 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#40
Fix multiple bugs with the API calls and queue system
18 commit with 793 additions and 293 deletion
3 months ago
3 months ago
3 months ago
  • 38cc02aModify timing for the dashboard alerts afte…
3 months ago
  • 350c074Fix infinite loop with email alerts and SMT…
3 months ago
  • acff4aaFix detection of base URL with built-in fun…
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#39
Add queue system for the security logs and cache improvement
5 commit with 517 additions and 541 deletion
3 months ago
3 months ago
  • 4c51445Fix static function call of non-static Site…
3 months ago
3 months ago
  • af47581Add changelog to release version 1.8.6
3 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#38
Add changelog to release version 1.8.5
6 commit with 4062 additions and 1841 deletion
3 months ago
  • dc1a05aAdd changelog to release version 1.8.5
3 months ago
Do you have a project idea? Let's make it together!