elementwise-operations

How to perform element-wise arithmetic operations (e.g. add, subtract, multiply) of two equally shaped lists with arbitrary nestings

别等时光非礼了梦想. 提交于 2021-02-08 08:35:17
问题 I want to perform element-wise mathematical operations (e.g. sum, multiply..) on two Python lists containing numbers or multiple nested lists which may contain again numbers or lists and so on. The shapes of the two lists are equal when performing the operation. Furthermore, the result should be of the same shape as the two input lists. A shape may differ in: length , width (i.e. number of nestings), order (e.g. the lists start with a number followed by a nested list, but it may also be that

Element wise comparison between 1D and 2D array

▼魔方 西西 提交于 2020-12-09 09:30:27
问题 Want to perform an element wise comparison between an 1D and 2D array. Each element of the 1D array need to be compared (e.g. greater) against the corresponding row of 2D and a mask will be created. Here is an example: A = np.random.choice(np.arange(0, 10), (4,100)).astype(np.float) B = np.array([5., 4., 8., 2. ]) I want to do A<B so that first row of A will be compared against B[0] which is 5. and the result will be an boolean array. If I try this I get: operands could not be broadcast

Are there builtin functions for elementwise boolean operators over boolean lists?

馋奶兔 提交于 2020-07-02 11:37:11
问题 For example, if you have n lists of bools of the same length, then elementwise boolean AND should return another list of that length that has True in those positions where all the input lists have True, and False everywhere else. It's pretty easy to write, i just would prefer to use a builtin if one exists (for the sake of standardization/readability). Here's an implementation of elementwise AND: def eAnd(*args): return [all(tuple) for tuple in zip(*args)] example usage: >>> eAnd([True, False

Why is zipped faster than zip in Scala?

 ̄綄美尐妖づ 提交于 2020-01-22 04:40:05
问题 I have written some Scala code to perform an element-wise operation on a collection. Here I defined two methods that perform the same task. One method uses zip and the other uses zipped . def ES (arr :Array[Double], arr1 :Array[Double]) :Array[Double] = arr.zip(arr1).map(x => x._1 + x._2) def ES1(arr :Array[Double], arr1 :Array[Double]) :Array[Double] = (arr,arr1).zipped.map((x,y) => x + y) To compare these two methods in terms of speed, I wrote the following code: def fun (arr : Array[Double

Fastest way to take elementwise sum of two Lists

淺唱寂寞╮ 提交于 2020-01-12 07:41:06
问题 I can do elementwise operation like sum using Zipped function. Let I have two Lists L1 and L2 as shown below val L1 = List(1,2,3,4) val L2 = List(5,6,7,8) I can take element wise sum in following way (L1,L2).zipped.map(_+_) and result is List(6, 8, 10, 12) as expected. I am using Zipped function in my actual code but it takes too much time. In reality My List Size is more than 1000 and I have more than 1000 Lists and my algorithm is iterative where iterations could be up to one billion . In

How does one perform the exp() operation element-wise in Juila?

China☆狼群 提交于 2020-01-11 09:41:49
问题 I'm new to Julia and this seems like a straight-forward operation but for some reason I am not finding the answer anywhere. I have been going through some tutorials online and they simply use exp(A) where A is a nxm matrix but this gives me a DimensionMismatch error. I looked through the documentation on the official website in the elementary functions as well as the linear algebra section and googled it multiple times but can't find it for the life of me. 回答1: In julia, operations on

numpy elementwise outer product with sparse matrices

孤街醉人 提交于 2020-01-03 20:22:08
问题 I want to do the element-wise outer product of three (or four) large 2D arrays in python (values are float32 rounded to 2 decimals). They all have the same number of rows "n", but different number of columns "i", "j", "k". The resulting array should be of shape (n, i*j*k). Then, I want to sum each column of the result to end up with a 1D array of shape (i*j*k). np.shape(a) = (75466, 10) np.shape(b) = (75466, 28) np.shape(c) = (75466, 66) np.shape(intermediate_result) = (75466, 18480) np.shape

Python: get the element-wise mean of multiple arrays in a dataframe

房东的猫 提交于 2019-12-24 07:19:11
问题 I have a 16x10 panda dataframe with 1x35000 arrays (or NaN) in each cell. I want to take the element-wise mean over rows for each column. 1 2 3 ... 10 1 1x35000 1x35000 1x35000 1x35000 2 1x35000 NaN 1x35000 1x35000 3 1x35000 NaN 1x35000 NaN ... 16 1x35000 1x35000 NaN 1x35000 To avoid misunderstandings: take the first element of each array in the first column and take the mean. Then take the second element of each array in the first column and take the mean again. In the end I want to have a

Numpy element-wise in operation

*爱你&永不变心* 提交于 2019-12-20 04:07:44
问题 Suppose I have a column vector y with length n, and I have a matrix X of size n*m. I want to check for each element i in y, whether the element is in the corresponding row in X. What is the most efficient way of doing this? For example: y = [1,2,3,4].T and X =[[1, 2, 3],[3, 4, 5],[4, 3, 2],[2, 2, 2]] Then the output should be [1, 0, 1, 0] or [True, False, True, False] which ever is easier. Of course we can use a for loop to iterate through both y and X, but is there any more efficient way of

Creating an array in Swift by applying binary operation to all elements of two other arrays

谁说胖子不能爱 提交于 2019-12-19 11:19:18
问题 Is there a concise way in Swift of creating an array by applying a binary operation on the elements of two other arrays? For example: let a = [1, 2, 3] let b = [4, 5, 6] let c = (0..<3).map{a[$0]+b[$0]} // c = [5, 7, 9] 回答1: If you use zip to combine the elements, you can refer to + with just + : let a = [1, 2, 3] let b = [4, 5, 6] let c = zip(a, b).map(+) // [5, 7, 9] 回答2: Update: You can use indices like this: for index in a.indices{ sum.append(a[index] + b[index]) } print(sum)// [5, 7, 9]