Questions tagged [numpy-ufunc]

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

198 questions
2
votes
2 answers

How to apply a Pearson Correlation Analysis over all pairs of pixels of a DataArray as a Correlation Matrix?

I am facing serious difficulties in generating a correlation matrix (pixel by pixel) of a single Netcdf with dimensions ('lon', 'lat', 'time'). My final intent is to generate what one calls a Teleconnectivity Map. This Map is composed of correlation…
Philipe Riskalla Leal
  • 954
  • 1
  • 10
  • 28
2
votes
1 answer

Why is NumPy subtraction slower on one large matrix $M$ than when dividing $M$ into smaller matrices and then subtracting?

I'm working on some code where I have several matrices and want to subtract a vector $v$ from each row of each matrix (and then do some other stuff with the result). As I'm using NumPy and want to 'vectorise' as much as possible, I thought I'd speed…
grapher
  • 43
  • 4
2
votes
1 answer

ufunc Vs basic arithmetic operators in Numpy

I see that Universal Function(ufunc) is used for performing element wise array operations. arr = np.arange(5) arr2 = np.arange(5,10) np.add(arr,arr2) This piece of code is similar to arr + arr2. In that case why should we use ufunc?
Meghana Bandaru
  • 67
  • 1
  • 2
  • 6
2
votes
1 answer

Combining outer subtraction with element wise subtraction on the last axis?

import numpy as np import itertools as it SPIN_POS = np.array([[0, 0, 0], [1, 1, 0], [1, 0, 1], [0, 1, 1], [2, 2, 0], [3, 3, 0], [3, 2, 1], [2, 3, 1], [2, 0, 2], [3, 1, 2], [3, 0, 3], [2, 1, 3], …
Troy
  • 518
  • 5
  • 11
2
votes
1 answer

universal function concept and notation in numpy

I'm learning numpy package, and I've found this code example: import numpy as np a = np.array([[1,2,3], [4,5,6]]) np.add.reduce(a) The thing I'm unable to understand is the dot notation: np.add.reduce(a) in contrast with, for…
31173
  • 21
  • 3
2
votes
1 answer

Disparity between result of numpy gradient applied directly and applied using xarray.apply_ufunc

I'm trying to use xarray's apply_ufunc to wrap numpy's gradient function, in order to take gradients along one dimension. However, apply_ufunc is returning an array with a different shape to the one which using np.gradient directly returns: import…
ThomasNicholas
  • 1,273
  • 11
  • 21
2
votes
1 answer

Testing Numpy operations

Whenever I need to test a moderately complex numpy expression, say, c = np.multiply.outer(a, b) d = np.einsum('kjij->ijk', c) I end up doings hacks such as, e.g., setting a and bthus a = np.arange(9).reshape(3,3) b = a / 10 so that I can then…
Schiphol
  • 1,101
  • 1
  • 9
  • 14
2
votes
2 answers

Fastest way to compare every element with every other in np array of strings

I have a numpy array of strings, some duplicated, and I'd like to compare every element with every other element to produce a new vector of 1's and 0's indicating whether each pair (i,j) is the same or different. e.g. ["a","b","a","c"] -> 12-element…
Jess
  • 1,515
  • 3
  • 23
  • 32
2
votes
1 answer

Modifying pandas dataframe in place using numpy ufuncs

I have four columns with values: 'A', 'B', 'C' and 'D' and one column containing either of the four letters. Letter A B C D max 12063289 D 5 9 0 39 0 12063290 D 3 25 0 79 …
tobsecret
  • 2,442
  • 15
  • 26
2
votes
1 answer

How do I ufuncify a function that contains LaTeX symbols?

This code from sympy import symbols from sympy.utilities.autowrap import ufuncify a = symbols(r'\alpha') phi = a**2 phi_f = ufuncify(a, phi) throws the errors below, which are clearly coming from the \alpha in the Python/C code. Unfortunately, it's…
Michael A
  • 4,391
  • 8
  • 34
  • 61
2
votes
0 answers

With Numba's `guvectorize` targeted to CUDA, how do I specify a variable as both input and output?

I want to use Numba's guvectorize method to run code on my CUDA card. I first defined a CPU method from numba import guvectorize import numpy as np @guvectorize(['float32[:,:], float32[:,:]', 'float64[:,:], float64[:,:]'], …
Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97
2
votes
1 answer

Why is ones_like listed as a ufunc?

I was surprised to see numpy.ones_like listed in the list of ufuncs here. Is this just an oversight, or is there specific use case?
Benjamin
  • 11,560
  • 13
  • 70
  • 119
2
votes
1 answer

Sympy - ufuncify a vector function

I have lambdify-ied a vector function of a modified FitzHugh–Nagumo model (with no diffusion terms): from sympy import symbols, Matrix, solve, Eq from sympy import diff, simplify, lambdify from sympy.utilities.autowrap import ufuncify u, v , e,…
Ohm
  • 2,312
  • 4
  • 36
  • 75
2
votes
1 answer

Use numpy.frompyfunc to add broadcasting to a python function with argument

From an array like db (which will be approximately (1e6, 300)) and a mask = [1, 0, 1] vector, I define the target as a 1 in the first column. I want to create an out vector that consists of ones where the corresponding row in db matches the mask and…
user3313834
  • 7,327
  • 12
  • 56
  • 99
2
votes
1 answer

What's the ufunc to do floor division in numpy with python3.x?

In python 2.x, I can use the universal function numpy.divide to do floor division. However, the ufunc divide and true_divide both do true division in python 3.x. In [24]: np.divide([1, 2, 3, 4], 2) Out[24]: array([ 0.5, 1. , 1.5, 2. ]) In [25]:…
Eastsun
  • 18,526
  • 6
  • 57
  • 81