我有一个方形矩阵> 1000行&列。在“边界”的许多字段中都有nan
,例如:
grid = [[nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan],
[nan, nan, 1, nan, nan],
[nan, 2, 3, 2, nan],
[ 1, 2, 2, 1, nan]]
现在我要删除只有nan
的所有行和列。这将是1.和2.行和最后一列。但是我也希望接收一个方阵,因此消除的行数必须等于消除列的数量。在这个例子中,我想得到这个:
grid = [[nan, nan, nan, nan],
[nan, nan, 1, nan],
[nan, 2, 3, 2],
[ 1, 2, 2, 1]]
我确信我可以通过循环来解决这个问题:检查每一列&如果内部只有nan
行,最后我使用numpy.delete来删除行&我找到的列(但只有最小的数字,因为得到一个正方形)。
但我希望任何人都可以帮助我找到更好的解决方案或一个好的图书馆。
答案 0 :(得分:2)
import numpy as np
nan = np.nan
grid = [[nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan],
[nan, nan, 1, nan, nan],
[nan, 2, 3, 2, nan],
[ 1, 2, 2, 1, nan]]
g = np.array(grid)
cols = np.isnan(g).all(axis=0)
rows = np.isnan(g).all(axis=1)
first_col = np.where(cols==False)[0][0]
last_col = len(cols) - np.where(cols[::-1]==False)[0][0] -1
first_row = np.where(rows==False)[0][0]
last_row = len(rows) - np.where(rows[::-1]==False)[0][0] -1
row_len = last_row - first_row
col_len = last_col - first_col
delta_len = row_len - col_len
if delta_len == 0:
pass
elif delta_len < 0:
first_row = first_row - abs(delta_len)
if first_row < 0:
delta_len = first_row
first_row = 0
last_row += abs(delta_len)
elif delta_len > 0:
first_col -= abs(delta_len)
if first_col < 0:
delta_len = first_col
first_col = 0
last_col += abs(delta_len)
print g[first_row:last_row+1, first_col:last_col+1]
输出:
[[ nan nan nan nan]
[ nan nan 1. nan]
[ nan 2. 3. 2.]
[ 1. 2. 2. 1.]]
答案 1 :(得分:2)
这样做,压缩rows \ cols的索引是关键,所以它们总是具有相同的长度,因此保留了矩阵的方形性。
nans_in_grid = np.isnan(grid)
nan_rows = np.all(nans_in_grid, axis=0)
nan_cols = np.all(nans_in_grid, axis=1)
indicies_to_remove = zip(np.nonzero(nan_rows)[0], np.nonzero(nan_cols)[0])
y_indice_to_remove, x_indice_to_remove = zip(*indicies_to_remove)
tmp = grid[[x for x in range(grid.shape[0]) if x not in x_indice_to_remove], :]
grid = tmp[:, [y for y in range(grid.shape[1]) if y not in y_indice_to_remove]]
继续E先生,解决方案,然后填充结果也有效。
def pad_to_square(a, pad_value=np.nan):
m = a.reshape((a.shape[0], -1))
padded = pad_value * np.ones(2 * [max(m.shape)], dtype=m.dtype)
padded[0:m.shape[0], 0:m.shape[1]] = m
return padded
g = np.isnan(grid)
grid = pad_to_square(grid[:, ~np.all(g, axis=0)][~np.all(g, axis=1)])
另一个解决方案,建立在另一个答案在这里。对于较大的矩阵,显着更快。
shape = grid.shape[0]
first_col = (i for i,col in enumerate(grid.T) if np.isfinite(col).any() == True).next()
last_col = (shape-i-1 for i,col in enumerate(grid.T[::-1]) if np.isfinite(col).any() == True).next()
first_row = (i for i,row in enumerate(grid) if np.isfinite(row).any() == True).next()
last_row = (shape-i-1 for i,row in enumerate(grid[::-1]) if np.isfinite(row).any() == True).next()
row_len = last_row - first_row
col_len = last_col - first_col
delta_len = row_len - col_len
if delta_len == 0:
pass
elif delta_len < 0:
first_row = first_row - abs(delta_len)
if first_row < 0:
delta_len = first_row
first_row = 0
last_row += abs(delta_len)
elif delta_len > 0:
first_col -= abs(delta_len)
if first_col < 0:
delta_len = first_col
first_col = 0
last_col += abs(delta_len)
grid = grid[first_row:last_row+1, first_col:last_col+1]
答案 2 :(得分:2)
这是一种较短的方式。它的工作原理是扫描主对角线,去除所有纳米的行+列,然后对辅助对角线做同样的事情:
import numpy as np
nan = np.nan
grid = [[nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan],
[nan, nan, 1, nan, nan],
[nan, 2, 3, 2, nan],
[ 1, 2, 2, 1, nan]]
g = np.array(grid)
for i in [1, 2]:
cols = np.isnan(g).all(axis=0)
rows = np.isnan(g).all(axis=1)
main_diagonal = np.logical_not(cols & rows)
ind = np.nonzero(main_diagonal)[0]
main_diagonal[ind[0]:ind[-1]+1] = True # do not remove inner row/col
removed_main_diag = g[main_diagonal][:, main_diagonal]
g = removed_main_diag[:][::-1]
print g
输出:
[[ nan nan nan nan]
[ nan nan 1. nan]
[ nan 2. 3. 2.]
[ 1. 2. 2. 1.]]