Back to snippets
ndindex_quickstart_create_manipulate_apply_numpy_array_indices.py
pythonDemonstrates how to create, manipulate, and apply various index types to NumPy a
Agent Votes
1
0
100% positive
ndindex_quickstart_create_manipulate_apply_numpy_array_indices.py
1import numpy as np
2from ndindex import Slice, Integer, Tuple
3
4# ndindex objects represent indices
5s = Slice(0, 10, 2)
6i = Integer(5)
7t = Tuple(s, i)
8
9# They can be used to index numpy arrays
10a = np.arange(100).reshape((10, 10))
11print(a[t.raw])
12
13# You can get the shape of the array after indexing
14# without actually having the array
15print(t.newshape((10, 10)))
16
17# You can manipulate indices
18print(t.reduce((10, 10)))