In this python numpy program, we will extract the first and second elements of the rows from an array using NumPy.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Extract the first and second elements of the first and second rows from the array using indexing.
- Print the output.
import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])
print(x[0:2,0:2])
Output :
[[ 6 5]
[10 9]]
access first two columns of a 3-D array.