100 numpy exercises(21 – 30)

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

今回は21問目から30問目までです。

21. Create a checkerboard 8×8 matrix using the tile function.

Z = np.tile(np.array([[0,1],[1,0]]), (4,4))
print(Z)
[[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]]

メソッド「tile」は与えた配列をタイル状に並べてくれます。

22. Normalize a 5×5 random matrix.

Z = np.random.random((5,5))
Z = (Z - np.mean (Z)) / (np.std (Z))
print(Z)
[[-1.15386639  0.25349436 -1.34700028  1.50198517 -0.87798254]
 [ 0.66758545  0.69862527  1.18777135 -0.76942776 -0.86469872]
 [ 0.80640373  0.87257176 -0.80934667  1.20730198  0.10682863]
 [-1.34667656 -1.34467025  1.19164124 -0.27454829 -1.82187067]
 [ 0.86900007 -0.64711533  0.230963    1.32989505  0.33313639]]

23. Create a custom dtype that describes a color as four unsigned bytes (RGBA).

color = np.dtype([("r", np.ubyte),("g", np.ubyte),("b", np.ubyte),("a", np.ubyte)])

24. Multiply a 5×3 matrix by a 3×2 matrix (real matrix product).

Z = np.dot(np.ones((5,3)), np.ones((3,2)))
print(Z)

Z = np.ones((5,3)) @ np.ones((3,2))
print(Z)
[[3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]]

[[3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]]

メソッド「dot」は行列の掛け算をしてくれます。

25. Given a 1D array, negate all elements which are between 3 and 8, in place.

Z = np.arange(11)
Z[(3 < Z) & (Z < 8)] *= -1
print(Z)
[ 0  1  2  3 -4 -5 -6 -7  8  9 10]

26. What is the output of the following script?

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
9
10

27. Consider an integer vector Z, which of these expressions are legal?

Z = np.arange(5)

print(Z**Z)
print(2 << Z >> 2)
print(Z <- Z)
print(1j*Z)
print(Z/1/1)
print(Z<Z>Z)
[  1   1   4  27 256]
[0 1 2 4 8]
[False False False False False]
[0.+0.j 0.+1.j 0.+2.j 0.+3.j 0.+4.j]
[0. 1. 2. 3. 4.]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-6af43e8cc83e> in <module>()
      5 print(1j*Z)
      6 print(Z/1/1)
----> 7 print(Z<Z>Z)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

28. What are the result of the following expressions?

np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide
  """Entry point for launching an IPython kernel.
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in floor_divide
  
array([-9.22337204e+18])

29. How to round away from zero a float array?

Z = np.random.uniform(-10,+10,10)
print(np.copysign(np.ceil(np.abs(Z)), Z))
[  8.  -9. -10.   8.   5.   2.   4.   9.   7.   3.]

30. How to find common values between two arrays?

Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print(np.intersect1d(Z1,Z2))
[0 6 7 8]

次回は31〜40問目に挑戦してみます。