Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a vector into a numpy array; discritize them into bins

I have a vector, say v=[0.001, 0.13, 0.2, ..., .9] with length 365. All values between 0 and 1. I want to turn it into a 2D numpy array of size 365-by-100, i.e. create bins of size 0.01 and see to which bin a given element of v belongs to on a given day in 1-365.

Let me call the 2D array M. I want to have 1 in M[1, 0] because v[0] on first day belongs to the first bin.

It seems the following gives the location/indicies (i,j)'s in M that must turn into ```1````.

matrix_indecies = pd.cut(x=v, bins=np.arange(0, 1, 0.01), labels=False).to_frame().reset_index().to_numpy()

But I do not know how to convert proper M[i,j]'s into 1 without a for-lopp.

like image 420
amir koorani Avatar asked Dec 14 '25 20:12

amir koorani


1 Answers

I don't understand why you don't want to use a for loop in this situation. Seems like a simple solution.

However, here is a version not using a for loop. I'm not sure if a for loop would be faster or slower.

ones_indices = np.floor(v * n_bins)
M = np.zeros((len(v), n_bins), np.bool)
M[np.arange(len(v)), ones_indices] = 1

If this is part of a performance critical part of your code you might want to preallocate the M array and the arange array used for indexing.

OR, rewrite the function in numba if this functionality really is the bottleneck in your code.

Best of luck!

like image 86
Johannes Kasimir Avatar answered Dec 16 '25 09:12

Johannes Kasimir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!