Lab 2, Numpy library in Python
Introduction to NumPy Library
1. NumPy (Numerical Python)
NumPy or Numerical Python is a Python library used for working with arrays. It also has functions
for working in domain of linear algebra and matrices. NumPy was created in 2005 by Travis
Oliphant. It is an open source project and you can use it freely. In Python we have lists that serve
the purpose of arrays, but they are slow to process. NumPy aims to provide an array object that is
up to 50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that make
working with ndarray very easy. Arrays are very frequently used in data science, where speed and
resources are very important.
Features
Multidimensional arrays
Slicing/indexing
Math and logic operations
Applications
Computation with vectors and matrices
Provides fundamental Python objects for data science algorithms
array is the main object provided by NumPy
Characteristics
Fixed Type
All its elements have the same type
Multidimensional
Allows representing vectors, matrices and n dimensional arrays
Simple Array in python can contain mix items
import array
L = list(range(10))
A = array.array('i', L)
print(A)
NumPy Array (contains only same type items)
Import numpy as np
np.array([1, 4, 2, 5, 3])
NumPy advantages:
Higher flexibility of indexing methods and operations
Higher efficiency of operations
Note: Remember that unlike Python lists, NumPy is constrained to arrays that all contain the same type.
If types do not match, NumPy will upcast if possible (here, integers are upcast to floating point):
By: Faizan Irshad Page 2
Introduction to NumPy Library
Creating arrays from Scratch
#Create a length 7 int-array filled with zeros
np.zeros(7, dtype=int)
# Create a 3x5 floating-point array filled with 1s
np.ones((3, 5), dtype=float)
# Create a 3x5 array filled with 2.9
np.full((3, 5), 2.9)
# Create a 3x3 identity matrix
np.eye(3)
The identity matrix is
characterized by having ones
on the main diagonal and
zeros elsewhere.
2. NumPy Arrays Manipulations:
Attributes of arrays
Determining the size, shape, memory consumption, and data types of arrays
Indexing of arrays
Getting and setting the value of individual array elements
Slicing of arrays
Getting and setting smaller subarrays within a larger array
Reshaping of arrays
Changing the shape of a given array
Joining and splitting of arrays
Combining multiple arrays into one, and splitting one array into many
6.1 Attributes of a NumPy array
Consider the array
x = np.array([[2, 3, 4],[5,6,7]])
x.ndim: number of dimensions of the array
Out: 2
x.shape : tuple with the array shape
Out: (2,3)
x.size : array size (product of the shape
Out: 2*3=6
By: Faizan Irshad Page 3
Introduction to NumPy Library
x.dtype: to get data type
Moreover, let’s start by defining three random arrays: a one-dimensional, two-dimensional, and
three-dimensional array. We’ll use NumPy’s random number generator:
import numpy as np
x1 = np.random.randint(10, size=5) # One-dimensional array
x2 = np.random.randint(10, size=(3, 4)) # Two-dimensional array of size 3 by 4
x3 = (np.random.randint(10, size=(3, 4, 5)) # Three-dimensional array of size 4 by 5
print("ndim: ", x3.ndim)
print("shape:", x3.shape)
print("size: ", x3.size)
Other are dtype and nbytes.
6.2 Array Slicing and Indexing
Just as we can use square brackets to access individual array elements, we can also use
them to access subarrays with the slice notation, marked by the colon (:) character.
The NumPy slicing syntax follows that of the standard Python list; to access a slice of
an array x, use this:
x[start:stop:step]
In case any of these are unspecified, they default to the values start=0, stop=size of dimension,
step=1. We’ll take a look at accessing subarrays in one dimension and in multiple dimensions.
x1 = np.random.randint(10, size=5)
print(x1)
x1[:3] //first three
x1[1:5:2] // every second from 0 to all.
A potentially confusing case is when the step value is negative. In this case, the defaults for start
and stop are swapped. This becomes a convenient way to reverse an array:
print(x1[::-1])
Multidimensional arrays
x2 = np.random.randint(10, size=(3, 4))
x2[:2, :3] # two rows (0, 1), three columns (0, 1, 2)
x2[:3, ::2] # all rows, every other column
By: Faizan Irshad Page 4