elementwise-operations

numpy elementwise outer product

别来无恙 提交于 2019-11-27 16:06:06
I want to do the element-wise outer product of two 2d arrays in numpy. A.shape = (100, 3) # A numpy ndarray B.shape = (100, 5) # A numpy ndarray C = element_wise_outer_product(A, B) # A function that does the trick C.shape = (100, 3, 5) # This should be the result C[i] = np.outer(A[i], B[i]) # This should be the result A naive implementation can the following. tmp = [] for i in range(len(A): outer_product = np.outer(A[i], B[i]) tmp.append(outer_product) C = np.array(tmp) A better solution inspired from stack overflow. big_outer = np.multiply.outer(A, B) tmp = np.swapaxes(tmp, 1, 2) C_tmp =

Element-wise string concatenation in numpy

自闭症网瘾萝莉.ら 提交于 2019-11-27 08:17:24
Is this a bug? import numpy as np a1=np.array(['a','b']) a2=np.array(['E','F']) In [20]: add(a1,a2) Out[20]: NotImplemented I am trying to do element-wise string concatenation. I thought Add() was the way to do it in numpy but obviously it is not working as expected. This can be done using numpy.core.defchararray.add . Here is an example: >>> import numpy as np >>> a1 = np.array(['a', 'b']) >>> a2 = np.array(['E', 'F']) >>> np.core.defchararray.add(a1, a2) array(['aE', 'bF'], dtype='<U2') There are other useful string operations available for NumPy data types. You can use the chararray

How to get element-wise matrix multiplication (Hadamard product) in numpy?

会有一股神秘感。 提交于 2019-11-27 07:43:42
I have two matrices a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]] , equaling [[5,12], [21,32]] I have tried print(np.dot(a,b)) and print(a*b) but both give the result [[19 22], [43 50]] which is the matrix product, not the element-wise product. How can I get the the element-wise product (aka Hadamard product) using built-in functions? Rahul K P For elementwise multiplication of matrix objects, you can use numpy.multiply : import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[5,6],[7,8]]) np.multiply(a,b)

How to get element-wise matrix multiplication (Hadamard product) in numpy?

 ̄綄美尐妖づ 提交于 2019-11-26 22:47:02
问题 I have two matrices a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]] , equaling [[5,12], [21,32]] I have tried print(np.dot(a,b)) and print(a*b) but both give the result [[19 22], [43 50]] which is the matrix product, not the element-wise product. How can I get the the element-wise product (aka Hadamard product) using built-in functions? 回答1: For elementwise multiplication of matrix objects, you can use numpy

Element-wise array replication according to a count [duplicate]

Deadly 提交于 2019-11-26 19:07:02
This question already has an answer here: Repeat copies of array elements: Run-length decoding in MATLAB 5 answers My question is similar to this one , but I would like to replicate each element according to a count specified in a second array of the same size. An example of this, say I had an array v = [3 1 9 4] , I want to use rep = [2 3 1 5] to replicate the first element 2 times, the second three times, and so on to get [3 3 1 1 1 9 4 4 4 4 4] . So far I'm using a simple loop to get the job done. This is what I started with: vv = []; for i=1:numel(v) vv = [vv repmat(v(i),1,rep(i))]; end I

Element-wise string concatenation in numpy

会有一股神秘感。 提交于 2019-11-26 13:57:15
问题 Is this a bug? import numpy as np a1=np.array(['a','b']) a2=np.array(['E','F']) In [20]: add(a1,a2) Out[20]: NotImplemented I am trying to do element-wise string concatenation. I thought Add() was the way to do it in numpy but obviously it is not working as expected. 回答1: This can be done using numpy.core.defchararray.add. Here is an example: >>> import numpy as np >>> a1 = np.array(['a', 'b']) >>> a2 = np.array(['E', 'F']) >>> np.core.defchararray.add(a1, a2) array(['aE', 'bF'], dtype='<U2')

How to perform element-wise multiplication of two lists?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 11:11:38
I want to perform an element wise multiplication, to multiply two lists together by value in Python, like we can do it in Matlab. This is how I would do it in Matlab. a = [1,2,3,4] b = [2,3,4,5] a .* b = [2, 6, 12, 20] A list comprehension would give 16 list entries, for every combination x * y of x from a and y from b . Unsure of how to map this. If anyone is interested why, I have a dataset, and want to multiply it by Numpy.linspace(1.0, 0.5, num=len(dataset)) =) . Use a list comprehension mixed with zip() :. [a*b for a,b in zip(lista,listb)] Since you're already using numpy , it makes sense

How to multiply all integers inside list [duplicate]

馋奶兔 提交于 2019-11-26 09:46:37
问题 This question already has answers here : How to multiply individual elements of a list with a number? (4 answers) How do I multiply each element in a list by a number? (7 answers) Closed last year . Hello so I want to multiply the integers inside a list. For example; l = [1, 2, 3] l = [1*2, 2*2, 3*2] output: l = [2, 4, 6] So I was searching online and most of the answers were regarding multiply all the integers with each other such as: [1*2*3] 回答1: Try a list comprehension: l = [x * 2 for x

Element-wise addition of 2 lists?

孤者浪人 提交于 2019-11-26 03:15:12
问题 I have now: list1 = [1, 2, 3] list2 = [4, 5, 6] I wish to have: [1, 2, 3] + + + [4, 5, 6] || || || [5, 7, 9] Simply an element-wise addition of two lists. I can surely iterate the two lists, but I don\'t want do that. What is the most Pythonic way of doing so? 回答1: Use map with operator.add: >>> from operator import add >>> list( map(add, list1, list2) ) [5, 7, 9] or zip with a list comprehension: >>> [sum(x) for x in zip(list1, list2)] [5, 7, 9] Timing comparisons: >>> list2 = [4, 5, 6]*10*

How to perform element-wise multiplication of two lists?

不问归期 提交于 2019-11-26 02:06:51
问题 I want to perform an element wise multiplication, to multiply two lists together by value in Python, like we can do it in Matlab. This is how I would do it in Matlab. a = [1,2,3,4] b = [2,3,4,5] a .* b = [2, 6, 12, 20] A list comprehension would give 16 list entries, for every combination x * y of x from a and y from b . Unsure of how to map this. If anyone is interested why, I have a dataset, and want to multiply it by Numpy.linspace(1.0, 0.5, num=len(dataset)) =) . 回答1: Use a list