PY-85

Compute Weighted Scores and Show Contributions

  • Medium
  • Weighted Scores
  • Python

Task

Write compute_weighted_scores(row, batch, weights). row is one finite one-dimensional NumPy array of sensor values. batch is a finite two- dimensional array whose columns have the same sensor order as row. weights is a finite one-dimensional array with one value per sensor. The three arrays must have matching sensor lengths; batch may have zero rows.

For every value and its same-position weight, define the contribution as value * weight. A row score is the sum of its sensor contributions. Return a dictionary with exactly these keys:

  • "row_contributions": an independent float64 array with the contribution for each sensor in row;
  • "row_score": the float sum of row_contributions;
  • "batch_contributions": an independent float64 array with the same shape as batch;
  • "batch_scores": an independent float64 array with one sum per batch row, reducing the sensor axis.

The batch and the single row use the same positional pairing. Do not add a bias, normalize the weights, or reorder sensors. Keep all inputs unchanged.

Example

With row=[2,-1,4], batch=[[2,-1,4],[0,3,-2]], and weights=[0.5,2,-1], the row contributions are [1,-2,-4] and its score is -5. The batch contributions are [[1,-2,-4],[0,6,2]], so batch scores are [-5,8].

Your implementation

You may import NumPy as np. Do not modify an input, print, or ask for input.