简体   繁体   English

从 3D numpy 数组中删除切片

[英]deleting slices from a 3D numpy array

I have a 3D numpy array (I call it a tensor) shaped (5,8, 15000) .我有一个形状为(5,8, 15000)

Because of some calculations which filled it, there are some NaN 's inside the tensor.由于一些计算填充了它,张量内有一些NaN The last axis shows the simulation index.最后一个轴显示模拟指数。 I had a process which repeated 15,000 times on the computer with slightly changed dynamics.我有一个过程在计算机上重复了 15,000 次,动态略有变化。

I want to go through all the 15,000 simulations and look at the 2D array (shaped (5,8) ) corresponding to one such simulation.我想通过所有 15,000 次模拟 go 并查看与此类模拟相对应的 2D 阵列(形状(5,8) )。 If there's a NaN across the 2nd row, across all the columns in the 2D array (ie [1,:] ), I want to delete the simulation from the 3D tensor.如果第二行有一个 NaN,在 2D 数组中的所有列(即[1,:] ),我想从 3D 张量中删除模拟。 This shall repeat for all 15,000 simulations.这将在所有 15,000 次模拟中重复。

That is, the new tensor shall be of shape (5, 8, 15000 - number_of_bad_sims) , with all the 2D arrays corresponding to that simulations which contained at least 1 NaN deleted.也就是说,新张量的形状应为(5, 8, 15000 - number_of_bad_sims) ,其中所有 2D arrays 对应于包含至少 1 个 NaN 的模拟。 The rest of the tensor shall remain the same.张量的 rest 应保持不变。 I don't want to touch any of the 2D arrays which don't have a NaN across their 2nd row.我不想触摸第二行没有 NaN 的任何 2D arrays 。

I have tried:我努力了:

# threeDimTensor has shape (5,8, 15000)

for idx in range(threeDimTensor.shape[2]): # for all simulations
    boolean_array = np.isnan(threeDimTensor[1, :, idx]) # check if any of the results is NaN
    condition = False
    for element in boolean_array:
        if element == True and condition == False:
            condition = True
            np.delete(threeDimTensor, idx, axis=2) # along last axis ???

My question is how do I store the np.delete() result?我的问题是如何存储np.delete()结果? I don't know how many NaN's I have, thus I cannot initially create a np.zeros(input_shape) 3D array to be populated with not-NaN's values.我不知道我有多少 NaN,因此我最初无法创建一个 np.zeros(input_shape) 3D 数组来填充非 NaN 的值。

Also, is my np.delete() instruction correct?另外,我的np.delete()指令是否正确?

What would be a correct, efficient way to store the ''clean'' new three dimensional tensor?存储“干净”的新三维张量的正确、有效方法是什么? That is, how do I finish this piece of code to return the new three dimensional tensor and to use it further?即如何完成这段代码,返回新的三维张量并进一步使用?

Thank you!谢谢!

You could just have used isnan for your entire array, generate an array that masks the bad sims from that, and use it to select the good sims您可以将 isnan 用于整个阵列,生成一个阵列来掩盖坏模拟人生,然后将其用于 select 好模拟人生

My advice for using Numpy is to vectorize your code, try to think of a way in which you do not need explicit for-loops:我对使用 Numpy 的建议是对代码进行矢量化,尝试考虑一种不需要显式 for 循环的方法:

is_element_nan = np.isnan(threeDimTensor[1, :, :]) # Across your 2nd row
any_nan = np.any(is_element_nan, axis=0) #Flattens the 2d matrix
clean_tensor = threeDimTensor[:, :, ~any_nan] # ~is bitwise not

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM