2017-05-05 60 views
1

我正在使用阈值向量在2d numpy数组中逐行创建二进制值。示例代码如下:根据系数调整numpy数组中的值

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])]) 

有没有办法让它更快?

回答

1

执行与coef比较给我们一个布尔数组,然后转换为int阵列,从而利用与NumPy的矢量化功能 -

x_out = (x >= coef).astype(int) 
相关问题