vectorization

Vectorizing operation on numpy array

纵饮孤独 提交于 2021-02-08 08:13:41
问题 I have a numpy array containing many three-dimensional numpy arrays, where each of these sub-elements is a grayscale image. I want to use numpy's vectorize to apply an affine transformation to each image in the array. Here is a minimal example that reproduces the issue: import cv2 import numpy as np from functools import partial # create four blank images data = np.zeros((4, 1, 96, 96), dtype=np.uint8) M = np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32) # dummy affine transformation matrix

Vectorize numpy array for loop

 ̄綄美尐妖づ 提交于 2021-02-08 04:41:50
问题 I'm trying to figure out how to vectorize the following loop: for i in range(1,size): if a[i] < a[i-1]: b[i] = a[i] else: b[i] = b[i-1] b is a (large) array of the same size as a. I could use numpy.where(a[1:]<a[:-1]) to replace the if statement but how do you simultaneously replace the else statement? 回答1: I think you want something like this: import numpy as np def foo(a, b): # cond is a boolean array marking where the condition is met cond = a[1:] < a[:-1] cond = np.insert(cond, 0, False)

generate a 2D array of numpy.random.choice without replacement

痞子三分冷 提交于 2021-02-08 03:38:34
问题 I'm tyring to make my code faster by removing some for loops and using arrays. The slowest step right now is the generation of the random lists. context: I have a number of mutations in a chromosome, i want to perform 1000 random "chromosomes" with the same length and same number of mutation but their positions are randomized. here is what I'm currently running to generate these randomized mutation positions: iterations=1000 Chr_size=1000000 num_mut=500 randbps=[] for k in range(iterations):

Checking membership in an array without for loop in matlab

邮差的信 提交于 2021-02-07 20:31:51
问题 I want to simplify this code to working without for loop. for i=1:N for j=1:N if ismember(j,A) PID(i,i)=TFP(i,j)+ PID(i,i); end end end in which A is a matrix that contains some labels. I previously stored TFP in the form of N*N sparse double. So, I came up with the following solution but I couldn't find a way to implement membership condition (Specified by ?) in that. PID = sum(TFP).*(?); Can it be implemented without a loop? 回答1: Your ismember(j,A) is equivalent of just using the values of

Checking membership in an array without for loop in matlab

给你一囗甜甜゛ 提交于 2021-02-07 20:30:33
问题 I want to simplify this code to working without for loop. for i=1:N for j=1:N if ismember(j,A) PID(i,i)=TFP(i,j)+ PID(i,i); end end end in which A is a matrix that contains some labels. I previously stored TFP in the form of N*N sparse double. So, I came up with the following solution but I couldn't find a way to implement membership condition (Specified by ?) in that. PID = sum(TFP).*(?); Can it be implemented without a loop? 回答1: Your ismember(j,A) is equivalent of just using the values of

Vectorize a 6 for loop cumulative sum in python

馋奶兔 提交于 2021-02-07 14:57:54
问题 The mathematical problem is: The expression within the sums is actually much more complex than the one above, but this is for a minimal working example to not over-complicate things. I have written this in Python using 6 nested for loops and as expected it performs very badly (the true form performs badly and needs evaluating millions of times), even with help from Numba, Cython and friends. Here it is written using nested for loops and a cumulative sum: import numpy as np def func1(a,b,c,d):

Shuffling non-zero elements of each row in an array - Python / NumPy

只愿长相守 提交于 2021-02-06 14:23:11
问题 I have a an array that is relatively sparse, and I would like to go through each row and shuffle only the non-zero elements. Example Input: [2,3,1,0] [0,0,2,1] Example Output: [2,1,3,0] [0,0,1,2] Note how the zeros have not changed position. To shuffle all elements in each row (including zeros) I can do this: for i in range(len(X)): np.random.shuffle(X[i, :]) What I tried to do then is this: for i in range(len(X)): np.random.shuffle(X[i, np.nonzero(X[i, :])]) But it has no effect. I've

Shuffling non-zero elements of each row in an array - Python / NumPy

自作多情 提交于 2021-02-06 14:21:25
问题 I have a an array that is relatively sparse, and I would like to go through each row and shuffle only the non-zero elements. Example Input: [2,3,1,0] [0,0,2,1] Example Output: [2,1,3,0] [0,0,1,2] Note how the zeros have not changed position. To shuffle all elements in each row (including zeros) I can do this: for i in range(len(X)): np.random.shuffle(X[i, :]) What I tried to do then is this: for i in range(len(X)): np.random.shuffle(X[i, np.nonzero(X[i, :])]) But it has no effect. I've

numba eager compilation? Whats the pattern?

馋奶兔 提交于 2021-02-05 09:13:58
问题 I looked into eager compilation on numba's website and couldnt figure out, how to specify the types: The example they use is this: from numba import jit, int32 @jit(int32(int32, int32)) def f(x, y): # A somewhat trivial example return x + y # source: http://numba.pydata.org/numba-doc/latest/user/jit.html#eager-compilation as you can see it gets 2 variables as input and returns one single variable. all of them should be int32. One way to understand the decorator is that @jit(int32(int32, int32

numba eager compilation? Whats the pattern?

与世无争的帅哥 提交于 2021-02-05 09:11:21
问题 I looked into eager compilation on numba's website and couldnt figure out, how to specify the types: The example they use is this: from numba import jit, int32 @jit(int32(int32, int32)) def f(x, y): # A somewhat trivial example return x + y # source: http://numba.pydata.org/numba-doc/latest/user/jit.html#eager-compilation as you can see it gets 2 variables as input and returns one single variable. all of them should be int32. One way to understand the decorator is that @jit(int32(int32, int32