-
numpy.s_ =
-
A nicer way to build up index tuples for arrays.
Note
Use one of the two predefined instances
index_exp
ors_
rather than directly usingIndexExpression
.For any index combination, including slicing and axis insertion,
a[indices]
is the same asa[np.index_exp[indices]]
for any arraya
. However,np.index_exp[indices]
can be used anywhere in Python code and returns a tuple of slice objects that can be used in the construction of complex index expressions.Parameters: maketuple : bool
If True, always returns a tuple.
See also
-
index_exp
- Predefined instance that always returns a tuple:
index_exp = IndexExpression(maketuple=True)
. -
s_
- Predefined instance without tuple conversion:
s_ = IndexExpression(maketuple=False)
.
Notes
You can do all this with
slice()
plus a few special objects, but there?s a lot to remember and this version is simpler because it uses the standard array indexing syntax.Examples
1234>>> np.s_[
2
::
2
]
slice
(
2
,
None
,
2
)
>>> np.index_exp[
2
::
2
]
(
slice
(
2
,
None
,
2
),)
12>>> np.array([
0
,
1
,
2
,
3
,
4
])[np.s_[
2
::
2
]]
array([
2
,
4
])
-
numpy.s_

2025-01-10 15:47:30
Please login to continue.