The first job (numpy practice)

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 as np
a = np.ones((10, 10))
for i in range(1, 9):
    for j in range(1, 9):
        a[i][j] = 0
print(a)
import numpy as np
A=np.ones((10,10))
A[1:-1,1:-1] = 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.zeros((5, 5))
for i in range(0, 5):
    a[i][i] = i+1
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 intermediate

 

import numpy as np
a = np.random.rand(5, 5)
amax, amin = np.max(a), np.min(a)
a = a-amin
a = a/(amax-amin)
print(a)

Guess you like

Origin www.cnblogs.com/cai2019/p/11565221.html