Python 如何删除numpy.array中的列

Python 如何删除numpy.array中的列,python,numpy,scipy,Python,Numpy,Scipy,我想删除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

我想删除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.]])

如何执行此操作?

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

  b = a.compress(logical_not(z), axis=1)

另一种方法是使用屏蔽阵列:

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方法返回一个屏蔽了NAN和infs的屏蔽数组:

print(np.ma.masked_invalid(a))
[[-- 2.0 3.0 --]
 [1.0 2.0 3.0 9.0]]
np.ma.compress\u cols方法返回一个二维数组,其中任何列包含 已抑制遮罩值:

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

鉴于其名称,我认为标准的方法应该是
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.delete的参数如下所示:

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

  • arr
    指的是输入数组
  • obj
    指的是哪个子数组(例如,数组的列/行号或切片)和
  • axis
    指的是按列(
    axis=1
    )或按行(
    axis=0
    )的删除操作
示例来自:


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

a[:, -z]
“-z”是布尔数组“z”的逻辑求反。这与:

a[:, logical_not(z)]

np.删除(arr、obj、轴=无) 返回一个沿轴删除子数组的新数组

>>> 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])

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

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.]]

酷。我希望matlab的语法能在这里工作:“a(:,z)=[]”非常有用simpler@bpowah:的确如此。更一般的方法是b=a[:,z]。您可能需要相应地更新您的答案。我认为您应该参考
numpy
,而不是
scipy
。只需添加说明:数组、索引和轴为parameters@alvas这里有一个组织良好的解释@Alvas,s_是:
为数组建立索引元组的一种更好的方法。
:我实际上不明白你的意思。这段代码是如何工作的?
>>> A = array([[ 1,  2,  3,  4],
               [ 5,  6,  7,  8],
               [ 9, 10, 11, 12]])

>>> A = A.transpose()

>>> A = A[1:].transpose()
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.]]