# vectorization

## Vectorize function acting on indexes

TL;DR: I understand that .apply() is slow in pandas. However, I have a function that acts on indexes, which I cannot figure out how to vectorize. I want this function to act on two sets of parameters (500,000 and 1,500 long, respectively)….

## How to vectorize a Pandas dataframe calculation using two rows to calculate a third?

I have an enormous dataframe containing sensor data harvested from a chassis containing nearly 30 embedded processors while it was undergoing environmental stress testing. The vast majority of the sensors are for temperature, but there ar…

## Can you make this faster? Sentence to Numpy Array mapping

I have a string of Korean language. I need to map it into a numpy array where each letter is encoded in one-hot fashion.
import numpy as np

def embed_letter(x: str) -> np.array:
# input: one Korean letter
# maps the letter into…

## Multiply Tensor-Slice by Matrix-Row

I’m trying to find a way to efficiently compute the matrix-vector-product of each depth-wise 2d-slice of a tensor (shape: (n, n, m)) with each row of a matrix (shape: (n, m)). What I’m trying to do looks like this without vectorization:
im…

## Multiply array of n 3×3 rotation matrices with 3d array of 3-vectors

I have a 3d array of position vectors p [np.shape(p) yields (Nx, Ny, Nz, 3)] and an array Rn of n rotation matrices [np.shape(R) yields (n, 3, 3)].
I am trying to get an array PR of shape (n, Nx, Ny, Nz, 3) where the i-th (0 < i < n)…

## Creating a list of instances(objects) of a class in Python without explicit list comprehension or for loops

The goal here is to have a class (let’s say Teachers) that has a single attribute (score) as shown below:
import numpy as np
maxScore = 10
numTeach = 50

class Teacher:

counter = 0

def __init__(self, score):
self.score =…

## Taking an outer subtraction between a list of tuples using Numpy

NOTE: I am not looking for syntax on how to use np.subtract.outer, instead I am trying to solve a very specific issue that I am facing during its application.
I have a list of tuples –
a = [(0,0), (1,0), (1,1), (2,0), (2,2)]
a = np.array(a…

## Optimize ndarray comparison operations in Pandas

I have a data frame to hold 4 ndarray values. 2 of them are np.ndarray size – (100,1) and 2 smaller values np.ndarray size – (10,1). Their datatypes are uint16. This is how my dataFrame looks
SN_x int64
BV_x object
SV_x obje…

## De-vectorizing array manipulation in Julia

This question is an extension from one of my previous questions on optimizing an array calculation in Python. The answer for my given problem was given via @tomjn as
tot_list = np.where((X == 1) | (Y == 1) | (Z == 1),np.random.random(),1)….

## Transitioning from pandas .apply to a vectoriztion approach

I am trying to improve a pandas iteration with a purely vectorization approach. I am a little new to vectoriztion and am having trouble getting it to work.
Within one dataframe field, I am finding all the unique string-based records of add…

## Get number of elements of array satisfying a list of conditionsin Python/Numpy

I have two arrays of the same size N, array1 and array2. You can essentially think of it as a single array with shape (N,2). The entries are all numbers. I have a list of conditions and I want to see how many entries satisfy all these cond…

## Vectorizing three nested loops with Numpy

I have a complex matrix C with dimensions (r, r) as well as a complex vector of size r. I need to compute a new matrix from C and v following this equation:

where K is also a square matrix of dimensions (r, r). Here is the code to compute…

## How do you parallelize MATLAB code to print all possible 3-tuples more quickly?

How do I parallelize the serial MATLAB code below? You can assume the printing of the 3-tuple in different order is fine, so long as all possible permutations are printed.
for dt=1e-10:1e-10:1e-9
for dx=1e-7:1e-7:1e-6

## removing loops with numpy.einsum

I have a some nested loops (three total) where I’m trying to use numpy.einsum to speed up the calculations, but I’m struggling to get the notation correct. I managed to get rid of one loop, but I can’t figure out the other two. Here’s what…

## numba vectorization with array

a and b are lists of 2d numpy object arrays (or of 2d numba-Lists) with a different length of the sublists.
(b is filled by "only-nan" 2d-arrays)
I need to merge them efficiently (and often).
normally a = np.concatenate((a,b),ax…

## Speeding up Dataframe apply pandas

I have a tXn (5000 X 100) dataframe wts_df,
wts_df.tail().iloc[:, 0:6]
Out[71]:
B C H L R T
2020-09-25 0.038746 0.033689 -0.047835 -0.002641 0.009501 -0.030689
2020-09-28 …

## Speeding up Dataframe apply pandas

I have a tXn (5000 X 100) dataframe wts_df,
wts_df.tail().iloc[:, 0:6]
Out[71]:
B C H L R T
2020-09-25 0.038746 0.033689 -0.047835 -0.002641 0.009501 -0.030689
2020-09-28 …