0% found this document useful (0 votes)
4 views8 pages

Scientific Python Ecosystem and NumPy

Chapter 4 provides an overview of the scientific Python ecosystem, focusing on the NumPy library and its multidimensional array structure, ndarrays. It discusses the core components of the ecosystem, including libraries like SciPy, Matplotlib, Pandas, and others, while demonstrating how to create and manipulate ndarrays. The chapter serves as a foundation for further exploration of the scientific Python ecosystem in subsequent chapters.

Uploaded by

ignitionofcredo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Scientific Python Ecosystem and NumPy

Chapter 4 provides an overview of the scientific Python ecosystem, focusing on the NumPy library and its multidimensional array structure, ndarrays. It discusses the core components of the ecosystem, including libraries like SciPy, Matplotlib, Pandas, and others, while demonstrating how to create and manipulate ndarrays. The chapter serves as a foundation for further exploration of the scientific Python ecosystem in subsequent chapters.

Uploaded by

ignitionofcredo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CHAPTER 4

Scientific Python
Ecosystem and NumPy
In Chapter 3, you learned how to create simple visualizations with Python
3 and the leather data visualization library. You also learned that only
primitive visualizations can be prepared using the leather data visualization
library. For more complex and elaborate visualizations, we need to use
libraries with the advanced data handling and visualization capabilities.
This chapter explores the scientific Python ecosystem and its
components. It also provides a brief overview of the NumPy library with
a few coding demonstrations. The following topics are explored in this
chapter:

• Scientific Python ecosystem

• NumPy and ndarrays

• Ndarray properties

• NumPy constants

Throughout the remaining chapters of this book, we will explore many


components of the scientific Python ecosystem one by one. Throughout
this book, we will be using different libraries that are part of this scientific
Python ecosystem. The valuable knowledge you will gain in this chapter
serves as foundation for the remaining chapters. As this is an introductory
chapter for a broad ecosystem, I have kept it short, yet practical.

© Ashwin Pajankar 2021 49


A. Pajankar, Practical Python Data Visualization,
https://doi.org/10.1007/978-1-4842-6455-3_4
Chapter 4 SCientifiC python eCoSyStem and numpy

Scientific Python Ecosystem


The scientific Python ecosystem (SciPy) is a collection of Python libraries
for mathematics, science, and engineering. SciPy has the following core
components:

1. Python programming languages: We explored


installation and a few basics of the Python 3
programming language in Chapter 1.

2. NumPy: This is the numerical Python library, the


fundamental package for numerical computation
in Python. It defines an N-dimensional data type
that can be used for numerical computations on
multidimensional data.

3. SciPy library: This includes many routines for


mathematical and scientific computations that can
be used for scientific applications.

4. Matplotlib: This is a MATLAB-inspired library for


data visualization in Python 3.

There are several additional important member libraries of this


ecosystem.

1. Pandas: This stands for Python data analysis. It


provides versatile data structures such as series and
data frame.

2. SymPy: This stands for symbolic Python. It is used


for symbolic mathematics and algebra.

3. scikit-image: This library has routines for image


processing.

50
Chapter 4 SCientifiC python eCoSyStem and numpy

4. scikit-learn: This library has routines for machine


learning.

The interactive environments that are usually used with SciPy are
IPython or Jupyter Notebook. We looked at Jupyter Notebook in detail in
Chapter 2 and used it in Chapter 3 as well. We will continue using it for rest
of this book.
The next section covers the NumPy library in greater detail.

NumPy and Ndarrays


As introduced earlier, NumPy is the fundamental package for numerical
computation in Python. The most useful feature of the NumPy library is
the multidimensional container data structure known as ndarray.
An ndarray is a multidimensional array (also known as a container)
of items that have the same datatype and size. We can define the size and
datatype of the items at the time of the creation of the ndarray. Just like the
other data structures such as lists, we can access the contents of an ndarray
with an index. The index in the ndarrays ranges from 0 (just like arrays in
C or lists in Python). We can use ndarrays for a variety of computations.
All the other libraries in SciPy and other libraries also recognize and use
NumPy ndarrays and associated routines to represent their own data
structures and operations on them.
Let’s get started with a hands-on example. Create a new Jupyter
notebook for this chapter, then run the following command to install the
NumPy library:

!pip3 install numpy

Import it into the current notebook by running the following command:

51
Chapter 4 SCientifiC python eCoSyStem and numpy

import numpy as np

You can create a list and then use it to create a simple ndarray as follows:

l1 = [1, 2, 3]
x = np.array(l1, dtype=np.int16)

Here we are creating an ndarray from a list. The datatype of the


members is 16-bit integer. You can find a detailed list of the datatypes
supported at https://numpy.org/devdocs/user/basics.types.html.
We can write the preceding code in a single line as follows:

x = np.array(l1, dtype=np.int16)

Let’s print the value of ndarray and its type (which, we know, is
ndarray).

print(x)
print(type(x))

The output is as follows:

[1 2 3]
<class 'numpy.ndarray'>

As we can observe in the output, it is of the class numpy.ndarray. As


we learned earlier, the indexing starts from 0. Let’s demonstrate that by
accessing the members of the ndarray as follows:

print(x[0]); print(x[1]); print(x[2])

Here is the output:

1
2
3

52
Chapter 4 SCientifiC python eCoSyStem and numpy

We can even use a negative index: -1 returns the last element, -2


returns the second to the last element, and so on. Here is an example:

print(x[-1])

If we provide any invalid index value, it throws an error.

print(x[3])

In this statement, we are trying to access the fourth element in the


ndarray, which is nonexistent. It thus returns the following error:

IndexError Traceback (most recent call last)


<ipython-input-4-d3c02b9c2b5d> in <module>
----> 1 print(x[3])

IndexError: index 3 is out of bounds for axis 0 with size 3

More Than One Dimension


We can have more than one dimension for an array as follows:

x1 = np.array([[1, 2, 3], [4, 5, 6]], np.int16)

This represents a two-dimensional matrix with two rows and three


columns. We can access individual elements as follows:

print(x1[0, 0]); print(x1[0, 1]); print(x1[0, 2]);

We can even access entire rows:

print(x1[0, :])
print(x1[1, :])

The output is shown here:

[1 2 3]
[4 5 6]

53
Chapter 4 SCientifiC python eCoSyStem and numpy

We can access an entire column with this syntax:

print(x[:, 0])

The output is as follows:

[1 4]

We can even have an ndarray with more than two dimensions. Here is
the syntax to create a three-dimensional (3D) array:

x2 = np.array([[[1, 2, 3], [4, 5, 6]],[[0, -1, -2], [-3, -4,


-5]]], np.int16)

Scientific and business applications often use multidimensional


data. Ndarrays are very useful for storing numerical data. Try to run the
following items and retrieve the elements of the preceding 3D matrix.

print(x2 [0, 0, 0])


print(x2 [1, 1, 2])
print(x2[:, 1, 1])

Ndarray Properties
We can learn more about the ndarrays by referring to their properties.
First, let’s look at all the properties with the demonstration. This example
uses the same 3D matrix we used earlier.

x2 = np.array([[[1, 2, 3], [4, 5, 6]],[[0, -1, -2], [-3, -4,


-5]]], np.int16)

We can learn the number of dimensions with the following statement:

print(x2.ndim)

The output returns the number of dimensions:

54
Chapter 4 SCientifiC python eCoSyStem and numpy

We can then learn the shape of the ndarray as follows:

print(x2.shape)

The shape indicates the size of the dimensions, as follows:

(2, 2, 3)

We can determine the datatype of the members as follows:

print(x2.dtype)

Here is the output:

int16

We can also learn the size (number of elements) and the number of
bytes required in memory for storage as follows:

print(x2.size)
print(x2.nbytes)

The output is as follows:

12
24

We can compute the transpose with the following code:

print(x2.T)

NumPy Constants
The NumPy library has many useful mathematical and scientific constants
you can use in your programs. The following code snippet prints all of
those important constants:

print(np.inf)

55
Chapter 4 SCientifiC python eCoSyStem and numpy

print(np.NAN)
print(np.NINF)
print(np.NZERO)
print(np.PZERO)
print(np.e)
print(np.euler_gamma)
print(np.pi)

The output is as follows:

inf
nan
-inf
-0.0
0.0
2.718281828459045
0.5772156649015329
3.141592653589793

Summary
This chapter introduced the basics of NumPy and ndarrays. The NumPy
library is extensive, including many routines. There are even separate
books dedicated to NumPy. For our purposes, we will explore more
routines from the NumPy library in the coming chapters as and when we
need them for our visualization demonstrations.
The next chapter introduces a few ndarray creation routines and the
basics of data visualization with Matplotlib.

56

You might also like