How do I create a numpy array using a function? How do I create a numpy array using a function? numpy numpy

How do I create a numpy array using a function?


The function needs to handle numpy arrays. An easy way to get this working is:

import numpy as nptest = [[1,0],[0,2]]f = lambda i, j: sum(test[i])matrix = np.fromfunction(np.vectorize(f), (len(test), len(test)), dtype=int)

np.vectorize returns a vectorized version of f, which will handle the arrays correctly.