【问题标题】:adjust values in numpy array based on coefficients [duplicate]根据系数调整numpy数组中的值[重复]
【发布时间】:2017-05-05 14:37:42
【问题描述】:

我正在使用阈值向量在 2d 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])])

有没有办法让它更快?

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    coef 进行比较,得到一个布尔数组,然后转换为int 数组,从而利用 NumPy 的矢量化功能 -

    x_out = (x >= coef).astype(int)
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多