How to Apply a Function to an Array

Numpy apply function to every item in array

No need to change anything in your function.

Just apply the vectorized version of your function to your array
and stack the result:

np.stack(np.vectorize(filter_func)(myarray), axis=2)

The result is:

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

[[1, 0, 1],
[4, 4, 4]]])

Apply different functions to elements in array of arrays in python

You could use map():

def func_1(num):
return num+1

def func_2(num):
return num+2

test_array = [ [10,1],[10,1],[11,2]]

out = list(map(lambda x: [func_1(x[0]), func_2(x[1])], test_array))

print(out)

Prints:

[[11, 3], [11, 3], [12, 4]]

Or using comprehension:

out = [[func_1(x), func_2(y)]  for x, y in test_array]

Numpy apply function to array

You can use below code to achieve desirable output

import numpy as np
array = np.linspace(0, 5, 6)
f2 = lambda x: x-x
print(f2(array))

Applying a function along a numpy array

Function numpy.apply_along_axis is not good for this purpose.
Try to use numpy.vectorize to vectorize your function: https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html
This function defines a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns an single or tuple of numpy array as output.

import numpy as np
import math

# custom function
def sigmoid(x):
return 1 / (1 + math.exp(-x))

# define vectorized sigmoid
sigmoid_v = np.vectorize(sigmoid)

# test
scores = np.array([ -0.54761371, 17.04850603, 4.86054302])
print sigmoid_v(scores)

Output: [ 0.36641822 0.99999996 0.99231327]

Performance test which shows that the scipy.special.expit is the best solution to calculate logistic function and vectorized variant comes to the worst:

import numpy as np
import math
import timeit

def sigmoid_(x):
return 1 / (1 + math.exp(-x))
sigmoidv = np.vectorize(sigmoid_)

def sigmoid(x):
return 1 / (1 + np.exp(x))

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(100)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(100)", number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np; scores = np.random.randn(100)", number=25)

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(1000)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(1000)", number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np; scores = np.random.randn(1000)", number=25)

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(10000)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(10000)", number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np; scores = np.random.randn(10000)", number=25)

Results:

size        vectorized      numpy                 expit
N=100: 0.00179314613342 0.000460863113403 0.000132083892822
N=1000: 0.0122890472412 0.00084114074707 0.000464916229248
N=10000: 0.109477043152 0.00530695915222 0.00424313545227

How to apply a function to an array element wise in Java?

Quite cool, you can pass a lambda (since java 8):

public float[][] elemen_wise_function(float[][] x,
Function<Float, Float> f) {

float[][] Constructed_array = new float[x.length][x[0].length];
for (int i=0; i<x.length; i++) {
for (int j = 0; j < x[0].length; j++) {
Constructed_array[i][j] = f.apply(x[i][j]);
}
}
return Constructed_array;
}

Then you can pass any function defined as lambda, for example:

Function<Float, Float> fun = x -> 2 * x;

Also one note - use camelCase in java for method names;)

Apply function on each row (row-wise) of a NumPy array

You can use np.apply_along_axis:

np.apply_along_axis(function, 1, array)

The first argument is the function, the second argument is the axis along which the function is to be applied. In your case, it is the first axis. The last argument is the array, of course.


You should be warned, however, that apply_along_axis is only a convenience function, not a magic bullet. It has a severe speed limitation, since it just hides a loop. You should always try to vectorize your computation, where possible. Here's how I'd do this:

v = array[:, 0] ** 2   # computing just once  
return np.exp((-v / 200) - 0.5 * (array[:, 1] + 0.05 * v - 5) ** 2)

Apply function to array where one of function's factors is dependent on row index of array

In the for loop, currently, the first iteration creates a, and the subsequent iterations concatenates a matrix to a. Each such concatenation is an expensive operation, and repeated concatenation is definitely a bad idea.

Do not create or concatenate to a inside the for loop.

Option 1:

In the for loop, just keep accumulating a list of references to matrix objects returned by value_function. After exiting the for loop, make just one call to numpy.concatenate(), passing the list of matrix objects as the first argument.

Option 2:

If the exact sizes (number of elements) of the returned matrix objects is known beforehand (before the for loop), use those numbers to create the a array beforehand (before the for loop), in its full size, but without any initialization. To do this, you can use numpy.empty().

Then, inside the for loop, use each matrix object to perform assignments to relevant portions of this full-size a array. Again, we are avoiding repeated concatenation, so that will definitely speed up things.

Apply function to all elements in arraystring column

I would use a similar idea as @wwnde - transform function. transform takes an array, transforms every its element according to the provided function and results in the array of the same size, but with changed elements. Exactly what you need.

However, having the same original idea, I would probably implement it differently.

2 options:

from pyspark.sql import functions as F
df = spark.createDataFrame(
[(["test.a", "random.ac"],),
(["test.41", "random.23", "test.123"],)],
['c1']
)

df = df.withColumn('c2', F.transform('c1', lambda x: F.element_at(F.split(x, '\.'), 1)))
df = df.withColumn('c3', F.transform('c1', lambda x: F.regexp_extract(x, r'(.+)\.', 1)))

df.show()
# +--------------------+--------------------+--------------------+
# | c1| c2| c3|
# +--------------------+--------------------+--------------------+
# | [test.a, random.ac]| [test, random]| [test, random]|
# |[test.41, random....|[test, random, test]|[test, random, test]|
# +--------------------+--------------------+--------------------+


Related Topics



Leave a reply



Submit