Q:

Write a NumPy program to add a border (filled with 0's) around an existing array

0

Write a NumPy program to add a border (filled with 0's) around an existing array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
x = np.ones((3,3))
print("Original array:")
print(x)
print("0 on the border and 1 inside in the array")
x = np.pad(x, pad_width=1, mode='constant', constant_values=0)
print(x)

Sample Output:

Original array:                                                         
[[ 1.  1.  1.]                                                          
 [ 1.  1.  1.]                                                          
 [ 1.  1.  1.]]                                                         
1 on the border and 0 inside in the array                               
[[ 0.  0.  0.  0.  0.]                                                  
 [ 0.  1.  1.  1.  0.]                                                  
 [ 0.  1.  1.  1.  0.]                                                  
 [ 0.  1.  1.  1.  0.]                                                  
 [ 0.  0.  0.  0.  0.]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now