NumPy数组与权重求和
我有一个二维的numpy数组.
I have a two dimensional numpy array.
每行的长度为3个元素,并且是0-3的整数.这表示一个6位整数,每个单元依次表示两位.
Each row is three elements long and is an integer 0-3. This represents a 6 bit integer, with each cell representing two bits, in order.
我正在尝试将它们转换为完整的整数.
I'm trying to transform them into the full integer.
例如
for i in range(len(myarray)):
myarray[i] = myarray[i][0] * 16 + myarray[i][1] * 4 + myarray[i][2]
例如我正在尝试对每行求和,但要根据[16,4,1]的某个权重向量.
E.g. I'm trying to sum each row but according to a certain weight vector of [16,4,1].
最优雅的方法是什么?我想我必须先进行某种点乘积运算,然后求和,但我不确定100%决定在何处进行点运算.
What is the most elegant way to do this? I'm thinking I have to do some sort of dot product followed by a sum, but I'm not 100% confident where to do the dot.
点积的倾斜度是正确的,其中包括所需的总和.因此,要获得目标数组元素和一组权重的乘积之和:
The dot product inclination is correct, and that includes the sum you need. So, to get the sum of the products of the elements of a target array and a set of weights:
>>> a = np.array([[0,1,2],[2,2,3]])
>>> a
array([[0, 1, 2],
[2, 2, 3]])
>>> weights = np.array([16,4,2])
>>> np.dot(a,weights)
array([ 8, 46])