1

Say I have the following simple function that I use to generate random numbers:

def my_func():

  rvs = np.random.random(size=3)
  
  return rvs[2] - rvs[1]

I want to call this function a number of times, lets say 1000 and I want to store the results in an array, for example:

result = []
for _ in range(1000):
   result += [my_func()]

Is there a way to use numpy to vectorize this operation and make everything faster? I don't mind if the workflow changes.

2
  • could you please give the example output you would like to have? Commented Aug 29, 2022 at 9:55
  • I’d have an array which contains 1000 random numbers that are generated from the myfunc function Commented Aug 29, 2022 at 10:19

3 Answers 3

2

If I understand your question correctly, you just need to use the np.random.rand function:

np.random.rand(1000)

This function create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

You can vectorize as follows:

rvs_vect = np.random.rand(10000, 3)
result = rvs_vect[:,2] - rvs_vect[:,1]

rvs_vect[:,1] selects all rows in column 1.
rvs_vect[:,2] selects all rows in column 2.

Execution times for instances of 10000 elements on my machine are about 100 times faster than your solution and the other proposed ones (np.vectorize and list comprehension).

Extras

I have prepared an example for you with Numba. Numba is an open source JIT compiler that translates a subset of Python and NumPy code into fast machine code. Although you will not gain substantial advantages over numpy on this type of operation.

import numba as nb
nb.njit 
def my_rand(n):
    rvs_vect = np.random.rand(n, 3)
    return rvs_vect[:,2] - rvs_vect[:,1]
Sign up to request clarification or add additional context in comments.

4 Comments

But I’m trying to generate random numbers of size 3, and repeat this 1000 times. I’d like to have it vectorized though. The output of myfunc is a single number, I call myfunc 1000 times so I get an array of 1000 numbers
Thanks for this, just wondering, what does the comma notation mean here? I’ve never seen it before
To make it faster, you can even add numba njit decorator.
@skidjoe rvs_vect[:,2] means that you are referring all the rows in the array with the 3rd number in that particular row.
0

You could try: result = [my_func() for i in range(1000)], it is already fast enough

1 Comment

I see your point but I want to use numpy for this particular example, is there a way to do this?
0

Try this:

import numpy as  np
def my_func(arr):
  rvs = np.random.random(size=3)
  return rvs[2] - rvs[1]

vfunc = np.vectorize(my_func)
result = []
result.append(vfunc([1]*1000))
print(result)

Hope it hepls

Explanation:

np.vectorize is vectorizing the function. In normal cases you will pass a numpy array to a function that performs some task on its elements, but here I just passed an anonymous list for the function to be executed 1000 times rest is as you were doing

1 Comment

Be aware that this basically corresponds to wrapping the supplied my_func in a for loop. No strict-sense vectorization is happening here. See the notes in numpy.org/doc/stable/reference/generated/numpy.vectorize.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.