nndl01_numpy exercises

Posted by lbraine on Wed, 09 Feb 2022 15:41:34 +0100

numpy exercises

This article is only for my study notes and is deleted

The title comes from https://github.com/nndl/nndl.github.io

array operation of numpy

1. Import numpy Library

import numpy as np

2. Create a one-dimensional array A and initialize it to [4,5,6], (1) output the type of a, (2) output the size of each dimension of a, (3) output the first element of a (the value is 4)

a = np.array([4, 5, 6])
print(a.dtype)
print(a.shape)
print(a[0])
int64
(3,)
4

3. Create a two-dimensional array b and initialize it as [[4, 5, 6], [1, 2, 3]] (1) output the size of each dimension (shape) (2) output the three elements of b(0,0), b (0,1) and b (1,1) (the corresponding values are 4,5,2 respectively)

b = np.array( [[4, 5, 6],
              [1, 2, 3]])
print(b.shape)
print(b[0][0], b[0][1], b[1][1])
(2, 3)
4 5 2

4. (1) establish a full 0 matrix A with the size of 3x3; The type is integer (hint: dtype = int) (2) establish a full 1 matrix b with a size of 4x5; (3) Establish an identity matrix c with a size of 4x4; (4) Generate a random number matrix d with a size of 3x2

a = np.zeros((3, 3), dtype = int)
b = np.ones((4, 5))
c = np.identity((4))
d = np.random.randint(1, 10, size=(3, 2))

5. Create an array a, (the values are [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), (1) print a; (2) The output subscripts are the values of the two array elements (2,3), (0,0)

a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
print(a[2][3], a[0][0])
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
12 1

6. Put 0 to 1 rows and 2 to 3 columns of the a array in the previous question into b, (there is no need to create a new one here, just call it directly) (1) and output b;(2) Output the value of the (0,0) element of b

b = a[0:2, 2:4]
b
array([[3, 4],
       [7, 8]])

7. Put all the elements in the last two lines of array a in question 5 into c, (prompt: a[1:2,:]) (1) output c; (2) Output the last element of the first line in c (hint, use - 1 for the last element)

c = a[1:,]
print(c)
print(c[0][-1])

[[ 5  6  7  8]
 [ 9 10 11 12]]
8

8. Create array a, initialize a as [[1,2], [3,4], [5,6]], and output three elements (0,0) (1,1) (2,0) (prompt: use print (a [[0,1,2], [0,1,0]])

a = np.array([[1, 2], [3, 4], [5, 6]])
print(a[[0, 1, 2], [0, 1, 0]])
a

[1 4 5]





array([[1, 2],
       [3, 4],
       [5, 6]])

9. Establish matrix A, initialize to [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], output (0,0), (1,2), (2,0), (3,1) (prompt to use B = NP. Array ([0, 2, 0, 1]) print (a [NP. Range (4, b]))

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
print(a)
b = np.array([0, 2, 0, 1])
print(a[np.arange(4), b])
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[ 1  6  7 11]

10. Add 10 to each of the four elements output in 9, and then re output matrix A. (prompt: a [NP. Orange (4), b] + = 10)

a[np.arange(4), b] += 10 
a
array([[11,  2,  3],
       [ 4,  5, 16],
       [17,  8,  9],
       [10, 21, 12]])

Mathematical operation of array

11. Execute x = NP Array ([1, 2]), and then output the data type of X

x = np.array([1, 2])
x.dtype
dtype('int64')

12. Execute x = NP Array ([1.0, 2.0]), and then output the data class type of X

x = np.array([1.0, 2.0])
x.dtype
dtype('float64')

13. Execute x = NP array([[1, 2], [3, 4]], dtype=np.float64) ,y = np.array([[5, 6], [7, 8]], dtype=np.float64), and then output x+y, and NP add(x,y)

x = np.array([[1, 2], [3, 4]], dtype=np.float64) 
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(x+y)
print(np.add(x,y))
[[ 6.  8.]
 [10. 12.]]
[[ 6.  8.]
 [10. 12.]]

14. Use X and Y in topic 13 to output x-y and NP subtract(x,y)

print(x-y)
np.subtract(x,y)
[[-4. -4.]
 [-4. -4.]]





array([[-4., -4.],
       [-4., -4.]])

15. Use X and Y in topic 13 to output x*y and NP Multiply (x, y) and NP Dot (x, y), compare the differences. Then try another one that is not square array.

print(x*y)
print(np.multiply(x,y))
print(np.dot(x,y))
[[ 5. 12.]
 [21. 32.]]
[[ 5. 12.]
 [21. 32.]]
[[19. 22.]
 [43. 50.]]

16. Output x / y by using X and Y in 13 topics (tip: use the function np.divide())

print(x/y)
print(np.divide(x,y))
[[0.2        0.33333333]
 [0.42857143 0.5       ]]
[[0.2        0.33333333]
 [0.42857143 0.5       ]]

17. Use x in topic 13 to output the formula of x. (tip: use the function np.sqrt())

print(x**0.5)
print(np.sqrt(x))
[[1.         1.41421356]
 [1.73205081 2.        ]]
[[1.         1.41421356]
 [1.73205081 2.        ]]

18. Execute print(x.dot(y)) and print(np.dot(x,y)) using x,y in topic 13

print(x.dot(y))
print(np.dot(x,y))
[[19. 22.]
 [43. 50.]]
[[19. 22.]
 [43. 50.]]
19. Use X in TITLE 13 to sum. Prompt: output three kinds of summation: (1)print(np.sum(x)): (2)print(np.sum(x, axis = 0)); (3)print(np.sum(x,axis = 1))
print(x)
print(np.sum(x))
print(np.sum(x,axis =0 ))
print(np.sum(x,axis = 1))
[[1. 2.]
 [3. 4.]]
10.0
[4. 6.]
[3. 7.]

20. Use X in 13 questions to calculate the average (prompt: output three kinds of averages (1)print(np.mean(x)) (2)print(np.mean(x,axis = 0))(3) print(np.mean(x,axis =1)))

print(np.mean(x))
print(np.mean(x,axis=0))
print(np.mean(x,axis=1))
2.5
[2. 3.]
[1.5 3.5]

21. Use X in topic 13 to transpose the matrix of X, and then output the transposed result. (prompt: x.T indicates the transposition of x)

print(x)
print(x.T)
[[1. 2.]
 [3. 4.]]
[[1. 3.]
 [2. 4.]]

22. Use x in topic 13 to find the exponent of e (hint: function np.exp())

print(np.exp(x))
[[ 2.71828183  7.3890561 ]
 [20.08553692 54.59815003]]

23. Use X in 13 questions to evaluate the largest subscript (prompt (1) print (np.argmax (x)), (2) print (np.argmax (x, axis = 0)) (3) print (np.argmax (x), axis = 1))

print(x)
print(np.argmax(x))
print(np.argmax(x,axis=0))
print(np.argmax(x,axis=1))
[[1. 2.]
 [3. 4.]]
3
[1 1]
[1 1]

24. Draw, y=x*x, where x = NP Orange (0, 100, 0.1) (hint: matplotlib.pyplot library is used here)

import matplotlib.pyplot as plt
x = np.arange(0, 100, 0.1)
y = x*x
plt.plot(x,y)
plt.show()

[the external link image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-eTHD2j0y-1622088455940)(nndl01_files/nndl01_51_0.png)]

25. Draw. Draw sine function and cosine function, x = NP Orange (0, 3 * NP. PI, 0.1) (hint: NP. Sin () NP. Pi is used here Cos() function and Matplotlib Pyplot Library)

x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
y = np.cos(x)
plt.plot(x, y)
plt.show()

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-As1UFEro-1622088455944)(nndl01_files/nndl01_53_0.png)]

[the external chain picture transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG dgnoxnsn-1622088455951) (nndl01_files / nndl01_53_1. PNG)]

[external chain picture transferring... (img-eTHD2j0y-1622088455940)]

25. Draw. Draw sine function and cosine function, x = NP Orange (0, 3 * NP. PI, 0.1) (hint: NP. Sin () NP. Pi is used here Cos() function and Matplotlib Pyplot Library)

x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
y = np.cos(x)
plt.plot(x, y)
plt.show()

[external chain picture transferring... (img-as1uferro-1622088455944)]

[external chain picture transferring... (IMG dgnoxnsn-1622088455951)]

Topics: Python numpy