Skip to content

ENH: create a method to index N-dim tensors using 1D index #23992

Closed as not planned

Description

@dancivitarese

Proposed new feature or change:

I work with geospatial data that requires tensors with many dimensions. One challenge I used to have was when I had to implement a __getitem__ to access those tensors using a 1D index. One use case is machine learning, when one needs to feed models sequentially with sub-tensors of the original one. A simple example is a matrix of shape 2x2 that has the positions (0, 0), (0, 1), (1, 0), (1, 1), and I want to access it via indices [0, 1, 2, 3]. So, when I say __getitem__(2), I want the position (1, 0).

The internals of numpy probably has such information because arrays are usually stored contiguously in memory, but I couldn't find a way to access this information.

I came up with this function that calculates tensor indices given the 1D index and the shape of the tensor:

from typing import Tuple
import numpy as np

def f(i: int, s: Tuple[int]) -> Tuple[int]:
    return (i,) if len(s) == 1 else f(i // s[-1], s[:-1]) + (i % s[-1],)

How to use it:

tensor = np.arange(12).reshape((3, 4))
row_id, col_id = f(i=6, s=(3, 4))

Would numpy like to include this method in its code? If yes, I can check how to submit a PR. If not, I would appreciate an indication of a more suitable library (scipy, maybe xarray).

Daniel

Activity

  1. rkern commented on Jun 20, 2023

    @rkern
    Member

    Actually accessing the data like that is already implemented with the ndarray.flat property.

    Given the availability of that property, I don't think we have many use cases for computing the ndim indices one by one in Python, so I would not add a function to numpy's API to do that.

  2. dancivitarese commented on Jun 20, 2023

    @dancivitarese
    Author

    Hi @rkern, I agree entirely with you. I would like to know about this function earlier. For some reason, it's not very popular because I looked into this and found nothing about it.

  3. dancivitarese commented on Jun 20, 2023

    @dancivitarese
    Author

    Hi @rkern, I was thinking here, and maybe this flat function does not solve the problem entirely. I need to get the coordinates of my index, not the value. Is there a way to get that in numpy?

  4. rkern commented on Jun 20, 2023

    @rkern
    Member
  5. dancivitarese commented on Jun 21, 2023

    @dancivitarese
    Author

    Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    33 - QuestionQuestion about NumPy usage or development

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions