根据系数调整numpy数组中的值 [英] adjust values in numpy array based on coefficients
本文介绍了根据系数调整numpy数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用阈值向量在2d numpy数组中逐行创建二进制值.下面提供了示例代码:
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])])
反正有更快的速度吗?
推荐答案
与 coef
进行比较,以得到一个布尔数组,然后转换为 int
数组,因此利用NumPy的矢量化功能-
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)
这篇关于根据系数调整numpy数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文