Using numpy from a function

im trying to use from a function to create a 5x5 matrix with gaussian values ​​mu = 3 and sig = 2, this is my attempt:

from random import gauss
import numpy as np
np.fromfunction(lambda i,j: gauss(3,2), (5, 5))

this is the result: 5.365244570434782

as I understand from docs , this should work, but I get a scalar instead of a 5x5 matrix ... why? and how to fix it?

+4
source share
1 answer

Documents are numpy.fromfunctionextremely misleading. Instead of repeatedly calling your function and building an array from the results, it fromfunctionactually only makes one call to the function you pass. In this one call, instead of separate indices, it passes multiple index arrays.

docstring, :

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

, , numpy.fromfunction , .

+9

Source: https://habr.com/ru/post/1661159/


All Articles