vectorization

Getting rows of a matrix which coincide with a series of vectors, without using apply

让人想犯罪 __ 提交于 2019-12-10 21:07:41
问题 My question is sort of related to my earlier question. Suppose I have one matrix and 4 vectors (can consider this another matrix, since the order of the vectors matters), and I want to get the row numbers which coincide to each vector, in order. I would like the solution to avoid repeating vectors and be as efficient as possible, since the problem is large scale. Example. set.seed(1) M = matrix(rpois(50,5),5,10) v1 = c(3, 2, 7, 7, 4, 4, 7, 4, 5, 6) v2= c(8, 6, 4, 4, 3, 8, 3, 6, 5, 6) v3= c(4,

Matlab: Sum corresponding values if index is within a range

ε祈祈猫儿з 提交于 2019-12-10 20:23:58
问题 I have been going crazy trying to figure a way to speed this up. Right now my current code talks ~200 sec looping over 77000 events. I was hoping someone might be able to help me speed this up because I have to do about 500 of these. Problem: I have arrays (both 200000x1) that correspond to Energy and Position of a hit over 77000 events. I have the range of each event separated into two arrays, event_start and event_end. First thing I do is look for the position in a specific range, then I

Quickly compute eigenvectors for each element of an array in python

最后都变了- 提交于 2019-12-10 18:55:40
问题 I want to compute eigenvectors for an array of data (in my actual case, i cloud of polygons) To do so i wrote this function: import numpy as np def eigen(data): eigenvectors = [] eigenvalues = [] for d in data: # compute covariance for each triangle cov = np.cov(d, ddof=0, rowvar=False) # compute eigen vectors vals, vecs = np.linalg.eig(cov) eigenvalues.append(vals) eigenvectors.append(vecs) return np.array(eigenvalues), np.array(eigenvectors) Running this on some test data: import cProfile

Reshaping/Combining several sub-matrices to one matrix in multi-dimensional space

醉酒当歌 提交于 2019-12-10 18:48:46
问题 I have a 5D binary array 'a' of size (2, 2, 4, 2, 2). The structure looks like this, for example: a[0,0]: [[[ 0. 1.] [ 0. 0.]] [[ 0. 0.] [ 0. 1.]] [[ 0. 0.] [ 0. 1.]] [[ 0. 0.] [ 1. 0.]]] What I want to do is to make a (2,2,4,4) matrix that combines the 2x2 matrices in the last two axis, but in a squared structure. The result should look like this: result[0,0]: [[0. 1. 0. 0.] [0. 0. 0. 1.] [0. 0. 0. 0.] [0. 1. 1. 0.]] I hope this is clear enough. If I put the brackets of original matrices in

How to use Matlab's bsxfun to solve cumulative sum

此生再无相见时 提交于 2019-12-10 18:46:58
问题 I have the following (slow) piece of code: % A is n-by-m matrix % B is n-by-m-by-d matrix % C is n-by-m-by-d matrix % R is 1-by-d vector A=zeros(n,m); for i=1:d A = A + sum(B(:,:,1:i),3).*(R(i)-C(:,:,i)); end I would like to make it more efficient by using the magical bsxfun to lose the loop. Can you show me how to do that? 回答1: This way - A = sum(cumsum(B,3).*bsxfun(@minus,permute(R,[1 3 2]),C),3) With size parameters n,m,d as 200 each, the runtimes were -----------------------------------

Optimize this function with numpy (or other vectorization methods)

独自空忆成欢 提交于 2019-12-10 18:37:46
问题 I am computing with Python a classic calculation in the field of population genetics. I am well aware that there exists many algorithm that do the job but I wanted to build my own for some reason. The below paragraph is a picture because MathJax is not supported on StackOverflow I would like to have an efficient algorithm to calculate those Fst . For the moment I only manage to make for loops and no calculations are vectorized How can I make this calculation using numpy (or other

Vectorize loop with nested if-clause

╄→гoц情女王★ 提交于 2019-12-10 18:29:21
问题 Problem I am trying to optimize the runtime of my code and have asked a similar question before that included several nested if-statements. Vectorizing nested if-statements As the code I posted there hoping for some ideas I might implement was a bit long and I am still struggeling with the implementation of vectorization for nested loops I would like to ask again with some easier code: Code NB_list_all=zeros(length(BML),4); for NB=1:length(BML); NB_list=zeros(4,1); %in +x direction if isempty

Numpy version of rolling MAD (mean absolute deviation)

扶醉桌前 提交于 2019-12-10 18:04:19
问题 How to make a rolling version of the following MAD function from numpy import mean, absolute def mad(data, axis=None): return mean(absolute(data - mean(data, axis)), axis) This code is an answer to this question At the moment i convert numpy to pandas then apply this function, then convert the result back to numpy pandasDataFrame.rolling(window=90).apply(mad) but this is inefficient on larger data-frames. How to get a rolling window for the same function in numpy without looping and give the

How do I speed up profiled NumPy code - vectorizing, Numba?

谁都会走 提交于 2019-12-10 17:46:20
问题 I am running a large Python program to optimize portfolio weights for (Markowitz) portfolio optimization in finance. When I Profile the code, 90% of the run time is spent calculating the portfolio return, which is done millions of times. What can I do to speed up my code? I have tried: vectorizing the calculation of returns: made the code slower , from 1.5 ms to 3 ms used the function autojit from Numba to speed up the code: no change See example below - any suggestions? import numpy as np

Multiply each column from 2D array with each column from another 2D array

谁说我不能喝 提交于 2019-12-10 17:13:04
问题 I have two Numpy arrays x with shape (m, i) and y with shape (m, j) (so the number of rows is the same). I would like to multiply each column of x with each column of y element-wise so that the result is of shape (m, i*j) . Example: import numpy as np np.random.seed(1) x = np.random.randint(0, 2, (10, 3)) y = np.random.randint(0, 2, (10, 2)) This creates the following two arrays x : array([[1, 1, 0], [0, 1, 1], [1, 1, 1], [0, 0, 1], [0, 1, 1], [0, 0, 1], [0, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1