Skip to content

Named axes with the Array API #14

@pfackeldey

Description

@pfackeldey

This is an idea to implement named dimensions/axes to any array API compatible backend. Named axes can be useful in many ways, especially for readability and reducing potential errors. There’s a nice writeup of the HarvardNLP group highlighting various benefits: https://nlp.seas.harvard.edu/NamedTensor.html.



Based on my research, the following libraries implement named axes (please suggest to add more):



Named axes could extend the Array API in the following way (inspired by #2):

from named_array import array_api_strict as nxpx = nxp.asarray([[1, 2], [3, 4]], named_axes=["height", "width"])

print(nxp.sum(x, axis="width"))  # array([3, 7])



In addition, this enables a new dict-based indexing syntax that many of the above listed packages implement:


import numpy as np



print(x[{"width": 0}]) # array([1, 3])
print(x[{"width": np.s_[0:1]}]) # array([[1], [3]])



This allows to specify slices in certain dimensions based on their names.



Best, Peter




PS: If people like this "new" named indexing, maybe we find some people to revive PEP 472 (https://peps.python.org/pep-0472/) given that indexing syntax with keywords recently became more-or-less possible in the type annotations for generics with bounds/constraints (https://docs.python.org/3/reference/compound_stmts.html#type-params), and a lot more applications/packages exist now compared to before with named axes?

The new syntax could look then like this:



print(x[width=0]) # array([1, 3])
print(x[width=0:1]) # array([[1], [3]])


Metadata

Metadata

Assignees

No one assigned

    Labels

    Data analysisStatistics, optimization, sparse data, plotting, and dataframes.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions