vectorization

Avoiding for-loop in NumPy 1D nearest neighbors

Avoiding for-loop in NumPy 1D nearest neighbors Question: I have the following code in which I get the N nearest neighbors in 1D: import numpy as np def find_nnearest(arr, val, N): idxs = [] for v in val: idx = np.abs(arr – v).argsort()[:N] idxs.append(idx) return np.array(idxs) A = np.arange(10, 20) test = find_nnearest(A, A, 3) …

Total answers: 1

Fastest way to add matrices of different shapes in Python/Numba

Fastest way to add matrices of different shapes in Python/Numba Question: I want to "add" two matrices, a matrix a with shape (K,T) and a matrix b of shape (K,N), to result in a matrix of shape (K,T,N) The following works ok: import numpy as np from numba import njit @njit def add_matrices(a, b): K, …

Total answers: 2

Python Vectorization Split String

Python Vectorization Split String Question: I want to use vectorization to create a column in a pandas data frame that retrieve the second/last part of a string, from each row in a column, that is split on ‘_’. I tried this code: df = pd.DataFrame() df[‘Var1’] = ["test1_test2","test3_test4"] df[‘Var2’] = [[df[‘Var1’].str.split(‘_’)][0]][0] df Var1 Var2 0 …

Total answers: 3

How to cycle through values in Pandas Dataframe?

How to cycle through values in Pandas Dataframe? Question: What’s the fastest way to cycle through and repeat names in one dataframe when updating certain rows in a second dataframe? Thank you for your help. I’ve spent many days on this and am getting nowhere. Rows with Rules 4,5,6,10,11,12 should cycle through and repeat names …

Total answers: 2

Problem solving without for loop,but with vectorization (pandas dataframe) considering multi condition

Problem solving without for loop,but with vectorization (pandas dataframe) considering multi condition Question: I have a dataframe like below : id = [‘A’,’A’,’A’,’A’,’A’,’B’,’B’,’B’] workcycle = [0,4,5,100,140,0,5,20] date = [‘2022-01-01′,’2022-01-01′,’2022-01-02′,’2022-02-04′,’2022-03-10′,’2022-01-01′,’2022-01-02′,’2022-02-04’] failure_type=[‘A’,’B’,None,’B’,None,’A’,None,None] repair_type=[None,None,’Repair_Type_1′,None,’Repair_Type_2′,None,’Repair_Type_1′,’Repair_Type_2′] event=[‘failure’,’failure’,’Repair’,’failure’,’Repair’,’failure’,’Repair’,’Repair’] This is dataframe about failure and repair of some machine. And there are multi failure type. There are two criteria (workcycle and date) …

Total answers: 1

Vectorizing multivariate normal distribution calculation

Vectorizing multivariate normal distribution calculation Question: I have n points in 3D space, each with a corresponding guess and certainty attached to it. I want to calculate the multivariate normal distribution for each point given its guess and certainty. Currently, I’m using an iterative approach and the Scipy.stats multivariate_normal function, as shown in the code …

Total answers: 1

Slice a multidimensional pytorch tensor based on values in other tensors

Slice a multidimensional pytorch tensor based on values in other tensors Question: I have 4 PyTorch tensors: data of shape (l, m, n) a of shape (k,) and datatype long b of shape (k,) and datatype long c of shape (k,) and datatype long I want to slice the tensor data such that it picks …

Total answers: 2

Problem with looping over pandas dataframe

Problem with looping over pandas dataframe Question: I have a dataframe (df) of 100 (will be more eventually) rows and 2689 columns. I have to apply a function say fn1(df,a,b) on each row. The fn1 itself has a for loop, which I cannot avoid. So, I want to speed up applying fn1 to all rows …

Total answers: 2

What is a vectorized way to perform a sliding window

What is a vectorized way to perform a sliding window Question: I have a nested for loop function. For each index i and j of a 2D matrix, it sums all the elements of a 2D slice of a 2D array, as in sum(data[i-1:i+1,j-1+i+1])). import numpy as np data=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]) # This is to specify at …

Total answers: 1

Numpy subtracting rows of a 2D array from another 2D array without for loop

Numpy subtracting rows of a 2D array from another 2D array without for loop Question: I know that if we try to subtract a row vector v(1,3072) from a 2D array A(5000,3072) if A and v does have same number of column v is broadcasted, but subtracting stack of row vectors V (each row of …

Total answers: 1