Numpy universal functions are "vectorized" functions operating on elements (element by element) on a Numpy array.
Questions tagged [numpy-ufunc]
198 questions
84
votes
7 answers
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced
I am trying to convert a csv into numpy array. In the numpy array, I am replacing few elements with NaN. Then, I wanted to find the indices of the NaN elements in the numpy array. The code is :
import pandas as pd
import matplotlib.pyplot as…

Thedeadman619
- 881
- 1
- 7
- 14
49
votes
2 answers
Numpy, multiply array with scalar
Is it possible to use ufuncs https://docs.scipy.org/doc/numpy/reference/ufuncs.html
In order to map function to array (1D and / or 2D) and scalar
If not what would be my way to achieve this?
For example:
a_1 = np.array([1.0, 2.0, 3.0])
a_2 =…

ktv6
- 685
- 1
- 6
- 14
27
votes
5 answers
Most memory-efficient way to compute abs()**2 of complex numpy ndarray
I'm looking for the most memory-efficient way to compute the absolute squared value of a complex numpy ndarray
arr = np.empty((250000, 150), dtype='complex128') # common size
I haven't found a ufunc that would do exactly np.abs()**2.
As an array…

Ondřej Grover
- 719
- 1
- 5
- 13
21
votes
2 answers
Numpy passing input array as `out` argument to ufunc
Is it generally safe to provide the input array as the optional out argument to a ufunc in numpy, provided the type is correct? For example, I have verified that the following works:
>>> import numpy as np
>>> arr = np.array([1.2, 3.4, 4.5])
>>>…

Mad Physicist
- 107,652
- 25
- 181
- 264
18
votes
3 answers
np.add.at indexing with array
I'm working on cs231n and I'm having a difficult time understanding how this indexing works. Given that
x = [[0,4,1], [3,2,4]]
dW = np.zeros(5,6)
dout = [[[ 1.19034710e-01 -4.65005990e-01 8.93743168e-01 -9.78047129e-01
…

MoneyBall
- 2,343
- 5
- 26
- 59
16
votes
1 answer
numpy ufuncs speed vs for loop speed
I've read a lot "avoid for loops with numpy". So, I tried. I was using this code (simplified version). Some auxiliary data:
In[1]: import numpy as np
resolution = 1000 # this parameter varies
tim =…

godaygo
- 2,215
- 2
- 16
- 33
13
votes
1 answer
Numpy mean of flattened large array slower than mean of mean of all axes
Running Numpy version 1.19.2, I get better performance cumulating the mean of every individual axis of an array than by calculating the mean over an already flattened array.
shape = (10000,32,32,3)
mat = np.random.random(shape)
# Call this Method…

Dan Ganea
- 540
- 4
- 18
13
votes
2 answers
Use cases of `numpy.positive`
There is a positive function in numpy (version 1.13+), which seemingly does nothing:
In [1]: import numpy as np
In [2]: A = np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j,…

Yury Kirienko
- 1,810
- 1
- 22
- 32
12
votes
4 answers
Fast alternative for numpy.median.reduceat
Relating to this answer, is there a fast way to compute medians over an array that has groups with an unequal number of elements?
E.g.:
data = [1.00, 1.05, 1.30, 1.20, 1.06, 1.54, 1.33, 1.87, 1.67, ... ]
index = [0, 0, 1, 1, 1, 1, …

Jean-Paul
- 19,910
- 9
- 62
- 88
7
votes
3 answers
Numpy element-wise addition with multiple arrays
I'd like to know if there is a more efficient/pythonic way to add multiple numpy arrays (2D) rather than:
def sum_multiple_arrays(list_of_arrays):
a = np.zeros(shape=list_of_arrays[0].shape) #initialize array of 0s
for array in…

heresthebuzz
- 678
- 7
- 21
6
votes
1 answer
element wise sum of structured arrays in numpy
I wonder if it is possible at all to perform element-wise sum (or other operations) of two structured numpy arrays of an identical shape.
arr1 = np.array([[1,2,3],[2,3,4]], dtype=[("x", "f8"),("y", "f8")])
arr2 = np.array([[5,4,3],[9,6,4]],…

Hoseung Choi
- 1,017
- 2
- 12
- 20
6
votes
2 answers
Scaling of time to broadcast an operation on 3D arrays in numpy
I am trying to broadcast a simple operation of ">" over two 3D arrays. One has dimensions (m, 1, n) the other (1, m, n). If I change the value of the third dimension (n), I would naively expect that the speed of the computation would scale as n.…

max
- 61
- 4
5
votes
4 answers
numpy: accumulate 'greater' operation
I'm trying to write a function that would detect all rising edges - indexes in a vector where value exceeds certain threshold. Something similar is described here: Python rising/falling edge oscilloscope-like trigger, but I want to add hysteresis,…

monkeyman79
- 602
- 4
- 13
5
votes
1 answer
Predict memory layout of ufunc output
Using numpy ndarrays most of the time we need't worry our pretty little heads about memory layout because results do not depend on it.
Except when they do. Consider, for example, this slightly overengineered way of setting the diagonal of a 3x2…

Paul Panzer
- 51,835
- 3
- 54
- 99
5
votes
1 answer
What's the difference between dask=parallelized and dask=allowed in xarray's apply_ufunc?
In the xarray documentation for the function apply_ufunc it says:
dask: ‘forbidden’, ‘allowed’ or ‘parallelized’, optional
How to handle applying to objects containing lazy data in the form of dask arrays:
‘forbidden’ (default): raise an…

ThomasNicholas
- 1,273
- 11
- 21