将一个数组的 50% 值随机替换为 Python 中第二个数组中随机选择的 50% 值

Randomly replace 50% values of one array with 50% of randomly selected values from a second array in Python

我对 Python 和编码完全陌生,我一直试图用第二个数组的值随机替换一个数组中的 selected 值。我的数据是从 2 个虹膜立方体中提取的,由 LAT 和 LON 数据组成。

加载两个立方体后,我可以从纬度和经度的 2 个观测数据集中提取数据,例如“obs_1”和“obs_2”,形状为 (475, 635) :

obs_1
<iris 'Cube' of OBSERVATIONS / (g/m2) (latitude: 475; longitude: 635)>

obs_2
<iris 'Cube' of OBSERVATIONS / (g/m2) (latitude: 475; longitude: 635)>

obs_1.data 和 obs_2.data 都可以线程化为 numpy 数组:

type(obs_1.data)
Out[174]: numpy.ndarray

size(obs_1.data)
Out[173]: 301625

我的 obs_1 包括 selected 日在 time=18:00 的观察结果,以及从 t=14 开始的同一天 obs_2 随时间变化的平均值: 00 到 t=17:00。

现在,我要做的是用 obs_2.[=16 中随机 select 的 50% 值随机替换 obs_1 中 50% 的值=]

数组中的数据如下所示(这是来自数组的 selection):

array([[       nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan, 3.6444201 , 3.6288068 ,
    3.4562614 , 3.1650603 , 2.837024  , 2.5862055 , 2.5824826 ,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
           nan,        nan, 4.126052  , 4.154033  , 3.6938105 ,
    3.1892183 , 2.837798  , 2.695081  ,        nan, 2.4830801 ,
    2.619453  , 2.744787  ,        nan,        nan,        nan,
    4.037193  , 3.9007418 , 3.918395  , 4.1123595 ,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
           nan, 4.479512  , 4.139696  , 3.7454944 ,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan, 1.7283309 , 2.0259488 , 2.6097915 , 2.8537903 ,
    3.3934724 ,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
    4.476785  , 4.5633755 , 3.7924814 , 3.270711  ,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan, 1.7360739 , 2.171296  ,
    2.6570952 , 3.58288   , 4.6880975 ,        nan,        nan,
           nan,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
    4.411482  , 3.9552238 , 3.7757099 , 2.875049  , 2.1458075 ,
           nan,        nan,        nan,        nan,        nan,
           nan, 1.7425493 , 1.8161889 ,        nan,        nan,
           nan,        nan,        nan, 1.2822593 , 1.4383382 ,
    1.5031592 , 1.5003852 , 1.9955662 , 4.0983477 ,        nan,
           nan,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan, 1.5202525 , 1.2684406 ,
    1.3887881 , 1.6239417 , 1.5679324 , 1.3143418 ,        nan,
           nan,        nan, 0.9014559 , 1.046359  , 1.1121098 ,
    1.2461395 , 1.3922306 , 1.5674534 , 1.7686707 , 4.694426  ,
    5.8581176 ,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan, 1.4250685 , 1.342187  ,
    1.460965  , 1.5898347 , 1.4935569 ,        nan,        nan,
    0.76497865, 0.7578024 , 0.9086805 , 1.1051334 , 1.0408422 ,
    1.0398425 , 1.1574577 ,        nan,        nan, 1.6596926 ,
    4.667655  ,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan, 1.4770626 , 1.3014681 ,
    1.2809513 ,        nan,        nan, 1.0585229 , 0.98995847,
    0.8447306 , 0.7979446 ,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
    2.920856  ,        nan,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan, 1.2806126 ,        nan,
    0.97792864, 0.8848762 , 2.0891907 , 1.4531214 , 1.2615036 ,
    0.97086287,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan, 4.1831126 ,        nan,        nan],
   [       nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan, 1.1235833 , 1.2448411 , 0.95834756, 0.99093884,
    1.0072019 , 1.1916308 , 0.9324562 , 1.0275717 , 1.2712531 ,
           nan,        nan,        nan,        nan,        nan,
           nan,        nan,        nan,        nan,        nan,
           nan, 3.2303405 , 4.449829  ,        nan]], dtype=float32)

其中 nan 是加载过程屏蔽的值(数据不相关)。

我进行了搜索并尝试使用 np.random 和掩码,但是我不明白如何从两个数组中随机 select,并将 obs_1 掩码替换为 obs_2 面具,因为面具有不同的形状。我正在努力编写代码,所以除了使用 iris 立方体加载数据(如果有任何帮助,我可以 post),我没有示例可以展示。

有人可以给我指出任何例子吗(到目前为止我找不到任何关于从不同数组交换数据的例子)或者给我任何如何继续的提示。

非常感谢。 祝一切顺利

请参阅 从 Numpy 数组中随机选择。

obs_1 = np.array(
    [[1, 3, 0],
     [3, 2, 0],
     [0, 2, 1],
     [1, 1, 4],
     [3, 2, 2],
     [0, 1, 0],
     [1, 3, 1],
     [0, 4, 1],
     [2, 4, 2],
     [3, 3, 1]]
)

obs_2 = np.array(
    [[10, 3, 0],
     [30, 2, 0],
     [100, 2, 1],
     [10, 1, 4],
     [30, 2, 2],
     [100, 1, 0],
     [10, 3, 1],
     [100, 4, 1],
     [20, 4, 2],
     [30, 3, 1]]
)

n_observation = min(obs_1.shape[0], obs_2.shape[0])
index_1 = np.random.choice(np.arange(obs_1.shape[0]), int(n_observation / 2), replace=False)
index_2 = np.random.choice(np.arange(obs_2.shape[0]), int(n_observation / 2), replace=False)
obs_1[index_1, :] = obs_2[index_2, :]