Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

python - Numpy Array summing with weights

I have a two dimensional numpy array.

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.

E.g.

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

E.g. I'm trying to sum each row but according to a certain weight vector of [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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...