NumPy数组与权重求和 [英] Numpy Array summing with weights

查看:339
本文介绍了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])

这篇关于NumPy数组与权重求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆