我有一个四行的数组A = array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])
.每行都有4
数字。如何删除row#3
和row#4
?在row#3
和row#4
中,1
和2
分别出现不止一次。
有没有更快的方法来为任意数量的行和列做到这一点?主要目的是删除那些多次出现非负数的行。
你可以使用这样的东西:首先使用 np.unique 创建子数组中每个值的出现字典,并且只保留多次出现没有正数的数组。
A = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])
new_array = []
# loop through each array
for array in A:
# Get a dictionary of the counts of each value
unique, counts = np.unique(array, return_counts=True)
counts = dict(zip(unique, counts))
# Find the number of occurences of postive numbers
positive_occurences = [value for key, value in counts.items() if key > 0]
# Append to new_array if no positive number appears more than once
if any(y > 1 for y in positive_occurences):
continue
else:
new_array.append(array)
new_array = np.array(new_array)
这将返回:
array([[-1, -1, -1, -1],
[-1, -1, 1, 2]])
我的完全矢量化方法:
- 对每一行进行排序
- 通过将排序后的数组向左移动 1 并与其自身进行比较来检测重复项
- 标记具有正重复项的行
- 落
import numpy as np
a = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])
# sort each row
b = np.sort(a)
# mark positive duplicates
drop = np.any((b[:,1:]>0) & (b[:,1:] == b[:,:-1]), axis=1)
# drop
aa = a[~drop, :]
Output:
array([[-1, -1, -1, -1],
[-1, -1, 1, 2]])
我也修改了以存储索引:
A = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])
new_array = []
**indiceStore = np.array([])**
# loop through each array
for array in A:
# Get a dictionary of the counts of each value
unique, counts = np.unique(array, return_counts=True)
counts = dict(zip(unique, counts))
# Find the number of occurences of postive numbers
positive_occurences = [value for key, value in counts.items() if key > 0]
# Append to new_array if no positive number appears more than once
if any(y > 1 for y in positive_occurences):
**indiceStore = np.append(indiceStore, int(array))**
continue
else:
new_array.append(array)
new_array = np.array(new_array)
如果这是对的,让我抨击一下。