question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apply NumPy's random functions using awkward inputs?

See original GitHub issue

Hello,

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:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jpivarskicommented, Oct 18, 2020

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.

1reaction
jpivarskicommented, Oct 18, 2020

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 internal ak._util.broadcast_and_apply that 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 use ak.layout.NumpyArray instead of Array and then .layout. The effect is the same, but it’s more direct.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found