Introduction to NumPy
Introduction to NumPy
Exercise
Question 1:
What is NumPy ? How to install it?
Answer 1:
NumPy stands for Numerical Python and it is a python library used for working
with arrays. It also has various functions for scientific computing and working in
the domain of Fourier transform, linear algebra, and matrices. Travis Oliphant
created NumPy in 2005 and NumPy is an open-source project and we can use it
freely.
Installing NumPy:
We can install NumPy by using Pip. Pip is a package manager, which is used for
installing and managing Python software packages.
With Pip set up, we can use its command line for installing NumPy.
Pip will download the NumPy package and notify when it has been successfully
installed.
Question 2:
What is an array and how is it different from a list? What is the name of the built-
in array class in NumPy?
Answer 2:
An array is a variable that can hold a fixed number of homogeneous items i.e. the
items of the array should be of the same type.
Question 3:
What do you understand by rank of an ndarray?
Answer 3:
Array in NumPy are of type ndarray and it is a table of elements (usually
numbers), all of the same data type, that are indexed by a tuple of positive
integers. The number of dimensions of the array in NumPy is called the rank of
the array. A tuple of integers that gives the size of the array along each
dimension of the array is called the shape of the array.
Question 4:
Create the following NumPy arrays:
Answer 4:
Question 5:
Using the arrays created in Question 4 above, write NumPy commands for the
following:
Answer 5:
(a) Lists of numerical data, vectors, and matrices are stored in NumPy arrays. For
creating, manipulating, and modifying NumPy arrays, the NumPy library includes
a wide number of built-in functions.
2.) ndarray.shape :- returns a list of numbers representing the array's size for
each dimension.
3.) ndarray.size :- returns the array's total number of items. This is equal to the
sum of the shape elements.
4.) ndarray.dtype :- is the data type of the elements of the array. An array's
elements all have the same data type. Int32, int64, float32, float64, U32, and
other data types are common.
5.) ndarray.itemsize :- This property sets the size of each array element in bytes.
Each element of an array with the data types int32 and float32 takes up 32 bits of
memory. A byte is an 8-bit unit of measurement. As a result, an array of int32
entries has 32/8=4 bytes of itemsize. Int64/float64, on the other hand, denotes
that each item is 64/8=8 bytes in size.
where array is input array and shape is in which shape we want to change our
array. These two are required inputs. numpy array shapes must be in the from of
tuples. For example, a shape tuple for a 2 row and 3 column array will be (2,3).
NumPy commands to reshape the array "ones" to have all the 10 elements in a
single row : b=np.reshape(ones,(10,)) print(b)
(c) NumPy command to display the 2nd and 3rd element of the array vowels :-
print(vowels[1:3])
(d) Command or Code to display all elements in the 2nd and 3rd row of the array
myarray1. :-
Code to display the elements in the 1st column of the 2nd and 3rd row of the
array myarray1 :-
a) To divide each element of array by a scalar we can simply use the divide
operator as follows:
ones / 3
b) To add two arrays their shape must be same and we can use the following
method:
But myarray1 has shape (3,3) whereas myarray2 has shape (3,5), so they cannot
be added.
c) To subtract two arrays their shape must be same and we can use the
following method:
But myarray1 has shape (3,3) whereas myarray2 has shape (3,5), so they cannot
be subtracted.
d) To multiply two arrays element-wise their shape must be same and we can
use the following method:
But myarray1 has shape (3,3) whereas myarray2 has shape (3,5), so they cannot
be multiplied.
e) To perform matrix multiply of two arrays, the column of first array should be
equal to the rows of second array and we can use the following method:
myarray3 = np.matmul(myarray1,myarray2)
f) To divide two arrays their shape must be same and we can use the following
method:
But myarray1 has shape (3,3) whereas myarray2 has shape (3,5), so they cannot
be divided.
g) To find the cube of elements of numpy array we can “np.power()” function, and
we need to provide the name of the array and the power in integer formate to be
calculated as follows:
np.power(myarray1,3)
np.power(myarray1,3) / 2
h) To find the square root of a NumPy array we use the “np.sqrt()” function and
pass the array as an argument, as follows:
np.sqrt(myarray2)
np.sqrt(myarray2).round(2)
Question 7:
Using the arrays created in Question 4 above, write NumPy commands for the
following:
Answer 7:
• ones.T
• myarray2.T
b) To sort an array we can use “np.sort” function and to sort in reverse we can
use negative indexing in the next step, as follows:
• vowels.sort()
• vowels[::-1]
c) To sort a 2-D array columnwise we can simply use “np.sort()” function and use
the default value of parameter “axis=-1”, as follows:
• myarray2.sort(axis=-1)
Question 8:
Using the arrays created in Question 4 above, write NumPy commands for the
following:
Answer 8:
So,
• myarray2A,myarray2B,myarray2C,myarray2D,myarray2E
=np.split(myarray2,indices_or_sections=5, axis=1)
To print the following sub arrays, we just pass them to print function.
b) To split the array according to the index, we pass the value of indices as tuple,
as follows:
indices_or_sections = (2,5,7,8)
So,
To print the following sub arrays, we just pass them to print function.
Question 9:
Create a 2-D array called myarray4 using arange() having 14 rows and 3 columns
with start value = -1, step size 0.25 having. Split this array row wise into 3 equal
parts and print the result.
Answer 9:
Here we have to create a 2-D array of shape (14,3), that means there should be
42 elements in the array, based on this we will define the values to be passed to
the function “np.arange()”, that are as follows:
Start = -1 (Given)
myarray4 = myarray4.reshape((14,3))
Question 10:
Using the myarray4 created in the above questions, write commands for the
following:
a) Find the sum of all elements.
b) Find the sum of all elements row wise.
c) Find the sum of all elements column wise.
d) Find the max of all elements.
e) Find the min of all elements in each row.
f) Find the mean of all elements in each row.
g) Find the standard deviation column wise.
Answer 10:
a) To find the sum of all the elements of an array we can use the function
“np.sum()”, as follows:
myarray4.sum()
b) To find the sum of the elements row-wise, we can use the axis argument of
the function “np.sum()”, and for row-wise calculation axis = 1.
So,
myarray4.sum(axis = 1)
c) To find the sum of the elements column-wise, we can use the axis argument of
the function “np.sum()”, and for column-wise calculation axis = 0.
So,
myarray4.sum(axis =0)
d) To find the max among all the elements row-wise, we can use the function
“np.max()”, as follows:
myarray4.max()
e) To find the min among all the elements row-wise, we can use the axis
argument of the function “np.min()”, and for row-wise calculation, axis = 1.
So,
myarray4.min(axis = 1)
f) To find the mean of the elements row-wise, we can use the axis argument of
the function “np.mean()”, and for row-wise calculation, axis = 1.
So,
myarray4.mean(axis = 1)
So,
myarray4.std(axis = 0)