teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正

2020/08/03 17:12

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -13,6 +13,9 @@
13
13
 
14
14
  # (6, 1) x (1, 6)
15
15
  c = a.reshape(-1, 1) @ b.reshape(1, -1)
16
+ # c = a[:, np.newaxis] @ b[np.newaxis] でも同じ
17
+
18
+
16
19
  print(c)
17
20
  # [[ 100 200 300]
18
21
  # [ 200 400 600]

1

修正

2020/08/03 17:12

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -20,4 +20,39 @@
20
20
  # [ 400 800 1200]
21
21
  # [ 500 1000 1500]
22
22
  # [ 600 1200 1800]]
23
- ```
23
+ ```
24
+
25
+ ## `a.reshape(-1, 1) * b.reshape(1, -1)` で同じ結果が計算できた理由
26
+
27
+ ```python
28
+ [[1]
29
+ [2]
30
+ [3]
31
+ [4]
32
+ [5]
33
+ [6]]
34
+ ```
35
+
36
+ ```python
37
+ [[100 200 300]]
38
+ ```
39
+ を演算しようとしたときにブロードキャストされて、
40
+
41
+ ```python
42
+ [[1, 1, 1],
43
+ [2, 2, 2],
44
+ [3, 3, 3],
45
+ [4, 4, 4],
46
+ [5, 5, 5],
47
+ [6, 6, 6]]
48
+ ```
49
+
50
+ ```python
51
+ array([[100, 200, 300],
52
+ [100, 200, 300],
53
+ [100, 200, 300],
54
+ [100, 200, 300],
55
+ [100, 200, 300],
56
+ [100, 200, 300]])
57
+ ```
58
+ の要素同士の積になって、たまたま行列積と同じ結果になりました。