Numpy library in python
1
Introduction
• NumPy (Numerical Python) is an open-source Python library that’s widely used in science
and engineering.
• The NumPy library contains multidimensional array data structures, such as the
homogeneous, N-dimensional and a large library of functions that operate efficiently on
these data structures.
• How to install numpy ?
• import numpy as np
Why to use Numpy?
• Python lists are excellent general-purpose container
• They can be heterogeneous in nature (since they contain elements of a variety of types,
and quite fast when they are used to perform individual operations on handful of elements
• Depending on the characteristics of the data and the types of operations that need to be
performed, other containers may be more appropriate
• we can improve speed, reduce memory consumption, and offer a high-level syntax for
performing a variety of common processing tasks.
2
What is an array?
• An array is a structure for storing and retrieving data
• It can be idealized as a grid in space with each space stores one
element of a data
One-dimensional array
Two-dimensional array
3
Continued…
• A three-dimensional array would be like a set of tables, perhaps stacked as
though they were printed on separate pages.
• In NumPy, this idea is generalized to an arbitrary number of dimensions, and so
the fundamental array class is called ndarray: it represents an “N-dimensional
array”.
➢Most NumPy arrays have some restrictions. For instance:
• All elements of the array must be of the same type of data.
• Once created, the total size of the array can’t change.
• The shape must be “rectangular”, not “jagged”; e.g., each row of a two-
dimensional array must have the same number of columns.
4
Array fundamentals
• One way to initialize an array is using a Python sequence, such as a list
• Elements of an array can be accessed in various ways.
• For instance, we can access an individual element of this array as we would
access an element in the original list: using the integer index of the element
within square brackets.
5
Accessing elements in a list Similar to original list, Python slice notation
can be used for indexing
Like the original list, the array is mutable.
One major difference is that slice indexing of a
list copies the elements into a new list, but
slicing an array returns a view: an object that
refers to the data in the original array. The
original array can be mutated using the view.
6
One major difference is that slice indexing of a list copies the elements into a new list, but slicing an array
returns a view: an object that refers to the data in the original array. The original array can be mutated
using the view.
7
• Two- and higher-dimensional arrays can be initialized from nested
Python sequences:
In NumPy, a dimension of an array is sometimes referred to as an “axis”. This terminology may be useful to disambiguate
between the dimensionality of an array and the dimensionality of the data represented by the array. For instance, the
array a could represent three points, each lying within a four-dimensional space, but a has only two “axes”.
Another difference between an array and a list of lists is that an element of the array can be accessed by specifying the
index along each axis within a single set of square brackets, separated by commas. For instance, the element 8 is in row 1
and column 3:
8
Array attributes
• This part covers the ndim, shape, size and dtype attributes of array
9
How to create a basic array
10
Specifying your datatype
11
• In addition to sort, which returns a sorted copy of an
array, you can use:
1. argsort, which is an indirect sort along a specified axis,
2. lexsort, which is an indirect stable sort on multiple keys,
3. searchsorted, which will find elements in a sorted array, and
4. partition, which is a partial sort
12
Sorting of array
13
How to know the shape and size of an array?
14
How to reshape a given array?
• Using arr.reshape() will give a new shape to an array without changing
the data.
• Point to be remembered is that when you use the reshape method,
the array you want to produce needs to have the same number of
elements as the original array.
• If you start with an array with 21 elements, you’ll need to make sure
that your new array also has a total of 21 elements.
15
References
• Data Analytics using Python, Bharti Motwani, Wiley, First Edition,
2020
16