Numpy remove a dimension from np array

前端 未结 5 1463
南方客
南方客 2020-12-14 15:02

I have some images I want to work with, the problem is that there are two kinds of images both are 106 x 106 pixels, some are in color and some are black and white.

5条回答
  •  遥遥无期
    2020-12-14 15:45

    Actually np.delete would work if you would apply it two times, if you want to preserve the first channel for example then you could run the following:

    Xtrain = np.delete(Xtrain,2,2) # this will get rid of the 3rd component of the 3 dimensions
    print(Xtrain.shape) # will now output (106,106,2)
    # again we apply np.delete but on the second component of the 3rd dimension
    Xtrain = np.delete(Xtrain,1,2)
    print(Xtrain.shape) # will now output (106,106,1)
    # you may finally squeeze your output to get a 2d array
    Xtrain = Xtrain.squeeze()
    print(Xtrain.shape) # will now output (106,106)
    

提交回复
热议问题