我想把二维的日期矩阵转换成基于一维矩阵中日期的布尔矩阵。即。,

[[20030102, 20030102, 20070102],
 [20040102, 20040102, 20040102].,
 [20050102, 20050102, 20050102]]

应该变成
[[True, True, False],
 [False, False, False].,
 [True, True, True]]

如果我提供一个一维数组[20010203、20030102、20030501、20050102、20060101]

最佳答案

import numpy as np

dateValues = np.array(
    [[20030102, 20030102, 20030102],
     [20040102, 20040102, 20040102],
     [20050102, 20050102, 20050102]])

requestedDates = [20010203, 20030102, 20030501, 20050102, 20060101]

ix = np.in1d(dateValues.ravel(), requestedDates).reshape(dateValues.shape)

print(ix)

返回:
[[ True  True  True]
 [False False False]
 [ True  True  True]]

有关更多信息(文档),请参阅numpy.in1d
http://docs.scipy.org/doc/numpy/reference/generated/numpy.in1d.html

关于python - 2d数组的numpy掩码,其中1d数组中的所有值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39413148/

10-14 18:02
查看更多