numpy-broadcasting

numpy shorthand for taking jagged slice

為{幸葍}努か 提交于 2019-11-29 14:25:33
I have an operation that I'm doing commonly which I'm calling a "jagged-slice" because I don't know the real name for it. It's best explained by example: a = np.random.randn(50, 10) entries_of_interest = np.random.randint(10, size = 50) # Vector of 50 indices between 0 and 9 # Now I want the values contained in each row of a at the corresponding index in "entries of interest" jagged_slice_of_a = a[np.arange(a.shape[0]), entries_of_interest] # jagged_slice_of_a is now a vector with 50 elements. Good. Only problem is it's a bit cumbersome to do this a[np.arange(a.shape[0]), entries_of_interest]

What is going on behind this numpy selection behavior?

强颜欢笑 提交于 2019-11-29 14:06:21
Answering this question , some others and I were actually wrong by considering that the following would work: Say one has test = [ [ [0], 1 ], [ [1], 1 ] ] import numpy as np nptest = np.array(test) What is the reason behind >>> nptest[:,0]==[1] array([False, False], dtype=bool) while one has >>> nptest[0,0]==[1],nptest[1,0]==[1] (False, True) or >>> nptest==[1] array([[False, True], [False, True]], dtype=bool) or >>> nptest==1 array([[False, True], [False, True]], dtype=bool) Is it the degeneracy in term of dimensions which causes this. nptest is a 2D array of object dtype, and the first

How to keep numpy from broadcasting when creating an object array of different shaped arrays

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 12:24:14
I try to store a list of different shaped arrays as a dtype=object array using np.save (I'm aware I could just pickle the list but I'm really curious how to do this). If I do this: import numpy as np np.save('test.npy', [np.zeros((2, 2)), np.zeros((3,3))]) it works. But this: np.save('test.npy', [np.zeros((2, 2)), np.zeros((2,3))]) Gives me an error: ValueError: could not broadcast input array from shape (2,2) into shape (2) I guess np.save converts the list into an array first, so I tried: x=np.array([np.zeros((2, 2)), np.zeros((3,3))]) y=np.array([np.zeros((2, 2)), np.zeros((2,3))]) Which

Ellipsis broadcasting in numpy.einsum

馋奶兔 提交于 2019-11-28 08:39:56
问题 I'm having a problem understanding why the following doesn't work: I have an array prefactor that can be three-dimensional or six-dimensional. I have an array dipoles that has four dimensions. The first three dimensions of dipoles match the last three dimensions of prefactor . As I don't know the shape of prefactor , I'm using an Ellipsis to account for the three optional dimensions in prefactor : numpy.einsum('...lmn,lmno->...o', prefactor, dipoles) (In the example here, prefactor.shape is

numpy shorthand for taking jagged slice

不打扰是莪最后的温柔 提交于 2019-11-28 08:33:39
问题 I have an operation that I'm doing commonly which I'm calling a "jagged-slice" because I don't know the real name for it. It's best explained by example: a = np.random.randn(50, 10) entries_of_interest = np.random.randint(10, size = 50) # Vector of 50 indices between 0 and 9 # Now I want the values contained in each row of a at the corresponding index in "entries of interest" jagged_slice_of_a = a[np.arange(a.shape[0]), entries_of_interest] # jagged_slice_of_a is now a vector with 50 elements

What is going on behind this numpy selection behavior?

纵饮孤独 提交于 2019-11-28 07:35:00
问题 Answering this question, some others and I were actually wrong by considering that the following would work: Say one has test = [ [ [0], 1 ], [ [1], 1 ] ] import numpy as np nptest = np.array(test) What is the reason behind >>> nptest[:,0]==[1] array([False, False], dtype=bool) while one has >>> nptest[0,0]==[1],nptest[1,0]==[1] (False, True) or >>> nptest==[1] array([[False, True], [False, True]], dtype=bool) or >>> nptest==1 array([[False, True], [False, True]], dtype=bool) Is it the

Numpy `ValueError: operands could not be broadcast together with shape …`

家住魔仙堡 提交于 2019-11-28 01:48:15
Im using python 2.7 and am attempting a forcasting on some random data from 1.00000000 to 3.0000000008. There are approx 196 items in my array and I get the error ValueError: operands could not be broadcast together with shape (2) (50) I do not seem to be able to resolve this issue on my own. Any help or links to relevant documentation would be greatly appreciated. Here is the code I am using that generates this error nsample = 50 sig = 0.25 x1 = np.linspace(0,20, nsample) X = np.c_[x1, np.sin(x1), (x1-5)**2, np.ones(nsample)] beta = masterAverageList y_true = ((X, beta)) y = y_true + sig * np

What are the rules for comparing numpy arrays using ==?

若如初见. 提交于 2019-11-28 01:21:23
For example, trying to make sense of these results: >>> x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> (x == np.array([[1],[2]])).astype(np.float32) array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32) >>> (x == np.array([1,2])) False >>> (x == np.array([[1]])).astype(np.float32) array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32) >>> (x == np.array([1])).astype(np.float32) array([ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32) >>> (x == np.array([[1,3],[2]])) False >>> What's going on here? In the case of [1], it's

Subtracting numpy arrays of different shape efficiently

非 Y 不嫁゛ 提交于 2019-11-27 23:08:49
Using the excellent broadcasting rules of numpy you can subtract a shape (3,) array v from a shape (5,3) array X with X - v The result is a shape (5,3) array in which each row i is the difference X[i] - v . Is there a way to subtract a shape (n,3) array w from X so that each row of w is subtracted form the whole array X without explicitly using a loop? You need to extend the dimensions of X with None/np.newaxis to form a 3D array and then do subtraction by w . This would bring in broadcasting into play for this 3D operation and result in an output with a shape of (5,n,3) . The implementation

Numpy broadcasting to the 4th dimension: … vs. : vs None

走远了吗. 提交于 2019-11-27 16:25:09
In a montecarlo simulation I have the following 7 pokercards for 2 players and 3 different montecarlo runs. self.cards: array([[[ 6., 12.], [ 1., 6.], [ 3., 3.], [ 8., 8.], [ 1., 1.], [ 4., 4.], [ 2., 2.]], [[ 6., 7.], [ 1., 1.], [ 3., 3.], [ 2., 2.], [ 12., 12.], [ 5., 5.], [ 10., 10.]], [[ 6., 3.], [ 1., 11.], [ 2., 2.], [ 6., 6.], [ 12., 12.], [ 6., 6.], [ 7., 7.]]]) The corresponding suits are: self.suits array([[[ 2., 1.], [ 1., 2.], [ 2., 2.], [ 2., 2.], [ 1., 1.], [ 2., 2.], [ 2., 2.]], [[ 2., 0.], [ 1., 3.], [ 2., 2.], [ 0., 0.], [ 1., 1.], [ 1., 1.], [ 1., 1.]], [[ 2., 2.], [ 1., 0.],