1st 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 NP AS 
A = np.ones ((10,10), int) # Create a 10 rows and 10 columns, all the elements of the array 1, the data type int 
A [1: 9,1:. 9] = 0 # the second row to the ninth row second column to the ninth column into all 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 NP AS 
A = np.zeros ((4,4 &), int) # Create a four rows and four columns, the array elements are all 0, the data type int 
for X in Range (. 5 ):
     for Y in Range ( . 5 ):
         IF (X == Y): 
            A [X -1] [-Y. 1] = X 
 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 NP AS 
A = np.random.rand (5, 5) # create a random 5x5 array 
a [a.max A == ()] = 1   # in the array to a maximum value represented by 
a [ a.min == a ()] = 0   # the minimum value of the array is represented by 0 
Print (a)

Guess you like

Origin www.cnblogs.com/huangyanhuang/p/11573045.html