numpy-100 日本語翻訳 (41-60)

numpy100 を翻訳したので100 numpy exercises (日本語翻訳版)、ブログにしました


41. np.sumより高速に小さい配列を集計する方法は? (★★☆)

 

答え
Z = np.arange(10)
%timeit np.add.reduce(Z)
%timeit Z.sum()
3.69 µs ± 162 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
6.33 µs ± 362 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

 

42. A と B の2つの乱数配列があるとき、それらが等しいかをチェックする (★★☆)

 

答え
A = np.random.randint(0,2,5)
B = np.random.randint(0,2,5)
# Assuming identical shape of the arrays and a tolerance for the comparison of values
equal = np.allclose(A,B)
print(equal)
# Checking both the shape and the element values, no tolerance (values have to be exactly equal)
equal = np.array_equal(A,B)
print(equal)
False
False

 

43. イミュータブル(読み取り専用)の配列を生成する (★★☆)

 

答え
Z = np.zeros(10)
Z.flags.writeable = False
Z[0] = 1
      1 Z = np.zeros(10)
      2 Z.flags.writeable = False
----> 3 Z[0] = 1

ValueError: assignment destination is read-only

 

44. 直交座標で 10x2 行列があるとき、それらを極座標に変換する (★★☆)

 

答え
Z = np.random.random((10,2))
X, Y = Z[:,0], Z[:,1]
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)
[0.87681217 0.3161743  0.90135585 0.6531784  0.64847374 0.68742857
 0.84485734 1.12235679 0.62017815 1.0990964 ]
[0.38481736 1.46375703 0.99300613 0.22501077 1.3079256  0.5497876
 0.44970742 0.92537148 1.02306295 1.064497  ]

 

45. 大きさ 10 の乱数のベクトルを生成して、その最大値を 0 に置き換える (★★☆)

 

答え
Z = np.random.random(10)
Z[Z.argmax()] = 0
print(Z)
[0.29018266 0.2156002  0.68783096 0.51589855 0.10440006 0.10542743
 0.5956617  0.06487708 0.         0.3238788 ]

 

46. [0,1]x[0,1] 領域を網羅する `x` 座標と `y` 座標を持つ構造化配列を生成する (★★☆)

 

答え
Z = np.zeros((5,5), [('x',float),('y',float)])
Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,5),
                            np.linspace(0,1,5))
print(Z)
[[(0.  , 0.  ) (0.25, 0.  ) (0.5 , 0.  ) (0.75, 0.  ) (1.  , 0.  )]
 [(0.  , 0.25) (0.25, 0.25) (0.5 , 0.25) (0.75, 0.25) (1.  , 0.25)]
 [(0.  , 0.5 ) (0.25, 0.5 ) (0.5 , 0.5 ) (0.75, 0.5 ) (1.  , 0.5 )]
 [(0.  , 0.75) (0.25, 0.75) (0.5 , 0.75) (0.75, 0.75) (1.  , 0.75)]
 [(0.  , 1.  ) (0.25, 1.  ) (0.5 , 1.  ) (0.75, 1.  ) (1.  , 1.  )]]

 

47. 2つの配列 X と Y が与えられたとき、コーシー行列 C (Cij =1/(xi - yj)) を作成する

 

答え
# Author: Evgeni Burovski
X = np.arange(8)
Y = X + 0.5
C = 1.0 / np.subtract.outer(X, Y)
print(np.linalg.det(C))
3638.163637117973

 

48. numpy スカラー型のそれぞれについて、表現可能な最小値と最大値を表示する (★★☆)

 

答え
for dtype in [np.int8, np.int32, np.int64]:
   print(np.iinfo(dtype).min)
   print(np.iinfo(dtype).max)
for dtype in [np.float32, np.float64]:
   print(np.finfo(dtype).min)
   print(np.finfo(dtype).max)
   print(np.finfo(dtype).eps)
-128
127
-2147483648
2147483647
-9223372036854775808
9223372036854775807
-3.4028235e+38
3.4028235e+38
1.1920929e-07
-1.7976931348623157e+308
1.7976931348623157e+308
2.220446049250313e-16

 

49. 配列のすべての値を表示する方法は? (★★☆)

 

答え
np.set_printoptions(threshold=np.nan)
Z = np.zeros((16,16))
print(Z)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

 

50. ベクトルの中で指定したスカラーに最も近い値を見つける方法は? (★★☆)

 

答え
Z = np.arange(100)
v = np.random.uniform(0,100)
index = (np.abs(Z-v)).argmin()
print(Z[index])
27

 

51. 位置 (x,y) と 色 (r,g,b) を表現する構造化配列を生成する (★★☆)

 

答え
Z = np.zeros(10, [ ('position', [ ('x', float, 1),
                                 ('y', float, 1)]),
                  ('color',    [ ('r', float, 1),
                                 ('g', float, 1),
                                 ('b', float, 1)])])
print(Z)
[((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
 ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
 ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
 ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
 ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))]

 

52. shape属性が (100,2) の座標を表現する乱数のベクトルがあるとき、各点ごとの距離を求める (★★☆)

 

答え
Z = np.random.random((10,2))
print(Z)
X,Y = np.atleast_2d(Z[:,0], Z[:,1])
print(X)
print(Y)
D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
print(D)
# Much faster with scipy
import scipy
# Thanks Gavin Heverly-Coulson (#issue 1)
import scipy.spatial
Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z)
#print(D)
[[0.7980893  0.33239862]
 [0.97533603 0.42018656]
 [0.85555926 0.98653372]
 [0.48482279 0.0796708 ]
 [0.16214624 0.86730474]
 [0.2950525  0.74110581]
 [0.48556481 0.85537827]
 [0.40602013 0.21004549]
 [0.5410404  0.14348235]
 [0.51723398 0.67408857]]
0.7980893  0.97533603 0.85555926 0.48482279 0.16214624 0.2950525
  0.48556481 0.40602013 0.5410404  0.51723398
0.33239862 0.42018656 0.98653372 0.0796708  0.86730474 0.74110581
  0.85537827 0.21004549 0.14348235 0.67408857
[[0.         0.19779567 0.6566548  0.40250126 0.83099226 0.64814164
  0.60924484 0.41071708 0.31900391 0.44230276]
 [0.19779567 0.         0.57887441 0.59712162 0.92800448 0.75218006
  0.65518522 0.60686066 0.51495429 0.52375922]
 [0.6566548  0.57887441 0.         0.97971726 0.70358878 0.61188454
  0.39255273 0.89722874 0.89980983 0.46052792]
 [0.40250126 0.59712162 0.97971726 0.         0.85116825 0.68811993
  0.77570783 0.15233981 0.08504313 0.59530075]
 [0.83099226 0.92800448 0.70358878 0.85116825 0.         0.18327641
  0.32363839 0.70104508 0.81699427 0.40425214]
 [0.64814164 0.75218006 0.61188454 0.68811993 0.18327641 0.
  0.22215565 0.54253007 0.64626918 0.23206878]
 [0.60924484 0.65518522 0.39255273 0.77570783 0.32363839 0.22215565
  0.         0.6502167  0.71405416 0.18403503]
 [0.41071708 0.60686066 0.89722874 0.15233981 0.70104508 0.54253007
  0.6502167  0.         0.15053612 0.47718393]
 [0.31900391 0.51495429 0.89980983 0.08504313 0.81699427 0.64626918
  0.71405416 0.15053612 0.         0.53114001]
 [0.44230276 0.52375922 0.46052792 0.59530075 0.40425214 0.23206878
  0.18403503 0.47718393 0.53114001 0.        ]]

 

53. 浮動小数点 (32 bits) 配列を整数型 (32 bits) 配列に変換する方法は?

 

答え
Z = np.arange(10, dtype=np.float32)
Z = Z.astype(np.int32, copy=False)
print(Z)
[0 1 2 3 4 5 6 7 8 9]

 

54. 以下のファイル(文字列)を読む方法は? (★★☆)

 

 1, 2, 3, 4, 5
 6,  ,  , 7, 8
  ,  , 9,10,11

 

 

答え
from io import StringIO
# Fake file
s = StringIO("""1, 2, 3, 4, 5\n
               6,  ,  , 7, 8\n
                ,  , 9,10,11\n""")
Z = np.genfromtxt(s, delimiter=",", dtype=np.int)
print(Z)
[[ 1  2  3  4  5]
 [ 6 -1 -1  7  8]
 [-1 -1  9 10 11]]

 

55. numpy 配列で enumerate に相当するものは何ですか?

 

答え
Z = np.arange(9).reshape(3,3)
for index, value in np.ndenumerate(Z):
   print(index, value)
for index in np.ndindex(Z.shape):
   print(index, Z[index])
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8

 

56. 一般的な2次元正規分布の配列を生成する (★★☆)

 

答え
X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
D = np.sqrt(X*X+Y*Y)
sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
print(G)
[[0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
  0.57375342 0.51979489 0.44822088 0.36787944]
 [0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
  0.69905581 0.63331324 0.54610814 0.44822088]
 [0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
  0.81068432 0.73444367 0.63331324 0.51979489]
 [0.57375342 0.69905581 0.81068432 0.89483932 0.9401382  0.9401382
  0.89483932 0.81068432 0.69905581 0.57375342]
 [0.60279818 0.73444367 0.85172308 0.9401382  0.98773022 0.98773022
  0.9401382  0.85172308 0.73444367 0.60279818]
 [0.60279818 0.73444367 0.85172308 0.9401382  0.98773022 0.98773022
  0.9401382  0.85172308 0.73444367 0.60279818]
 [0.57375342 0.69905581 0.81068432 0.89483932 0.9401382  0.9401382
  0.89483932 0.81068432 0.69905581 0.57375342]
 [0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
  0.81068432 0.73444367 0.63331324 0.51979489]
 [0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
  0.69905581 0.63331324 0.54610814 0.44822088]
 [0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
  0.57375342 0.51979489 0.44822088 0.36787944]]

 

57. 2次元配列中に p 個の要素をランダムに配置する方法は? (★★☆)

 

答え
# Author: Divakar
n = 10
p = 3
Z = np.zeros((n,n))
np.put(Z, np.random.choice(range(n*n), p, replace=False),1)
print(Z)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [1. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

 

58. 行列の各行からその平均を減算する (★★☆)

 

答え
# Author: Warren Weckesser
X = np.random.rand(5, 10)
# Recent versions of numpy
Y = X - X.mean(axis=1, keepdims=True)
# Older versions of numpy
Y = X - X.mean(axis=1).reshape(-1, 1)
print(Y)
[[ 0.17467156  0.39610569 -0.15474385 -0.10768295 -0.30852725 -0.23045708
   0.30775199 -0.20708909  0.31331994 -0.18334896]
 [-0.34197514 -0.25747791  0.20978006  0.29652348  0.1252613  -0.01937976
   0.19479692 -0.36073304  0.28232472 -0.12912062]
 [-0.32253107  0.12600989 -0.47445115  0.27678715 -0.04236603 -0.49955442
   0.3475865   0.33831996 -0.04925552  0.29945469]
 [-0.32450816 -0.0818643   0.29232411  0.31703995  0.54894657 -0.31751116
   0.48092611 -0.3087898  -0.33459605 -0.27196727]
 [-0.30280297 -0.23644529 -0.20628806 -0.30144542  0.25597645 -0.01116676
   0.45472822  0.0503685  -0.30044417  0.5975195 ]]

 

59. 配列を n 番目の列でソートする方法は? (★★☆)

 

答え
# Author: Steve Tjoa
Z = np.random.randint(0,10,(3,3))
print(Z)
print(Z[Z[:,1].argsort()])
[[0 3 9]
 [8 0 7]
 [8 3 1]]
[[8 0 7]
 [0 3 9]
 [8 3 1]]

 

60. 与えられた2次元配列に全てがゼロの列があるかを調べる方法は? (★★☆)

 

答え
# Author: Warren Weckesser
Z = np.random.randint(0,3,(3,10))
print(Z)
print((~Z.any(axis=0)).any())
[[1 2 2 2 2 2 0 0 1 2]
 [2 1 2 1 1 2 1 0 2 2]
 [2 1 1 0 0 1 1 0 2 1]]
True