Questions tagged [array-broadcasting]

Broadcasting (or singleton expansion) applies a function element-wise across one or more multidimensional arrays, matching shapes of the arguments by repeating missing or singleton dimensions. Be sure to also tag the programming language; many languages with strong array support have implicit or explicit broadcasting behaviors, sometimes with idiosyncratic rules.

Many languages and frameworks have implementations of broadcasting (also known as singleton expansion), including but not limited to:

Some lower-level languages, like (with getelementptr) and (with synchronizing warps) support broadcasting between scalars and vectors, but without support for higher dimensional arrays.

879 questions
3
votes
1 answer

Efficient way of horizontal concatenation without tiling

I have two (large) arrays. For illustration purposes I'm using a simple example below: In [14]: arr1 = np.arange(32*512).reshape(32, 512) In [15]: arr2 = np.arange(512).reshape(1, 512) And I wanted to do a horizontal concatenation of these arrays…
kmario23
  • 57,311
  • 13
  • 161
  • 150
3
votes
1 answer

what happens under the hood of broadcasting a numpy array

I explored the standard python documentation on broadcasting https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html Everytime I explore this topic, I couldn't find enough internal details of how broadcasting works in reality, does it…
TheCodeCache
  • 820
  • 1
  • 7
  • 27
3
votes
0 answers

Need some kind of numpy broadcasting inverse summary operation

My problem is as follows: import numpy as np # given are two arrays of random, different shapes and sizes a = np.array(...).reshape(?) b = np.array(...).reshape(?) # if broadcasting a and b works c = a * b # I want to guarantee that the assignment…
3
votes
1 answer

Numpy dot product of a 4D array with its transpose fails

For a 4D array A with dimensions of (60,64,2,2), need to calculate the dot product with its transpose A_t. A_t is of dimension(2,2,64,60). Below is what I do. A_t = np.transpose(A) A_At = A_t.dot(A) The dot product throws an error ValueError:…
vermachint
  • 93
  • 1
  • 9
3
votes
1 answer

Numpy: Finding minimum and maximum values from associations through binning

Prerequisite This is a question derived from this post. So, some of the introduction of the problem will be similar to that post. Problem Let's say result is a 2D array and values is a 1D array. values holds some values associated with each element…
mrtpk
  • 1,398
  • 4
  • 18
  • 38
3
votes
2 answers

numpy indexing using 'None' for pairwise operations

If I had two numpy arrays that looked like this a = np.array([1, 2]) b = np.array([3, 4]) and I wanted to add all pairwise combinations, I could easily do c = a + b[:, None] c array([[4, 5], [5, 6]]) to get the result of 1+3, 2+3 and 1+4,…
Alex Gao
  • 97
  • 10
3
votes
2 answers

cosine similarity between a vector and pandas column(a linear vector)

I have a pandas data frame containing list of wines with their respective wine attributes. Then I made a new column vector that contains numpy vectors from these attributes. def get_wine_profile(id): wine = wines[wines['exclusiviId'] ==…
3
votes
2 answers

tensorflow concat with transpose using the broadcast semantic

Say v1 and v2 has the same shape. Is it possible in tensorflow to concat v1 and the transposed version of v2 using the broadcast semantic? For example, v1 = tf.constant([[1,1,1,1],[3,3,3,3],[5,5,5,5]]) v2 = tf.constant([[2,2,2,2],[4,4,4,4]]) I…
walkerlala
  • 1,599
  • 1
  • 19
  • 32
3
votes
1 answer

Compute trace of matrix product using numpy/pytorch broadcasting

Let A be an (nxm)-matrix and M an (mxm)-matrix. Writing tr() for the trace of a matrix, I need to compute tr(AM(A^T)). However, the final trace operation throws away most of the computation. Can I use numpy's or pytorch's broadcasting rules to…
ASML
  • 199
  • 12
3
votes
1 answer

Fast and efficient slice of array avoiding delete operation

I am trying to get a slice (for example elements 1-3 and 5-N) of an array A(N,3) avoiding using numpy.delete. And example of the process will be the following: [[1,2,3],[4,5,6],[7,8,9],[3,2,1]] ==> [[1,2,3],[3,2,1]] I was hoping to use something…
3
votes
2 answers

NumPy broadcasting to improve dot-product performance

This is a rather simple operation, but it is repeated millions of times in my actual code and, if possible, I'd like to improve its performance. import numpy as np # Initial data array xx = np.random.uniform(0., 1., (3, 14, 1)) # Coefficients used…
Gabriel
  • 40,504
  • 73
  • 230
  • 404
3
votes
3 answers

Numpy: conditional np.where replace

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…
Pylander
  • 1,531
  • 1
  • 17
  • 36
3
votes
4 answers

How to combine multi-index column values on condition using numpy broadcasting

I have a problem that I am 99% sure has a numpy broadcasting solution, but I'm unable to figure it out. Suppose I have the following dataframe: iterables = [['US', 'DE'], ['A', 'B'], [1, 2, 3, 4, 5]] idx3 = pd.MultiIndex.from_product(iterables,…
Simon
  • 333
  • 2
  • 8
3
votes
1 answer

numpy way of doing outer product of list

I'm trying to do a normalized sum of outer product of a 60000x100 matrix. I would like to do it using numpy way, since my solution is constrained by the python for loop in the list comprehension: def covariance_over_time(X): B =…
asdf
  • 685
  • 7
  • 23
3
votes
1 answer

How to compare two numpy arrays of strings with the "in" operator to get a boolean array using array broadcasting?

Python allows for a simple check if a string is contained in another string: 'ab' in 'abcd' which evaluates to True. Now take a numpy array of strings and you can do this: import numpy as np A0 = np.array(['z', 'u', 'w'],dtype=object) A0[:,None]…
Khris
  • 3,132
  • 3
  • 34
  • 54