python - 将 np.where 与索引一起使用

标签 python performance numpy

我有一个零数组 (17520,5),我想用两个值填充它:0 和 0.05。我有两个条件,我正在使用函数 np.where,但是,我只需要在数组的特定索引处应用第二个条件。我使用的代码如下:

independent = np.zeros([17520,5])
w1 = np.where(independent == 0)
independent[w1] = np.random.choice([0.0, 0.05], size=len(w1[0]))

这部分代码工作正常,并用所需值填充零数组(独立):0 和 0.05 具有相同的比例 (50/50)。另一方面,第二个条件只需要在特定索引处实现,如下所示:

for n in range(0, 365):
    start = 24 + n*48
    end = 46 + n*48
    w2 = np.where(independent == 0.05)
    independent[w2][start:end,0:5]=np.random.choice([0.0, 0.05], (22,5),size=len(w2[0]))

其中 [start:end,0:5] 表示我要实现条件 w2 的索引。

非常感谢您的帮助,指出使用带索引的函数 np.where 的正确方法,因为目前我遇到以下错误

 SyntaxError: invalid syntax

最佳答案

请注意 np.where也可以采用两个array_like 参数,根据条件 从中进行选择。以下是在您的案例中如何使用 np.where:

for n in range(0, 365):
    start = 24 + n*48
    end = 46 + n*48
    independent[start:end,0:5] = (np.where(independent== 0.05, 
                                          np.random.choice([0.0, 0.05], 
                                                    size=independent.shape), 
                                          independent)[start:end,0:5])

这有点棘手,但上面的内容可以矢量化。关键是获取我们希望 independent 更新的范围列表。为此,我们可以使用 n_ranges来自链接的答案,可用于从相应的 startend 获取具有所有范围的平面数组:

start = 24 + np.arange(0, 365)*48
end = 46 + np.arange(0, 365)*48
ranges = n_ranges(start, end)
independent[ranges,0:5] = (np.where(independent== 0.05, 
                                   np.random.choice([0.0, 0.05], 
                                                    size=independent.shape), 
                                   independent)[ranges,0:5])

检查计时,我们可以看到使用第二种方法我们获得了 260x 加速!

def vect_approach(a):
    start = 24 + np.arange(0, 365)*48
    end = 46 + np.arange(0, 365)*48
    ranges = n_ranges(start, end)
    a[ranges,0:5] = (np.where(a== 0.05, 
                             np.random.choice([0.0, 0.05], size=a.shape ),
                             a)[ranges,0:5])

def loopy_approach(x):
    for n in range(0, 365):
        start = 24 + n*48
        end = 46 + n*48
        independent[start:end,0:5] = (np.where(independent== 0.05, 
                                              np.random.choice([0.0, 0.05], 
                                                        size=independent.shape), 
                                              independent)[start:end,0:5])

independent = np.zeros([17520,5])

%timeit loopy_approach(independent)
# 475 ms ± 19.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit vect_approach(independent)
# 1.87 ms ± 95.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

关于python - 将 np.where 与索引一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183569/

相关文章:

python - Numpy 像 python 一样否定索引

python - 使用 python 从其后代中提取主词

python - 在 Python 3.x 中使用 Pandas 合并基于列和特定列的值的两个 DataFrame

android - 来自网络的具有不同高度的图像的 ListView 导致 "flickers"

c# - 为什么 Entity Framework 在 SELECT 上生成 JOIN

python - 将 numpy 数组的特定索引中的数字替换为 NaN

python - 在 numpy 数组上使用拟合变换

python - 在 Pandas 中根据条件添加行

python - 我想在文件对话框中的 "save as"选项时使用扩展列表在 Python 3 中进行文件写入

performance - 在 LoadRunner 中添加 Dynatrace header 是否会增加响应时间?