Basic of Python Programming (313011) Practical No.15: Develop Python program to demonstrate use of NumPy package for creating, accessing and performing different array operations
NumPy can be used to perform a wide variety of mathematical operations on arrays. It adds powerful data structures to Python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high- level mathematical functions that operate on these arrays and matrices.
Practical related Questions
1. Write the output of the following program.
import numpy as np
arr = np.zeros((2, 3),dtype=float) # 2 rows and 3 columns
print (arr)
Answer:
[[0. 0. 0.]
[0. 0. 0.]]
2. Write the output of the following program.
import numpy as np
arr = np.random.random((3, 3)) # 3 rows and 3 columns
print (arr)
Answer:
[[0.84556229 0.43172942 0.44085081]
[0.52778292 0.80677987 0.67786648]
[0.63301872 0.32068272 0.23648907]]
3. Insert the correct method for creating a NumPy array.
arr = np. ([1, 2, 3, 4, 5])
Answer:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
Exercise
1. Write a Python NumPy program to create a 3x3 identity matrix.
Answer:
import numpy as np
# Create a 3x3 identity matrix
identity_matrix = np.identity(3)
# Print the matrix
print("3x3 Identity Matrix:")
print(identity_matrix)
2. Write a Python NumPy program to create a 4x4 matrix with values ranging from 2 to
10.
Answer:
import numpy as np
# Create a 4x4 matrix with values ranging from 2 to 10
matrix = np.arange(2, 18).reshape(4, 4)
# Print the matrix
print("4x4 Matrix with values ranging from 2 to 17:")
print(matrix)