Questions tagged [numpy-ufunc]

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

198 questions
5
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
  • 57,311
  • 13
  • 161
  • 150
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
5
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,713
  • 6
  • 42
  • 95
4
votes
1 answer

Why do I get the 'loop of ufunc does not support argument 0 of type numpy.ndarray' error for log method?

First, I used np.array to perform operations on multiple matrices, and it was successful. import numpy as np import matplotlib.pyplot as plt f = np.array([[0.35, 0.65]]) e = np.array([[0.92, 0.08], [0.03, 0.97]]) r = np.array([[0.95, 0.05], [0.06,…
4
votes
3 answers

Python list reversed can't be accessed more than once

Can someone explain this to me? import numpy as np arr = reversed(np.arange(11)) print(list(arr)) print(list(arr)) print(list(arr)) the output of this code is : why can't I access the arr variable more than once?
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,726
  • 2
  • 17
  • 28
4
votes
1 answer

Avoiding memory overflow while using xarray dask apply_ufunc

I need to apply a function along the time dimension of an xarray dask array of this shape: dask.array
4
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
  • 542
  • 2
  • 6
  • 16
4
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
  • 459
  • 1
  • 6
  • 17
4
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
  • 11,560
  • 13
  • 70
  • 119
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
  • 1
  • 7
4
votes
4 answers

Equivalent for np.add.at in tensorflow

How do I convert a np.add.at statement into tensorflow? np.add.at(dW, self.x.ravel(), dout.reshape(-1, self.D)) Edit self.dW.shape is (V, D), self.D.shape is (N, D) and self.x.size is N
Amey Agrawal
  • 132
  • 9
4
votes
3 answers

numpy dot or einsum with arbitrary operator

I would like to use something like np.dot or (preferably) np.einsum to efficiently perform their same function but with an alternate ufunc instead of np.multiply. For example, consider these two arrays: >>> a array([[0, 1], [1, 1], [1,…
bogatron
  • 18,639
  • 6
  • 53
  • 47
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
  • 101
  • 2
  • 9
4
votes
2 answers

How to use numpy.frompyfunc to return an array of elements instead of array of arrays?

I am using the PLegendre function from the SHTOOLS package. It returns an array of Legendre polynomials for a particular argument. PLegendre(lmax,x) returns an array of Legendre polynomials P_0(x) to P_lmax(x). It works like this: In [1]: from…
Jishnu
  • 769
  • 8
  • 17
1
2
3
13 14