vectorization

Efficient element-wise function computation in Python

我的梦境 提交于 2019-12-19 11:33:06
问题 I have the following optimization problem. Given two np.arrays X , Y and a function K I would like to compute as fast as possible the matrix incidence gram_matrix where the (i,j)-th element is computed as K(X[i],Y[j]) . Here there an implementation using nested for-loops, which are acknowledged to be the slowest to solve these kind of problems. def proxy_kernel(X,Y,K): gram_matrix = np.zeros((X.shape[0], Y.shape[0])) for i, x in enumerate(X): for j, y in enumerate(Y): gram_matrix[i, j] = K(x,

How to vectorize finding the closest point out of a vector

南笙酒味 提交于 2019-12-19 10:22:55
问题 BigList = rand(20, 3) LittleList = rand(5, 3) I'd like to find for each row in the big list the 'closest' row in the little list, as defined by the euclidean norm (i.e. sum of squared distances between the corresponding values in the k=3 dimension). I can see how to do this using two loops, but it seems like there ought to be a better way to do this using built in matrix operations. 回答1: Approach #1 There is a built in MATLAB function pdist2 which finds "Pairwise distance between two sets of

Vectorize numpy array expansion

只谈情不闲聊 提交于 2019-12-19 10:18:42
问题 I'm trying to find a way to vectorize an operation where I take 1 numpy array and expand each element into 4 new points. I'm currently doing it with Python loop. First let me explain the algorithm. input_array = numpy.array([1, 2, 3, 4]) I want to 'expand' or 'extend' each element in this array to 4 points. So, element zero (value 1) would be expanded to these 4 points: [0, 1, 1, 0] This would happen for each element to end up with a final array of: [0, 1, 1, 0, 0, 2, 2, 0, 0, 3, 3, 0, 0, 4,

subsetting list in R

匆匆过客 提交于 2019-12-19 09:28:13
问题 I'm using Mcomp package in R which contains dataset for forecasting. The data is organized as yearly, quarterly and monthly frequencies. I can easily subset this into a list but cannot further subset using additional condition. ##Subset monthly data library("Mcomp") mon <- subset(M3,"monthly") Each element in the mon list has following structure, as an example mon$N1500 has the following struture $ N1500:List of 9 ..$ st : chr "M99" ..$ type : chr "MICRO" ..$ period : chr "MONTHLY" ..$

Generalize stacking of array elements' neighbors into 3-D array

两盒软妹~` 提交于 2019-12-19 09:07:13
问题 Setup Given a 2D array, I would like to create a 3D array where the values along the third dimension at (i.e. stacked[row, col, :] ) are the flattened neighbors of the original array at [row, col] . I would like to generalize this process to handle an arbitrary (but reasonable) search radius. Prior research This question seemed promising, but I'm not sure I can really utilize its approach without a (couple of) for loops. My current approach, applied with a search radius of 1, for brevity's

R lag/lead irregular time series data

懵懂的女人 提交于 2019-12-19 04:47:13
问题 I have irregular time series data frame with time (seconds) and value columns. I want to add another column, value_2 where values are lead by delay seconds. So value_2 at time t equals to value at time t + delay or right after that. ts=data.frame( time=c(1,2,3,5,8,10,11,15,20,23), value=c(1,2,3,4,5,6,7,8,9,10) ) ts_with_delayed_value <- add_delayed_value(ts, "value", 2, "time") > ts_with_delayed_value time value value_2 1 1 1 3 2 2 2 4 3 3 3 4 4 5 4 5 5 8 5 6 6 10 6 8 7 11 7 8 8 15 8 9 9 20 9

R lag/lead irregular time series data

女生的网名这么多〃 提交于 2019-12-19 04:47:11
问题 I have irregular time series data frame with time (seconds) and value columns. I want to add another column, value_2 where values are lead by delay seconds. So value_2 at time t equals to value at time t + delay or right after that. ts=data.frame( time=c(1,2,3,5,8,10,11,15,20,23), value=c(1,2,3,4,5,6,7,8,9,10) ) ts_with_delayed_value <- add_delayed_value(ts, "value", 2, "time") > ts_with_delayed_value time value value_2 1 1 1 3 2 2 2 4 3 3 3 4 4 5 4 5 5 8 5 6 6 10 6 8 7 11 7 8 8 15 8 9 9 20 9

Efficient way to perform running total in the last 365 day window

坚强是说给别人听的谎言 提交于 2019-12-19 03:44:21
问题 This is what my data frame looks like: library(data.table) df <- fread(' Name EventType Date SalesAmount RunningTotal Runningtotal(prior365Days) John Email 1/1/2014 0 0 0 John Sale 2/1/2014 10 10 10 John Sale 7/1/2014 20 30 30 John Sale 4/1/2015 30 60 50 John Webinar 5/1/2015 0 60 50 Tom Email 1/1/2014 0 0 0 Tom Sale 2/1/2014 15 15 15 Tom Sale 7/1/2014 10 25 25 Tom Sale 4/1/2015 25 50 35 Tom Webinar 5/1/2015 0 50 35 ') df[,Date:= as.Date(Date, format="%m/%d/%Y")] The last column was my

Efficient search for permutations that contain sub-permutations via array operations?

时光怂恿深爱的人放手 提交于 2019-12-19 03:33:38
问题 I have a set of integers, say S = {1,...,10}, and two matrices N and M, whose rows are some (but not necessarily all possible) permutations of elements from S of orders, say, 3 and 5 respectively, e.g. N = [1 2 3; 2 5 3;...], M = [1 2 3 4 5; 2 4 7 8 1;...]. A sub-permutation Q of a permutation P is just an indexed subset of P such that the order of the indices of the elements of Q is the same as the order of their indices in P. Example: [2,4,7] is a sub-permutation of [2,3,4,6,7,1], but [1,2

Efficient search for permutations that contain sub-permutations via array operations?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 03:33:25
问题 I have a set of integers, say S = {1,...,10}, and two matrices N and M, whose rows are some (but not necessarily all possible) permutations of elements from S of orders, say, 3 and 5 respectively, e.g. N = [1 2 3; 2 5 3;...], M = [1 2 3 4 5; 2 4 7 8 1;...]. A sub-permutation Q of a permutation P is just an indexed subset of P such that the order of the indices of the elements of Q is the same as the order of their indices in P. Example: [2,4,7] is a sub-permutation of [2,3,4,6,7,1], but [1,2