numpy-ufunc

Numpy index of the maximum with reduction - numpy.argmax.reduceat

给你一囗甜甜゛ 提交于 2021-02-16 18:43:33
问题 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) >>> array([2, 3], dtype=int32) But... Is there a way to find the index of the maximum <edit> within a chunk </edit> (like numpy.argmax ), with vectorized operations (no lists, loops)? 回答1: Borrowing the idea from this post. Steps involved : Offset all

Numpy multidimensional indexing for np.ufunc.at and np.ix_

拜拜、爱过 提交于 2021-01-29 10:01:00
问题 I would like to know how I can take index from an array and multiply with another array. I have two 4d arrays and one 2d index array: base = np.ones((2, 3, 5, 5)) to_multiply = np.arange(120).reshape(2, 3, 4, 5) index = np.array([[0, 2, 4, 2], [0, 3, 3, 2]]) The row index of the index array corresponds to the 1st dimension of base and to_multiply, and the value of the index array corresponds to the 3rd dimension of base. I want to take the slice from base according to the index and multiply

Numpy, multiply array with scalar [duplicate]

断了今生、忘了曾经 提交于 2020-06-11 18:50:11
问题 This question already has answers here : How to multiply individual elements of a list with a number? (4 answers) Closed 12 months ago . 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 = np.array([[1., 2.], [3., 4.]]) b = 2.0 Expected result: a_1 * b = array([2.0, 4.0, 6.0]); a_2 * b = array([[2., 4.],

Numpy, multiply array with scalar [duplicate]

只谈情不闲聊 提交于 2020-06-11 18:49:03
问题 This question already has answers here : How to multiply individual elements of a list with a number? (4 answers) Closed 12 months ago . 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 = np.array([[1., 2.], [3., 4.]]) b = 2.0 Expected result: a_1 * b = array([2.0, 4.0, 6.0]); a_2 * b = array([[2., 4.],

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

江枫思渺然 提交于 2020-04-13 08:27:56
问题 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 np import pandas as pd dft = pd.DataFrame({'A': ['one', 'one'], 'B': ['A', 'A'], 'C': ['bar', 'bar'], 'D': [-0.866740402,1.490732028]}) np.std(dft['D']) #equivalent:np.std([-0.866740402,1.490732028]) (which:defaualt ddof=0) #the result: 1.178736215 dft

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

给你一囗甜甜゛ 提交于 2020-04-13 08:27:55
问题 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 np import pandas as pd dft = pd.DataFrame({'A': ['one', 'one'], 'B': ['A', 'A'], 'C': ['bar', 'bar'], 'D': [-0.866740402,1.490732028]}) np.std(dft['D']) #equivalent:np.std([-0.866740402,1.490732028]) (which:defaualt ddof=0) #the result: 1.178736215 dft

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

ぃ、小莉子 提交于 2020-03-16 07:09:09
问题 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 xarray as xr import numpy as np def wrapped_gradient(da, coord): """Finds the gradient along a given dimension of a dataarray.""" dims_of_coord = da.coords[coord].dims if len(dims_of_coord) == 1: dim = dims_of_coord[0] else: raise ValueError(

numpy ufuncs speed vs for loop speed

[亡魂溺海] 提交于 2020-01-01 02:33:09
问题 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 = np.linspace(-np.pi, np.pi, resolution) prec = np.arange(1, resolution + 1) prec = 2 * prec - 1 values = np.zeros_like(tim) My first implementation was with for loop: In[2]: for i, ti in enumerate(tim): values[i] = np.sum(np.sin(prec * ti)) Then, I got rid of the explicit for cycle, and achieved

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

我的梦境 提交于 2019-12-24 20:52:27
问题 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 in add.reduceat [0, 6) If I do it like this np.add.reduceat(data, [0, 5, 3])[::2] I get the wrong answer (10 should be 15): array([10, 12]) The only solution I have been able to come up with is to mask the locations where the last index is