logo
Tags down

shadow

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
mask = digitsum < 20
mask
# array([True, True, True, True, True, True, True, True, False, True])


Share : facebook icon twitter icon

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()
Related Posts Related Posts :
  • Pandas cut results in Nan values
  • Logic operation: Select two values from a column in a dataframe
  • How to search and find a syntax error and then correct the syntax by adding to the string?
  • How to add aggregated rows based on other rows in Pandas dataframe
  • Select the last value in time after multiple groupings
  • updating non-null values of a column via function
  • iLocation based boolean indexing on an integer type is not available
  • Pandas resample with percentage change
  • Categorical variables usage in pandas for ANOVA and regression?
  • How to determine the end of a non-NaN series in pandas
  • How to use groupby on the following dataset
  • ValueError: key must be provided when HDF5 file contains multiple datasets while reading h5 file in pandas i am getting
  • How to add return value from function into dataframe Column?
  • Pandas identify # of items which generate 80 of sales
  • How can I find index of rows just same as a array from a pandas dataframe?
  • Pandas period(month) to last day of the month in YYYY-MM-DD format
  • analysis of groups in pandas dataframe
  • Datetime column coerced to int when setting with .loc and slice
  • Why does changing "Date" column to datetime ruin graph?
  • Splitting value dataframe over multiple timeslots
  • Cannot open a csv file
  • Pandas access first column with duplicate column names
  • Perform operations after styling in a dataframe
  • Reading values within pandas.groupby
  • Any fix for UserWarning: pyarrow.open_stream is deprecated, please use pyarrow.ipc.open_stream?
  • Create pandas dataframe from set of dictionaries
  • Name not defined in a for loop
  • stop pandas from renaming columns with same name so i can use wide to long
  • Docker build failing while installing pandas in docker in python2.7-alpine
  • Fastest way to get a cumulative list in pandas DataFrame with multi-index, grouped by index
  • import pandas results in ModuleNotFoundError :_lzma
  • Create a new column with IF-THEN in grouped pandas df
  • groupby list of lists of indexes
  • Multindex Join, Column Names
  • How to count the number of categorical features with Pandas?
  • Get_dummies produces more columns than its supposed to
  • select DataFrame Rows Based on multiple conditions on columns when column name are in a list
  • One hot encoding when a string is in column of dataframe
  • How to filter on values above x number?
  • Concatenation error (Wrong number of items passed x, placement implies 1)
  • convert dates to int in pandas
  • Resolving error when merging dataframes on two columns
  • How to select by 1 xbar date/second in kdb+
  • Installing Pandas for PyPy on Alpine Linux?
  • Extremely high memory usage with pyarrow reading gzipped parquet files
  • Random Choice loop through groups of samples
  • Calculating win percentage for individual teams based on pandas df
  • Pyarrow for parqet files, or just pandas?
  • how to find column whose name contains a specific string
  • how to plot bar gaps in pandas dataframe with timedelta and timestamp
  • Pandas running sum
  • I am not able to upload the github data link to Google Colab
  • Plotting certain bars in a series and groupnig the rest in one bar
  • I removed infinite values from my dataset but it didnt work?
  • how to enable Apache Arrow in Pyspark
  • Merge values with missing Series Index with a main index
  • How to compute mean for different size subsets within pandas dataframe?
  • How to merge categorical values that are actually same in pandas?
  • Pandas Normalization using groupby
  • Use PANDAS instead of Spark
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk