You are on page 1of 12

NUMPY TUTORIAL

Numpy is a library in python that is used


to handle n-dimensional arrays as python
does not have inbuilt arrays.
In [1]: #Installing Numpy in Google Collab
!pip install numpy

Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages


(1.22.4)

NOTES
Generally Arrays are have same data type elements and are stored in continuous
memory location.
The numpy dimensions are called as Axes.
The number of Axes/Dimensions is called as Rank.
Numpy array is called as Ndarray.

In [2]: #Importing numpy library


import numpy as np

#Creating an array
arr1 = np.array([[1,2,3],[11,12,13]]) #[a,b,c] - This is an array

#Printing the type


print('The type is:',type(arr1))

#Printing the number of dimensions in the array


print('The number of dimensions: ',arr1.ndim) #array_name.ndim is used to get the n

#Printing the shape of the array


print("The shape of the array is:",arr1.shape) #Shape(rank,number of elements in ea

"""We get output as (2,3) that means there are 2 arrays with 3 elements each. Shape
are equal elements in all the arrays"""

#Printing the number of elements in the array


print("The number of elements in the array: ",arr1.size)

#Printing the datatype of elements in the array


print("The datatype of elements is: ",arr1.dtype)
The type is: <class 'numpy.ndarray'>
The number of dimensions: 2
The shape of the array is: (2, 3)
The number of elements in the array: 6
The datatype of elements is: int64

In [3]: #Creating an array with different datatypes and unequal elements


arr2 = np.array([[21,'Vinay',34],['Maharshi',50,12,24]])

#Printing the type


print('The type is:',type(arr2))

#Printing the number of dimensions in the array


print('The number of dimensions: ',arr2.ndim)

#Printing the shape of the array


print("The shape of the array is:",arr2.shape) #This will only return the rank of t

#Printing the number of elements in the array


print("The number of elements in the array: ",arr2.size)

#Printing the datatype of elements in the array


print("The datatype of elements is: ",arr2.dtype)
#In Python, the "object" type is the base class for all data types.

The type is: <class 'numpy.ndarray'>


The number of dimensions: 1
The shape of the array is: (2,)
The number of elements in the array: 2
The datatype of elements is: object
<ipython-input-3-72c5ba72057e>:2: VisibleDeprecationWarning: Creating an ndarray f
rom ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarra
ys with different lengths or shapes) is deprecated. If you meant to do this, you m
ust specify 'dtype=object' when creating the ndarray.
arr2 = np.array([[21,'Vinay',34],['Maharshi',50,12,24]])

In [4]: #Creating an array from a list


li = [1,2,3,4,5,6,7]
num_arr = np.array(li)
print(num_arr)
print("The type is: ",type(num_arr))
print("\n")

#Creating an array from tuple


tu = (11,12,13,14,15)
arr_num = np.array(tu)
print(arr_num)
print("The type is: ",type(arr_num))

[1 2 3 4 5 6 7]
The type is: <class 'numpy.ndarray'>

[11 12 13 14 15]
The type is: <class 'numpy.ndarray'>

Basics of Numpy Array


Numpy stands for numerical python and used in working with arrays.
Axis: Axis gives the order of indexing of an array.
Shape: The number of elements along with each axis. Shape(Rank, number of elements)
Rank: Rank is simply the number axis.
Data Type(dtype): It describes how the bytes in the fixed-size block of memory
corresponding to an array item should be interpreted.

In [5]: #Different ways of creating numpy arrays.

# 1- Using the numpy.array() method


a = np.array([1,2,3])
print("This is the array using np.array:",a)
print("\n")

# 2- np.arange(start,stop,step,dtype)
b = np.arange(1,20,3,dtype=np.int64)
print("This is the array using np.arange:",b)
print("\n")

# 3- np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axi


c = np.linspace(2,20,10,dtype = np.int32)
print("This is the array using the linespace method: ",c)
print("\n")
""" start: The starting value of the sequence.
stop: The end value of the sequence (inclusive unless endpoint=False).
num: The number of equally spaced samples to be generated. Default is 50.
endpoint: If True (default), the stop value is included in the sequence.
If False, the stop value is not included in the sequence.
retstep: If True, it returns the spacing between consecutive values as a second
dtype: The data type of the output array. If not specified, it will be determin
axis: The axis along which the array is constructed. By default, it's 0."""

# 4- np.empty(shape, dtype=float, order=’C’)


# order: The memory layout of the array. It can be 'C' (row-major) or 'F' (column-m
d = np.empty([1,3], dtype = np.int32, order='f')
print("This is the array generated using np.empty: ",d)
print("\n")

# 5 - np.ones(shape, dtype=None, order=’C’) used to get only 1 matrix


e = np.ones([1,3],dtype =np.int32,order='f')
print("This is the array generated using np.ones: ",e)
print("\n")

# 6- np.zeros(shape,dtype=None) used to generate 0 matrix


f = np.zeros([1,3],dtype=np.int32)
print("This is the array generated using np.zeros: ",f)
This is the array using np.array: [1 2 3]

This is the array using np.arange: [ 1 4 7 10 13 16 19]

This is the array using the linespace method: [ 2 4 6 8 10 12 14 16 18 20]

This is the array generated using np.empty: [[916857369 23321 0]]

This is the array generated using np.ones: [[1 1 1]]

This is the array generated using np.zeros: [[0 0 0]]

Array Indexing
Array indexing in NumPy refers to the process of accessing elements, sub-arrays, or
specific slices of a NumPy array.
There are 3 ways to do array indexing:

1) 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.

2) 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.

3) Boolean array indexing: This method is used when we want to pick elements from
array which satisfy some condition.

Index number always starts with 0.

In [6]: #Using array indexing


z = np.array ([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#Using the slicing method
""" 1) arr[2:5] selects elements from index 2 (inclusive) to index 5 (exclusive)
2) arr[:4] selects elements from the beginning of the array up to index 4 (excl
3) arr[5:] selects elements from index 5 (inclusive) to the end of the array
4) arr[::2] selects elements starting from the beginning and every second eleme
temp1 = z[3,::2]
print("This is temp1:",temp1) #This will print (3,1) & (3,3)
temp2 = z[:2,3]
print("This is temp2:",temp2) #This will print (0,3) & (1,3)
print("\n")

#Using Integer array indexing


temp3 = z[2,3]
print("This is temp3:",temp3) #This will print (2,3)
temp4 = z[[2,3],[1,2]]
print("This is temp4:",temp4) #This will print (2,1) & (3,2) values
temp5 = z[[0,2,1],[1,2,3]]
print("This is temp5:",temp5) #This will print (0,1),(2,2) & (1,3)
print("\n")

#Using the Boolean Array indexing


cond = z > 5
temp6 = z[cond]
print("This is temp6:",temp6) #This will print all the values that are greater than

This is temp1: [13 15]


This is temp2: [4 8]

This is temp3: 12
This is temp4: [10 15]
This is temp5: [ 2 11 8]

This is temp6: [ 6 7 8 9 10 11 12 13 14 15 16]

Data Tpyes in Numpy


The following are the data types in Numpy:

Integer
Float
Object
Boolean
String
Datetime
Timedelta

Methods that are used to know the data types:

arr.dtype() used to get the datatypes


arr.byteorder - Used to get the byte order in memory (little/big)
arr.itemsize - Number of bytes occupied by single item.
arr.name - Gives the name of the datatype.

In [7]: #DataTypes in Numpy


dt_int32 = np.dtype(np.int32)
print("Byteorder:", dt_int32.byteorder) # Output: '=' (Native byteorder, depends o
print("Item Size:", dt_int32.itemsize) # Output: 4 (4 bytes for a 32-bit integer,
print("Name:", dt_int32.name)
print("\n")

dt_float32 = np.dtype(np.float32)
print("Byteorder:", dt_float32.byteorder)
print("Item Size:", dt_float32.itemsize)
print("Name:", dt_float32.name)
print("\n")

dt_bool = np.dtype(np.bool_)
print("Byteorder:", dt_bool.byteorder)
print("Item Size:", dt_bool.itemsize)
print("Name:", dt_bool.name)
print("\n")

dt_string = np.dtype(np.string_)
print("Byteorder:", dt_string.byteorder)
print("Item Size:", dt_string.itemsize)
print("Name:", dt_string.name)
print("\n")

dt_datetime = np.dtype(np.datetime64)
print("Byteorder:", dt_datetime.byteorder)
print("Item Size:", dt_datetime.itemsize)
print("Name:", dt_datetime.name)
print("\n")

dt_timedelta = np.dtype(np.timedelta64)
print("Byteorder:", dt_timedelta.byteorder)
print("Item Size:", dt_timedelta.itemsize)
print("Name:", dt_timedelta.name)

Byteorder: =
Item Size: 4
Name: int32

Byteorder: =
Item Size: 4
Name: float32

Byteorder: |
Item Size: 1
Name: bool

Byteorder: |
Item Size: 0
Name: bytes

Byteorder: =
Item Size: 8
Name: datetime64

Byteorder: =
Item Size: 8
Name: timedelta64

Copy and View in Numpy


Copy creates a new array where as view is only the view of the original array.
If any changes are made to the copy, it won't impact the original data.
If changes are made to the view, it will impact the original data.
arr.base is used to check if the data belongs to the array or not. It will return None for
copy.

In [8]: a1 = np.array([1,2,3,4])
z = a1.copy()
y = a1.view()
print("This is the copy: ",z)
print("This is the view: ",y)
print("This is to check if the copy array holds the data: ",z.base)
print("This is to check if the view array holds the data: ",y.base)
This is the copy: [1 2 3 4]
This is the view: [1 2 3 4]
This is to check if the copy array holds the data: None
This is to check if the view array holds the data: [1 2 3 4]

Reshape
The reshape is used to change the shape of the array.

In [9]: a2 = np.array([[1,2,3,4],[5,6,7,8]])
print("This is the original array: \n",a2)
print("The shape of the original array: ",a2.shape)
print("\n")

new_arr = a2.reshape(4,2)
print("This is the array after reshaping: \n",new_arr)
print('This is shape of the new_arr: ',new_arr.shape)

This is the original array:


[[1 2 3 4]
[5 6 7 8]]
The shape of the original array: (2, 4)

This is the array after reshaping:


[[1 2]
[3 4]
[5 6]
[7 8]]
This is shape of the new_arr: (4, 2)

Iterating through the array


In [10]: #iterating through a 2D array.
a1 = np.array([1,2,3,4])
for i in a1:
print(i) #This will print all the elements in the array.
print('\n')

#iterating through a 2D array.


a2 = np.array([[1,2,3],[4,5,6]])
for i in a2:
print(i)
print('\n')

#Getting the invidual values in 2D array.


a4 = np.array([[1,2,3],[4,5,6]])
for i in a4:
for y in i:
print(y)
print('\n') #This will print out each scalar item in the array.

#iterating through a 3D array.


a5 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for i in a5:
print(i)
print('\n')

#Iterating through each scalar item in the 3D array.


a3 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in a3:
for i in x:
for y in i:
print(y)
print('\n')

1
2
3
4

[1 2 3]
[4 5 6]

1
2
3
4
5
6

[[1 2 3]
[4 5 6]]
[[ 7 8 9]
[10 11 12]]

1
2
3
4
5
6
7
8
9
10
11
12

In Built Methods for array iterations


np.nditer(arr_name): Used to iterate through single elements of n-dimension array.
You can set the order using np.nditer(arr, order='F'). F is used for column by column
iteration, C is used for row by row iteration.
You can also change the datatype of the array using nditer. np.nditer(arr, flags=
['buffered'], op_dtypes=['new_dtype'])
NumPy does not change the data type of the element in-place (where the element is in
array) so it needs some other space to perform this action, that extra space is called
buffer.

In [11]: #Using built in methods for iteration


s1 = np.array([[[11, 12, 13], [14, 15, 16]], [[17, 18, 19], [20, 21, 22]]])
for i in np.nditer(s1):
print(i)
print('\n')

# Custom iteration order


arr = np.array([[1, 2, 3], [4, 5, 6]])
for element in np.nditer(arr, order='F'): #if order ='F' it will iterate column by
print(element)
print('\n')

#Broadcasting 2 arrays using nditer


z1 = np.array([1,2,3,4])
z2 = np.array([5,6,7,8])
for a,b in np.nditer([z1,z2]):
print(a,b)
print('\n')

#Changing the datatype using nditer


arr = np.array([1, 2, 3], dtype='int32')
for i in np.nditer(arr, flags=['buffered'], op_dtypes=['float64']):
print(i)
"""NumPy does not change the data type of the element in-place
(where the element is in array) so it needs some other space to perform this actio
#The op_dtypes is used to set the new datatype to the elements in the array.
print('\n')

11
12
13
14
15
16
17
18
19
20
21
22

1
4
2
5
3
6

1 5
2 6
3 7
4 8

1.0
2.0
3.0

ndenumerate()
It is used when we need to iterate through each element of the array while also getting
the index number of the element that we are iterating through.

In [12]: #For an 1D array.


q1 = np.array([1,2,3])
for idx,x in np.ndenumerate(q1):
print(idx,x)
print('\n')

#For an 2D array
q2 = np.array([[1,2,3],[4,5,6]])
for idx, x in np.ndenumerate(q2):
print(idx,x)

(0,) 1
(1,) 2
(2,) 3

(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6

Numpy Array Joining


We use np.conacate(arr1,arr2)
We can also use np.stack(arr1,arr2)
We can along rows using hstack(), columns using vstack() and depth usign dstack()

In [13]: #Using the concatenate method


a1 = np.array([4,5,6])
a2 = np.array([11,12,13])
new_arr = np.concatenate((a1,a2))
print(new_arr) #For concatenate to work, both the arrays must be of the same dimens
print('\n')

#Using the stack method


a1 = np.array([1,2,3])
a2 = np.array([11,12,13])
new_arr2 = np.stack((a1,a2))
print(new_arr2) #Both the arrays must have same shape for stack to work.
print('\n')

#using hstack - row stack


new_arr3 = np.hstack((a1,a2))
print(new_arr3)
print('\n')

#using vstack - column stack


new_arr4 = np.vstack((a1,a2))
print(new_arr4)
print('\n')

#using dstack - height stack


new_arr5 = np.dstack((a1,a2))
print(new_arr5)
print('\n')
[ 4 5 6 11 12 13]

[[ 1 2 3]
[11 12 13]]

[ 1 2 3 11 12 13]

[[ 1 2 3]
[11 12 13]]

[[[ 1 11]
[ 2 12]
[ 3 13]]]

Splitting the array


We use the array_split method to split the array.
array_split(array_name,no of splits)

In [18]: a1 = np.array([1,2,3])
a2 = np.array_split(a1,2)
print('This is the original array: ',a1)
print('This is the split array: ',a2)

This is the original array: [1 2 3]


This is the split array: [array([1, 2]), array([3])]

Array Sorting
We use the np.sort(array_name) to sort the array.

In [19]: v1 = np.array([1,2,3,5,4,8,6,9,7])
print('This is the sorted array: ',np.sort(v1))

This is the sorted array: [1 2 3 4 5 6 7 8 9]

Array Searching
Where(array_name operator value) this is used to search the value based on the
conditon inside the where.
Searchsorted(array_name, value)

In [24]: #Using the where method


v2 = np.array([11,13,21,34])
x = np.where(v2 >= 11)
print(x)

#Using the searchsorted method


v3 = np.array([11,13,15,12,21,34])
y = np.searchsorted(v3,12)
print(y) #This will sort the array and then search for the index of the value menti

(array([0, 1, 2, 3]),)
1

You might also like