100 numpy exercises(11 – 20)

numpyの理解を深めるために100問エクササイズに挑戦してみました。

今回は11問目から20問目までです。

11. Create a 3×3 identity matrix.

Z = np.eye(3)
print(Z)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

メソッド「eye」は単位行列を返してくれます。

12. Create a 3x3x3 array with random values.

Z = np.random.random((3,3,3))
print(Z)
[[[0.28179706 0.61269354 0.57068999]
  [0.30147387 0.08340296 0.99637681]
  [0.18105145 0.14915956 0.91383341]]

 [[0.33035667 0.97654906 0.52470362]
  [0.24024819 0.93350079 0.02700881]
  [0.67909582 0.54869667 0.65991305]]

 [[0.97400089 0.23038993 0.75927998]
  [0.63940624 0.12074749 0.15649112]
  [0.61289098 0.02951947 0.58659009]]]

13. Create a 10×10 array with random values and find the minimum and maximum values.

Z = np.random.random((10,10))
print(Z)

Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)
[[0.64392705 0.83492077 0.27264589 0.42881687 0.7244463  0.99072729
  0.94407225 0.79532065 0.29647308 0.62324418]
 [0.62572497 0.44826571 0.53233306 0.49070273 0.62623473 0.79905776
  0.57759036 0.13203692 0.34427921 0.03872049]
 [0.31038691 0.85001065 0.61958595 0.68969289 0.42009286 0.84885672
  0.6089173  0.57469812 0.71988665 0.27145373]
 [0.97409662 0.97469953 0.67395586 0.27074655 0.79450774 0.73889172
  0.67665032 0.54839815 0.56454792 0.01955364]
 [0.06158619 0.71380028 0.26981281 0.86208451 0.82524166 0.9932261
  0.96900087 0.80296961 0.17120421 0.5658327 ]
 [0.50416781 0.90504324 0.43878456 0.96244882 0.41210544 0.8236831
  0.6291091  0.27588851 0.76782772 0.59724563]
 [0.31987041 0.97677942 0.57861599 0.55703751 0.81814699 0.43641211
  0.42235956 0.36600973 0.51098928 0.6025165 ]
 [0.05551759 0.16369124 0.85389294 0.29025306 0.05023793 0.626482
  0.06458042 0.11289654 0.82003749 0.27499114]
 [0.34033722 0.75728809 0.39974825 0.53349716 0.55773867 0.77543693
  0.82334188 0.76918694 0.25490894 0.62587044]
 [0.10302671 0.93618334 0.84126923 0.42683301 0.42873405 0.94147949
  0.81210301 0.04779431 0.56032324 0.92478197]]

0.019553643123644382 0.9932261038423082

14. Create a random vector of size 30 and find the mean value.

Z = np.random.random(30)
print(Z)

m = Z.mean()
print(m)
[0.73953571 0.04203395 0.85087589 0.24546228 0.46199432 0.09959345
 0.2901608  0.28424949 0.21887485 0.84774251 0.05566518 0.4043503
 0.52706835 0.02921733 0.6607664  0.55840846 0.77918918 0.85178871
 0.91151383 0.26134366 0.75315102 0.9717943  0.64199912 0.45422447
 0.59320079 0.17493613 0.85061903 0.86680601 0.58955521 0.03576795]

0.501729622926152

15. Create a 2d array with 1 on the border and 0 inside.

Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
[[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.]]

メソッド「ones」は1埋めの行列を生成してくれます。

16. How to add a border (filled with 0’s) around an existing array?

Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print(Z)
[[0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]

メソッド「pad」は行列の周りを特定の値でパディングしてくれます。

17. What is the result of the following expression?

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)
print(3 * 0.1)
nan
False
False
nan
True
False
0.30000000000000004

18. Create a 5×5 matrix with values 1,2,3,4 just below the diagonal.

Z = np.diag(1+np.arange(4),k=-1)
print(Z)
[[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]]

メソッド「diag」は対角行列を生成してくれる。kの値によって開始位置を変更できる。

19. Create a 8×8 matrix and fill it with a checkerboard pattern.

Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
print(Z)
Z[::2,1::2] = 1
print(Z)
[[0 0 0 0 0 0 0 0]
 [1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0]
 [1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0]
 [1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0]
 [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]
 [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(99,(6,7,8)))
(1, 5, 3)

メソッド「unravel_index」は(x,y,z)の行列の中でk番目がどこかを返してくれます。

次回は21〜30問目に挑戦してみます。