วิธีลบคอลัมน์ใน 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's ฉันคาดว่าคำสั่งสุดท้ายจะส่งผลให้:

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

ฉันจะทำเช่นนั้นได้อย่างไร?


person Boris Gorelik    schedule 29.10.2009    source แหล่งที่มา


คำตอบ (7)


ด้วยชื่อของมัน ฉันคิดว่าวิธีมาตรฐานควรเป็น 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)
person Steve Tjoa    schedule 17.02.2011
comment
ฉันเชื่อว่าคุณควรอ้างอิง numpy ไม่ใช่ scipy docs.scipy.org/doc/numpy/reference/generated/ numpy.delete.html - person hlin117; 26.03.2015
comment
เพียงเพิ่มคำอธิบาย: อาร์เรย์ ดัชนี และแกนเป็นพารามิเตอร์ - person maximus; 23.06.2018

ตัวอย่างจากเอกสารประกอบ:

>>> 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]])
person Nikolay Frick    schedule 05.07.2011
comment
@alvas นี่คือคำอธิบายที่มีการจัดระเบียบอย่างดี! stackoverflow.com/questions/32682754/ - person Daeyoung Lim; 17.09.2016
comment
@Alvas , s_ คือ: A nicer way to build up index tuples for arrays.: docs.scipy.org/ doc/numpy/reference/generated/numpy.s_.html - person user_007; 16.07.2019

อีกวิธีหนึ่งคือการใช้อาร์เรย์ที่สวมหน้ากาก:

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 ส่งคืนอาร์เรย์ 2 มิติพร้อมกับคอลัมน์ใดๆ ที่มีค่ามาสก์ที่ถูกระงับ:

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

ดู manipulating-a-maskedarray

person unutbu    schedule 29.10.2009

สิ่งนี้จะสร้างอาร์เรย์อื่นโดยไม่มีคอลัมน์เหล่านั้น:

  b = a.compress(logical_not(z), axis=1)
person Paul    schedule 29.10.2009
comment
เย็น. ฉันหวังว่าไวยากรณ์ของ Matlab จะทำงานที่นี่: a(:,z) = [] ง่ายกว่ามาก - person Boris Gorelik; 29.10.2009
comment
@bpowah: แน่นอน วิธีทั่วไปมากกว่าคือ b = a[:,z] คุณอาจต้องการอัปเดตคำตอบของคุณตามนั้น - person Boris Gorelik; 29.10.2009

จากเอกสาร 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])
person Parag Gupta    schedule 10.11.2013

ในสถานการณ์ของคุณ คุณสามารถดึงข้อมูลที่ต้องการด้วย:

a[:, -z]

"-z" คือการปฏิเสธเชิงตรรกะของอาร์เรย์บูลีน "z" นี่เป็นเช่นเดียวกับ:

a[:, logical_not(z)]
person Olivier    schedule 16.10.2011

การลบคอลัมน์เมทริกซ์ที่มี 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.]]
person Uki D. Lucas    schedule 13.11.2016
comment
จริงๆแล้วฉันไม่ได้ติดตามคุณ รหัสนี้ทำงานอย่างไร? - person RamenChef; 14.11.2016