简体   繁体   English

Numpy数组与权重相加

[英]Numpy Array summing with weights

I have a two dimensional numpy array. 我有一个二维numpy数组。

Each row is three elements long and is an integer 0-3. 每行长三个元素,整数为0-3。 This represents a 6 bit integer, with each cell representing two bits, in order. 这表示6位整数,每个单元按顺序表示两位。

I'm trying to transform them into the full integer. 我正在尝试将它们转换为完整的整数。

Eg 例如

for i in range(len(myarray)):
  myarray[i] = myarray[i][0] * 16 + myarray[i][1] * 4 + myarray[i][2]

Eg I'm trying to sum each row but according to a certain weight vector of [16,4,1]. 例如,我试图对每一行求和,但是根据[16,4,1]的某个权重向量。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM