vectorization

python: vectorized cumulative counting

半世苍凉 提交于 2019-12-24 09:58:21
问题 I have a numpy array and would like to count the number of occurences for each value, however, in a cumulative way in = [0, 1, 0, 1, 2, 3, 0, 0, 2, 1, 1, 3, 3, 0, ...] out = [0, 0, 1, 1, 0, 0, 2, 3, 1, 2, 3, 1, 2, 4, ...] I'm wondering if it is best to create a (sparse) matrix with ones at col = i and row = in[i] 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0 Then we could

In R, apply a function to the rows of a data frame and return a data frame

假如想象 提交于 2019-12-24 07:04:58
问题 I am trying to apply a self-written function to the rows of a data frame. library(dplyr) # only used for data_frame DF = data_frame(x = c(50, 49, 20), y = c(132, 124, 130), z = c(0.82, 1, 0.63)) x y z <dbl> <dbl> <dbl> 1 50 132 0.82 2 49 124 1.00 3 20 130 0.63 The actual data frame has thousands of rows, this is just a sample. My function is very complicated and does many things, and in the end I get for each row of DF a new row. Let's say for simplicity that the function adds 1 to column 1,

Numpy vectorized 2d array operation error

随声附和 提交于 2019-12-24 05:44:34
问题 I'm trying to apply a vectorized function over a 2-d array in numpy row-wise, and I'm encountering ValueError: setting an array element with a sequence. import numpy as np X = np.array([[0, 1], [2, 2], [3, 0]], dtype=float) coeffs = np.array([1, 1], dtype=float) np.apply_along_axis( np.vectorize(lambda row: 1.0 / (1.0 + np.exp(-coeffs.dot(row)))), 0, X ) I don't totally know how to interpret this error. How am I setting an array element with a sequence? When I test the lambda function on a

How to set an indexed value in a matrix based on another matrix's values

荒凉一梦 提交于 2019-12-24 05:02:48
问题 Say I have a matrix A A = 0 1 2 2 1 1 3 1 2 and another matrix B B = 0 42 1 24 2 32 3 12 I want to replace each value in A by the one associated to it in B . I would obtain A = 42 24 32 32 24 24 12 24 32 How can I do that without loops? 回答1: There are several ways to accomplish this, but here is an short one: [~,ind]=ismember(A,B(:,1)); Anew = reshape(B(ind,2),size(A)) If you can assume that the first column of B is always 0:size(B,1)-1 , then it is easier, becoming just reshape(B(A+1,2),size

How to set an indexed value in a matrix based on another matrix's values

╄→尐↘猪︶ㄣ 提交于 2019-12-24 05:02:30
问题 Say I have a matrix A A = 0 1 2 2 1 1 3 1 2 and another matrix B B = 0 42 1 24 2 32 3 12 I want to replace each value in A by the one associated to it in B . I would obtain A = 42 24 32 32 24 24 12 24 32 How can I do that without loops? 回答1: There are several ways to accomplish this, but here is an short one: [~,ind]=ismember(A,B(:,1)); Anew = reshape(B(ind,2),size(A)) If you can assume that the first column of B is always 0:size(B,1)-1 , then it is easier, becoming just reshape(B(A+1,2),size

Check element wise equality of a 3D matrix Matlab

半城伤御伤魂 提交于 2019-12-24 04:56:13
问题 I have a 3D matrix say for eg. A(10x5x8) . I need to get a 2D matrix (Boolean) out of it of size 10x5 . True if its elemental 3 Dimensional values are all same. i.e. Result(1,1) = 1 if A(1,1,1) == A(1,1,2) == A(1,1,3) etc.. False if at least one is different. I expect a vectored approach which is fast and efficient. Sample input: A(:,:,1) = 1 2 2 2 A(:,:,2) = 1 1 2 3 Expected Output: Result = 1 0 1 0 回答1: Use bsxfun with the eq function and use the first slice as the first input and compare

pandas vectorized operation to get the length of string [duplicate]

眉间皱痕 提交于 2019-12-24 04:08:08
问题 This question already has answers here : Adding a DataFrame column with len() of another column's values (2 answers) Closed 3 years ago . I have a pandas dataframe. df = pd.DataFrame(['Donald Dump','Make America Great Again!','Donald Shrimp'], columns=['text']) What I like to have is another column in Dataframe which has the length of the strings in the 'text' column. For above example, it would be text text_length 0 Donald Dump 11 1 Make America Great Again! 25 2 Donald Shrimp 13 I know I

pandas vectorized operation to get the length of string [duplicate]

ぃ、小莉子 提交于 2019-12-24 04:08:05
问题 This question already has answers here : Adding a DataFrame column with len() of another column's values (2 answers) Closed 3 years ago . I have a pandas dataframe. df = pd.DataFrame(['Donald Dump','Make America Great Again!','Donald Shrimp'], columns=['text']) What I like to have is another column in Dataframe which has the length of the strings in the 'text' column. For above example, it would be text text_length 0 Donald Dump 11 1 Make America Great Again! 25 2 Donald Shrimp 13 I know I

Is it possible to vectorize this nested for with SSE?

徘徊边缘 提交于 2019-12-24 03:01:07
问题 I've never written assembly code for SSE optimization, so sorry if this is a noob question. In this aritcle is explained how to vectorize a for with a conditional statement. However, my code (taken from here ) is of the form: for (int j=-halfHeight; j<=halfHeight; ++j) { for(int i=-halfWidth; i<=halfWidth; ++i) { const float rx = ofsx + j * a12; const float ry = ofsy + j * a22; float wx = rx + i * a11; float wy = ry + i * a21; const int x = (int) floor(wx); const int y = (int) floor(wy); if

MATLAB: Efficient (vectorized) way to apply function on two matrices?

 ̄綄美尐妖づ 提交于 2019-12-24 02:32:54
问题 I have two matrices X and Y , both of order mxn . I want to create a new matrix O of order mxm such that each i,j th entry in this new matrix is computed by applying a function to ith and jth row of X and Y respectively. In my case m = 10000 and n = 500 . I tried using a loop but it takes forever. Is there an efficient way to do it? I am targeting two functions dot product -- dot(row_i, row_j) and exp(-1*norm(row_i-row_j)) . But I was wondering if there is a general way so that I can plugin