Skip to content Skip to sidebar Skip to footer

Use Of Numpy Fromfunction

im trying to use fromfunction to create a 5x5 matrix with gaussian values of mu=3 and sig=2, this is my attempt : from random import gauss import numpy as np np.fromfunction(lambda

Solution 1:

The numpy.fromfunction docs are extremely misleading. Instead of calling your function repeatedly and building an array from the results, fromfunction actually only makes one call to the function you pass it. In that one call, it passes a number of index arrays to your function, instead of individual indices.

Stripping out the docstring, the implementation is as follows:

def fromfunction(function, shape, **kwargs):
    dtype = kwargs.pop('dtype', float)
    args = indices(shape, dtype=dtype)
    returnfunction(*args,**kwargs)

That means unless your function broadcasts, numpy.fromfunction doesn't do anything like what the docs say it does.

Solution 2:

I know this is an old post, but for anyone stumbling upon this, the reason why it didn't work is, the expression inside lambda is not making use of the i, j variables

what you need could you achieved like this:

np.zeros((5, 5)) + gauss(3, 2)

Post a Comment for "Use Of Numpy Fromfunction"