1

I'm using a threshold vector to create binary values in a 2d numpy array row-wise. Sample code is provided below:

import numpy as np
x = np.random.rand(100000, 200)
coef = np.random.random(x.shape[1])
x = np.array([[1 if x[i,j]>=coef[j] else 0 for j in range(x.shape[1])] for i in range(x.shape[0])])

Is there anyway to make it faster?

Abhishek Thakur
  • 16,337
  • 15
  • 66
  • 97

1 Answers1

1

Perform the comparison with coef to give us a boolean array and then convert to int array, thus leveraging vectorized capabilities of NumPy -

x_out = (x >= coef).astype(int)
Divakar
  • 218,885
  • 19
  • 262
  • 358