تلخيص numPy
تلخيص numPy
np.argmax(), np.argmin(): To find the index of the maximum and minimum values.
0 1 2
Multiple-Choice
1. What does np.zeros((2, 3)) return?
a. A 1D array of zeros with 6 elements
b. A 2D array of zeros with 2 rows and 3 columns
c. A scalar value of 0
d. A 3D array of zeros
2. What is the primary difference between a Python list and a NumPy ndarray?
a. Python lists are faster for numerical operations
b. ndarrays require all elements to be of the same type
c. ndarrays do not support slicing
d. Python lists use less memory
3. What is the output of the following code?
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr * arr)
a. [[1, 2, 3], [4, 5, 6]]
b. [[1, 4, 9], [16, 25, 36]]
c. [[2, 4, 6], [8, 10, 12]]
d. An error occurs
4. How do you generate a 3x3 identity matrix in NumPy?
a. np.identity(3)
b. np.eye(3)
c. np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
d. All of the above
5. What is the output of this slicing operation?
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[:, 2])
a. [3, 6, 9]
b. [7, 8, 9]
c. [1, 2, 3]
d. [2, 5, 8]
6. What does numpy.array() function do?
a. Creates a NumPy array from a list
b. Converts a NumPy array to a Python list
c. Computes the sum of elements in an array
d. Reshapes an array
7. What does broadcasting mean in NumPy?
a. Performing matrix multiplication
b. Adding two arrays of different shapes
c. Creating random numbers in an array
d. Reshaping an array
8. Which of the following methods would you use to create a 2x2 matrix of random numbers between 0 and 1?
a. np.random.randint(2, size=(2, 2))
b. np.random.random((2, 2))
c. np.zeros((2, 2))
d. np.ones((2, 2))
import numpy as np
# Define the 2D array
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Last Column:
arr2d[:, -1:] slices all rows (:) and selects only the last column (-1:). The result is a 2D array with the last
column as a separate column vector.
arr2d[1, -2:] slices the middle row (1) and selects the last two elements (-2:). The result is a 1D array [5,
6].
2. Write a python code to accomplish the following tasks using numpy:
1) Create a 1D NumPy array with values from 0 to 9 and print it.
2) Create a 3x3 NumPy array filled with random values between 0 and 1.
3) Allow user to enter the number of elements for the 1D array and elements then compute the
following:
1. Print all elements in the array.
2. Compute the sum of all elements in the array.
3. Compute the avg of all elements in the array.
4. Slice the array to extract the first two rows.
5. Multiply array with number 3.
import numpy as np
# 2)Create a 3x3 NumPy array filled with random values between 0 and 1.
arr = np.random.random((3, 3))
print(arr)
# 2)Create a 3x3 NumPy array filled with random values between 0 and 1.
arr = np.random.random((3, 3))
print(arr)
Input Example:
Output:
import numpy as np
print(f"Enter the elements row by row ({rows} rows and {cols} columns):")
for i in range(rows):
row = [float(input(f"Enter element for row {i+1}, column {j+1}: ")) for j in
range(cols)]
elements.append(row)