numpy-broadcasting

Broadcasted NumPy arithmetic - why is one method so much more performant?

↘锁芯ラ 提交于 2019-12-04 16:29:55
问题 This question is a follow up to my answer in Efficient way to compute the Vandermonde matrix. Here's the setup: x = np.arange(5000) # an integer array N = 4 Now, I'll compute the Vandermonde matrix in two different ways: m1 = (x ** np.arange(N)[:, None]).T And, m2 = x[:, None] ** np.arange(N) Sanity check: np.array_equal(m1, m2) True These methods are identical, but their performance is not: %timeit m1 = (x ** np.arange(N)[:, None]).T 42.7 µs ± 271 ns per loop (mean ± std. dev. of 7 runs,

When broadcasting is a bad idea ? (numpy)

家住魔仙堡 提交于 2019-12-04 06:45:26
The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Example 1: from numpy import array a = array([1.0,2.0,3.0]) b = array([2.0,2.0,2.0]) # multiply element-by-element () a * b >> array([ 2., 4., 6.]) Example 2 : from numpy import array a = array([1.0,2.0,3.0]) b = 2.0 # broadcast b to all a a * b >>array([ 2., 4., 6.]) We can think of the scalar b being stretched during the arithmetic operation into an array with the same shape as a. Numpy is smart enough to use the original scalar value without actually making copies so that broadcasting

Broadcasted NumPy arithmetic - why is one method so much more performant?

泪湿孤枕 提交于 2019-12-03 10:26:46
This question is a follow up to my answer in Efficient way to compute the Vandermonde matrix . Here's the setup: x = np.arange(5000) # an integer array N = 4 Now, I'll compute the Vandermonde matrix in two different ways: m1 = (x ** np.arange(N)[:, None]).T And, m2 = x[:, None] ** np.arange(N) Sanity check: np.array_equal(m1, m2) True These methods are identical, but their performance is not: %timeit m1 = (x ** np.arange(N)[:, None]).T 42.7 µs ± 271 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) %timeit m2 = x[:, None] ** np.arange(N) 150 µs ± 995 ns per loop (mean ± std. dev. of 7

TensorFlow broadcasting

感情迁移 提交于 2019-12-02 03:55:16
Broadcasting is the process of making arrays with different shapes have compatible shapes for arithmetic operations. In numpy, we can broadcast arrays. Does TensorFlow graph support broadcasting similar to the numpy one? yes it is supported. Open a terminal and try this: import tensorflow as tf #define tensors a=tf.constant([[10,20],[30,40]]) #Dimension 2X2 b=tf.constant([5]) c=tf.constant([2,2]) d=tf.constant([[3],[3]]) sess=tf.Session() #start a session #Run tensors to generate arrays mat,scalar,one_d,two_d = sess.run([a,b,c,d]) #broadcast multiplication with scalar sess.run(tf.multiply(mat

How to slice a numpy.ndarray made up of numpy.void numbers?

寵の児 提交于 2019-12-02 02:07:59
问题 So here's the deal: I have variable x which is a numpy.ndarray . The size of this structure is 1000. If I do x[0] , then I get a numpy.void , of 4 numbers. If I do x[1] , then I get another numpy.void , also of 4 numbers, etc. What I simply want to do: I want to slice this data structure, so that I extract a numpy matrix, of size 1000x3. How do I do that? Thanks 回答1: Sounds like you have a structured array, something like this simple example: In [158]: x = np.ones((5,), dtype='i,i,f,f') In

NumPy ndarray broadcasting - shape (X,) vs (X, 1) to operate with (X,Y)

萝らか妹 提交于 2019-12-01 22:02:06
问题 I have a NumPy ndarray which is shaped (32, 1024) and holds 32 signal measurements which I would like to combine into a single 1024 element long array, with a different weight for each of the 32. I was using numpy.average but my weights are complex and average performs a normalisation of the weights based on the sum which throws off my results. Looking at the code for average I realised that I can accomplish the same thing by multiplying the weights by the signal array and then summing over

Subtract a column vector from matrix at specified vector of columns using only broadcast

时光总嘲笑我的痴心妄想 提交于 2019-12-01 20:20:48
问题 I want to subtract a column vector from a numpy matrix using another vector which is index of columns where the first column vector needs to be subtracted from the main matrix. For eg. M = array([[ 1, 2, 1, 1], [ 2, 1, 1, 1], [ 1, 1, 2, 1], [ 2, 1, 1, 1], [ 1, 1, 1, 2]]) # An example matrix V = array([1, 1, 1, 1, 1]) # An example column vector I = array([0, 3, 2, 3, 1, 3, 3]) # The index maxtrix Now I want to subtract V from M at column numbers given in I. For eg. I[0] is 0, so subtract V

Subtract a column vector from matrix at specified vector of columns using only broadcast

点点圈 提交于 2019-12-01 19:02:05
I want to subtract a column vector from a numpy matrix using another vector which is index of columns where the first column vector needs to be subtracted from the main matrix. For eg. M = array([[ 1, 2, 1, 1], [ 2, 1, 1, 1], [ 1, 1, 2, 1], [ 2, 1, 1, 1], [ 1, 1, 1, 2]]) # An example matrix V = array([1, 1, 1, 1, 1]) # An example column vector I = array([0, 3, 2, 3, 1, 3, 3]) # The index maxtrix Now I want to subtract V from M at column numbers given in I. For eg. I[0] is 0, so subtract V from first column (zero index) of matrix M. Similarly I[1] = 3, subtract V from fourth column (three index

Element-wise broadcasting for comparing two NumPy arrays?

旧城冷巷雨未停 提交于 2019-12-01 17:43:09
问题 Let's say I have an array like this: import numpy as np base_array = np.array([-13, -9, -11, -3, -3, -4, 2, 2, 2, 5, 7, 7, 8, 7, 12, 11]) Suppose I want to know: "how many elements in base_array are greater than 4?" This can be done simply by exploiting broadcasting: np.sum(4 < base_array) For which the answer is 7 . Now, suppose instead of comparing to a single value, I want to do this over an array. In other words, for each value c in the comparison_array , find out how many elements of

Element-wise broadcasting for comparing two NumPy arrays?

余生长醉 提交于 2019-12-01 17:41:33
Let's say I have an array like this: import numpy as np base_array = np.array([-13, -9, -11, -3, -3, -4, 2, 2, 2, 5, 7, 7, 8, 7, 12, 11]) Suppose I want to know: "how many elements in base_array are greater than 4?" This can be done simply by exploiting broadcasting: np.sum(4 < base_array) For which the answer is 7 . Now, suppose instead of comparing to a single value, I want to do this over an array. In other words, for each value c in the comparison_array , find out how many elements of base_array are greater than c . If I do this the naive way, it obviously fails because it doesn't know how