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

回答編集履歴

1

追加質問への回答を追加

2021/02/18 03:51

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -23,4 +23,39 @@
23
23
  [0 1 2 3 4]
24
24
  >>> print(np.dot(a, w))
25
25
  [4 4 6]
26
+ ```
27
+ forで回したいなら
28
+ ```python
29
+ >>> import numpy as np
30
+ >>> def compute(d2array, warray):
31
+ ... result = np.zeros(d2array.shape[0], dtype=a.dtype)
32
+ ... for i in range(d2array.shape[0]):
33
+ ... for j in range(d2array.shape[1]):
34
+ ... result[i] = result[i] + d2array[i, j] * warray[j]
35
+ ... return result
36
+ ...
37
+ >>> a = np.array([[1,0,0,0,1],
38
+ ... [0,0,0,0,1],
39
+ ... [0,1,1,1,0]])
40
+ >>> w = np.array([0, 1, 2, 3, 4])
41
+ >>> print(compute(a,w))
42
+ [4 4 6]
43
+ ```
44
+ 重みを他のものに変えたいときは、wを好きなように変更していください。
45
+
46
+ numpyの機能を一切使わないなら、以下ですね。
47
+ ```python
48
+ >>> def compute_l(alist_of_lists, aweight):
49
+ ... result = []
50
+ ... for alist in alist_of_lists:
51
+ ... asum = 0
52
+ ... for j in range(len(aweight)):
53
+ ... asum = asum + alist[j] * aweight[j]
54
+ ... result.append(asum)
55
+ ... return result
56
+ ...
57
+ >>> list_of_lists = [[1,0,0,0,1], [0,0,0,0,1], [0,1,1,1,0]]
58
+ >>> weight = [0, 1, 2, 3, 4]
59
+ >>> print(compute_l(list_of_lists, weight))
60
+ [4, 4, 6]
26
61
  ```