2009-10-29 70 views
35

我想刪除numpy.array中的選定列。這是我做的:如何刪除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.]]) 

我怎麼能這樣做?

回答

48

鑑於它的名字,我認爲標準的方法應該是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 
+12

我相信你應該引用'numpy',而不是'scipy'。 http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html – hlin117 2015-03-26 03:37:03

7

這造成在沒有那些列另一個數組:

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

很酷。我希望matlab的語法在這裏工作:「a(:,z)= []」要簡單得多 – 2009-10-29 11:22:58

+1

類似:b = a [:,[1,2]] – Paul 2009-10-29 11:52:15

+1

@bpowah:的確如此。更一般的方式是b = a [:,z]。你可能想相應地更新你的答案 – 2009-10-29 12:12:46

12

的另一種方法是使用掩蔽陣列:

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掩蔽陣列out:

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

np.ma.compress_cols方法返回一個2-D數組,其中包含任何列荷蘭國際集團忍住 屏蔽值:

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

manipulating-a-maskedarray

7

來自實例the numpy documentation

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

什麼是's_'? – alvas 2016-06-24 07:52:17

+0

@alvas這裏是一個組織的解釋! http://stackoverflow.com/questions/32682754/np-delete-and-np-s-whats-so-special-about-np-s – 2016-09-17 08:50:08

2

在你的情況,你可以提取所需的數據:

a[:, -z] 

「-z」是布爾數組「Z」的邏輯否定。這是相同的:

a[:, logical_not(z)] 
5

Numpy Documentation

np.delete(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]) 
1
>>> A = array([[ 1, 2, 3, 4], 
       [ 5, 6, 7, 8], 
       [ 9, 10, 11, 12]]) 

>>> A = A.transpose() 

>>> A = A[1:].transpose() 
0

刪除包含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.]] 
+0

我其實不會跟着你。這段代碼如何工作? – RamenChef 2016-11-13 23:49:52