UNIT - IV
Python Numpy
Numpy is a general-purpose array-processing package.
It provides a high-performance multidimensional array object, and tools for working with
these arrays.
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers.
In Numpy, number of dimensions of the array is called rank of the array.
A tuple of integers giving the size of the array along each dimension is known as shape of
the array.
An array class in Numpy is called as ndarray.
Elements in Numpy arrays are accessed by using square brackets and can be initialized by
using nested Python Lists.
Note: Type of array can be explicitly defined while creating the array.
Example 1:
# Python program for Creation of Arrays
import numpy as np
# Creating a rank 1 Array
arr = np.array([1, 2, 3])
print("Array with Rank 1: \n",arr)
# Creating a rank 2 Array
arr = np.array([[1, 2, 3],[4, 5, 6]])
print("Array with Rank 2: \n", arr)
# Creating an array from tuple
arr = np.array((1, 3, 2))
print("\nArray created using " "passed tuple:\n", arr)
Example 2:
1
# Python program to demonstrate indexing in numpy array
import numpy as np
# Initial Array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
print("Initial Array: ")
print(arr)
# Printing a range of Array with the use of slicing method
sliced_arr = arr[:2, ::2]
print ("Array with first 2 rows and"
" alternate columns(0 and 2):\n", sliced_arr)
# Printing elements at specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), " "(1, 2), (0, 1), (3, 0):\n", Index_arr)
Basic Array Operations
# Python program to demonstrate basic operations on single array
import numpy as np
# Defining Array 1
a = np.array([[1, 2],
[3, 4]])
# Defining Array 2
b = np.array([[4, 3],
[2, 1]])
# Adding 1 to every element
print ("Adding 1 to every element:", a + 1)
# Subtracting 2 from each element
print ("\nSubtracting 2 from each element:", b - 2)
# sum of array elements Performing Unary operations
print ("\nSum of all array " "elements: ", a.sum())
# Adding two arrays Performing Binary operations
print ("\nArray sum:\n", a + b)
Example:
2
import numpy as np
# Creating array object
arr = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
# Printing type of arr object
print("Array is of type: ", type(arr))
# Printing array dimensions (axes)
print("No. of dimensions: ", arr.ndim)
# Printing shape of array
print("Shape of array: ", arr.shape)
# Printing size (total number of elements) of array
print("Size of array: ", arr.size)
# Printing type of elements in array
print("Array stores elements of type: ", arr.dtype)
Example:
import numpy as np
# Creating array from list with type float
a = np.array([[1, 2, 4], [5, 8, 7]], dtype = 'float')
print ("Array created using passed list:\n", a)
Example:
# Reshaping 3X4 array to 2X2X3 array
arr = np.array([[1, 2, 3, 4],
[5, 2, 4, 2],
[1, 2, 0, 1]])
newarr = arr.reshape(2, 2, 3)
print ("Original array:\n", arr)
print("---------------")
print ("Reshaped array:\n", newarr)
Flatten array: We can use flatten method to get a copy of the array collapsed into one
dimension.
arr = np.array([[1, 2, 3], [4, 5, 6]])
3
flat_arr = arr.flatten()
print ("Original array:\n", arr)
print ("Fattened array:\n", flat_arr)
NumPy Array Indexing
Knowing the basics of NumPy array indexing is important for analyzing and manipulating
the array object. NumPy in Python offers many ways to do array indexing.
Slicing: Just like lists in Python, NumPy arrays can be sliced. As arrays can be
multidimensional, you need to specify a slice for each dimension of the array.
Integer array indexing: In this method, lists are passed for indexing for each
dimension. One-to-one mapping of corresponding elements is done to construct
a new arbitrary array.
Boolean array indexing: This method is used when we want to pick elements
from the array which satisfy some condition.
# Python program to demonstrate indexing in numpy
import numpy as np
# An exemplar array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
# Slicing array
temp = arr[:2, ::2]
print ("Array with first 2 rows and alternate"
"columns(0 and 2):\n", temp)
# Integer array indexing example
temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]]
print ("\nElements at indices (0, 3), (1, 2), (2, 1),"
"(3, 0):\n", temp)
# boolean array indexing example
cond = arr > 0 # cond is a boolean array
temp = arr[cond]
print ("\nElements greater than 0:\n", temp)
NumPy Basic Operations
The Plethora of built-in arithmetic functions is provided in Python NumPy.
4
Operations on a single NumPy array
We can use overloaded arithmetic operators to do element-wise operations on the array to
create a new array. In the case of +=, -=, *= operators, the existing array is modified.
# Python program to demonstrate basic operations on single array
import numpy as np
a = np.array([1, 2, 5, 3])
# add 1 to every element
print ("Adding 1 to every element:", a+1)
# subtract 3 from each element
print ("Subtracting 3 from each element:", a-3)
# multiply each element by 10
print ("Multiplying each element by 10:", a*10)
# square each element
print ("Squaring each element:", a**2)
# modify existing array
a *= 2
print ("Doubled each element of original array:", a)
# transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])
print ("\nOriginal array:\n", a)
print ("Transpose of array:\n", a.T)
NumPy – Unary Operators
Many unary operations are provided as a method of ndarray class. This includes sum, min,
max, etc. These functions can also be applied row-wise or column-wise by setting an axis
parameter.
# Python program to demonstrate unary operators in numpy
import numpy as np
arr = np.array([[1, 5, 6],
5
[4, 7, 2],
[3, 1, 9]])
# maximum element of array
print ("Largest element is:", arr.max())
print ("Row-wise maximum elements:",
arr.max(axis = 1))
# minimum element of array
print ("Column-wise minimum elements:",
arr.min(axis = 0))
# sum of array elements
print ("Sum of all array elements:",
arr.sum())
# cumulative sum along each row
print ("Cumulative sum along each row:\n",
arr.cumsum(axis = 1))
NumPy – Binary Operators
These operations apply to the array elementwise and a new array is created. You can use all
basic arithmetic operators like +, -, /, etc. In the case of +=, -=, = operators, the existing
array is modified.
# Python program to demonstrate binary operators in Numpy
import numpy as np
a = np.array([[1, 2],
[3, 4]])
b = np.array([[4, 3],
[2, 1]])
# add arrays
print ("Array sum:\n", a + b)
# multiply arrays (elementwise multiplication)
print ("Array multiplication:\n", a*b)
# matrix multiplication
print ("Matrix multiplication:\n", a.dot(b))
NumPy Sorting Arrays
There is a simple np.sort() method for sorting Python NumPy arrays. Let’s explore it a bit.
6
# Python program to demonstrate sorting in numpy
import numpy as np
a = np.array([[1, 4, 2],
[3, 4, 6],
[0, -1, 5]])
# sorted array
print ("Array elements in sorted order:\n",
np.sort(a, axis = None))
# sort array row-wise
print ("Row-wise sorted array:\n",
np.sort(a, axis = 1))
# specify sort algorithm
print ("Column wise sort by applying merge-sort:\n",
np.sort(a, axis = 0, kind = 'mergesort'))
# Example to show sorting of structured array
# set alias names for dtypes
dtypes = [('name', 'S10'), ('grad_year', int), ('cgpa', float)]
# Values to be put in array
values = [('Hrithik', 2009, 8.5), ('Ajay', 2008, 8.7),
('Pankaj', 2008, 7.9), ('Aakash', 2009, 9.0)]
# Creating array
arr = np.array(values, dtype = dtypes)
print ("\nArray sorted by names:\n",
np.sort(arr, order = 'name'))
print ("Array sorted by graduation year and then cgpa:\n",
np.sort(arr, order = ['grad_year', 'cgpa']))
Example:
# dtype parameter
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print a
# this resizes the ndarray
import numpy as np
7
a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print a
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print b
# an array of evenly spaced numbers
import numpy as np
a = np.arange(24)
print a
# this is one dimensional array
import numpy as np
a = np.arange(24)
a.ndim
# now reshape it
b = a.reshape(2,4,3)
print b
# convert list to ndarray
import numpy as np
x = [1,2,3]
a = np.asarray(x)
print a
# dtype is set
import numpy as np
x = [1,2,3]
a = np.asarray(x, dtype = float)
print a
# ndarray from tuple
import numpy as np
x = (1,2,3)
a = np.asarray(x)
print a
8
# ndarray from list of tuples
import numpy as np
x = [(1,2,3),(4,5)]
a = np.asarray(x)
print a
numpy.arange
This function returns an ndarray object containing evenly spaced values within a given
range. The format of the function is as follows −
numpy.arange(start, stop, step, dtype)
The constructor takes the following parameters.
Sr.No. Parameter & Description
1 start
The start of an interval. If omitted, defaults to 0
2 stop
The end of an interval (not including this number)
3 step
Spacing between values, default is 1
dtype
4 Data type of resulting ndarray. If not given, data type of input is
used
# start and stop parameters set
import numpy as np
x = np.arange(10,20,2)
print x
import numpy as np
a = np.arange(10)
s = slice(2,7,2)
print a[s]
import numpy as np
a = np.arange(10)
9
b = a[2:7:2]
print b
# slice items starting from index
import numpy as np
a = np.arange(10)
print a[2:]
# slice items between indexes
import numpy as np
a = np.arange(10)
print a[2:5]
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print a
# slice items starting from index
print 'Now we will slice the array from the index a[1:]'
print a[1:]
import numpy as np
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print 'Our array is:'
print x
print '\n'
# slicing
z = x[1:4,1:3]
print 'After slicing, our array becomes:'
print z
print '\n'
# using advanced index for column
y = x[1:4,[1,2]]
print 'Slicing using advanced index for column:'
print y
NumPy – Broadcasting
10
The term broadcasting refers to the ability of NumPy to treat arrays of different shapes
during arithmetic operations. Arithmetic operations on arrays are usually done on
corresponding elements.
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c=a*b
print c
import numpy as np
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
print 'First array:'
print a
print '\n'
print 'Second array:'
print b
print '\n'
print 'First Array + Second Array'
print a + b
Example
import numpy as np
# create 1-D array
array1 = np.array([1, 2, 3])
# create 2-D array
array2 = np.array([[1], [2], [3]])
# add arrays of different dimension
# size of array1 expands to match with array2
sum = array1 + array2
print(sum)
11
Compatibility Rules for Broadcasting
Broadcasting only works with compatible arrays. NumPy compares a set of array
dimensions from right to left.
Every set of dimensions must be compatible with the arrays to be broadcastable. A
set of dimension lengths is compatible when
one of them has a length of 1 or
they are equal
Let's see an example.
array1 = shape(6, 7)
array2 = shape(6, 1)
Here, array1 and array2 are arrays with different
dimensions (6,7) and (6,1) respectively.
The dimension length 7 and 1 are compatible because one of them is 1.
Similarly, 6 and 6 are compatible since they are the same.
As both sets of dimensions are compatible, the arrays are broadcastable.
Examples of Broadcastable Shapes
Now, we'll see the list of broadcastable and non-broadcastable shapes.
Broadcastable Shapes
(6, 7) and (6, 7)
(6, 7) and (6, 1)
(6, 7) and (7, )
Two arrays need not have the same number of dimensions to be broadcastable.
The last set of shapes is broadcastable because the right-most dimensions are
both 7.
Non-Broadcastable Shapes
(6, 7) and (7, 6)
12
(6, 7) and (6, )
The last set of shapes is not broadcastable because the right-most dimensions are
not the same.
Comparision:
import numpy as np
x = np.array([1, 2, 3, 4, 5])
print(x<3)
print(x<=3)
print(x!=3)
print(x==3)
print(2*x)
print(x**2)
print((2 * x) == (x ** 2))
Example:
rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
print(x)
print(x<6)
np.less(x, 6)
Comparison operators and their equivalent ufunc is shown here
Operator Equivalent ufunc
== np.equal
!= np.not_equal
< np.less
<= np.less_equal
> np.greater
>= np.greater_equal
Example:
import numpy as np
13
x = np.array([[1, 4, 2],
[3, 4, 6],
[0, -1, 5]])
#x = np.array([1, 2, 3, 4, 5])
print(np.count_nonzero(x > 6))
print(np.sum(x < 6))
# how many values less than 6 in each row?
print(np.sum(x < 6, axis=1)) #row wise count
# are there any values greater than 8?
print(np.any(x > 4))
# are all values less than 10?
print(np.all(x < 10))
# are all values equal to 6?
print(np.all(x == 6))
# are all values in each row less than 8?
print(np.all(x < 8, axis=1))
print(x[x < 5])
Now to select these values from the array, we can simply index on this Boolean array;
this is known as a masking operation:
Boolean Logic
A = np.array([1, 0, 1, 0, 1, 0], dtype=bool)
B = np.array([1, 1, 1, 0, 1, 1], dtype=bool)
print(A | B)
x = np.arange(10)
print((x > 4) & (x < 8))
Select Multiple Elements Using NumPy Fancy Indexing
import numpy as np
# create a numpy array
array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# select elements at index 1, 2, 5, 7
select_elements = array1[[1, 2, 5, 7]]
14
print(select_elements)
Example
import numpy as np
array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# select a single element
simple_indexing = array1[3]
print("Simple Indexing:",simple_indexing) # 4
# select multiple elements
fancy_indexing = array1[[1, 2, 5, 7]]
print("Fancy Indexing:",fancy_indexing) # [2 3 6 8]
Fancy Indexing for Sorting NumPy Array
import numpy as np
array1 = np.array([3, 2, 6, 1, 8, 5, 7, 4])
# sort array1 using fancy indexing
sorted_array = array1[np.argsort(array1)]
print(sorted_array)
Example
#We could also use fancy indexing to sort the array in descending order.
import numpy as np
array1 = np.array([3, 2, 6, 1, 8, 5, 7, 4])
# sort array1 using fancy indexing in descending order
sorted_array = array1[np.argsort(-array1)]
print(sorted_array)
15
Fancy Indexing to Assign New Values to Specific Elements
We can also assign new values to specific elements of a NumPy array using fancy
indexing. For example,
import numpy as np
array1 = np.array([3, 2, 6, 1, 8, 5, 7, 4])
# create a list of indices to assign new values
indices = [1, 3, 6]
# create a new array of values to assign
new_values = [10, 20, 30]
# use fancy indexing to assign new values to specific elements
array1[indices] = new_values
print(array1)
Fancy Indexing on N-d Arrays
We can also use fancy indexing on multi-dimensional arrays.
import numpy as np
# create a 2D array
array1 = np.array([[1, 3, 5],
[11, 7, 9],
[13, 18, 29]])
# create an array of row indices
row_indices = np.array([0, 2])
# use fancy indexing to select specific rows
selected_rows = array1[row_indices, :]
print(selected_rows)
Structured arrays
16
Structured arrays are ndarrays whose datatype is a composition of simpler datatypes
organized as a sequence of named fields. For example,
x = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],dtype=[('name', 'U10'), ('age', 'i4'), ('weight',
'f4')])
print(x)
Here x is a one-dimensional array of length two whose datatype is a structure with three
fields: 1. A string of length 10 or less named ‘name’, 2. a 32-bit integer named ‘age’, and 3. a
32-bit float named ‘weight’.
x = np.array([(1, 2, 3), (4, 5, 6)], dtype='i8, f4, f8')
x[1] = (7, 8, 9)
print(x)
Example
import numpy as np
a = np.array([('Sana', 2, 21.0), ('Mansi', 7, 29.0)],
dtype=[('name', (np.str_, 10)), ('age', np.int32), ('weight', np.float64)])
# Sorting according to the name
b = np.sort(a, order='name')
print('Sorting according to the name', b)
# Sorting according to the age
b = np.sort(a, order='age')
print('\nSorting according to the age', b)
max_age = np.max(a['age'])
min_age = np.min(a['age'])
print("Max age = ",max_age)
print("Min age = ", min_age)
17
Concatenating Structured Array
import numpy as np
a = np.array([('Sana', 2, 21.0), ('Mansi', 7, 29.0)],
dtype=[('name', (np.str_, 10)), ('age', np.int32), ('weight', np.float64)])
b = np.array([('Ayushi', 5, 30.0)], dtype=a.dtype)
c = np.concatenate((a, b))
print(c)
Reshaping a Structured Array
import numpy as np
a = np.array([('Sana', 2, 21.0), ('Mansi', 7, 29.0)],
dtype=[('name', (np.str_, 10)), ('age', np.int32), ('weight', np.float64)])
reshaped_a = np.reshape(a, (2, 1))
print(reshaped_a)
18