Questions tagged [numpy-ufunc]

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

198 questions
3
votes
1 answer

Core dimension error when running numba ufunc on dask array

I'm trying to run custom numba vectorized/ufunc functions in a lazy dask pipeline. When I run the code below I get a ValueError: Core dimension 'm' consists of multiple chunks. I don't understand why m is considered a core dimension. Any idea how I…
Loïc Dutrieux
  • 381
  • 5
  • 16
3
votes
2 answers

How to access lower triangle of N*M*M numpy array

I have a numpy array of the shape arr.shape = N,M,M. I want to access the lower triangles for each M,M array. I tried using arr1 = arr[:,np.tril_indices(M,-1)] arr1 = arr[:][np.tril_indices(M,-1)] etc, with the kernel dying in the first case, while…
3
votes
2 answers

Overriding __iadd__ from the right hand side in Python

Imagine I have a class A that is outside of my control (numpy.ndarray is the specific application), and class B within my control (scipy.sparse.coo_matrix). I want to implement in-place addition of B to A without touching class A. Is this at all…
3
votes
2 answers

How to make element of 3D array into upper triangular and then tranpose it

For example, I got the 3D array below [[[1,2,3], [4,5,6] [7,8,9]], [[1,3,5], [2,4,6], [5,7,9]] [[1,4,6], [2,4,7], [5,8,9]] ] The first question is that how I can make each element along the first axis become the triangular matrix,…
Nicolas H
  • 535
  • 3
  • 13
3
votes
1 answer

Numpy vectorize for a lambda with multiple arguments

I tried for a while last night to use the various different numpy vectorisation functions for a task (which to be clear is perfectly doable without them), to create linear interpolations between points. Let's say I have a vector of floats (let's…
Louis Maddox
  • 5,226
  • 5
  • 36
  • 66
3
votes
1 answer

why np.std() and pivot_table(aggfunc=np.std) return the different result

I have some code and do not understand why the difference occurs: np.std() which default ddof=0,when it's used alone. but why when it's used as an argument in pivot_table(aggfunc=np.std),it changes into ddof=1 automatically. import numpys as…
SirenL
  • 33
  • 5
3
votes
1 answer

Fastest Python log-sum-exp in a 'reduceat'

As part of a statistical programming package, I need to add log-transformed values together with the LogSumExp Function. This is significantly less efficient than adding unlogged values together. Furthermore, I need to add values together using the…
3
votes
1 answer

Combined vectorized functions in Numba

I'm using Numba (version 0.37.0) to optimize code for GPU. I would like to use combined vectorized functions (using @vectorize decorator of Numba). Imports & Data: import numpy as np from math import sqrt from numba import vectorize,…
jetxeberria
  • 187
  • 1
  • 8
3
votes
1 answer

Map Pandas dataframe based on index, column name and original value?

I would like to map the values of a dataframe to values from a different dataframe (might also be a dict). The element to which I want to map depends on three things: the original value, the index name and the column name. For example I have the…
niclash
  • 33
  • 1
  • 5
3
votes
1 answer

Can numpy.add.at be used with 2D indices?

I have 2 arrays: - image is a NxN array, - indices is a Mx2 array, where the last dimension stores valid indices into image. I want to add 1 in image for each occurrence of that index in indices. It seems like numpy.add.at(image, indices, 1) should…
user1411900
  • 384
  • 4
  • 14
3
votes
1 answer

Numpy power ufunc operating on specific axis

I find it weird that numpy.power has no axis argument... is it because there is a better/safer way to achieve the same goal (elevating each 2D array in a 3D array to the power of a 1D array). Suppose you have a (3,10,10) array (A) and you want to…
Antonin
  • 33
  • 3
3
votes
1 answer

How to specify the last index explicitly to np.ufunc.reduceat

Say I have an array data = np.arange(6) I want to find the sum of the entire array and the second half using np.add.reduceat.1 If I do it like this: np.add.reduceat(data, [0, 6, 3])[::2] I immediately get an error IndexError: index 6 out-of-bounds…
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
3
votes
1 answer

Numpy: Finding minimum and maximum values from associations through binning

Prerequisite This is a question derived from this post. So, some of the introduction of the problem will be similar to that post. Problem Let's say result is a 2D array and values is a 1D array. values holds some values associated with each element…
mrtpk
  • 1,398
  • 4
  • 18
  • 38
3
votes
1 answer

Fast and efficient slice of array avoiding delete operation

I am trying to get a slice (for example elements 1-3 and 5-N) of an array A(N,3) avoiding using numpy.delete. And example of the process will be the following: [[1,2,3],[4,5,6],[7,8,9],[3,2,1]] ==> [[1,2,3],[3,2,1]] I was hoping to use something…
3
votes
0 answers

numpy ufunc c-api ndarray(dtype=custom_dtype) operaration scalar of custom_dtype

I'm struggling with the situation when I need a code like this to work: from custom_lib import custom_type, custom_dtype import numpy as np a = custom_type(1) arr = np.array([a,a],dtype=custom_dtype) arr+a // doesn't work ( No cast function…
1 2
3
13 14