elementwise-operations

Using the values of a previous “row” in a pandas series

人走茶凉 提交于 2019-12-05 11:01:24
I have a CSV that looks like this (and when brought into a pandas Dataframe with read_csv() , it looks the same). I want to update the values in column ad_requests according to the following logic: For a given row, if ad_requests has a value, leave it alone. Else, give it a value of the previous row's value for ad_requests minus the previous row's value for impressions . So in the first example, we would like to end up with: I get partially there: df["ad_requests"] = [i if not pd.isnull(i) else ??? for i in df["ad_requests"]] And this is where I get stuck. After the else , I want to "go back"

elementwise combination of two lists in R

天大地大妈咪最大 提交于 2019-12-05 00:42:33
Say, I have two lists: list.a <- as.list(c("a", "b", "c")) list.b <- as.list(c("d", "e", "f")) I would like to combine these lists recursively, such that the result would be a list of combined elements as a vector like the following: [[1]] [1] a d [[2]] [1] a e [[3]] [1] a f [[4]] [1] b d and so on. I feel like I'm missing something relatively simple here. Any help? Cheers. expand.grid(list.1, list.b) gives you the desired result in a data.frame structure. This tends to be the most useful format for working with data in R. However, you could get the exact structure you ask for (save the

inconsistent results using isreal

 ̄綄美尐妖づ 提交于 2019-12-03 14:15:49
Take this simple example: a = [1 2i]; x = zeros(1,length(a)); for n=1:length(a) x(n) = isreal(a(n)); end In an attempt to vectorize the code, I tried: y = arrayfun(@isreal,a); But the results are not the same: x = 1 0 y = 0 0 What am I doing wrong? This certainly appears to be a bug, but here's a workaround: >> y = arrayfun(@(x) isreal(x(1)),a) ans = 1 0 Why does this work? I'm not totally sure, but it appears that when you perform an indexing operation on the variable before calling ISREAL it removes the "complex" attribute from the array element if the imaginary component is zero. Try this

Numpy element-wise in operation

对着背影说爱祢 提交于 2019-12-02 05:46:45
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 doing this? Vectorized approach using broadcasting - ((X == y[:,None]).any(1)).astype(int) Sample run -

Parallelization of elementwise matrix multiplication

大兔子大兔子 提交于 2019-12-01 08:24:07
I'm currently optimizing parts of my code and therefore perform some benchmarking. I have NxN -matrices A and T and want to multiply them elementwise and save the result in A again, i.e. A = A*T . As this code is not parallelizable I expanded the assignment into !$OMP PARALLEL DO do j = 1, N do i = 1, N A(i,j) = T(i,j) * A(i,j) end do end do !$OMP END PARALLEL DO (Full minimal working example at http://pastebin.com/RGpwp2KZ .) The strange thing happening now is that regardless of the number of threads (between 1 and 4) the execution time stays more or less the same (+- 10%) but instead the CPU

Parallelization of elementwise matrix multiplication

半腔热情 提交于 2019-12-01 06:51:48
问题 I'm currently optimizing parts of my code and therefore perform some benchmarking. I have NxN -matrices A and T and want to multiply them elementwise and save the result in A again, i.e. A = A*T . As this code is not parallelizable I expanded the assignment into !$OMP PARALLEL DO do j = 1, N do i = 1, N A(i,j) = T(i,j) * A(i,j) end do end do !$OMP END PARALLEL DO (Full minimal working example at http://pastebin.com/RGpwp2KZ.) The strange thing happening now is that regardless of the number of

Element-wise matrix multiplication for multi-dimensional array

允我心安 提交于 2019-11-29 16:05:49
I want to realize component-wise matrix multiplication in MATLAB, which can be done using numpy.einsum in Python as below: import numpy as np M = 2 N = 4 I = 2000 J = 300 A = np.random.randn(M, M, I) B = np.random.randn(M, M, N, J, I) C = np.random.randn(M, J, I) # using einsum D = np.einsum('mki, klnji, lji -> mnji', A, B, C) # naive for-loop E = np.zeros(M, N, J, I) for i in range(I): for j in range(J): for n in range(N): E[:,n,j,i] = B[:,:,i] @ A[:,:,n,j,i] @ C[:,j,i] print(np.sum(np.abs(D-E))) # expected small enough So far I use for-loop of i , j , and n , but I don't want to, at least

Differences between Numpy divide and Python divide?

泪湿孤枕 提交于 2019-11-29 11:17:44
What are the similarities and differences between numpy.divide and the Python slash / operator? As far as I can tell they behave the same, both implementing an element-wise division. The Numpy documentation mentions: numpy.divide(x1, x2) ... Equivalent to x1 / x2 in terms of array-broadcasting. ... Implying that np.divide(x1, x2) is not completely equivalent to x1 / x2. I have run the following snippet to compare their speed: import numpy as np import time a = np.random.rand(10000, 10000) b = np.random.rand(10000, 10000) tic = time.time() c = a / b toc = time.time() print("Python divide took:

Differences between Numpy divide and Python divide?

自作多情 提交于 2019-11-28 04:29:22
问题 What are the similarities and differences between numpy.divide and the Python slash / operator? As far as I can tell they behave the same, both implementing an element-wise division. The Numpy documentation mentions: numpy.divide(x1, x2) ... Equivalent to x1 / x2 in terms of array-broadcasting. ... Implying that np.divide(x1, x2) is not completely equivalent to x1 / x2. I have run the following snippet to compare their speed: import numpy as np import time a = np.random.rand(10000, 10000) b =

How can I sum arrays element-wise in Perl?

十年热恋 提交于 2019-11-27 16:16:55
问题 I have two arrays: @arr1 = ( 1, 0, 0, 0, 1 ); @arr2 = ( 1, 1, 0, 1, 1 ); I want to sum items of both arrays to get new one like ( 2, 1, 0, 1, 2 ); Can I do it without looping through arrays? 回答1: for Perl 5: use List::MoreUtils 'pairwise'; @sum = pairwise { $a + $b } @arr1, @arr2; 回答2: If you're using Perl 6: @a = (1 0 0 0 1) <<+>> (1 1 0 1 1) #NB: the arrays need to be the same size The Perl 6 Advent Calendar has more examples. 回答3: Fundamentally, no, you can't do it without "looping through