Good ways to “expand” a numpy ndarray?

后端 未结 6 669
一个人的身影
一个人的身影 2020-12-04 16:43

Are there good ways to \"expand\" a numpy ndarray? Say I have an ndarray like this:

[[1 2]
 [3 4]]

And I want each row to contains more ele

6条回答
  •  我在风中等你
    2020-12-04 17:15

    You can use numpy.pad, as follows:

    >>> import numpy as np
    >>> a=[[1,2],[3,4]]
    >>> np.pad(a, ((0,0),(0,3)), mode='constant', constant_values=0)
    array([[1, 2, 0, 0, 0],
           [3, 4, 0, 0, 0]])
    

    Here np.pad says, "Take the array a and add 0 rows above it, 0 rows below it, 0 columns to the left of it, and 3 columns to the right of it. Fill these columns with a constant specified by constant_values".

提交回复
热议问题