how to modify a 2D numpy array at specific locations without a loop?

前端 未结 4 1450
迷失自我
迷失自我 2020-12-09 15:01

I have a 2D numpy array and I have a arrays of rows and columns which should be set to a particular value. Lets consider the following example

 a = array([[1         


        
相关标签:
4条回答
  • 2020-12-09 15:36

    It should be as simple as a[0,2]=0 and a[1,2]=0. You could also do a[0:2,2]=0. The ':' based range indexing in python is a half-open interval [0,2) which actually range from 0 to 1 (the end point of 2 is not included).

    0 讨论(0)
  • 2020-12-09 15:45

    First off, your description of the "correct" array doesn't match the columns and rows you specify...

    To get your "correct" array, you'd do this:

    a[:2, 2] = 0
    

    To modify the second and third columns of the first and third rows, (rows [0,2] and columns [1,2]) you'd do what you're doing... (Your description of modifying rows [0,2] and columns [1,2] is exactly the result you get, right?)

    0 讨论(0)
  • 2020-12-09 15:48

    Adding to what others have said, you can modify these elements using fancy indexing as follows:

    In [39]: rows = [0,1]
    
    In [40]: cols = [2,2]
    
    In [41]: a = np.arange(1,10).reshape((3,3))
    
    In [42]: a[rows,cols] = 0
    
    In [43]: a
    Out[43]: 
    array([[1, 2, 0],
           [4, 5, 0],
           [7, 8, 9]])
    

    You might want to read the documentation on indexing multidimensional arrays: http://docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing-multi-dimensional-arrays

    The key point is:

    if the index arrays have a matching shape, and there is an index array for each dimension of the array being indexed, the resultant array has the same shape as the index arrays, and the values correspond to the index set for each position in the index arrays.

    Importantly this also allows you to do things like:

    In [60]: a[rows,cols] = np.array([33,77])
    
    In [61]: a
    Out[61]: 
    array([[ 1,  2, 33],
           [ 4,  5, 77],
           [ 7,  8,  9]])
    

    where you can set each element independently using another array, list or tuple of the same size.

    0 讨论(0)
  • 2020-12-09 15:51

    one work around: ndarray.flatten, np.put(), ndarray.reshape

    try ndarray.flatten(array), that way you are dealing with a one dim array which can be manipulated with numpy.put(array,[indices],[values]). Then use ndarray.reshape() to get to the original dimensions.

    0 讨论(0)
提交回复
热议问题