When you want to do scientific work in Python, the first library you can turn to is SciPy. As you’ll see in this tutorial, SciPy is not just a library, but a whole ecosystem of libraries that work together to help you accomplish complicated scientific tasks quickly and reliably.
In this tutorial, you’ll learn how to:
- Find information about all the things you can do with SciPy
- Install SciPy on your computer
- Use SciPy to cluster a dataset by several variables
- Use SciPy to find the optimum of a function
You can follow along with the examples in this tutorial by downloading the source code available at the link below:
Get Sample Code: Click here to get the sample code you’ll use to learn about SciPy in this tutorial.
Differentiating SciPy the Ecosystem and SciPy the Library
When you want to use Python for scientific computing tasks, there are several libraries that you’ll probably be advised to use, including:
- NumPy
- SciPy
- Matplotlib
- IPython
- SymPy
- Pandas
Collectively, these libraries make up the SciPy ecosystem and are designed to work together. Many of them rely directly on NumPy arrays to do computations. This tutorial expects that you have some familiarity with creating NumPy arrays and operating on them.
Note: If you need a quick primer or refresher on NumPy, then you can check out these tutorials:
In this tutorial, you’ll learn about the SciPy library, one of the core components of the SciPy ecosystem. The SciPy library is the fundamental library for scientific computing in Python. It provides many efficient and user-friendly interfaces for tasks such as numerical integration, optimization, signal processing, linear algebra, and more.
Understanding SciPy Modules
The SciPy library is composed of a number of modules that separate the library into distinct functional units. If you want to learn about the different modules that SciPy includes, then you can run help() on scipy, as shown below:
>>> import scipy >>> help(scipy) This produces some help output for the entire SciPy library, a portion of which is shown below:
Subpackages ----------- :: cluster --- Vector Quantization / Kmeans fft --- Discrete Fourier transforms fftpack --- Legacy discrete Fourier transforms integrate --- Integration routines ... This code block shows the Subpackages portion of the help output, which is a list of all of the available modules within SciPy that you can use for calculations.
Before SciPy 1.9, you had to explicitly import a subpackage before you could use it. Newer versions load subpackages lazily as soon as you access them, but it’s still good practice to import the module that you want to use specifically. You’ll see some examples of this a little later in the tutorial, and guidelines for importing libraries from SciPy are shown in the SciPy documentation.
Once you decide which module you want to use, you can check out the SciPy API reference, which contains all of the details on each module in SciPy. If you’re looking for something with a little more exposition, then the Scientific Python Lectures on SciPy are a great resource to go in-depth on many of the SciPy modules.
Later in this tutorial, you’ll learn about cluster and optimize, which are two of the modules in the SciPy library. But first, you’ll need to install SciPy on your computer.
Installing SciPy on Your Computer
As with most Python packages, there are two main ways to install SciPy on your computer:
Here, you’ll learn how to use both of these approaches to install the library. SciPy’s only direct dependency is the NumPy package. Either installation method will automatically install NumPy in addition to SciPy, if necessary.
Anaconda
Read the full article at https://realpython.com/python-scipy-cluster-optimize/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

