Apply NumPy's random functions using awkward inputs?
See original GitHub issueHello,
Thanks for the excellent work!! So I’m working on an application where I want to apply np.random.normal on each element of the awkward array. I’m trying to do the followings:
a = ak.from_iter([[8],[7],[9,11],[5]])
f = lambda x : np.random.normal(x,x*0.15)
# f(a) gives errors
f_arr = np.frompyfunc(f, 1, 1) # try making ufunc
# f_arr(a) also gives errors
I’m wondering if anyone here have suggestions as to what I should do in this case. 😄 Our solution right now is to basically do nested loops to change each element, which is not very efficient.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Apply NumPy's random functions using awkward inputs? #489
So I'm working on an application where I want to apply np.random.normal on each element of the awkward array.
Read more >How do you apply a function incorporating random numbers to ...
Here is my attempt at it. Basically the idea is you will have to somehow split the matrix away from the last dimension...
Read more >NumPy quickstart — NumPy v1.24 Manual
It demonstrates how n-dimensional ( n >= 2 ) arrays are represented and can be manipulated. In particular, if you don't know how...
Read more >Look Ma, No For-Loops: Array Programming With NumPy
In this tutorial, you'll see step by step how to take advantage of vectorization and broadcasting, so that you can use NumPy to...
Read more >10 quick Numpy tricks that will make life easier for a data ...
numpy.random is a module in NumPy that contains functions used for generating ... The outputs shown below will change when you use the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ll want to start a new submodule for this, so it might be done before there’s enough of a pattern to build on. However, I could start with the randomization functions and if there are any others you need, it should be clear how to build on that pattern.
Your NumpyArray is unnecessarily wrapped (
Array) and unwrapped (.layout), but otherwise, yes. Also, the general function would work for all data structures. (There’s an internalak._util.broadcast_and_applythat generalizes the process of unwrapping and re-wrapping. That’s how all of the ufuncs work. But since it’s an internal function and not a part of the stable API, you should use the technique you use here.)Oh! I guess the reason you wrapped and unwrapped the NumpyArray is because you didn’t know it was called that.
:)Your can useak.layout.NumpyArrayinstead ofArrayand then.layout. The effect is the same, but it’s more direct.