89

我想删除 numpy.array 中的选定列。这就是我所做的:

n [397]: a = array([[ NaN,   2.,   3., NaN],
   .....:        [  1.,   2.,   3., 9]])

In [398]: print a
[[ NaN   2.   3.  NaN]
 [  1.   2.   3.   9.]]

In [399]: z = any(isnan(a), axis=0)

In [400]: print z
[ True False False  True]

In [401]: delete(a, z, axis = 1)
Out[401]:
 array([[  3.,  NaN],
       [  3.,   9.]])

在此示例中,我的目标是删除所有包含 NaN 的列。我希望最后一个命令会导致:

array([[2., 3.],
       [2., 3.]])

我怎样才能做到这一点?

4

8 回答 8

150

鉴于它的名字,我认为标准的方式应该是delete

import numpy as np

A = np.delete(A, 1, 0)  # delete second row of A
B = np.delete(B, 2, 0)  # delete third row of B
C = np.delete(C, 1, 1)  # delete second column of C

根据numpy的文档页面,参数numpy.delete如下:

numpy.delete(arr, obj, axis=None)

  • arr指输入数组,
  • obj指的是哪些子数组(例如列/行号或数组的切片)和
  • axis指按列 ( axis = 1) 或按行 ( axis = 0) 删除操作。
于 2011-02-17T20:57:37.237 回答
30

来自numpy 文档的示例:

>>> a = numpy.array([[ 0,  1,  2,  3],
               [ 4,  5,  6,  7],
               [ 8,  9, 10, 11],
               [12, 13, 14, 15]])

>>> numpy.delete(a, numpy.s_[1:3], axis=0)                       # remove rows 1 and 2

array([[ 0,  1,  2,  3],
       [12, 13, 14, 15]])

>>> numpy.delete(a, numpy.s_[1:3], axis=1)                       # remove columns 1 and 2

array([[ 0,  3],
       [ 4,  7],
       [ 8, 11],
       [12, 15]])
于 2011-07-05T17:42:42.077 回答
15

另一种方法是使用掩码数组:

import numpy as np
a = np.array([[ np.nan,   2.,   3., np.nan], [  1.,   2.,   3., 9]])
print(a)
# [[ NaN   2.   3.  NaN]
#  [  1.   2.   3.   9.]]

np.ma.masked_invalid 方法返回一个屏蔽数组,其中 nans 和 infs 被屏蔽:

print(np.ma.masked_invalid(a))
[[-- 2.0 3.0 --]
 [1.0 2.0 3.0 9.0]]

np.ma.compress_cols 方法返回一个二维数组,其中包含被抑制的任何列的掩码值:

a=np.ma.compress_cols(np.ma.masked_invalid(a))
print(a)
# [[ 2.  3.]
#  [ 2.  3.]]

参见 manipulating-a-maskedarray

于 2009-10-29T12:44:05.720 回答
8

这将创建另一个没有这些列的数组:

  b = a.compress(logical_not(z), axis=1)
于 2009-10-29T11:19:34.160 回答
7

来自Numpy 文档

np.delete(arr, obj, axis=None) 返回一个新数组,其中删除了沿轴的子数组。

>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> np.delete(arr, 1, 0)
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])

>>> np.delete(arr, np.s_[::2], 1)
array([[ 2,  4],
       [ 6,  8],
       [10, 12]])
>>> np.delete(arr, [1,3,5], None)
array([ 1,  3,  5,  7,  8,  9, 10, 11, 12])
于 2013-11-10T05:55:31.577 回答
4

在您的情况下,您可以使用以下方法提取所需的数据:

a[:, -z]

“-z”是布尔数组“z”的逻辑否定。这与以下内容相同:

a[:, logical_not(z)]
于 2011-10-16T19:36:52.460 回答
1
>>> A = array([[ 1,  2,  3,  4],
               [ 5,  6,  7,  8],
               [ 9, 10, 11, 12]])

>>> A = A.transpose()

>>> A = A[1:].transpose()
于 2015-03-23T23:55:44.613 回答
-1

删除包含 NaN 的 Matrix 列。这是一个冗长的答案,但希望很容易理解。

def column_to_vector(matrix, i):
    return [row[i] for row in matrix]
import numpy
def remove_NaN_columns(matrix):
    import scipy
    import math
    from numpy import column_stack, vstack

    columns = A.shape[1]
    #print("columns", columns)
    result = []
    skip_column = True
    for column in range(0, columns):
        vector = column_to_vector(A, column)
        skip_column = False
        for value in vector:
            # print(column, vector, value, math.isnan(value) )
            if math.isnan(value):
                skip_column = True
        if skip_column == False:
            result.append(vector)
    return column_stack(result)

### test it
A = vstack(([ float('NaN'), 2., 3., float('NaN')], [ 1., 2., 3., 9]))
print("A shape", A.shape, "\n", A)
B = remove_NaN_columns(A)
print("B shape", B.shape, "\n", B)

A shape (2, 4) 
 [[ nan   2.   3.  nan]
 [  1.   2.   3.   9.]]
B shape (2, 2) 
 [[ 2.  3.]
 [ 2.  3.]]
于 2016-11-13T23:30:53.110 回答