numpy-broadcasting

What is supported by broadcasting in tensorflow? How dimensions matches determined?

瘦欲@ 提交于 2019-12-11 02:07:15
问题 I raised an issue in github at: https://github.com/tensorflow/tensorflow/issues/14924. Here is the details. This is OK: import tensorflow as tf sess = tf.InteractiveSession() xx = tf.constant(1, shape=[32,1,4,4,1], dtype=tf.float32) yy = tf.constant(1, shape=[1,32,1,4,4], dtype=tf.float32) zz = xx * yy sess.run([zz]) However: x2 = tf.constant(1, shape=[10,32,1,4,4,1]) y2 = tf.constant(1, shape=[10,1,32,1,4,4]) z2 = x2 * y2 sess.run(z2) Gives an error: UnimplementedError (see above for

Numpy “:” operator broadcasting issues

China☆狼群 提交于 2019-12-11 00:30:49
问题 In the following code I have written 2 methods that theoretically(in my mind) should do the same thing. Unfortunately they don't, I am unable to find out why they don't do the same thing per the numpy documentation. import numpy as np dW = np.zeros((20, 10)) y = [1 for _ in range(100)] X = np.ones((100, 20)) # =================== # Method 1 (works!) # =================== for i in range(len(y)): dW[:, y[i]] -= X[i] # =================== # Method 2 (does not work) # =================== dW[:, y]

Pandas v0.20 returns NotImplemented when multiplying dataframe columns

扶醉桌前 提交于 2019-12-11 00:08:52
问题 In attempt to answer another question I've been playing around with column-wise multiplication operations in pandas. A = pd.DataFrame({'Col1' : [1, 2, 3], 'Col2' : [2, 3, 4]}) B = pd.DataFrame({'Col1' : [10, 20, 30]}) print(A) Col1 Col2 0 1 2 1 2 3 2 3 4 print(B) Col1 0 10 1 20 2 30 I tried to use df.apply in an attempt to multiply Col1 of B with each column of A. So my desired output is: Col1 Col2 0 10 20 1 40 60 2 90 120 My first attempt was to use a lambda and it worked fine. df_new = A

broadcast an array to different shape (adding “fake” dimensions)

笑着哭i 提交于 2019-12-10 12:56:03
问题 In python (using numpy), I can broadcast an array to a different shape: >>> import numpy as np >>> a = np.array([2,3,4]) >>> b = np.zeros((3,2)) >>> b[:,:] = np.zeros((3,2)) >>> b[:,:] = a[:,np.newaxis] #<-- np.newaxis allows `a` to be "broadcasted" to the same shape as b. >>> b array([[ 2., 2.], [ 3., 3.], [ 4., 4.]]) >>> c = np.zeros((2,3)) >>> c[:,:] = a[np.newaxis,:] >>> c array([[ 2., 3., 4.], [ 2., 3., 4.]]) Is there any way to achieve the same effect in fortran ? I have a subroutine

Broadcast an operation along specific axis in python

我与影子孤独终老i 提交于 2019-12-10 02:54:18
问题 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? 回答1: 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

How to convert 3D RGB label image (in semantic segmentation) to 2D gray image, and class indices start from 0?

谁说我不能喝 提交于 2019-12-09 13:13:47
问题 I have a rgb semantic segmentation label, if there exists 3 classes in it, and each RGB value is one of: [255, 255, 0], [0, 255, 255], [255, 255, 255] respectively, then I want to map all values in RGB file into a new 2D label image according to the dict: {(255, 255, 0): 0, (0, 255, 255): 1, (255, 255, 255): 2} after that, all values in the new gray label file is one of 0, 1 or 2. Is there an efficient way to solve this problem? For example broadcasting in NumPy. 回答1: You can do this: # the

How to broadcast a row to a column in Python NumPy?

一个人想着一个人 提交于 2019-12-08 10:50:30
问题 I have a row vector R and a column vector C. I want to add them to create an array A with height equal to size of R and width equal to size of C as follows: A[i,j] = R[i] + C[j] What's the most efficient way of doing this? 回答1: R + C[:, numpy.newaxis] Does the trick for me. For example import numpy as np r = np.ones(5) c = np.ones(4) * 2 r + c[:, np.newaxis] gives array([[ 3., 3., 3., 3., 3.], [ 3., 3., 3., 3., 3.], [ 3., 3., 3., 3., 3.], [ 3., 3., 3., 3., 3.]]) 来源: https://stackoverflow.com

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-08 06:15:25
问题 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

Multiplying Numpy 3D arrays by 1D arrays

不打扰是莪最后的温柔 提交于 2019-12-07 17:13:57
问题 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?

numpy - vectorize functions: apply_over_axes / apply_along_axis

北战南征 提交于 2019-12-07 16:40:54
问题 I want to calculate the determinant of m m subarrays of a m m*n dimensional arrays, and would like to do this in a fast/more elegant way. The brute-force approach works: import numpy as n array=n.array([[[0.,1.,2.,3.],[2,1,1,0]],[[0.5, 0.5,2,2],[0.5,1,0,2]]]) detarray=n.zeros(4) for i in range(4): detarray[i]= n.linalg.det(array[:,:,i]) I would have tried doing this with apply_along_axis, but I know this is only for 1D arguments to the function, and so I presume I can't get this to work.