IP - NumPy
IP - NumPy
NumPy (Numerical Python) is a powerful library used for numerical computing in Python. It
provides support for large, multi-dimensional arrays and matrices, along with a collection of
mathematical functions to operate on these arrays. NumPy is widely used in data science,
machine learning, and scientific computing.
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?
Array: An array is a data structure that can store multiple elements of the same type,
typically of a fixed size. In NumPy, arrays are implemented as ndarray (n-
dimensional array).
List vs Array:
o List (Python list): Lists are flexible data structures that can store elements of
different data types, like integers, strings, and floats.
o Array (NumPy ndarray): Arrays are more efficient for numerical
computations and are homogeneous, meaning all elements must be of the same
data type (e.g., all integers or all floats). Operations on arrays are also faster
and can be performed element-wise.
The built-in array class in NumPy is ndarray.
The rank of an ndarray refers to the number of dimensions (or axes) it has. For example:
a) A 1-D array called zeros having 10 elements and all the elements are set to zero.
import numpy as np
zeros = np.zeros(10)
print(zeros)
b) A 1-D array called vowels having the elements ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
vowels = np.array(['a', 'e', 'i', 'o', 'u'])
print(vowels)
c) A 2-D array called ones having 2 rows and 5 columns and all the elements are set to 1
and dtype as int.
d) Use nested Python lists to create a 2-D array called myarray1 having 3 rows and 3
columns and store the following data:
e) A 2-D array called myarray2 using arange() having 3 rows and 5 columns with start
value = 4, step size = 4, and dtype as float.
5. Using the arrays created in Question 4 above, write NumPy commands for
the following:
a) Find the dimensions, shape, size, data type of the items, and itemsize of arrays zeros,
vowels, ones, myarray1, and myarray2.
b) Reshape the array ones to have all the 10 elements in a single row.
d) Display all elements in the 2nd and 3rd row of the array myarray1.
e) Display the elements in the 1st and 2nd column of the array myarray1.
6. Using the arrays created in Question 4, write NumPy commands for the
following:
ones_divided = ones / 3
print(ones_divided)
c) Subtract myarray1 from myarray2 and store the result in a new array.
e) Do the matrix multiplication of myarray1 and myarray2 and store the result in a new
array myarray3.
g) Find the cube of all elements of myarray1 and divide the resulting array by 2.
cube_and_divide = (myarray1 ** 3) / 2
print(cube_and_divide)
h) Find the square root of all elements of myarray2 and divide the resulting array by 2.
The result should be rounded to two places of decimals.
sqrt_and_divide = np.sqrt(myarray2) / 2
sqrt_and_divide_rounded = np.round(sqrt_and_divide, 2)
print(sqrt_and_divide_rounded)
7. Using the arrays created in Question 4 above, write NumPy commands for
the following:
ones_transposed = ones.T
myarray2_transposed = myarray2.T
print(ones_transposed)
print(myarray2_transposed)
vowels_sorted_reverse = np.sort(vowels)[::-1]
print(vowels_sorted_reverse)
c) Sort the array myarray1 such that it brings the lowest value of the column in the first
row and so on.
8. Using the arrays created in Question 4 above, write NumPy commands for
the following:
a) Use np.split() to split the array myarray2 into 5 arrays column-wise. Store the
resulting arrays in myarray2A, myarray2B, myarray2C, myarray2D, and myarray2E. Print
the arrays.
b) Split the array zeros at array index 2, 5, 7, 8 and store the resulting arrays in
zerosA, zerosB, zerosC, and zerosD and print them.
c) Concatenate the arrays myarray2A, myarray2B, and myarray2C into an array having 3
rows and 3 columns.
10. Using the myarray4 created in the above questions, write commands for the
following:
sum_all_elements = np.sum(myarray4)
print(sum_all_elements)
max_value = np.max(myarray4)
print(max_value)