Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# apply custom function in numpy array

By : KingD
Date : October 18 2020, 06:10 PM
I wish this help you If you want to check if the sums of the digits are > 20, here a pure numpy solution (here can find how to decompose an integer in its digits):
code :
``````import numpy as np

mylist=np.array([120,3,10,33,5,54,2,23,599,801])

mylist = mylist**2
max_digits = np.ceil(np.max(np.log10(mylist)))  # max number of digits in mylist
digits = mylist//(10**np.arange(max_digits)[:, None])%10  # matrix of digits
digitsum = np.sum(digits, axis=0)  # array of sums
# array([True, True, True, True, True, True, True, True, False, True])
``````

Share :

## Apply a custom function to multidimensional numpy array, keeping the same shape

By : fatih yilmaz
Date : March 29 2020, 07:55 AM
it helps some times Basically I want to map over each value of a multidimensional numpy array. The output should have the same shape as the input. , Just call your function on the array:
code :
``````f(input)
``````
``````import numpy as np

def f(x):
return x*x

arr = np.arange(3*4*5).reshape(3,4,5)
print(np.alltrue(f(arr) == np.array(list(map(f, input)))))
``````
``````True
``````
``````def f(x):
return x+1 if x%2 else 2*x
``````
``````np.vectorize(f)(arr)
``````
``````>>> np.alltrue(np.vectorize(f)(arr) == np.where(arr % 2, arr + 1, arr * 2))
True
``````
``````%%timeit
np.vectorize(f)(arr)
34 µs ± 996 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%%timeit
np.where(arr % 2, arr + 1, arr * 2)
5.16 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
``````
``````big_arr = np.arange(30 * 40 * 50).reshape(30, 40, 50)

%%timeit
np.vectorize(f)(big_arr)
15.5 ms ± 318 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
np.where(big_arr % 2, big_arr + 1, big_arr * 2)
797 µs ± 11.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
``````

## Apply custom function to 2 or more rows (or columns) in numpy

By : Joshua Jorgensen
Date : March 29 2020, 07:55 AM
this will help I am fairly new to numpy. I want to apply a custom function to 1, 2 or more rows (or columns). How can I do this? Before this is marked as duplicate, I want to point out that the only thread I found that does this is how to apply a generic function over numpy rows? and how to apply a generic function over numpy rows?. There are two issues with this post: , You can simply roll your axis along the 0th axis
code :
``````np.roll(M, -1, axis=0)
# array([[6, 1, 2],
#        [1, 2, 4],
#        [8, 3, 2]])
``````
``````M * np.roll(M, -1, axis=0)
# array([[48,  3,  4],
#        [ 6,  2,  8],
#        [ 8,  6,  8]])
``````
``````M * np.roll(M, -1, axis=0) * np.roll(M, -2, axis=0)
# array([[48,  6, 16],
#        [48,  6, 16],
#        [48,  6, 16]])
``````

## Numpy apply function to array

By : Corletta Evert
Date : March 29 2020, 07:55 AM
wish of those help For example, I have function: , You can use below code to achieve desirable output
code :
``````import numpy as np
array = np.linspace(0, 5, 6)
f2 = lambda x: x-x
print(f2(array))
``````

## Apply custom function on numpy matrices

By : user2694631
Date : March 29 2020, 07:55 AM
Hope that helps You can slightly modify your function to use take_along_axis instead of take, which will allow you to adapt to the 2D solution.
code :
``````def my_function_2d(x, y, k=1):
t = np.take_along_axis(x, y, -1)
u = np.power(2, t) - 1
v = np.log2(np.arange(3, k+3))
return (u / v).sum(-1)

my_function_2d(r, p, k=1)
``````
``````array([ 139.43547554, 1128.73332914])
``````
``````In [96]: k = 1

In [97]: my_function([5,6,7],[2,1,0])
Out[97]: 139.4354755392921

In [98]: my_function([8,9,10],[0,2,1])
Out[98]: 1128.7333291393375
``````
``````In [145]: my_function_2d(r[0], p[0], k=1)
Out[145]: 139.4354755392921
``````
``````In [157]: r = np.random.randint(1, 5, (2, 2, 2, 2, 2, 3))

In [158]: p = np.random.randint(0, r.shape[-1], r.shape)

In [159]: my_function_2d(r, p, k=3)
Out[159]:
array([[[[[ 8.34718483, 14.25597598],
[12.25597598, 19.97868221]],

[[12.97868221,  4.68481893],
[ 2.42295943,  1.56160631]]],

[[[23.42409467,  9.82346582],
[10.93124418, 16.42409467]],

[[23.42409467,  1.56160631],
[ 3.68481893, 10.68481893]]]],

[[[[15.97868221, 10.93124418],
[ 5.40752517, 14.93124418]],

[[ 4.14566566,  6.34718483],
[14.93124418,  3.68481893]]],

[[[ 9.20853795, 13.39462286],
[23.42409467,  3.82346582]],

[[23.42409467,  9.85293763],
[ 4.56160631, 10.93124418]]]]])
``````

## NumPy apply function to groups of rows corresponding to another numpy array

By : user3729954
Date : March 29 2020, 07:55 AM
wish of those help The problem is that the groups of rows with the same Z can have different sizes so you cannot stack them into one 3D numpy array which would allow to easily apply a function along the third dimension. One solution is to use a for-loop, another is to use np.split:
code :
``````a = np.array([[0, 0, 1],
[1, 1, 2],
[4, 5, 1],
[4, 5, 2],
[4, 3, 1]])

a_sorted = a[a[:,2].argsort()]

inds = np.unique(a_sorted[:,2], return_index=True)[1]

a_split = np.split(a_sorted, inds)[1:]

# [array([[0, 0, 1],
#         [4, 5, 1],
#         [4, 3, 1]]),

#  array([[1, 1, 2],
#         [4, 5, 2]])]

f = np.sum  # example of a function

result = list(map(f, a_split))
# [19, 15]
``````
``````b = np.unique(a[:,2])
result = [f(a[a[:,2] == z]) for z in b]
``````
``````df = pd.DataFrame(a, columns=list('XYZ'))
result = df.groupby(['Z']).apply(lambda x: f(x.values)).tolist()
``````