[英]Remove rows of a numpy array based on a specific condition
我有一個四行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]])
我的完全矢量化方法:
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)
讓我知道這是否正確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.