You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Numpy中二维与三维数组的指定乘积及标量运算实现

Got it, let's break down what you need here and how to solve it with NumPy. The key is aligning the dimensions correctly and using either np.einsum (super intuitive for custom tensor operations) or broadcasting with matrix multiplication.

First, let's recap your array shapes to make sure we're on the same page:

  • a: (2, 3) → 2 rows, each with 3 elements
  • b: (2, 3, 3) → 2 separate 3x3 matrices (each matrix has 3 rows, 3 elements per row)
  • c: (3,) → 3 scalars to use as denominators for each column in your final result

Your core requirement is: For each row in a (say row i), compute the dot product with each row in the corresponding 3x3 matrix in b (row k of matrix i), then divide each dot product by the matching element in c (element k).

Solution 1: Using np.einsum (Most Readable for This Case)

np.einsum lets you explicitly define which dimensions to multiply and sum over, which avoids confusion with axis parameters. The subscript string 'ij,ikj->ik' translates directly to your operation:

  • ij: a uses indices i (row) and j (element in the row)
  • ikj: b uses indices i (matrix), k (row in the matrix), j (element in the row)
  • ->ik: We sum over the j dimension (this is the dot product) and keep the i and k dimensions for the final (2,3) result.

Here's the code:

import numpy as np

a = np.array([[1, 2, 3], [3, 4, 5]])
b = np.array([[[1, 0, 1], [1, 1, 0], [0, 1, 1]], [[1, 1, 1], [0, 1, 0], [0, 0, 1]]])
c = np.array([1, 2, 3])

# Compute the dot products and divide by c
result = np.einsum('ij,ikj->ik', a, b) / c
# Round to 2 decimal places to match your expected output
rounded_result = np.round(result, 2)

print(rounded_result)

Output:

[[ 4.    1.5   1.67]
 [12.    2.    1.67]]

Solution 2: Using Broadcasting + Matrix Multiplication

If you prefer sticking to familiar linear algebra operations, you can reshape a to align its dimensions with b:

  1. Reshape a to (2, 1, 3) (add an extra middle axis) so it can multiply with each 3x3 matrix in b
  2. Use @ for matrix multiplication, which will compute the dot product between the 1x3 row of reshaped a and each row in the corresponding 3x3 matrix
  3. Squeeze the extra axis to get a (2,3) array, then divide by c (NumPy's broadcasting handles the division automatically)

Code for this approach:

import numpy as np

a = np.array([[1, 2, 3], [3, 4, 5]])
b = np.array([[[1, 0, 1], [1, 1, 0], [0, 1, 1]], [[1, 1, 1], [0, 1, 0], [0, 0, 1]]])
c = np.array([1, 2, 3])

result = (a[:, None, :] @ b).squeeze() / c
rounded_result = np.round(result, 2)

print(rounded_result)

This gives exactly the same output as the einsum method.

A Quick Note on the axis Parameter

I get why you struggled with axis here—functions like np.dot use it for summation, but when dealing with multi-dimensional arrays where you need to match specific axes (like linking a's rows to b's matrices), einsum makes the dimension relationships explicit, which is way less error-prone. Once you get the hang of its subscript syntax, it becomes your go-to for custom tensor operations.

内容的提问来源于stack exchange,提问作者minovsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:27:08