0

EDIT:

As my question was badly formulated, I decided to rewrite it.

Does numpy allow to create an array with a function, without using Python's standard list comprehension ?

With list comprehension I could have:

array = np.array([f(i) for i in range(100)])

with f a given function.

But if the constructed array is really big, using Python's list would be slow and would eat a lot of memory.

If such a way doesn't exist, I suppose I could first create an array of my wanted size

array = np.arange(100)

And then map a function over it.

array = f(array)

According to results from another post, it seems that it would be a reasonable solution.


Let's say I want to use the add function with a simple int value, it will be as follows:

array = np.array([i for i in range(5)])
array + 5

But now what if I want the value (here 5) as something that varies according to the index of the array element. For example the operation:

array + [i for i in range(5)]

What object can I use to define special rules for a variable value within a vectorized operation ?

Pythalex
  • 143
  • 1
  • 8
  • It really depends on `f`. If it works with a whole array of indices, use it directly, If it only works with one scalar index, you have to use one the methods described in the link, Your list comprehension is a competitive choice. The big time consumer is having to call `f(i)` many times. – hpaulj May 12 '19 at 15:13
  • `broadcasting` refers to the rules for combining arrays of possibly different dimensions to make a new one. They apply when using operators like plus, `ufuncs` and indexing. It's the best way of making new arrays in `numpy`, but it requires a different way of thinking. – hpaulj May 12 '19 at 16:20

2 Answers2

0

You can add two arrays together like this:

Simple adding two arrays using numpy in python?

This assumes your "variable by index" is just another array.

cwalvoort
  • 1,851
  • 1
  • 18
  • 19
0

For your specific example, a jury-rigged solution would be to use numpy.arange() as in:

In [4]: array + np.arange(5)
Out[4]: array([0, 2, 4, 6, 8])

In general, you can find some numpy ufunc that does the job of your custom function or you can compose then in a python function to do so, which then returns an ndarray, something like:

def custom_func():
    # code for your tasks

    return arr

You can then simply add the returned result to your already defined array as in:

array + cusom_func()
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • I realize my question is poorly formulated. I was looking for a way to efficiently create the said array. For my example a simple arange would work, but what if a function could be called a certain number of times (called for each array element), this way you could potentially create complex arrays without having to create them in standard python first. As the main advantage of ufuncs is efficiency over standard python functions, needing to create a full list is a problem. – Pythalex May 12 '19 at 12:49
  • @Pythalex can you please then add a concrete description of your problem? – kmario23 May 12 '19 at 12:58
  • The advantage of `ufuncs` is that they take whole arrays as input, iterate in compiled code, and return an array. If your function is written in Python, and has to be called individually for each element of an array, you lose that speed. Creating an intermediate list result is a minor part of the issue. – hpaulj May 12 '19 at 20:36