numpy-broadcasting

Python: Numpy Multiply each row of a array with each rows of another array

≯℡__Kan透↙ 提交于 2019-12-12 18:29:16
问题 I know there has been some questions about this and it should be possible with broadcasting. But somehow I dont really get how broadcasting works with adding ann additional axis. There is a similar question where each column of one array is multiplied with each column of another array here: Multiply each column with each column. In my case I want to multiply each row of the 2d arrays against each other I simply have a 3 dimensional array created as the triangular matrix: matrix = np.tril(np

Numpy: conditional np.where replace

风格不统一 提交于 2019-12-12 17:04:56
问题 I have the following dataframe: 'customer_id','transaction_dt','product','price','units' 1,2004-01-02 00:00:00,thing1,25,47 1,2004-01-17 00:00:00,thing2,150,8 2,2004-01-29 00:00:00,thing2,150,25 3,2017-07-15 00:00:00,thing3,55,17 3,2016-05-12 00:00:00,thing3,55,47 4,2012-02-23 00:00:00,thing2,150,22 4,2009-10-10 00:00:00,thing1,25,12 4,2014-04-04 00:00:00,thing2,150,2 5,2008-07-09 00:00:00,thing2,150,43 5,2004-01-30 00:00:00,thing1,25,40 5,2004-01-31 00:00:00,thing1,25,22 5,2004-02-01 00:00

Subtract each row of matrix A from every row of matrix B without loops

陌路散爱 提交于 2019-12-12 12:10:30
问题 Given two arrays, A (shape: M X C) and B (shape: N X C), is there a way to subtract each row of A from each row of B without using loops? The final output would be of shape (M N X C). Example A = np.array([[ 1, 2, 3], [100, 200, 300]]) B = np.array([[ 10, 20, 30], [1000, 2000, 3000], [ -10, -20, -2]]) Desired result (can have some other shape) (edited): array([[ -9, -18, -27], [-999, -1998, -2997], [ 11, 22, 5], [ 90, 180, 270], [-900, -1800, -2700], [ 110, 220, 302]]) Shape: 6 X 3 (Loop is

numpy ravel on inconsistent dimensional object

﹥>﹥吖頭↗ 提交于 2019-12-12 05:10:06
问题 I have below code that allows me to iterate over a flattened list and maps the updates back to the structured array - a = np.asarray([1,2,3]) b = np.asarray([4,5,6]) c = np.asarray([a, b]) print c r = c.ravel() print r r[1] = 10 print c setting r[1] = 10 modifies c[0][1] to 10. I want to do something similar with the lower snippet but this fails and I am assuming it's because the dimensions are inconsistent. Is there some way to get similar behavior so I can modify a flattened version without

How to do numpy matmul broadcasting between two numpy tensors?

丶灬走出姿态 提交于 2019-12-11 15:52:21
问题 I have the Pauli matrices which are (2x2) and complex II = np.identity(2, dtype=complex) X = np.array([[0, 1], [1, 0]], dtype=complex) Y = np.array([[0, -1j], [1j, 0]], dtype=complex) Z = np.array([[1, 0], [0, -1]], dtype=complex) and a depolarizing_error function which takes in a normally distributed random number param , generated by np.random.normal(noise_mean, noise_sd) def depolarizing_error(param): XYZ = np.sqrt(param/3)*np.array([X, Y, Z]) return np.array([np.sqrt(1-param)*II, XYZ[0],

Broadcasting a 1D array to a particular dimension of a varying nD array via .reshape(generator)

偶尔善良 提交于 2019-12-11 14:39:43
问题 I have a large matrix of the shape (2,2,2,...n) of nD dimensions, which often varies. However I am also receiving incoming data which is always a 1D array of shape (2,). Now I want to multiply my former matrix of nD dimensions with my 1D array via reshape... and I also have an 'index' of which dimensions I want to broadcast and modify in particular. Thus I'm doing the following (within a loop): matrix_nd *= array_1d.reshape(1 if i!=index else dimension for i, dimension in enumerate(matrix_nd

Multiplying tensors containing images in numpy

巧了我就是萌 提交于 2019-12-11 12:36:34
问题 I have the following 3rd order tensors. Both tensors matrices the first tensor containing 100 10x9 matrices and the second containing 100 3x10 matrices (which I have just filled with ones for this example). My aim is to multiply the matrices as the line up one to one correspondance wise which would result in a tensor with shape: (100, 3, 9) This can be done with a for loop that just zips up both tensors and then takes the dot of each but I am looking to do this just with numpy operators. So

Figuring out broadcasting shape in numpy

守給你的承諾、 提交于 2019-12-11 08:57:25
问题 I understand the basics of numpy (Pandas) broadcasting but got stuck on this simple example: x = np.arange(5) y = np.random.uniform(size = (2,5)) z = x*y print(z.shape) #(2,5) My understanding of the z shape is that you had a (1,5) array multiplied with a (2,5) array, the trailing dimension for 5 is equal so you end up with a 2x5 array. Okay that sounds good. My problem is why is x.shape = (5,) ? Isn't it one-dimensional so it's really 1x5 ? 回答1: NumPy 1D array like x gives you shape such as

Multiple indices for numpy array: IndexError: failed to coerce slice entry of type numpy.ndarray to integer

…衆ロ難τιáo~ 提交于 2019-12-11 06:56:11
问题 Is there a way to do multiple indexing in a numpy array as described below? arr=np.array([55, 2, 3, 4, 5, 6, 7, 8, 9]) arr[np.arange(0,2):np.arange(5,7)] output: IndexError: too many indices for array Desired output: array([55,2,3,4,5],[2,3,4,5,6]) This problem might be similar to calculating a moving average over an array (but I want to do it without any function that is provided). 回答1: Here's an approach using strides - start_index = np.arange(0,2) L = 5 # Interval length n = arr.strides[0]

Replace looping-over-axes with broadcasting

痴心易碎 提交于 2019-12-11 05:35:12
问题 Say I have: a = np.array([[2, 4], [6, 8]]) b = np.array([[1, 3], [1, 5]]) I want to get to: c = np.array([[20,32], [28, 44]]) where c is the result of multiplying each column of a by b , then summing that result along the first axis. I.e.: print(np.sum(a[:, 0] * b, axis=1)) [20 32] print(np.sum(a[:, 1] * b, axis=1)) [28 44] Can I do through broadcasting rather than: using np.apply_along_axis or looping through each column? 回答1: You can use np.dot - b.dot(a).T Alternatively, using np.einsum