python - Apply a function across numpy matrix row and concatenate the result? -


i tried use numpy.apply_along_axis, seems work when applied function collapses dimension , not when expands it.

example:

def dup(x):     return np.array([x, x]) = np.array([1,2,3]) np.apply_along_axis(dup, axis=0, arr=a) # doesn't work 

i expecting matrix below (notice how dimension has expanded input matrix a):

np.array([[1, 1], [2, 2], [3, 3]]) 

in r, accomplished **ply set of functions plyr package. how numpy?

if want repeat elements can use np.repeat :

>>> np.repeat(a,2).reshape(3,2) array([[1, 1],        [2, 2],        [3, 3]]) 

and apply function use np.frompyfunc , convert integrate array use np.vstack:

>>> def dup(x): ...     return np.array([x, x]) >>> oct_array = np.frompyfunc(dup, 1, 1) >>> oct_array(a) array([array([1, 1]), array([2, 2]), array([3, 3])], dtype=object) >>> np.vstack(oct_array(a)) array([[1, 1],        [2, 2],        [3, 3]]) 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -