Changing values on the edges of an array in NumPy

后端 未结 3 780
孤独总比滥情好
孤独总比滥情好 2021-01-20 07:29

I am to create an array using only NumPy tools. There it is:

[[2 2 2 2 2]
 [2 1 1 1 2]
 [2 1 1 1 2]
 [2 1 1 1 2]
 [2 2 2 2 2]]

That is my

3条回答
  •  没有蜡笔的小新
    2021-01-20 08:23

    import numpy as np
    
    a = np.ones((5, 5))
    b = np.pad(a[1:-1,1:-1], pad_width=((1, 1), (1, 1)), mode='constant', 
    constant_values=2)
    print b
    

提交回复
热议问题