LoginSignup
0
0

More than 3 years have passed since last update.

for文を使わずに、numpy の掛け算でやってみよう

Posted at

for文を使いたくないけど、numpyでどう計算していいかわからない

ここで扱う計算は、色々な組み合わせをかけ合わせたい場合。それ以外はほかの記事を当たってください。

\boldsymbol A = \left(a_0, \dots, a_I  \right) \\
\boldsymbol B = \left(b_0, \dots, b_J  \right) \\
\boldsymbol C = \left(c_0, \dots, c_K  \right) \\

から

a_i b_j c_k

を計算したい場合

A = np.arange(2)+1
B = np.arange(3)+1
C = np.arange(4)+1
print("A=", A)
print("B=", B)
print("C=", C)

D = A[:, np.newaxis, np.newaxis] * B[:, np.newaxis] * C
print("\nD=", D) 
print("\nD.shape=", D.shape) 
A= [1 2]
B= [1 2 3]
C= [1 2 3 4]

D= [[[ 1  2  3  4]
  [ 2  4  6  8]
  [ 3  6  9 12]]

 [[ 2  4  6  8]
  [ 4  8 12 16]
  [ 6 12 18 24]]]

D.shape= (2, 3, 4)

なお、$b_j c_k$ だけであれば、numpy.outer で十分。

print(np.outer(B,C))
print(B[:, np.newaxis] * C)
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]]
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]]

ちょっと複雑な例

\boldsymbol A = \left(a_0, \dots, a_I  \right) \\
\boldsymbol B = \left(b_0, \dots, b_J  \right) \\
\boldsymbol C = \left(c_0, \dots, c_K  \right) \\
\\
\boldsymbol X = \left(x_0, \dots, x_L  \right) \\
\boldsymbol Y = \left(y_0, \dots, y_J  \right) \\
\boldsymbol Z = \left(z_0, \dots, z_K  \right) \\
\\
a_i x_l \sum_{j=0}^J b_j  y_j \sum_{k=0}^K c_k z_k \\

\newcommand{\argmax}{\mathop{\rm arg~max}\limits}
\newcommand{\argmin}{\mathop{\rm arg~min}\limits}

\argmax_{j}{a_i x_l b_j  y_j \sum_{k=0}^K c_k z_k} \\

の場合

ABC = A[:, np.newaxis, np.newaxis] * B[:, np.newaxis] * C
XYZ = X[:, np.newaxis, np.newaxis] * Y[:, np.newaxis] * Z
print("\nABC=", ABC)
print("\nXYZ=", XYZ)

O = ABC[:, np.newaxis] * XYZ
P = np.sum(O, axis=(2,3))
Q = np.argmax(np.sum(O, axis=3), axis=2)
print("O=", O)
print("P=", P)
print("Q=", Q)

とすればいいみたい。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0