11. Create a 3x3 identity matrix (★☆☆)
mat1 = np.identity(3)
mat2 = np.eye(3)
print(mat1)
print(mat2)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
12. Create a 3x3x3 array with random values (★☆☆)
mat = np.random.random((3, 3, 3))
print(mat)
[[[0.5282567 0.309261 0.76337568]
[0.43948225 0.90048663 0.58643259]
[0.20646 0.56080675 0.1688716 ]]
[[0.50961462 0.17716085 0.85752585]
[0.8406967 0.99543453 0.43732824]
[0.50370789 0.99516363 0.19173698]]
[[0.47938892 0.96097005 0.1733774 ]
[0.56713231 0.21205097 0.19300557]
[0.5208537 0.78577222 0.15337698]]]
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
mat = np.random.random((10, 10))
print('minimum value: {}'.format(np.round(mat.min(), 4)))
print('maximum value: {}'.format(np.round(mat.max(), 4)))
minimum value: 0.0096
maximum value: 0.9994
14. Create a random vector of size 30 and find the mean value (★☆☆)
vec = np.random.random(30)
print(vec)
print(vec.mean())
[0.76323043 0.54033623 0.94991599 0.78204597 0.24917805 0.3770417
0.9129393 0.75447057 0.1387152 0.36906972 0.72154508 0.7217678
0.08403568 0.2067085 0.90654192 0.706014 0.61524905 0.07674507
0.04043784 0.30408746 0.6400086 0.32194014 0.86346487 0.01618079
0.76605111 0.0030094 0.13585649 0.1082499 0.12093449 0.17011285]
0.445529473449048
15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
mat = np.ones((5, 5))
print(mat)
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
mat[1:-1, 1:-1] = 0
print(mat)
[[1. 1. 1. 1. 1.]
[1. 0. 0. 0. 1.]
[1. 0. 0. 0. 1.]
[1. 0. 0. 0. 1.]
[1. 1. 1. 1. 1.]]
16. How to add a border (filled with 0's) around an existing array? (★☆☆)
mat = np.ones((3, 3))
print(mat)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
mat = np.pad(mat, pad_width=1, mode='constant', constant_values=0)
print(mat)
[[0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]]
17. What is the result of the following expression? (★☆☆)
0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1
print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)
nan
False
False
nan
True
False
18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
mat = np.diag([1, 2, 3, 4], k=-1)
print(mat)
[[0 0 0 0 0]
[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]]
19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
mat = np.tile(np.array([[0, 1], [1, 0]]), (4, 4))
print(mat)
mat = np.zeros((8, 8), dtype=int)
mat[::2, 1::2] = 1
mat[1::2, ::2] = 1
print(mat)
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
print(np.unravel_index(100, (6, 7, 8)))
print(7*8 + 8*5 + 4)
(1, 5, 4)
100
'繩鋸木斷水滴石穿 > Python' 카테고리의 다른 글
[pandas] 딕셔너리를 데이터프레임의 열로 변경하는 방법 (0) | 2021.06.07 |
---|---|
[pandas] object와 category의 차이 (0) | 2021.06.04 |
[100 Numpy exercies] 1번 ~ 10번 (0) | 2021.01.30 |
[Python] 값을 변경하여 새로운 칼럼을 만드는 방법 (0) | 2020.06.12 |
Pandas 기초 - 요약하기 (0) | 2020.04.15 |