How to slice a 2D NumPy array in Python

Slicing a 2D NumPy array selects specific rows or columns of the array. For example, slicing [[1, 2, 3], [4, 5, 6]] for row 0 and columns 0 and 2 results in [1, 3].

Solution for How to slice a 2D NumPy array in Python : You can use numpy.ix_() to slice a NumPy array Call numpy.ix_(rows, coolumns) with rows and columns as lists of row and column indices, respectively. Use the syntax array[indices] with indices as the previous result to slice array for the specified rows and columns.

numpy.ix_() provides a syntactically straightforward method to slice an array, but isn’t necessary. Use the syntax array[rows, columns] with rows as a list of one element lists that each contain a row index and columns as a list of column indices to slice array.

print(an_array)


how-to-slice-a-2d-numpy-array-in-python