Numpy universal functions are "vectorized" functions operating on elements (element by element) on a Numpy array.
Questions tagged [numpy-ufunc]
149 questions
4
votes
3 answers
NumPy ufuncs are 2x faster in one axis over the other
I was doing some computation, and measured the performance of ufuncs like np.cumsum over different axes, to make the code more performant.
In [51]: arr = np.arange(int(1E6)).reshape(int(1E3), -1)
In [52]: %timeit arr.cumsum(axis=1)
2.27 ms ± 10.5…
kmario23
- 42,075
- 12
- 123
- 130
4
votes
1 answer
Use of __numpy_ufunc__()
I'm trying to use the __numpy_ufunc__() method explained here in the Numpy v1.11 docs, to override the behavior of numpy ufuncs on a subclass of ndarray, but it never seems to get called. Despite this use case being listed in the guide, I can't…
Sam Bader
- 185
- 7
4
votes
1 answer
How to properly install a NumPy ufunc?
I am trying to install an example ufunc from the SciPy Docs but when I run python setup.py build or python setup.py install I get a few warnings about a deprecated NumPy API.
When I run python setup.py install this is the output:
$ python setup.py…
Will Norvelle
- 91
- 3
- 9
4
votes
3 answers
fastest way to obtain cross product
It looks like calculating the cross-product of an array of vectors explicitly is a lot faster than using np.cross. I've tried vector-first and vector-last, it doesn't seem to make a difference, though that was proposed in an answer to a similar…
uhoh
- 3,168
- 4
- 26
- 78
3
votes
1 answer
why np.std() and pivot_table(aggfunc=np.std) return the different result
I have some code and do not understand why the difference occurs:
np.std() which default ddof=0,when it's used alone.
but why when it's used as an argument in pivot_table(aggfunc=np.std),it changes into ddof=1 automatically.
import numpys as…
SirenL
- 33
- 5
3
votes
1 answer
Fastest Python log-sum-exp in a 'reduceat'
As part of a statistical programming package, I need to add log-transformed values together with the LogSumExp Function. This is significantly less efficient than adding unlogged values together.
Furthermore, I need to add values together using the…
Wilder Wohns
- 33
- 6
3
votes
1 answer
Combined vectorized functions in Numba
I'm using Numba (version 0.37.0) to optimize code for GPU.
I would like to use combined vectorized functions (using @vectorize decorator of Numba).
Imports & Data:
import numpy as np
from math import sqrt
from numba import vectorize,…
jetxeberria
- 148
- 1
- 7
3
votes
1 answer
Map Pandas dataframe based on index, column name and original value?
I would like to map the values of a dataframe to values from a different dataframe (might also be a dict).
The element to which I want to map depends on three things:
the original value,
the index name and
the column name.
For example I have the…
NiclasH
- 33
- 1
- 5
3
votes
1 answer
Numpy power ufunc operating on specific axis
I find it weird that numpy.power has no axis argument... is it because there is a better/safer way to achieve the same goal (elevating each 2D array in a 3D array to the power of a 1D array).
Suppose you have a (3,10,10) array (A) and you want to…
Antonin
- 33
- 2
3
votes
1 answer
Numpy: Finding minimum and maximum values from associations through binning
Prerequisite
This is a question derived from this post. So, some of the introduction of the problem will be similar to that post.
Problem
Let's say result is a 2D array and values is a 1D array. values holds some values associated with each element…
tpk
- 1,083
- 4
- 15
- 28
3
votes
1 answer
Iterating and accumulating over a numpy array with a custom function
There has been some related questions for over 7 years, but I raise this issue again as I could see no 'numpy' way iteration method provided.
The task is as follows:
If I have an numpy array 'arr' and have a custom function 'fn', how could I …
sdr2002
- 492
- 2
- 5
- 14
3
votes
1 answer
Fast and efficient slice of array avoiding delete operation
I am trying to get a slice (for example elements 1-3 and 5-N) of an array A(N,3) avoiding using numpy.delete. And example of the process will be the following:
[[1,2,3],[4,5,6],[7,8,9],[3,2,1]] ==> [[1,2,3],[3,2,1]]
I was hoping to use something…
Basetxerri
- 49
- 3
3
votes
2 answers
Can bessel functions (from scipy.special) be used with Numba
I'm trying to optimize evaluation of an integral (scipy.integrate.quad) over a function containing Bessel functions with Numba.
While Numba seems to work well for "common" numpy functions, it throws an error when I attempt to include the Bessel…
SLater01
- 399
- 4
- 15
3
votes
2 answers
Avoid Numpy Index For loop
Is there any way to avoid using a second for loop for an operation like this?
for x in range(Size_1):
for y in range(Size_2):
k[x,y] = np.sqrt(x+y) - y
Or is there a better way to optimize this? Right now it is incredibly slow for large…
John
- 33
- 2
3
votes
1 answer
Numpy index of the maximum with reduction - numpy.argmax.reduceat
I have a flat array b:
a = numpy.array([0, 1, 1, 2, 3, 1, 2])
And an array c of indices marking the start of each "chunk":
b = numpy.array([0, 4])
I know I can find the maximum in each "chunk" using a reduction:
m = numpy.maximum.reduceat(a,b)
>>>…
Benjamin
- 10,449
- 13
- 64
- 112