gpt4 book ai didi

python - 如果一个元素小于或大于某个值,如何删除 2D numpy 数组中的列

转载 作者:行者123 更新时间:2023-12-01 01:28:08 25 4
gpt4 key购买 nike

现在我有一个二维 numpy 数组来表示图像的坐标像素

points = [[-1,-2,0,1,2,3,5,8] [-3,-4,0,-3,5,9,2,1]]

每一列代表图像中的一个坐标,例如:array[0] = [-1,-3] 表示 x = -1 且 y = -3

现在,我想删除 x 小于 0 && 大于 5 或​​ y 小于 0 && 大于 5 的列

我知道如何删除特定值的元素

#remove x that is less than 0 and more than 5
x = points[0,:]
x = x[np.logical_and(x>=0, x<=5)]

#remove y that is less than 0 and more than 5
y = points[1,:]
y = y[np.logical_and(y>=0,y<=5)]

有没有办法删除与被删除的x共享相同索引的y?(换句话说,当满足x删除或y删除的条件时删除列)

最佳答案

您可以转换listndarray ,然后创建 bool 值掩码并重新分配 x , y 。嵌套logical_and意味着您创建了一个 x>=0 and x<=5 的掩码和y>=0 and y<=5 ,然后 AND运算符确保 if 一次 x[i]已删除,y[i]也被删除了

points = [[-1,-2,0,1,2,3,5,8], [-3,-4,0,-3,5,9,2,1]]
x = np.array(points[0,:])
y = np.array(points[1,:])

mask = np.logical_and(np.logical_and(x>=0, x<=5), np.logical_and(y>=0, y<=5))
# mask = array([False, False, True, False, True, False, True, False])

x = x[mask] # x = array([0, 2, 5])
y = y[mask] # y = array([0, 5, 2])

关于python - 如果一个元素小于或大于某个值,如何删除 2D numpy 数组中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53168015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com