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.
Installing Numpy library
pip install numpy # for regular python environments
conda install numpy # for anaconda environment
!pip install numpy # for jupytor notebook
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])
By: Faizan Irshad Page 2
Introduction to NumPy Library
NumPy advantages:
Higher flexibility of indexing methods and operations
Higher efficiency of operations
Characteristics of NumPy arrays:
Fixed type
Contiguous memory addresses (faster indexing)
E.g. my_numpy_array = np.array([0.67, 0.45, 0.33])
Integer Types:
int8, int16, int32, int64: Signed integers with 8, 16, 32, or 64 bits.
uint8, uint16, uint32, uint64: Unsigned integers with 8, 16, 32, or 64 bits.
Floating-Point Types:
float16, float32, float64: Floating-point numbers with 16, 32, or 64 bits.
Boolean Type:
bool: Boolean type representing True or False values.
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 floatingpoint):
Creating arrays with specified data types
arr_int32 = np.array([1, 2, 3], dtype=np.int32)
arr_float64 = np.array([1.0, 2.0, 3.0], dtype=np.float64)
arr_bool = np.array([True, False, True], dtype=bool)
NumPy data types are crucial for managing memory efficiently, ensuring compatibility, and
facilitating various mathematical operations on arrays.
By: Faizan Irshad Page 3
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
x.dtype: to get data type
By: Faizan Irshad Page 4
Introduction to NumPy Library
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.
By: Faizan Irshad Page 5
Introduction to NumPy Library
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
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
Finally, subarray dimensions can even be reversed together: x2[::-1, ::-1]
6.3 Reshaping of Array
Another useful type of operation is reshaping of arrays. The most flexible way of doing this is
with the reshape() method. For example, if you want to put the numbers 1 through 9 in a 3×3
grid, you can do the following:
grid = np.arange(1, 10).reshape((3, 3)) #range is builtin python function while arrange is numpy
function. Both take start, end and step parameters.
print(grid)
[[1 2 3]
[4 5 6]
[7 8 9]]
Change dimension
x = np.array([1, 4, 3])
print(x[:, np.newaxis])
[[1]
[4]
[3]]
By: Faizan Irshad Page 6
Introduction to NumPy Library
6.4 Array Concatenation and Splitting
Concatenation of arrays
Concatenation, or joining of two arrays in NumPy, is primarily accomplished through the
routines np.concatenate, np.vstack, and np.hstack. np.concatenate takes a tuple or list of arrays as
its first argument, as we can see here:
x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
print(np.concatenate([x, y]))
More than two arrays at once
z = np.array([99, 99, 99])
print(np.concatenate([x, y, z]))
By: Faizan Irshad Page 7
Introduction to NumPy Library
Splitting of arrays
The opposite of concatenation is splitting, which is implemented by the functions np.split,
np.hsplit, and np.vsplit. For each of these, we can pass a list of indices giving the split points:
x = [1, 2, 3, 99, 99, 3, 2, 1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)
Output
x1 = [1, 2, 3]
x2 = [99, 99]
x3 = [3, 2, 1]
So, x is split into three parts based on the specified indices [3, 5]. x1 contains the first three elements, x2 c
ontains the elements at indices 3 and 4, and x3 contains the remaining elements.
Hsplit (along columns)
arr = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
A
rr_hsplit = np.hsplit(arr, [2]) # Split at index 2 (column 2)
Output
[array([[ 1, 2],
[ 5, 6],
[ 9, 10]]),
array([[ 3, 4],
[ 7, 8],
[11, 12]])]
Vsplit (along rows)
arr_vsplit=np.vsplit(arr, [2])
Output
[array([[1, 2, 3, 4],
[5, 6, 7, 8]]),
array([[ 9, 10, 11, 12]])]
By: Faizan Irshad Page 8
Introduction to NumPy Library
Practice Task
x = 'This is python class'
What is the output of this? x[:3], x[3:] # space in text is also a text
Practice Task
What would be an appropriate slice to get the name "Christopher" from the string "Dr. Christopher
Brooks"?
x = 'Dr. Christopher Brooks'
print(x[????])
By: Faizan Irshad Page 9