Concepts / Installing scikit-learn

Installing scikit-learn

A single pip install command in the terminal installs scikit-learn together with NumPy, SciPy, matplotlib, ipython, and pandas.

  • Programming

Why One Package Is Rarely Enough

Before you can train a model with scikit-learn, you need Python and a collection of supporting packages on your computer. It is easy to assume that installing scikit-learn alone is enough. In practice, a working scientific setup usually involves several libraries that scikit-learn depends on or works alongside. This article explains how those pieces fit together, how to install them with one step, and how to confirm that the installation actually works.

Keep one question in mind as you read: when something fails later, which of these pieces is missing, and where would you look first? The final sections return to that question with a concrete failure.

What a Python Distribution Adds

Definition

A Python distribution is a packaged version of Python that arrives with a chosen set of additional libraries already in place.

Python on its own gives you the language and its standard tools. A distribution adds the scientific and data libraries that a particular kind of work needs, so you do not have to find, download, and combine each one yourself. The distribution is selected with certain tasks in mind. Anaconda is described as a Python distribution made for large-scale data processing, predictive analytics, and scientific computing, and it bundles several libraries in one installation.

Anaconda is available on Mac OS, Windows, and Linux, so the same distribution can be used across the three common desktop platforms. The source recommends it specifically for people who do not yet have an existing installation of the scientific Python packages. If you already have those packages through some other route, the source does not say you must switch. The recommendation is aimed at learners starting from scratch.

The Packages Anaconda Bundles

The source names a specific group of packages that Anaconda comes with. Seeing them as a list makes clear which tools you get without a separate install. The bundled packages include NumPy, SciPy, matplotlib, pandas, IPython, Jupyter Notebook, and scikit-learn. The diagram below shows the bundle as one distribution containing these named components.

AnacondaPython distributionNumPySciPymatplotlibpandasIPythonJupyter Notebookscikit-learnIntel MKL librarycontainscontainscontainscontainscontainscontainscontainscontains
What does it mean that Anaconda contains scikit-learn and other packages, and how does that differ from installing one package on its own?

The list has two kinds of entries. Most are general scientific and data tools: NumPy, SciPy, matplotlib, pandas, IPython, and Jupyter Notebook. scikit-learn is the machine learning library this course is built around. Because all of them arrive together, the packages are already set up to be used side by side. That is the practical advantage the source highlights.

How Intel MKL Speeds Up Computation

Anaconda now also includes the Intel MKL library. The source describes MKL as a commercial library that is provided for free within Anaconda, so you do not have to obtain it separately. Using MKL happens automatically when Anaconda is installed, which means there is no extra configuration step for you to remember.

The reason MKL matters for a scikit-learn learner is speed. The source says that using MKL can give significant speed improvements for many algorithms in scikit-learn. The wording is deliberately limited. The improvement applies to many algorithms, not all of them, and the size of the gain is described as significant rather than given as a fixed number. Expect faster computation in many cases without treating the speedup as guaranteed for every workload.

Install AnacondaMKL includedfree, no separate downloadMKL usedautomaticallyno extra configurationFaster scikit-learnalgorithmsfor many algorithms
Which steps does the source actually describe between installing Anaconda and a faster scikit-learn algorithm?

The Supporting Tools Around scikit-learn

The source explains that scikit-learn is built on top of two scientific Python libraries, NumPy and SciPy. These form the foundation layer that scikit-learn depends on directly. The source also names pandas and matplotlib as tools used alongside scikit-learn, and it introduces Jupyter Notebook as a browser-based interactive environment for working with them. The diagram below shows these relationships.

scikit-learnNumPyfoundation layerSciPyfoundation layerpandasmatplotlibJupyter Notebookbrowser-basedbuilt on top ofbuilt on top ofused alongsideused alongsideworks with
Which packages does scikit-learn depend on, which sit alongside it, and what does each one contribute?

The source is brief about these tools. It states what each one is for in relation to scikit-learn and does not describe their full feature sets, so this article does the same. The important point for installation is that NumPy and SciPy are what scikit-learn depends on directly. If you install only scikit-learn and nothing else, you have not met the full set of tools the source recommends. The source also lists IPython in the install command without describing it in this section, and you do not need its details to complete the installation.

One Pip Command for the Working Group

The source gives one command that installs the full set of packages in a single step. You type it into a terminal, not into a Python prompt. The leading dollar sign shown in the source is the terminal's prompt symbol and is not part of the command you type. The command names scikit-learn along with NumPy, SciPy, matplotlib, IPython, and pandas, and pip handles each name as a separate package. The source describes this as the way to install all of these packages when a Python installation is already in place.

bash

When you run the command, you are not just installing scikit-learn. You are placing the whole working group into the Python installation you intend to use. The command reaches the other packages through the list you type, so they come along in the same step rather than as separate chores.

Checking the Installation from Python

A successful pip run tells you that pip finished without an error, but it does not show you what Python can see. The clearest check is to ask Python to import each package. The program below is a generated illustration of how to check the bundle. It is not a source example. If any import fails, the package is not available in the Python environment you are running.

python
Notes on the code
  • Each import line asks Python to load one package into the current session.
  • The print lines show the version string each package reports.
  • The program does not import Jupyter Notebook, which is an interactive environment rather than a library you import in code.

Running the program should print one line per package with its version string. The exact version numbers will differ from one installation to another, so do not expect any particular numbers. What matters is that every import succeeds. That shows the packages are present and can be used together in the same Python session.

Output
A successful run prints one line for each import, in order: numpy, scipy, pandas, matplotlib, IPython, and scikit-learn, each followed by its version string. The numbers vary by installation.

Reading a Failed Import

Now consider the failure. Suppose the same single-line check is run in an environment where scikit-learn is missing. A traceback appears. The source says the traceback names the file and line number where Python stopped, so the failure happened at the import rather than later in your analysis code. The final line names the missing module.

python
Output (expected)
Traceback (most recent call last):
  File "check.py", line 1, in <module>
    import sklearn
ModuleNotFoundError: No module named 'sklearn'
Notes on the code
  • The file and line tell you where Python stopped.
  • The last line names the module Python could not find.

The two pieces of information in that traceback let you split the problem into two questions. Is the package absent from the Python environment you are running? Or did the installation go somewhere other than where your program looks? The output above is an illustration. The exact wording of a traceback depends on the setup, but the file, line, and missing name are the parts to read first.

Diagnosing a Missing scikit-learn

When import sklearn fails, the goal is to find which of three things is wrong: the wrong Python, the wrong environment, or a missing install. The flowchart below orders the checks. Start at the failed import and follow the questions that the traceback and your setup make answerable.

import sklearnfailsDoes the tracebackname sklearn asmissing?Did pip install intothis Python?a successful pip run alonedoes not prove itIs this theenvironment youinstalled into?Run the program inthe installedenvironmentImport succeedsInstall into thePython you runcheckyesyesnonoyesretry importretry import
If import sklearn fails, which checks should I make, and in what order, to find whether the cause is the wrong Python, the wrong environment, or a missing install?

What do you think happens?

If pip finished without an error, does that prove Python can import scikit-learn?

  • Yes, a clean pip run proves the import works

  • No, you should check with an import statement in the same Python environment

Reveal answer
Answer:

No, you should check with an import statement in the same Python environment

The source states that a successful pip run does not prove Python can import the package, so the import itself is the check.

MEDIUM

You ran the pip command successfully, but import sklearn still fails in your editor. Using the flowchart, list the checks you would make in order, and say which answer would point to the wrong Python and which would point to a missing install.

Hints
  • Start with the traceback: does it name sklearn as the missing module?
  • Compare the Python that pip installed into with the Python your editor runs.
  • If the traceback and environment match but the import still fails, the install itself may be the problem.

Mistakes Beginners Make

  • Common mistake

    Assuming installing scikit-learn alone is enough

    Why it fails

    The source explains that a working scientific setup usually involves several libraries that scikit-learn depends on or works alongside.

    Do this instead

    Install the group of packages in one pip command, or install Anaconda, which bundles them.

  • Common mistake

    Treating a successful pip run as proof that the import works

    Why it fails

    The source states that pip finishing without error does not show what Python can see.

    Do this instead

    Run an import statement in the same Python environment you intend to use.

  • Common mistake

    Typing the pip command into a Python prompt

    Why it fails

    The source specifies that the command is typed into a terminal, not into Python.

    Do this instead

    Open a terminal, type the pip command there, and leave the dollar sign out of what you type.

  • Common mistake

    Expecting MKL to speed up every computation

    Why it fails

    The source limits the claim to many scikit-learn algorithms and does not give a fixed amount of gain.

    Do this instead

    Expect faster computation in many cases, and do not treat the speedup as guaranteed for every workload.

  • Common mistake

    Ignoring the file and line in a traceback, which point to where the import failed

Checking Your Understanding

Work through the following question using only what you have read. Then compare your answer with the flowchart and the explanation above.

Key Takeaways

  • A Python distribution such as Anaconda adds scientific and data libraries to Python, so you do not have to find and combine them yourself.
  • Anaconda bundles NumPy, SciPy, matplotlib, pandas, IPython, Jupyter Notebook, and scikit-learn, and it includes the Intel MKL library, which is used automatically and can speed up many scikit-learn algorithms.
  • A single pip install command typed in a terminal places scikit-learn together with its supporting packages into the Python installation you intend to use.
  • A successful pip run does not prove the package can be imported, so check with an import statement in the same Python environment and read the traceback's file, line, and missing module when it fails.

Related concepts

More concepts starting with I