Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorize over the rows of an array

I have an array X and I want to apply a function f to all the rows of X:

# silly example
X = numpy.array([[1, 2, 3, 4, 5],
                 [6, 7, 8, 9, 0]], 'i')

def f(row): return sum(row)

y = numpy.vectorize(f, 'i')(rows(X))

Now, y should be array([15,30], 'i'). Which method or slicing magic will implement rows in the most efficient way?

like image 913
Fred Foo Avatar asked May 09 '11 19:05

Fred Foo


People also ask

What is array vectorization?

"Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.

What does vectorize do in NumPy?

The vectorized function evaluates pyfunc over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy. The data type of the output of vectorized is determined by calling the function with the first element of the input.

Is NumPy vectorize faster than for loop?

Vectorized implementations (numpy) are much faster and more efficient as compared to for-loops. To really see HOW large the difference is, let's try some simple operations used in most machine learnign algorithms (especially deep learning).

What is vectorize in Numba?

Numba's vectorize allows Python functions taking scalar input arguments to be used as NumPy ufuncs. Creating a traditional NumPy ufunc is not the most straightforward process and involves writing some C code. Numba makes this easy.


1 Answers

NumPy implements the concept of "action over a particular axis". The general function is numpy.apply_along_axis():

>>> numpy.apply_along_axis(sum, 1, X)
array([15, 30])

(where sum can of course be replaced by anything).

like image 100
Eric O Lebigot Avatar answered Nov 15 '22 10:11

Eric O Lebigot