Numpy universal functions are "vectorized" functions operating on elements (element by element) on a Numpy array.
Questions tagged [numpy-ufunc]
198 questions
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
2 answers
Add N Business Days to a Numpy datetime64 That's Not of Unit 'D'
I'm trying to add business days to a long list of data that's currently formatted as datetime64 objects, but with type 'ns'.
As per the Numpy documentation, the busday_offset function only works on objects with units of 'D'. The functionality I…

Batman
- 8,571
- 7
- 41
- 80
3
votes
2 answers
Why does accumulate work for numpy.maximum but not numpy.argmax
These two look like they should be very much equivalent and therefore what works for one should work for the other? So why does accumulate only work for maximum but not argmax?
EDIT: A natural follow-up question is then how does one go about…

RAY
- 6,810
- 6
- 40
- 67
2
votes
2 answers
Use numpy functions on objects extending numbers.Real
PEP 3141 introduces abstract base classes for different kinds of numbers to allow custom implementations.
I want to derive a class from numbers.Real and calculate its sine value. Using pythons math-module, this works fine. When I try the same in…

502E532E
- 431
- 2
- 11
2
votes
2 answers
Why is there no 'is' ufunc in numpy?
I can certainly do
a[a == 0] = something
that sets every entry of a that equals zero to something. Equivalently, I could write
a[np.equal(a, 0)] = something
Now, imagine a is an array of dtype=object. I cannot write a[a is None] because, of…

Bubaya
- 615
- 3
- 13
2
votes
2 answers
Using numpy to test for false positives and false negatives
I am trying to figure out how to calculate false positives and false negatives using numpy.
I am able to calculate accuracy and inaccuracy with the following:
In the following examples y_prediction is a 2d array of the predictions made on the…

Zuckerbrenner
- 323
- 2
- 15
2
votes
1 answer
Extract fixed number of elements per row in numpy array
Suppose I have an array a, and a boolean array b, I want to extract a fixed number of elements from the valid elements in each row of a. The valid elements are the ones indicated by b.
Here is an example:
a = np.arange(24).reshape(4,6)
b =…

tczj
- 438
- 4
- 17
2
votes
1 answer
How to avoid iteration inside an array based function in numpy?
Supose I have, in numpy, a matrix multiplication function parameterized by 2 variables x and y:
import numpy as np
def func(x, y):
a = np.array([[1, x],
[x, 2]])
b = np.array([[y, 2*x],
[x,…

enthusialgebra
- 35
- 3
2
votes
1 answer
Speed up loops over numpy arrays that modify the array
I have numpy arrays with the shape (X, Y, 4), where X and Y are large. I want to do the following operation on axis 2 of the array (i.e., A[x, y] for each x and y): for each 1-dimensional vector A[x, y], if each of the first 3 elements is within…

Mark Wexler
- 81
- 5
2
votes
0 answers
Equivalent of np.multiply.at in Pytorch
Are there equivalent of np.multiply.at in Pytorch? I have two 4d arrays and one 2d index array:
base = torch.ones((2, 3, 5, 5))
to_multiply = torch.arange(120).view(2, 3, 4, 5)
index = torch.tensor([[0, 2, 4, 2], [0, 3, 3, 2]])
As shown in this…

MachineLearner
- 413
- 5
- 10
2
votes
1 answer
Numpy with complex numbers and +=
I found something strange while working with complex matrices in python using numpy. I'll just make a short example to explain it:
This code is working totally fine:
import numpy as np
a = np.zeros((2, 2))
b = np.array([[1j, 1j], [1j, 2]])
a = a +…

JD.
- 169
- 2
- 14
2
votes
1 answer
Applying a Numba guvectorize function over time dimension of an 3D-Array with Xarray's apply_ufunc
I have some problems getting this to work properly and I'm also open to other suggestions as I'm not 100% sure if I'm going the right way with this.
Here is some simple dummy data:
times = pd.date_range(start='2012-01-01',freq='1W',periods=25)
x =…

maawoo
- 25
- 4
2
votes
0 answers
Prefix reduce operation on Numpy Array
Given an array say a = [2,6,4,1,5] how can I apply a prefix reduce operator on it.
For example:
Prefix sum of a = [2,8,12,13,18] or
Prefix max of a = [2,6,6,6,6]
I want a method using which I can use any suitable reduce operators like mean, max or…

djin31
- 58
- 1
- 5
2
votes
2 answers
All possible combinations of 2D numpy array
I have four numpy arrays, and example given below:
a1=np.array([[-24.4925, 295.77 ],
[-24.4925, 295.77 ],
[-14.3925, 295.77 ],
[-16.4125, 295.77 ],
[-43.6825, 295.77 ],
[-22.4725,…

Waleed Esmail
- 77
- 7
2
votes
4 answers
Raise TypeError (TypeError: object of type cannot be safely interpreted as an integer)
I am using a package that needs NumPy. Until now it was working fine. But today, because of extending my code I needed the newest version of NumPy. The old version was 17.something and I installed the latest version. After that I am facing the below…

user105697
- 39
- 2
- 9