Based on this StackOverflow answer:

NumPy does not support jagged arrays natively. gives an array that may or may not behave as you expect.

A workaround using masked arrays can be as follows:

import numpy as np
import numpy.ma as ma

a = np.array([0, 1])
b = np.array([2, 3, 4, 5])
c = np.array([6, 7, 8, 9, 10, 11])

jagged_array = ma.vstack(
    [
        ma.array(np.resize(a, c.shape[0]), mask=[False, False, True, True, True, True]),
        ma.array(
            np.resize(b, c.shape[0]), mask=[False, False, False, False, True, True]
        ),
        c,
    ]
)
print(jagged_array)
print(jagged_array.ndim)
print(jagged_array.shape)

Your output would look like:

❯ python3 sample.py
[[0 1 -- -- -- --]
 [2 3 4 5 -- --]
 [6 7 8 9 10 11]]
2
(3, 6)
Answer from user4109800 on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › numpy stack jagged arrays – can i make this code cleaner?
r/learnpython on Reddit: NumPy stack jagged arrays – can I make this code cleaner?
September 4, 2015 - I have 3 NumPy arrays of different lengths and want to combine them into a matrix, filling in 0s to make them equal length. I've used a rather dirty for-loop solution – is there a better way to do this? #this matrix may be jagged.
Discussions

Awkward: Nested, jagged, differentiable, mixed type, GPU-enabled, JIT'd NumPy
Just a note, I noticed that there is an unfortunate error in the page describing the bike route calculations. Right in the box where it's supposed to show how much faster things are after JIT, instead a traceback is displayed ending with: · I don't think this is intentional. More on news.ycombinator.com
🌐 news.ycombinator.com
44
144
December 20, 2021
Conversion of JaggedArray to numpy array broken
import numpy as np from awkward import * from awkward.type import * a = JaggedArray([0, 3, 3, 5], [3, 3, 5, 10], [0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]) np.asarray(a) ... File "/home/phxlk/.local/lib/python2.7/site-packages/awkward/array/base.py", line 39, in __array__ return ... More on github.com
🌐 github.com
3
October 24, 2018
RDataFrame -> AsNumpy as jagged arrays
Hello I would like to use something like scikit-hep jagged array to retrieve information from the ROOT trees → dictionary-of-flat numpy-arrays. E.g.: event entry with dynamic array tracks with parameter attributes track entry with array of clusters (position charge) In many use cases we need ... More on root-forum.cern.ch
🌐 root-forum.cern.ch
0
0
February 17, 2022
numpy functions on jagged arrays don't produce compatible arrays
I wonder if this is my misunderstanding of the API, but I'm running in to the following on a CMS NanoAOD file with a varying number of muons per event: mu_pt = arrs[b'Muon_pt'] mu_phi = arrs[b'Muon... More on github.com
🌐 github.com
10
December 12, 2018
🌐
Hacker News
news.ycombinator.com › item
Awkward: Nested, jagged, differentiable, mixed type, GPU-enabled, JIT'd NumPy | Hacker News
December 20, 2021 - Just a note, I noticed that there is an unfortunate error in the page describing the bike route calculations. Right in the box where it's supposed to show how much faster things are after JIT, instead a traceback is displayed ending with: · I don't think this is intentional.
🌐
GitHub
github.com › scikit-hep › awkward-0.x › issues › 13
Conversion of JaggedArray to numpy array broken · Issue #13 · scikit-hep/awkward-0.x
October 24, 2018 - Conversion of JaggedArray to numpy array broken#13 · Copy link · kreczko · opened · on Oct 24, 2018 · Issue body actions · It seems that things broke recently as · np.linalg.norm(<a jagged array>) No longer works. The problem can be reproduced by · import numpy as np from awkward import * from awkward.type import * a = JaggedArray([0, 3, 3, 5], [3, 3, 5, 10], [0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]) np.asarray(a) causes ·
Author: scikit-hep
🌐
YouTube
youtube.com › hey delphi
Array : How to make 2D jagged array using NumPy - YouTube
Array : How to make 2D jagged array using NumPyTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"I promised to share a hidden f...
Published: April 20, 2023
Views: 28
🌐
CERN
root-forum.cern.ch › t › rdataframe-asnumpy-as-jagged-arrays › 48835
RDataFrame -> AsNumpy as jagged arrays - ROOT - ROOT Forum
February 17, 2022 - Hello I would like to use something like scikit-hep jagged array to retrieve information from the ROOT trees → dictionary-of-flat numpy-arrays. E.g.: event entry with dynamic array tracks with parameter attributes track entry with array of clusters (position charge) In many use cases we need ...
🌐
GitHub
github.com › scikit-hep › awkward-0.x › issues › 59
numpy functions on jagged arrays don't produce compatible arrays · Issue #59 · scikit-hep/awkward-0.x
December 12, 2018 - Now when computing the np.sin function on the selected subarray, the jaggedness (starts/stops structure) of the subarray changes. mu_phi_sel.starts[:10] array([ 0, 2, 4, 9, 11, 13, 16, 18, 20, 22]) np.sin(mu_phi_sel).starts[:10] array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
Author: scikit-hep
Find elsewhere
🌐
GitHub
github.com › scikit-hep › awkward-0.x › blob › master › docs › classes.adoc
awkward-0.x/docs/classes.adoc at master · scikit-hep/awkward-0.x
June 21, 2022 - If jagged arrays are passed into a Numpy ufunc (or equivalent mapped kernel), they are computed elementwise at the deepest level of jaggedness, adjusting for different starts/stops/content representations of the same logical structure, and broadcasting scalars and non-jagged values to the jagged structure.
Author: scikit-hep
Top answer
1 of 1
1

You might think, your input is a list of tuples. However, it is a list of integers and tuples. (880) will be interpreted as an integer, but not as a tuple. So you have to deal with both datatypes.

First of all I suggest converting your input data to a list of lists. Each of the lists contained in that list should have the same length, because an array supports constant dimensions only. Therefore, I would convert the elements into a list and fill missing values with zeros (to make all elements equal in length).

If we do this for all of the elements given in the input list, we create a new list containing lists of equal length which can be converted into an array.

A very basic (and error-prone) approach would look like this:

import numpy as np


original_list = [
    (880),
    (880, 1080),
    (880, 1080, 1080),
    (470, 470, 470, 1250),
]


def get_len(item):
    try:
        return len(item)
    except TypeError:
        # `(880)` will be interpreted as an int instead of a tuple
        # so we need to handle tuples and integers
        # as integers do not support len(), a TypeError will be raised
        return 1


def to_list(item):
    try:
        return list(item)
    except TypeError:
        # `(880)` will be interpreted as an int instead of a tuple
        # so we need to handle tuples and integers
        # as integers do not support __iter__(), a TypeError will be raised
        return [item]


def fill_zeros(item, max_len):
    item_len = get_len(item)
    to_fill = [0] * (max_len - item_len)
    as_list = to_list(item) + to_fill
    return as_list


max_len = max([get_len(item) for item in original_list])
filled = [fill_zeros(item, max_len) for item in original_list]

arr = np.array(filled)
print(arr)

Printing:

[[ 880    0    0    0]
[ 880 1080    0    0]
[ 880 1080 1080    0]
[ 470  470  470 1250]]
🌐
GitHub
github.com › scikit-hep › awkward-0.x
GitHub - scikit-hep/awkward-0.x: Manipulate arrays of complex data structures as easily as Numpy. · GitHub
The shape and stride are constants, enforcing a regular layout. Awkward's JaggedArray is a generalization of Numpy's rank-2 arrays—that is, arrays of arrays—in that the inner arrays of a JaggedArray may all have different lengths.
Starred by 214 users
Forked by 38 users
Languages: Python 63.7% | Jupyter Notebook 36.3%
🌐
Tonysyu
tonysyu.github.io › ragged-arrays.html
Ragged arrays - Tony S. Yu
In my first attempt, I saved each array individually (as separate keys in an .npz file); this approach gave slow save/load times and larger file sizes. A better approach is to stack all the ragged arrays along the dimension that varies in length---i.e. the ragged dimension. Then, I use numpy's .npz file to save the array data.
🌐
Frank Sauerburger
frank.sauerburger.io › 2020 › 03 › 11 › awkward-and-numba.html
Awkward arrays and numba | Frank Sauerburger
March 11, 2020 - Jagged arrays give access to all its content via the content property. The lengths of the rows are stored in the counts property. Both are numpy arrays.
🌐
GitHub
github.com › topics › jagged-array
jagged-array · GitHub Topics · GitHub
December 2, 2022 - python data-science data-structure ... jagged-array ... A Python library for numpy arrays that persist on disk in a format that is simple, self-documented and tool-independent, and maximizes universal readability....
Top answer
1 of 2
3

Your array is 2x2:

In [298]: A
Out[298]: 
array([[array([1, 2, 3]), array([4, 5])],
       [array([6, 7, 8, 9]), array([10])]], dtype=object)

While A+A works, boolean tests have not been implemented for this kind of array:

In [299]: A>4
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm going to flatten A because it makes it easier to compare with list operations:

In [301]: A1=A.flatten()

In [303]: A1+A1
Out[303]: 
array([array([2, 4, 6]), array([ 8, 10]), array([12, 14, 16, 18]),
       array([20])], dtype=object)

In [304]: [a+a for a in A1]
Out[304]: [array([2, 4, 6]), array([ 8, 10]), array([12, 14, 16, 18]), array([20])]

In [305]: timeit A1+A1
100000 loops, best of 3: 6.85 µs per loop

In [306]: timeit [a+a for a in A1]
100000 loops, best of 3: 9.09 µs per loop

The array operation is a bit faster than a list comprehension. But if I first turn the array into a list:

In [307]: A1l=A1.tolist()

In [308]: A1l
Out[308]: [array([1, 2, 3]), array([4, 5]), array([6, 7, 8, 9]), array([10])]

In [309]: timeit [a+a for a in A1l]
100000 loops, best of 3: 5.2 µs per loop

times improve. This is a good indication that the A1+A1 (or even A+A) is using a similar sort of iteration.

So the straight forward way of performing your A,B calculation is

In [310]: A2=[a[a>4] for a in A1]
In [311]: B=[a+a for a in A2]
In [312]: B
Out[312]: [array([], dtype=int32), array([10]), array([12, 14, 16, 18]), array([20])]

(we can convert to/from arrays and lists as needed).

A numpy array stores its data a flat databuffer, and uses the shape and strides attributes to quickly calculate the location of any element, regardless of the dimensions. The fast array operations use compiled code that rapidly steps though the databuffers of arguments, performing the operations element by element (or some other combination).

A dtype object array also has the flat databuffer, but the elements are pointers to lists or arrays elsewhere. So while it can index individual elements quickly, it still has to perform a Python call(s) to access the arrays. So especially when the array is 1d, it is virtually the same as a flat list with the same pointers.

Multidimensional object arrays are nicer than nested lists. You can reshape them, access elements (A[1,3] v Al[1][3]), transpose them, etc. But when it comes to iterating through all the subarrays they don't offer much of a benefit.

Looking again at your 2d array:

In [315]: timeit A+A
100000 loops, best of 3: 6.93 µs per loop  # 6.85 for A1+A1 (above)

In [316]: timeit [[j+j for j in i] for i in A]
100000 loops, best of 3: 17.1 µs per loop

In [317]: Al = A.tolist()

In [318]: timeit [[j+j for j in i] for i in Al]
100000 loops, best of 3: 7.01 µs per loop    # 5.2 for A1l flat list

Basically the same time for summing the array and iterating through the equivalent nested list.

2 of 2
0

The performance of numpy jagged array may not be optimal, but there are enough reasons to believe that it should be much better than using python nested list. As explained in your earlier post:

On principle you should have some performance bonus because every element is a numpy array. So you just need a 2 dimensional loop rather than a 3D loop (if you store every number in nested lists). Also it always saves you lots of memory allocation time to avoid using python list.

Here is a simple test:

import time,sys,random
import numpy as np
rand = np.random.rand
L = np.array([[rand(100), rand(200)],[rand(400), rand(300)]], dtype=object)
L1 = [random.random() for i in range(1000)]
arrFunc = np.vectorize(lambda x:x[x>0.3],otypes=[np.ndarray])

start = time.time()
if sys.argv[1]=='np':
  for i in range(100000):
    B=i*L
else:
  for i in range(100000):
    B=[i*x for x in L1]

end = time.time()
print ('Arithmetic Op: ', end-start)


start = time.time()
if sys.argv[1]=='np':
  for i in range(100000):
    B=arrFunc(L)
else:
  for i in range(100000):
    B=[x for x in L1 if x<0.3]
end = time.time()
print ('Indexing       ', end-start)

Result:

> python testNpJarray.py np
Arithmetic Op:  3.9719998836517334
Indexing        8.079999923706055

> python testNpJarray.py list
Arithmetic Op:  53.289000034332275
Indexing        52.10899996757507

This test may not be quite fare because the outter numpy array is quite small, you are welcome to change the size to fit into your application and tell us the results.

🌐
GitHub
github.com › scikit-hep › awkward-0.x › issues › 17
Saving a jagged array · Issue #17 · scikit-hep/awkward-0.x
October 25, 2018 - And I actually have several arrays so I don't want to save three differently named arrays per jagged arrays by hand if possible: from awkward import JaggedArray import numpy as np ja = JaggedArray([0,4],[4,6],[1,2,3,4,5,6]) from tempfile import TemporaryFile outfile = TemporaryFile() np.savez(outfile, ja=[ja.starts, ja.stops, ja.content]) outfile.seek(0) f = np.load(outfile) JaggedArray(*f['ja'])
Author: scikit-hep
Top answer
1 of 1
1

Arrays of arrays are generally inefficient because the sub array are object causing Numpy to fallback on a slow path interacting with the interpreter (due to reference counting, checks, indirections, etc.) or to implicitly convert them into a big array internally (which is generally not possible with jagged arrays). AFAIK, your Boost code should also interact with the interpreter internally in this case. In fact An array of arrays is generally slower than a list of array because Numpy arrays are not built-ins type so CPython needs to calls Numpy functions doing many checks over and over. Additionally, Numpy does not support jagged array natively (see this related post).

Question 1: Is this the proper way of handling jagged arrays?

This is the usual way but clearly not a fast way. An efficient way to encore jagged array is to concatenate them in a big 1D array and use an additional array to store the start/stop indices (or offset/size informations). That way enable you to still use some basic Numpy vectorized methods on all arrays of sub-slices. Numba can be used to speed up the iteration over sub-arrays. The same thing applies for C++ with Boost.

Question 2: I create an object np::ndarray row to index into the second dimensions. This seems a potential performance bottleneck. Can that somehow be avoided? Can I use somehow the length of the 2nd arrays and index directly into the data buffer? I am not sure if there is padding or if there is a contiguous block of memory behind a jagged array? I assume it is not.

AFAIK, the performance bottleneck is due to the interaction with the CPython interpreter (via the CPython API) so to deal with CPython object. The above solution solve this problem since you only need to read an integer from the slicing array. The jagged array can be seen as a custom type or as a simple tuple of two arrays (possibly three if you want to split the start/stop or offsets/size). This representation is a bit similar to sparse matrices.

Note that arrays of arrays objects are indeed not stored contiguously in memory (each array object is stored are a different location in memory independently of other arrays that is dependant of the underlying CPython allocator). Arrays of arrays objects are typically stored as a pointer to an object structure containing a pointer to a memory buffer that contains pointers to objects containing each a pointer to other memory buffers. This cause a lot of pointer indirections and thus bad performance.

🌐
Navaneeth Suresh
navaneeth.net › blog › indexing-ragged-arrays-in-python
Indexing Ragged Arrays in Python • Navaneeth Suresh
June 2, 2021 - Instead of creating copies, numpy uses views to access elements in O(1) time. However, a numpy array having arrays of different shapes is considered as a normal Python list and it’s not possible to form a view out of it since the data are not contiguous in memory and operations are not vectorized.
🌐
James D. McCaffrey
jamesmccaffreyblog.com › home › loading a jagged numeric matrix from text file using python
Loading a Jagged Numeric Matrix From Text File Using Python - James D. McCaffreyJames D. McCaffrey
July 8, 2019 - In short, there’s no explicit jagged matrix type — it’s implemented as a list of arrays or tensors. Instead of a list, I could have used a NumPy array of object type items, but for this problem there’s no advantage gained by doing so.