Filling a numpy matrix using function and array

I have an array with the name phases, let's say it looks like this:

phases = numpy.random.uniform(0,1,10)

Now I want to fill in a matrix, where each row is some function fapplied to a sequential index of phases, and each column is a multiple of it, looking something like this:

[[ f(phases[0]) f(2*phases[0]) f(3*phases[0]) ]
 [ f(phases[1]) f(2*phases[1]) f(3*phases[1]) ]
       ...            ...           ...      
 [ f(phases[9]) f(2*phases[9]) f(3*phases[9]) ]]

It can be said that it fis something simple for example, for example f(x) = x+1.

So, I decided that I just use it numpy.fromfunctionas follows:

numpy.fromfunction(lambda i,j: (j+1)*phases[i]+1,
                   (phases.size, 3), dtype=float)

but this gives me an error:

IndexError: arrays used as indices must be of integer (or boolean) type

How can I access the ith element phasesinside fromfunction?

Or is this the wrong approach?

+4
source share
2

numpy.fromfunction , . , .

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

, , :

In [57]: vf = numpy.vectorize(f)

In [58]: vf(numpy.outer(phases, numpy.arange(1,4)))
Out[58]:
array([[ 1.87176928,  2.74353857,  3.61530785],
       [ 1.23090955,  1.4618191 ,  1.69272866],
       [ 1.29294723,  1.58589445,  1.87884168],
       [ 1.05863891,  1.11727783,  1.17591674],
       [ 1.28370397,  1.56740794,  1.85111191],
       [ 1.87210286,  2.74420573,  3.61630859],
       [ 1.08652975,  1.1730595 ,  1.25958925],
       [ 1.33835545,  1.6767109 ,  2.01506634],
       [ 1.74479635,  2.48959269,  3.23438904],
       [ 1.76381301,  2.52762602,  3.29143903]])

outer , , , .

. , . .

+4

, , NumPy (, , ), , f.

>>> phases = numpy.random.uniform(0,1,10)
>>> phases = phases.reshape((10, 1))
>>> phases = np.tile(phases, (1, 3))

( a ndarray)

[[ phases[0] 2*phases[0] 3*phases[0] ]
 [ phases[1] 2*phases[1] 3*phases[1] ]
    ...        ...        ...      
 [ phases[9] 2*phases[9] 3*phases[9] ]]

.

>>> def f(x):
...     return numpy.sin(x)
>>> f(phases)
array([[ 0.56551297,  0.93280166,  0.97312359],
       [ 0.38704365,  0.71375602,  0.92921009],
       [ 0.62778184,  0.97731738,  0.89368501],
       [ 0.0806512 ,  0.16077695,  0.23985519],
       [ 0.4140241 ,  0.75374405,  0.95819095],
       [ 0.25929821,  0.50085902,  0.70815838],
       [ 0.25399811,  0.49133634,  0.69644753],
       [ 0.7754078 ,  0.97927926,  0.46134512],
       [ 0.53301912,  0.90197836,  0.99331443],
       [ 0.44019133,  0.79049912,  0.9793933 ]])

, f "", ndarray . , numpy.vectorize, , .

>>> import math
>>> def f(x):
...     return math.sin(x)
>>> f(phases)
TypeError: only length-1 arrays can be converted to Python scalars
>>> f = numpy.vectorize(f)
>>> f(phases)
array([[ 0.56551297,  0.93280166,  0.97312359],
       [ 0.38704365,  0.71375602,  0.92921009],
       [ 0.62778184,  0.97731738,  0.89368501],
       [ 0.0806512 ,  0.16077695,  0.23985519],
       [ 0.4140241 ,  0.75374405,  0.95819095],
       [ 0.25929821,  0.50085902,  0.70815838],
       [ 0.25399811,  0.49133634,  0.69644753],
       [ 0.7754078 ,  0.97927926,  0.46134512],
       [ 0.53301912,  0.90197836,  0.99331443],
       [ 0.44019133,  0.79049912,  0.9793933 ]])
+1

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


All Articles