WORKING WITH NUMPY
Numpy pronounced mostly as Num pie is a very useful and powerful. NumPy is
the fundamental package for scientific computing in Python. NumPy arrays
facilitate advanced mathematical and other types of operations on large
numbers of data. Typically, such operations are executed more efficiently and
with less code than is possible using Python’s built-in sequences. NumPy is not
another programming language but a Python extension module. It provides fast
and efficient operations on arrays of homogeneous data.
Some important points about Numpy arrays:
I. We can create a N-dimensional array in python using [Link]().
II. Array are by default Homogeneous, which means data inside an array
must be of the same Datatype. (Note you can also create a structured
array in python).
III. Element wise operation is possible.
IV. Numpy array has the various function, methods, and variables, to ease
our task of matrix computation.
V. Elements of an array are stored contiguously in memory. For example,
all rows of a two dimensioned array must have the same number of
columns. Or a three dimensioned array must have the same number of
rows and columns on each card.
Advantages of using Numpy Arrays Over Python Lists:
i. Consumes less memory.
ii. Fast as compared to the python List.
iii. Convenient to use.
To use Numpy in a program first statement should be import numpy
i.e. import numpy as np
Array- It is a collection of element of the same data type. Elements of an array
are accessed through its index value which starts from 0.
For example:
Student [ ] = {10,20,30,40,50}
Then student[0] = 10
[1] = 20
[2] = 30
[3] = 40 and so on
Page 1 of 5
What is Numpy array?
Numpy array is a grid that contains values of the same data type.
Numpy Array comes in two forms:-
i. 1D one dimensional array known as Vectors which has only single
row or single column.
ii. Multi Dimensional Array known as Matrices (can have multiple rows
and columns). In some cases Matrices can havealso one row or one
column.
2D two dimensional array is an example of Multidimensional array. It
is an array in which each elements is itself a 1D array.
Two dimension Numpy array:
It is an array in which each elements is itself a 1D array.
For instance A[M,N] is an M by N table where M is the number of rows and N is
the no. of columns, containing M x N element.
--------Columns----------
0 1 2 3 4
0
1
2
Rows
3
4
5
A[5] [4]
The Number of element in a 2D array can be determined by:
i. Calculating number of rows and number of columns and then by
multiplying number of columns:
Number of elements A[5,7] is calculated as: 5x7=35 element
Storage of 2D Array in memory-
Elements of array are stored in contiguous memory Location.
Elements of 2D Array are stored on any of the 2 Alternatives:
i. Row-major or row wise,
ii. Column-major or column wise.
Page 2 of 5
Row Major Implementation of 2D Array:
The Linearization technique stores the first row of the array then 2nd then 3rd
and so on.
Diagram of logical implementation of Row Major of 4 X 5 dimensions:
0 1 2 3 4
0
1
2
3
Physical row major implementation is like given below:
Row1 Row2 Row3 Row4
Row5
Column Major Implementation of 2D Array:
The Linearization technique stores firstly the firstly the first column then
second column then 3rd column and so on.
For example:
import numpy as np
list1=[1,2,3,4]
arr1=[Link](list1)
print(arr1)
output will be:
[1,2,3,4]
import numpy as np
list1=[1,2,3,4]
list2=[10,20,30,40]
arr2=[Link]([ list1,list2])
print(arr2)
print([Link])
print([Link])
print([Link])
will give an output:
Page 3 of 5
[[ 1 2 3 4]
[10 20 30 40]]
2
(2, 4)
8
[ ] – Square bracket is known as indexing operator. It is used to pass index
values with an array object.
Syntax to access individual element of an array:
Arrayname[index value]
It works as an index in 1D array.
Syntax to to access elements of multidimensional array :
array[row][col]
or
array[row,column]
Terms associated with Numpy array-
(i) Axes – NumPy array refers dimension of an array as axes. The axes
of an ndarray describes the order of indexing in multidimensional
ndarrays.
Axes in case of one dimensional array:
1 2 3 4 5
One row only
axes=0 one dimensional
Page 4 of 5
Axes: Numpy refers to the dimensions of its arrays as axes. The axes of an
ndarray also describes the order of indexing in multi-dimensional ndarrays. It is
always numbered 0 onward for ndarrays.
Rank: The rank of ndarray are there in ndarray. Axes of an ndarray is called its
rank.
Shape: The shape of elements along axis.
The shape of an array is a tuple of integers giving the size of the array along
each dimensions.
Datatype (d Type): Datatype or dType tells about the type of data stored in
the ndarray. Since ndarrays store homogeneous elements therefore elements
of Numpy datatype are of same type.
Item Size: It refers to size of each element of an ndarray in bytes.
It returns the size of ndarray’s dtype in bytes.
For datatype int 16 Size> 2 bytes> 16 bit
Difference between Numpy and Python List
Page 5 of 5