array-broadcasting

Applying function to 2 vectors to obtain a 2D matrix

Applying function to 2 vectors to obtain a 2D matrix Question: My question is exactly the same as this one but in Python instead of R. I couldn’t find a similar question for python and so I’m asking this one. I have 2 vectors (lists actually) a = [1,2,3] b = [5,6,7] A defined function …

Total answers: 1

Adding block-constant matrices in numpy

Adding block-constant matrices in numpy Question: Let’s say we have a n*n matrix A m*m matrix B a vector b=[b_1,…,b_m] of block sizes with b_1 + … + b_m = n and b_i >= 1. Then I define a n*n block matrix B_block whose (i,j)-th block is a b_i*b_j matrix of constant value B_{ij}. How …

Total answers: 1

numpy: limiting min value of a scalar, 1D or nD array

numpy: limiting min value of a scalar, 1D or nD array Question: Given a scalar, a 1D or an N-D numpy (numeric) array, I’d like to replace all values less than threshold with threshold. So, for example: def fn(a, threshold): return ??? fn(2, 2.5) => 2.5 # scalar fn([1, 2, 3, 4], 2.5) => [2.5, …

Total answers: 1

Understanding fancy einsum equation

Understanding fancy einsum equation Question: I was reading about attention and came across this equation: import einops from fancy_einsum import einsum import torch x = torch.rand((200, 10, 768)) y = torch.rand((20, 768, 64)) res = einsum("batch query_pos d_model, n_heads d_model d_head -> batch query_pos n_heads d_head", x, y) And I am not able to understand …

Total answers: 1

using integer as index for multidimensional numpy array

using integer as index for multidimensional numpy array Question: I have boolean array of shape (n_samples, n_items) which represents a set: my_set[i, j] tells if sample i contains item j. To populate it, the array is initialized as zeros, and receive another array of integers, with shape (n_samples, 3), telling for each example, three elements …

Total answers: 1

How can I update a 2d numpy array based on condition, but still using it absolute value?

How can I update a 2d numpy array based on condition, but still using it absolute value? Question: having the following function: def one_more_fix(matrix): height, width = matrix.shape for i in range(height): for j in range(width): if abs(matrix[i,j]) >= 180: matrix[i,j] = abs(matrix[i,j]) – 360 Example input and output: simple_matrix = np.array([ [179, 181, -182], …

Total answers: 1

Apply numpy broadcast_to on each vector in an array

Apply numpy broadcast_to on each vector in an array Question: I want to apply something like this: a = np.array([1,2,3]) np.broadcast_to(a, (3,3)) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) On each vector in a multi-vector array: a = np.array([[1,2,3], [4,5,6]]) np.broadcast_to(a, (2,3,3)) ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: …

Total answers: 1

Numpy dot product between a 3d matrix and 2d matrix

Numpy dot product between a 3d matrix and 2d matrix Question: I have a 3d array that has shape (2, 10, 3) and a 2d array that has shape (2, 3) like this: print(t) #2d array Output: [[1.003 2.32 3.11 ] [1.214 5.32 2.13241]] print(normal) #3d array Output: [[[0.69908573 0.0826756 0.84485978] [0.51058213 0.4052637 0.5068118 ] …

Total answers: 1