I haven't grokked the key concepts in numpy
yet.
I would like to create a 3-dimensional array and populate each cell with the result of a function call - i.e. the function would be called many times with different indices and return different values.
Note: Since writing this question, the documentation has been updated to be clearer.
I could create it with zeros (or empty), and then overwrite every value with a for loop, but it seems cleaner to populate it directly from the function.
fromfunction
sounds perfect. Reading the documentation it sounds like the function gets called once per cell.
But when I actually try it...
from numpy import *
def sum_of_indices(x, y, z):
# What type are X, Y and Z ? Expect int or duck-type equivalent.
# Getting 3 individual arrays
print "Value of X is:"
print x
print "Type of X is:", type(x)
return x + y + z
a = fromfunction(sum_of_indices, (2, 2, 2))
I expect to get something like:
Value of X is:
0
Type of X is: int
Value of X is:
1
Type of X is: int
repeated 4 times.
I get:
Value of X is:
[[[ 0. 0.]
[ 0. 0.]]
[[ 1. 1.]
[ 1. 1.]]]
[[[ 0. 0.]
[ 1. 1.]]
[[ 0. 0.]
[ 1. 1.]]]
[[[ 0. 1.]
[ 0. 1.]]
[[ 0. 1.]
[ 0. 1.]]]
Type of X is: <type 'numpy.ndarray'>
The function is only called once, and seems to return the entire array as result.
What is the correct way to populate an array based on multiple calls to a function of the indices?