Error summary and grammar summary after writing Numpy 100 basic exercises

Foreword: Because of watching videos for learning, there are many grammars that do not appear in the videos in the writing of these 100 topics. However, many grammars are relatively simple to use directly, and there are many less looping sentences. So far, I have written this error summary and grammar summary, I hope I can give some help to my friends, because the bloggers have helped you step on the thunder! !

        1、Create a null vector of size 10 (★☆☆)')

                Create an empty vector of size 10

There are four solutions to this problem: The syntax used is as follows

① np.empty constructs an uninitialized array of
size shape ②np.zeros constructs an array of all 0s of
size ③np.ones constructs an array of all 1s of size shape ④np.full constructs an array of
size shape Fill with the specified value full array

print(np.empty(10))
print(np.ones(10))
print(np.zeros(10))
print(np.full((2,5),5.0))

 

What we need to pay attention to is that after filling with the np.full syntax, and using np.empty to construct an uninitialized array with a size of shape, we will find that the constructed function is still the content created with the full statement, so we need to finish the creation. initialize after

 

          2、Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)')

                Construct a 3x3 matrix with 9 numbers 0-8

There are 2 solutions to this problem: The syntax used is as follows

np.arange().reshape(3,3)

np.arange().reshape((3,-1))

x3=np.arange(0,9).reshape(3,3)
print(x3)

sample3 = np.arange(9).reshape((3, -1))
print(sample3)

 When I first saw the second syntax, I thought it was very strange. I didn't know about -1 very well. Let's briefly introduce the nature of reshape:

The reshape function is to perform dimension transformation on the data structure of narray. Since the transformation follows the constant number of object elements, during transformation, it is assumed that the total number of elements of a data object narray is N, if we give a dimension of (m, -1), we understand that the object is transformed into a two-dimensional matrix, the size of the first dimension of the matrix is ​​m, and the size of the second dimension is N/m

So it is also satisfied when it is (3, -1)!

        3、Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)')

                Find the subscript of the non-zero element from the array [1,2,0,0,4,0]

There are 2 solutions to this problem: The syntax used is as follows

①Using loop nested statements

②Use your own grammar directly

x3=np.array([1,2,0,0,4,0])
print(x3)
for i in range(len(x3)):
    if x3[i]!=0:
        print(i)
        
print(np.nonzero([1,2,0,0,4,0]))

 

Here I made a big mistake, I used inertial thinking to use loop nested statements to find non-zero elements. Just call the syntax that comes with numpy directly! ! ! Hope you guys don't make the same low-level mistakes as me! ! !

        4、Create a 3x3 identity matrix (★☆☆)')

                Create a 3x3 diagonal matrix

There are 3 solutions to this problem: The syntax used is as follows

①np.identity

②np.eye

③np.diag

##“对角矩阵(diagonal matrix)是一个主对角线之外的元素皆为0的矩阵


# identity 只能创建方阵,eye要灵活一些,可以创建NxM的矩阵,也可以控制对角线的位置

print(np.identity(3))
print('-----------------------------------')
print(np.eye(3,3,0)) #默认第一个和第二个参数相等,第三个参数为对角线位置
np.diag([1,1,1])

 After so long in the postgraduate entrance examination, I have forgotten what a diagonal matrix is, and the postgraduate exam is over! ! Here is also a brief introduction to you:

A diagonal matrix is ​​a matrix whose elements outside the main diagonal are all 0 , often written as diag(a1, a2,...,an). Diagonal matrix can be considered as the simplest kind of matrix. It is worth mentioning that the elements on the diagonal can be 0 or other values. A diagonal matrix with equal elements on the diagonal is called a quantity matrix ; A diagonal matrix with all 1s on the line is called the identity matrix . Operations on diagonal matrices include sum, difference, number multiplication, and product operations on diagonal matrices of the same order, and the result is still a diagonal matrix.

  identity can only create a square matrix, eye is more flexible, can create an NxM matrix, and can also control the position of the diagonal

So the blogger's suggestion is that we should use the eye function more to create a matrix flexibly! !

        5、Create a 3x3x3 array with random values (★☆☆)')

                Create a 3x3x3 matrix with random numbers

x4=np.random.random((3,3,3))
x4

What we need to pay attention to is that the random function can be used directly to call different types

X=np.random.randint((3,3,3))
X

 At first, the floating point numbers created by double random did not look very neat, so I wanted to use random.randint to create an array of integers, and found that the one-dimensional array was created. At this time, we want to create an integer array. We can use the ceil and floor functions to round up and down! !

Guess you like

Origin blog.csdn.net/qq_46044325/article/details/126734780