Python Numpy
Python Numpy
NumPy, short for Numerical Python, is one of the fundamental packages 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
efficiently. NumPy forms the foundation for many other scientific computing libraries
in Python, making it an essential tool for data scientists, engineers, and researchers.
Key Features:
1.
Arrays: NumPy's primary object is the homogeneous multidimensional array. These
arrays are collections of elements of the same data type and are indexed by a tuple of
nonnegative integers. NumPy's array class is called numpy.ndarray.
2.
3.
Vectorized Operations: NumPy provides a broad range of mathematical functions
that operate element-wise on arrays, making it efficient for numerical computations.
These operations are optimized and implemented in C, leading to faster execution
compared to equivalent operations in pure Python.
4.
5.
Broadcasting: NumPy allows operations between arrays of different shapes and
sizes. When performing arithmetic operations between arrays, NumPy automatically
broadcasts the smaller array to match the shape of the larger array, enabling efficient
computation without unnecessary copying of data.
6.
7.
Linear Algebra: NumPy includes a rich set of functions for linear algebra operations
such as matrix multiplication, decomposition, eigenvalues, and solving linear
equations. These functions are implemented using highly optimized BLAS and LAPACK
libraries.
8.
9.
Random Number Generation: NumPy provides facilities to generate random
numbers using various probability distributions. These functions are useful for tasks
such as simulating random processes, generating random samples for statistical
analysis, and initializing arrays with random values.
10.
11.
Integration with Other Libraries: NumPy seamlessly integrates with other Python
libraries such as SciPy (scientific computing), Matplotlib (plotting), Pandas (data
analysis), and scikit-learn (machine learning), forming a powerful ecosystem for
scientific computing and data analysis in Python.
12.
Basic Usage:
1.
Array Creation:
2.
3.
python
4.
5.
Copy code
6.
7.
import numpy as np
# Creating an array from a list
arr1 = np.array([1, 2, 3, 4, 5])
# Creating a multi-dimensional array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
# Creating arrays with specific values
zeros = np.zeros((3, 3)) # Creates a 3x3 array of zeros
ones = np.ones((2, 2)) # Creates a 2x2 array of ones
random_array = np.random.rand(3, 3) # Creates a 3x3 array of random values
8.
9.
10.
Array Operations:
11.
12.
python
13.
14.
Copy code
15.
16.
# Element-wise arithmetic operations
result = arr1 + arr2
result = arr1 * 2
# Matrix multiplication
matrix_product = np.dot(arr1, arr2)
# Statistical operations
mean = np.mean(arr1)
std_dev = np.std(arr2)
# Reshaping arrays
reshaped_array = arr1.reshape(2, 2)
17.
18.
19.
Indexing and Slicing:
20.
21.
python
22.
23.
Copy code
24.
25.
# Accessing elementsprint(arr1[0]) # Access first elementprint(arr2[1,
1]) # Access element at row 1, column 1
# Slicing
sliced_array = arr1[1:4] # Extract elements from index 1 to 3
26.
27.
Conclusion:
MCQs
2. Which of the following is the primary object in NumPy for storing data?
- A) List
- B) Tuple
- C) ndarray
- D) DataFrame
- **Answer: C) ndarray**
13. Which of the following is NOT a valid way to create a NumPy array?
- A) np.array([1, 2, 3])
- B) np.matrix([1, 2, 3])
- C) np.zeros((2, 2))
- D) np.arange(10)
- **Answer: B) np.matrix([1, 2, 3])**
17. Which function is used to find the maximum value in a NumPy array?
- A) np.max()
- B) np.maximum()
- C) np.argmax()
- D) np.amax()
- **Answer: A) np.max()**
following functions is used to find the index of the maximum value in a NumPy array?
- A) np.argmax()
- B) np.max_index()
- C) np.maximum_index()
- D) np.argmax_value()
- **Answer: A) np.argmax()**
25. Which of the following functions is used to compute the natural logarithm in NumPy?
- A) np.log()
- B) np.exp()
- C) np.sin()
- D) np.cos()
- **Answer: A) np.log()**
27. Which function is used to compute the element-wise exponential of a NumPy array?
- A) np.exp()
- B) np.log()
- C) np.sqrt()
- D) np.sin()
- **Answer: A) np.exp()**
29. Which function is used to compute the dot product of two arrays in NumPy?
- A) np.dot()
- B) np.multiply()
- C) np.divide()
- D) np.add()
- **Answer: A) np.dot()**
31. Which function is used to compute the element-wise square root of a NumPy array?
- A) np.sqrt()
- B) np.square()
- C) np.exp()
- D) np.log()
- **Answer: A) np.sqrt()**
35. Which function is used to compute the cumulative sum of elements in a NumPy array?
- A) np.sum()
- B) np.cumsum()
- C) np.mean()
- D) np.median()
- **Answer: B) np.cumsum()**
37. Which of the following functions is used to compute the cumulative product of elements in a
NumPy array?
- A) np.prod()
- B) np.cumprod()
- C) np.mean()
- D) np.median()
- **Answer: B) np.cumprod()**
39. Which of the following is NOT a valid way to access elements in a NumPy array?
- A) Using indexing
- B) Using slicing
- C) Using loops
- D) Using fancy indexing
- **Answer: C) Using loops**
41. Which function is used to compute the element-wise sine of a NumPy array?
- A) np.sin()
- B) np.cos()
- C) np.tan()
- D) np.arcsin()
- **Answer: A) np.sin()**
43. Which of the following functions is used to compute the element-wise cosine of a NumPy array?
- A) np.sin()
- B) np.cos()
- C) np.tan()
- D) np.arccos()
- **Answer: B) np.cos()**
45. Which function is used to compute the element-wise tangent of a NumPy array?
- A) np.sin()
- B) np.cos()
- C) np.tan()
- D) np.arctan()
- **Answer: C) np.tan()**
47. Which function is used to compute the element-wise arcsine of a NumPy array?
- A) np.sin()
- B) np.arcsin()
- C) np.cos()
- D) np.arccos()
- **Answer: B) np.arcsin()**
49. Which function is used to compute the element-wise arctangent of a NumPy array?
- A) np.tan()
- B) np.arctan()
- C) np.sin()
- D) np.cos()
- **Answer: B) np.arctan()**
51. Which function is used to compute the element-wise hyperbolic sine of a NumPy array?
- A) np.sinh()
- B) np.cosh()
- C) np.tanh()
- D) np.arcsinh()
- **Answer: A) np.sinh()**
53. Which function is used to compute the element-wise hyperbolic cosine of a NumPy array?
- A) np.sinh()
- B) np.cosh()
- C) np.tanh()
- D) np.arccosh()
- **Answer: B) np.cosh()**
55. Which function is used to compute the element-wise hyperbolic tangent of a NumPy array?
- A) np.sinh()
- B) np.cosh()
- C) np.tanh()
- D) np.arctanh()
- **Answer: C) np.tanh()**
57. Which function is used to compute the element-wise floor division of two NumPy arrays?
- A) np.divide()
- B) np.floor_divide()
- C) np.mod()
- D) np.power()
- **Answer: B) np.floor_divide()**
59. Which function is used to compute the element-wise remainder of division in NumPy?
- A) np.divide()
- B) np.floor_divide()
- C) np.mod()
- D) np.power()
- **Answer: C) np.mod()**
61. Which function is used to compute the element-wise exponentiation of two Num
Py arrays?
- A) np.power()
- B) np.sqrt()
- C) np.log()
- D) np.exp()
- **Answer: A) np.power()**
63. Which function is used to compute the element-wise square root of a NumPy array?
- A) np.sqrt()
- B) np.square()
- C) np.exp()
- D) np.log()
- **Answer: A) np.sqrt()**
65. Which function is used to compute the element-wise exponential of a NumPy array?
- A) np.exp()
- B) np.log()
- C) np.sqrt()
- D) np.sin()
- **Answer: A) np.exp()**
67. Which function is used to compute the element-wise ceiling of a NumPy array?
- A) np.ceil()
- B) np.floor()
- C) np.round()
- D) np.trunc()
- **Answer: A) np.ceil()**
69. Which function is used to compute the element-wise absolute value of a NumPy array?
- A) np.abs()
- B) np.absolute()
- C) np.sign()
- D) np.ceil()
- **Answer: A) np.abs()**
71. Which function is used to compute the element-wise sign of a NumPy array?
- A) np.abs()
- B) np.absolute()
- C) np.sign()
- D) np.ceil()
- **Answer: C) np.sign()**
73. Which function is used to compute the element-wise logarithm of a NumPy array?
- A) np.log()
- B) np.exp()
- C) np.sqrt()
- D) np.sin()
- **Answer: A) np.log()**
75. Which function is used to compute the element-wise square of a NumPy array?
- A) np.square()
- B) np.sqrt()
- C) np.log()
- D) np.exp()
- **Answer: A) np.square()**
77. Which function is used to compute the element-wise cube root of a NumPy array?
- A) np.sqrt()
- B) np.cbrt()
- C) np.log()
- D) np.exp()
- **Answer: B) np.cbrt()**
79. Which function is used to compute the element-wise floor of a NumPy array?
- A) np.floor()
- B) np.ceil()
- C) np.round()
- D) np.trunc()
- **Answer: A) np.floor()**
81. Which function is used to compute the element-wise round of a NumPy array?
- A) np.round()
- B) np.floor()
- C) np.ceil()
- D) np.trunc()
- **Answer: A) np.round()**
83. Which function is used to compute the element-wise maximum of two NumPy arrays?
- A) np.max()
- B) np.maximum()
- C) np.min()
- D) np.minimum()
- **Answer: B) np.maximum()**
85. Which function is used to compute the element-wise minimum of two NumPy arrays?
- A) np.max()
- B) np.maximum()
- C) np.min()
- D) np.minimum()
- **Answer: D) np.minimum()**
: D) [1, 3, 2]**
87. Which function is used to compute the element-wise absolute difference between two NumPy
arrays?
- A) np.absdiff()
- B) np.absolute_diff()
- C) np.abs_diff()
- D) np.diff_abs()
- **Answer: A) np.absdiff()**
89. Which function is used to compute the element-wise reciprocal of a NumPy array?
- A) np.reciprocal()
- B) np.inverse()
- C) np.inv()
- D) np.flip()
- **Answer: A) np.reciprocal()**
91. Which function is used to compute the element-wise square of a NumPy array?
- A) np.square()
- B) np.sqrt()
- C) np.log()
- D) np.exp()
- **Answer: A) np.square()**
93. Which function is used to compute the element-wise cube of a NumPy array?
- A) np.cube()
- B) np.power()
- C) np.sqrt()
- D) np.log()
- **Answer: B) np.power()**
95. Which function is used to compute the element-wise cube root of a NumPy array?
- A) np.cbrt()
- B) np.power()
- C) np.sqrt()
- D) np.log()
- **Answer: A) np.cbrt()**
97. Which function is used to compute the element-wise absolute difference between two NumPy
arrays?
- A) np.absdiff()
- B) np.absolute_diff()
- C) np.abs_diff()
- D) np.diff_abs()
- **Answer: A) np.absdiff()**
99. Which function is used to compute the element-wise reciprocal of a NumPy array?
- A) np.reciprocal()
- B) np.inverse()
- C) np.inv()
- D) np.flip()
- **Answer: A) np.reciprocal()**