回答編集履歴

1

追加質問への回答を追加

2021/02/18 03:51

投稿

ppaul
ppaul

スコア24670

test CHANGED
@@ -49,3 +49,73 @@
49
49
  [4 4 6]
50
50
 
51
51
  ```
52
+
53
+ forで回したいなら
54
+
55
+ ```python
56
+
57
+ >>> import numpy as np
58
+
59
+ >>> def compute(d2array, warray):
60
+
61
+ ... result = np.zeros(d2array.shape[0], dtype=a.dtype)
62
+
63
+ ... for i in range(d2array.shape[0]):
64
+
65
+ ... for j in range(d2array.shape[1]):
66
+
67
+ ... result[i] = result[i] + d2array[i, j] * warray[j]
68
+
69
+ ... return result
70
+
71
+ ...
72
+
73
+ >>> a = np.array([[1,0,0,0,1],
74
+
75
+ ... [0,0,0,0,1],
76
+
77
+ ... [0,1,1,1,0]])
78
+
79
+ >>> w = np.array([0, 1, 2, 3, 4])
80
+
81
+ >>> print(compute(a,w))
82
+
83
+ [4 4 6]
84
+
85
+ ```
86
+
87
+ 重みを他のものに変えたいときは、wを好きなように変更していください。
88
+
89
+
90
+
91
+ numpyの機能を一切使わないなら、以下ですね。
92
+
93
+ ```python
94
+
95
+ >>> def compute_l(alist_of_lists, aweight):
96
+
97
+ ... result = []
98
+
99
+ ... for alist in alist_of_lists:
100
+
101
+ ... asum = 0
102
+
103
+ ... for j in range(len(aweight)):
104
+
105
+ ... asum = asum + alist[j] * aweight[j]
106
+
107
+ ... result.append(asum)
108
+
109
+ ... return result
110
+
111
+ ...
112
+
113
+ >>> list_of_lists = [[1,0,0,0,1], [0,0,0,0,1], [0,1,1,1,0]]
114
+
115
+ >>> weight = [0, 1, 2, 3, 4]
116
+
117
+ >>> print(compute_l(list_of_lists, weight))
118
+
119
+ [4, 4, 6]
120
+
121
+ ```