Questions tagged [numpy-ufunc]

Numpy universal functions are "vectorized" functions operating on elements (element by element) on a Numpy array.

149 questions
28
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
  • 463
  • 1
  • 5
  • 12
23
votes
4 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…
17
votes
1 answer

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
  • 76,709
  • 19
  • 122
  • 186
15
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
  • 1,757
  • 1
  • 12
  • 29
14
votes
2 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
  • 1,628
  • 2
  • 21
  • 41
13
votes
3 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
  • 149
  • 1
  • 2
  • 10
12
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…
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
  • 15,930
  • 7
  • 55
  • 78
11
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,266
  • 1
  • 18
  • 25
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
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
  • 47,318
  • 2
  • 37
  • 82
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
  • 983
  • 7
  • 18
5
votes
1 answer

"where" clause in numpy-1.13 ufuncs

I occasionally use the where clause in numpy's ufuncs. For example, the following: import numpy as np a = np.linspace(-1, 1, 10) np.sqrt(a, where=a>0) * (a>0) In Numpy 1.12 and earlier, this used to give me square root values where possible and…
Kessel
  • 136
  • 6
4
votes
2 answers

How to get "dot addition" in numpy similar to dot product?

I'm somewhat new to numpy and am strugging with this problem. I have two 2-dimensional numpy arrays: array1 = [a1, a2, ..., an] array2 = [b1, b2, ..., am] a1, a2, b1, and b2 are all 1-d arrays with exactly 100 floats in them. However, array1 and…
Abs
  • 1,536
  • 1
  • 14
  • 28
4
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
  • 707
  • 8
  • 18
1
2 3
9 10