NumPy arrays can be sliced using the colon (:) operator, which allows you to extract a portion of the array. The slicing syntax is similar to that of Python lists, but with additional features for multidimensional arrays. Here are some examples of slicing NumPy arrays:
- We pass slice instead of index like this:
[start:end]
. - We can also define the step, like this:
[start:end:step]
. - If we don't pass start its considered 0
- If we don't pass end its considered length of array in that dimension
- If we don't pass step its considered 1
You can slice a 1-D array using the colon (:) operator. For example:
2. Slicing a 2-D array:You can slice a 2-D array using the colon (:) operator for each dimension. For example:
3. Slicing with strides:You can use the colon (:) operator with a third parameter, called stride, to skip elements in the array. For example:
4. Assigning a sliced array:You can assign a sliced array to a new value. For example:
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.