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

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Numpy Broadcasting -Fehler beim Versuch, 2D -Array und 1D -Array hinzuzufügen

by Anonymous » 12 Jul 2025, 23:27

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.

Top