Page 1 of 1

Numpy Broadcasting -Fehler beim Versuch, 2D -Array und 1D -Array hinzuzufügen

Posted: 12 Jul 2025, 23:27
by Anonymous
Ich versuche, eine Elementzusatzung zwischen einem 2D-Numpy-Array und einem 1D-Array (wie dem Hinzufügen eines Zeilenvektors zu jeder Zeile einer Matrix) durchzuführen, aber ich stoße auf einen Rundfunkfehler.

Code: Select all

import numpy as np

# Create a 2D array with shape (3, 4)
matrix = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
])

# Create a 1D array with shape (3,) – I intended this to be a row vector
row_vector = np.array([1, 2, 3])

# Try to add the row vector to each row of the matrix
result = matrix + row_vector
< /code>
Wenn ich ausführe, erhalte ich den folgenden Fehler: < /p>
ValueError: operands could not be broadcast together with shapes (3,4) (3,)

Ideen? klar.