【问题标题】:best way to find indices and change value in python [duplicate]在python中查找索引和更改值的最佳方法[重复]
【发布时间】:2017-12-05 20:01:00
【问题描述】:

我有一个 numpy.ndarray X。我想找到 X 中所有有 X>阈值的地方,然后执行 X=threshold。

什么是最便宜的方法(就时间复杂度而言)?我需要运行这个程序数百万次。谢谢!

【问题讨论】:

    标签: python numpy time-complexity


    【解决方案1】:

    据我所知,您可以使用 numpy 索引来替换所有大于某个阈值的元素。

    虽然,我不确定这是最快的方式。

    threshold = 10 # for example
    some_array[some_array > threshold] = threshold
    

    【讨论】:

      【解决方案2】:

      试试 numpy.where:

          from numpy import where
      
          Y  = where( X> treshold, threshold,X)
      

      where 应用和 if 语句以 ufunc 方式使用 where(condition, if True, else)

      【讨论】:

        猜你喜欢
        • 2020-03-24
        • 1970-01-01
        • 2016-12-09
        • 2018-11-17
        • 1970-01-01
        • 2010-09-12
        • 1970-01-01
        • 2022-12-15
        • 1970-01-01
        相关资源
        最近更新 更多