1. Create a value of 1 and the inner boundary of the array is 0, the following legend:
[Note:] solving this problem can first of all values are set to 1, this is a big square; Next, the setting of all boundaries except small square 0.
The title uses a slice of numpy principle. X multidimensional arrays follow the same principle [start: step: stop] is.
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0.0 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Import numpy NP AS A = np.ones ((10,10),) # Create 10 rows and 10 columns, all the elements of an array a [1: 9:, [ 1,2,3,4,5,6, 7,8]] = 0 # to all boundaries except small square to 0 Print (A)
2. Create a value on the main diagonal array 1,2,3,4 5x5 matrix, the following legend:
[1000]
[0200]
[0030]
[0004 ]
import numpy as np a = np.array([[1,0,0,0],[0,2,0,0],[0,0,3,0],[0,0,0,4]]) print(a)
3. The operation of the normalization array
generating a random 5 * 5 matrix, to find the maximum and minimum values, and the maximum and minimum values 1 and 0, respectively, said other values between 0 and 1 in the middle.
import numpy as np a= np.random.rand(5,5) print(a) print(np.max(a)) print(np.min(a)) a[a == a.max()] = 1 a[a == a.min()] = 0 print(a)