Writting in sub-ndarray of a ndarray in the most pythonian way. Python 2

前端 未结 2 1626
有刺的猬
有刺的猬 2021-01-25 20:28

I have a ndarray like this one:

number_of_rows = 3
number_of_columns = 3
a = np.arange(number_of_rows*number_of_columns).reshape(number_of_rows,number_of_columns         


        
相关标签:
2条回答
  • 2021-01-25 20:57

    For the continuous rows and columns case, you can use basic slicing like this:

    In [634]: a
    Out[634]: 
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    In [635]: b = np.asarray([[100, 101],[102, 103]])
    
    In [636]: a[:rows_to_keep[1]+1, columns_to_keep[0]:] = b
    
    In [637]: a
    Out[637]: 
    array([[  0, 100, 101],
           [  3, 102, 103],
           [  6,   7,   8]])
    

    0 讨论(0)
  • 2021-01-25 21:08

    Indexing with lists like [1,2] is called advanced indexing. By itself it produces a copy, not a view. You have to use one indexing expression, not two to assign or change values. That is a[[1,2],:] is a copy, a[[1,2],:][:,[1,2]] += 100 modifies that copy, not the original a.

    In [68]: arr = np.arange(12).reshape(3,4)
    

    Indexing with slices; this is basic indexing:

    In [69]: arr[1:,2:]
    Out[69]: 
    array([[ 6,  7],
           [10, 11]])
    
    In [70]: arr[1:,2:] += 100
    
    In [71]: arr
    Out[71]: 
    array([[  0,   1,   2,   3],
           [  4,   5, 106, 107],
           [  8,   9, 110, 111]])
    

    Doing the same indexing with lists requires arrays that 'broadcast' against each other. ix_ is a handy way of generating these:

    In [73]: arr[np.ix_([1,2],[2,3])]
    Out[73]: 
    array([[106, 107],
           [110, 111]])
    
    In [74]: arr[np.ix_([1,2],[2,3])] -= 100
    
    In [75]: arr
    Out[75]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    

    Here's what ix_ produces - a tuple of arrays, one is (2,1) in shape, the other (1,2). Together they index a (2,2) block:

    In [76]: np.ix_([1,2],[2,3])
    Out[76]: 
    (array([[1],
            [2]]), array([[2, 3]]))
    
    0 讨论(0)
提交回复
热议问题