numpy-broadcasting

Broadcast 1D array against 2D array for lexsort : Permutation for sorting each column independently when considering yet another vector

六月ゝ 毕业季﹏ 提交于 2019-12-06 22:01:09
问题 Consider the array a np.random.seed([3,1415]) a = np.random.randint(10, size=(5, 4)) a array([[0, 2, 7, 3], [8, 7, 0, 6], [8, 6, 0, 2], [0, 4, 9, 7], [3, 2, 4, 3]]) I can create b which contains the permutation to sort each column. b = a.argsort(0) b array([[0, 0, 1, 2], [3, 4, 2, 0], [4, 3, 4, 4], [1, 2, 0, 1], [2, 1, 3, 3]]) I can sort a with b a[b, np.arange(a.shape[1])[None, :]] array([[0, 2, 0, 2], [0, 2, 0, 3], [3, 4, 4, 3], [8, 6, 7, 6], [8, 7, 9, 7]]) That was the primer to illustrate

Can't use /= on numpy array

血红的双手。 提交于 2019-12-06 20:27:57
问题 On a numpy array, why is it I can successfully use / 2 : >>> a=np.array([2, 4, 6]) >>> a = a / 2 >>> a array([ 1., 2., 3.]) But I cannot use a /= 2 ? >>> a=np.array([2, 4, 6]) >>> a /= 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: No loop matching the specified signature and casting was found for ufunc true_divide I've seen numpy Issue 6464, but don't understand from reading it and the linked release notes the reason this doesn't work. Is there any way to

Matlab equivalent of Numpy broadcasting?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 18:33:14
问题 I'm trying to find some way to substract a size 3 vector from each column of a 3*(a big number) matrix in Matlab. Of course I could use a loop, but I'm trying to find some more efficient solution, a bit like numpy broadcasting. Oh, and I can't use repmat because I just don't have enough memory to use it (as it creates yet another 3*(a big number) matrix)... Is this possible? 回答1: Loops aren't bad in MATLAB anymore thanks to compiler optimizations like just-in-time acceleration (JITA). etc.

What's the most efficient way to increment an array by a reference while broadcasting row to column in NumPy Python? Can it be vectorized?

限于喜欢 提交于 2019-12-06 15:38:46
I have this piece of code in Python for i in range(len(ax)): for j in range(len(rx)): x = ax[i] + rx[j] y = ay[i] + ry[j] A[x,y] = A[x,y] + 1 where A.shape = (N,M) ax.shape = ay.shape = (L) rx.shape = ry.shape = (K) I wanted to vectorize or otherwise make it more efficient, i.e. faster, and if possible more economical in memory consumption. Here, my ax and ay refer to the absolute elements of an array A, while rx and ay are relative coordinates. So, I'm updating the counter array A. My table A can be 1000x1000, while ax,ay are 100x1 and cx,cy are 300x1. The whole thing's inside the loop,

Calculate Distances Between One Point in Matrix From All Other Points

蹲街弑〆低调 提交于 2019-12-06 03:07:13
问题 I am new to Python and I need to implement a clustering algorithm. For that, I will need to calculate distances between the given input data. Consider the following input data - [[1,2,8], [7,4,2], [9,1,7], [0,1,5], [6,4,3]] What I am looking to achieve here is, I want to calculate distance of [1,2,8] from ALL other points, and find a point where the distance is minimum. And I have to repeat this for ALL other points. I am trying to implement this with a FOR loop, but I am sure that SciPy/

When broadcasting is a bad idea ? (numpy)

陌路散爱 提交于 2019-12-06 01:49:51
问题 The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Example 1: from numpy import array a = array([1.0,2.0,3.0]) b = array([2.0,2.0,2.0]) # multiply element-by-element () a * b >> array([ 2., 4., 6.]) Example 2 : from numpy import array a = array([1.0,2.0,3.0]) b = 2.0 # broadcast b to all a a * b >>array([ 2., 4., 6.]) We can think of the scalar b being stretched during the arithmetic operation into an array with the same shape as a.

Numpy 3d array indexing

ぐ巨炮叔叔 提交于 2019-12-05 19:32:49
I have a 3d numpy array ( n_samples x num_components x 2 ) in the example below n_samples = 5 and num_components = 7. I have another array ( indices ) which is the selected component for each sample which is of shape ( n_samples ,). I want to select from the data array given the indices so that the resulting array is n_samples x 2 . The code is below: import numpy as np np.random.seed(77) data=np.random.randint(low=0, high=10, size=(5, 7, 2)) indices = np.array([0, 1, 6, 4, 5]) #how can I select indices from the data array? For example for data 0, the selected component should be the 0th and

Multiplying Numpy 3D arrays by 1D arrays

落爺英雄遲暮 提交于 2019-12-05 19:28:27
I am trying to multiply a 3D array by a 1D array, such that each 2D array along the 3rd (depth: d) dimension is calculated like: 1D_array[d]*2D_array And I end up with an array that looks like, say: [[ [1,1] [1,1]] [ [2,2] [2,2]] [ [3,3] [3,3]]] Which would be the result of correctly multiplying np.ones((3,2,2)) with [1,2,3]. I've been trying for some time now and whatever I seem to do I can't end up with this result, just variations on the theme. How do I correctly go about doing this? Thanks for any help. 来源: https://stackoverflow.com/questions/14513222/multiplying-numpy-3d-arrays-by-1d

Numpy: Replace every value in the array with the mean of its adjacent elements

て烟熏妆下的殇ゞ 提交于 2019-12-05 11:21:18
I have an ndarray, and I want to replace every value in the array with the mean of its adjacent elements. The code below can do the job, but it is super slow when I have 700 arrays all with shape (7000, 7000) , so I wonder if there are better ways to do it. Thanks! a = np.array(([1,2,3,4,5,6,7,8,9],[4,5,6,7,8,9,10,11,12],[3,4,5,6,7,8,9,10,11])) row,col = a.shape new_arr = np.ndarray(a.shape) for x in xrange(row): for y in xrange(col): min_x = max(0, x-1) min_y = max(0, y-1) new_arr[x][y] = a[min_x:(x+2),min_y:(y+2)].mean() print new_arr Well, that's a smoothing operation in image processing ,

Broadcast an operation along specific axis in python

筅森魡賤 提交于 2019-12-05 02:12:30
In python, suppose I have a square numpy matrix X , of size n x n and I have a numpy vector a of size n . Very simply, I want to perform a broadcasting subtraction of X - a , but I want to be able to specify along which dimension, so that I can specify for the subtraction to be either along axis 0 or axis 1. How can I specify the axis? Let's generate arrays with random elems Inputs : In [62]: X Out[62]: array([[ 0.32322974, 0.50491961, 0.40854442, 0.36908488], [ 0.58840196, 0.1696713 , 0.75428203, 0.01445901], [ 0.27728281, 0.33722084, 0.64187916, 0.51361972], [ 0.39151808, 0.6883594 , 0