Installing scikit-learn (2024)

There are different ways to install scikit-learn:

  • Install the latest official release. Thisis the best approach for most users. It will provide a stable versionand pre-built packages are available for most platforms.

  • Install the version of scikit-learn provided by youroperating system or Python distribution.This is a quick option for those who have operating systems or Pythondistributions that distribute scikit-learn.It might not provide the latest release version.

  • Building the package from source. This is best for users who want thelatest-and-greatest features and aren’t afraid of runningbrand-new code. This is also needed for users who wish to contribute to theproject.

Installing the latest release#

Install the 64-bit version of Python 3, for instance from theofficial website.

Now create a virtual environment (venv) and install scikit-learn.Note that the virtual environment is optional but strongly recommended, inorder to avoid potential conflicts with other packages.

python -m venv sklearn-envsklearn-env\Scripts\activate # activatepip install -U scikit-learn

In order to check your installation, you can use:

python -m pip show scikit-learn # show scikit-learn version and locationpython -m pip freeze # show all installed packages in the environmentpython -c "import sklearn; sklearn.show_versions()"

Install Python 3 using homebrew (brew install python)or by manually installing the package from the official website.

Now create a virtual environment (venv) and install scikit-learn.Note that the virtual environment is optional but strongly recommended, inorder to avoid potential conflicts with other packges.

python -m venv sklearn-envsource sklearn-env/bin/activate # activatepip install -U scikit-learn

In order to check your installation, you can use:

python -m pip show scikit-learn # show scikit-learn version and locationpython -m pip freeze # show all installed packages in the environmentpython -c "import sklearn; sklearn.show_versions()"

Python 3 is usually installed by default on most Linux distributions. Tocheck if you have it installed, try:

python3 --versionpip3 --version

If you don’t have Python 3 installed, please install python3 andpython3-pip from your distribution’s package manager.

Now create a virtual environment (venv) and install scikit-learn.Note that the virtual environment is optional but strongly recommended, inorder to avoid potential conflicts with other packages.

python3 -m venv sklearn-envsource sklearn-env/bin/activate # activatepip3 install -U scikit-learn

In order to check your installation, you can use:

python3 -m pip show scikit-learn # show scikit-learn version and locationpython3 -m pip freeze # show all installed packages in the environmentpython3 -c "import sklearn; sklearn.show_versions()"

Install conda using the Anaconda or miniconda installersor the miniforge installers (no administratorpermission required for any of those). Then run:

conda create -n sklearn-env -c conda-forge scikit-learnconda activate sklearn-env

In order to check your installation, you can use:

conda list scikit-learn # show scikit-learn version and locationconda list # show all installed packages in the environmentpython -c "import sklearn; sklearn.show_versions()"

Using an isolated environment such as pip venv or conda makes it possible toinstall a specific version of scikit-learn with pip or conda and its dependenciesindependently of any previously installed Python packages. In particular under Linuxit is discouraged to install pip packages alongside the packages managed by thepackage manager of the distribution (apt, dnf, pacman…).

Note that you should always remember to activate the environment of your choiceprior to running any Python command whenever you start a new terminal session.

If you have not installed NumPy or SciPy yet, you can also install these usingconda or pip. When using pip, please ensure that binary wheels are used,and NumPy and SciPy are not recompiled from source, which can happen when usingparticular configurations of operating system and hardware (such as Linux ona Raspberry Pi).

Scikit-learn plotting capabilities (i.e., functions starting with plot_and classes ending with Display) require Matplotlib. The examples requireMatplotlib and some examples require scikit-image, pandas, or seaborn. Theminimum version of scikit-learn dependencies are listed below along with itspurpose.

Dependency

Minimum Version

Purpose

numpy

1.19.5

build, install

scipy

1.6.0

build, install

joblib

1.2.0

install

threadpoolctl

3.1.0

install

cython

3.0.10

build

meson-python

0.15.0

build

matplotlib

3.3.4

benchmark, docs, examples, tests

scikit-image

0.17.2

docs, examples, tests

pandas

1.1.5

benchmark, docs, examples, tests

seaborn

0.9.0

docs, examples

memory_profiler

0.57.0

benchmark, docs

pytest

7.1.2

tests

pytest-cov

2.9.0

tests

ruff

0.2.1

tests

black

24.3.0

tests

mypy

1.9

tests

pyamg

4.0.0

tests

polars

0.20.23

docs, tests

pyarrow

12.0.0

tests

sphinx

7.3.7

docs

sphinx-copybutton

0.5.2

docs

sphinx-gallery

0.16.0

docs

numpydoc

1.2.0

docs, tests

Pillow

7.1.2

docs

pooch

1.6.0

docs, examples, tests

sphinx-prompt

1.4.0

docs

sphinxext-opengraph

0.9.1

docs

plotly

5.14.0

docs, examples

sphinxcontrib-sass

0.3.4

docs

sphinx-remove-toctrees

1.0.0.post1

docs

sphinx-design

0.5.0

docs

pydata-sphinx-theme

0.15.2

docs

conda-lock

2.5.6

maintenance

Warning

Scikit-learn 0.20 was the last version to support Python 2.7 and Python 3.4.Scikit-learn 0.21 supported Python 3.5-3.7.Scikit-learn 0.22 supported Python 3.5-3.8.Scikit-learn 0.23-0.24 required Python 3.6 or newer.Scikit-learn 1.0 supported Python 3.7-3.10.Scikit-learn 1.1, 1.2 and 1.3 support Python 3.8-3.12Scikit-learn 1.4 requires Python 3.9 or newer.

Third party distributions of scikit-learn#

Some third-party distributions provide versions ofscikit-learn integrated with their package-management systems.

These can make installation and upgrading much easier for users sincethe integration includes the ability to automatically installdependencies (numpy, scipy) that scikit-learn requires.

The following is an incomplete list of OS and python distributionsthat provide their own version of scikit-learn.

Alpine Linux#

Alpine Linux’s package is provided through the official repositories aspy3-scikit-learn for Python.It can be installed by typing the following command:

sudo apk add py3-scikit-learn

Arch Linux#

Arch Linux’s package is provided through the official repositories aspython-scikit-learn for Python.It can be installed by typing the following command:

sudo pacman -S python-scikit-learn

Debian/Ubuntu#

The Debian/Ubuntu package is split in three different packages calledpython3-sklearn (python modules), python3-sklearn-lib (low-levelimplementations and bindings), python3-sklearn-doc (documentation).Note that scikit-learn requires Python 3, hence the need to use the python3-suffixed package names.Packages can be installed using apt-get:

sudo apt-get install python3-sklearn python3-sklearn-lib python3-sklearn-doc

Fedora#

The Fedora package is called python3-scikit-learn for the python 3 version,the only one available in Fedora.It can be installed using dnf:

sudo dnf install python3-scikit-learn

NetBSD#

scikit-learn is available via pkgsrc-wip:https://pkgsrc.se/math/py-scikit-learn

MacPorts for Mac OSX#

The MacPorts package is named py<XY>-scikits-learn,where XY denotes the Python version.It can be installed by typing the followingcommand:

sudo port install py39-scikit-learn

Anaconda and Enthought Deployment Manager for all supported platforms#

Anaconda andEnthought Deployment Managerboth ship with scikit-learn in addition to a large set of scientificpython library for Windows, Mac OSX and Linux.

Anaconda offers scikit-learn as part of its free distribution.

Intel Extension for Scikit-learn#

Intel maintains an optimized x86_64 package, available in PyPI (via pip),and in the main, conda-forge and intel conda channels:

conda install scikit-learn-intelex

This package has an Intel optimized version of many estimators. Wheneveran alternative implementation doesn’t exist, scikit-learn implementationis used as a fallback. Those optimized solvers come from the oneDALC++ library and are optimized for the x86_64 architecture, and areoptimized for multi-core Intel CPUs.

Note that those solvers are not enabled by default, please refer to thescikit-learn-intelexdocumentation for more details on usage scenarios. Direct export example:

from sklearnex.neighbors import NearestNeighbors

Compatibility with the standard scikit-learn solvers is checked by running thefull scikit-learn test suite via automated continuous integration as reportedon intel/scikit-learn-intelex. If you observe any issuewith scikit-learn-intelex, please report the issue on theirissue tracker.

WinPython for Windows#

The WinPython project distributesscikit-learn as an additional plugin.

Troubleshooting#

If you encounter unexpected failures when installing scikit-learn, you may submitan issue to the issue tracker.Before that, please also make sure to check the following common issues.

Error caused by file path length limit on Windows#

It can happen that pip fails to install packages when reaching the default pathsize limit of Windows if Python is installed in a nested location such as theAppData folder structure under the user home directory, for instance:

C:\Users\username>C:\Users\username\AppData\Local\Microsoft\WindowsApps\python.exe -m pip install scikit-learnCollecting scikit-learn...Installing collected packages: scikit-learnERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 'C:\\Users\\username\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python37\\site-packages\\sklearn\\datasets\\tests\\data\\openml\\292\\api-v1-json-data-list-data_name-australian-limit-2-data_version-1-status-deactivated.json.gz'

In this case it is possible to lift that limit in the Windows registry byusing the regedit tool:

  1. Type “regedit” in the Windows start menu to launch regedit.

  2. Go to theComputer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystemkey.

  3. Edit the value of the LongPathsEnabled property of that key and setit to 1.

  4. Reinstall scikit-learn (ignoring the previous broken installation):

    pip install --exists-action=i scikit-learn
Installing scikit-learn (2024)
Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5506

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.