Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 574905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:52:52+00:00 2026-05-13T13:52:52+00:00

Let’s say I have a 2D Numpy array: >>> a = np.random.random((4,6)) and I

  • 0

Let’s say I have a 2D Numpy array:

>>> a = np.random.random((4,6))

and I want to add a 1D array to each row:

>>> c = np.random.random((6,))
>>> a + c

This works. Now if I try adding a 1D array to each column, I get an error:

>>> b = np.random.random((4,))
>>> a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape

I can fix this by using np.newaxis:

>>> a + b[:,np.newaxis]

which does work as expected.

What are the shape-matching rules to avoid having to use np.newaxis? Is it that the last element of the numpy shape tuple has to match? Does this rule also apply to higher dimensions? For example, the following works:

>>> a = np.random.random((2,3,4,5))
>>> b = np.random.random((4,5))
>>> a + b

So my question is whether this is documented anywhere, and if it is a behavior that can be relied on, or whether it is best to always use np.newaxis?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:52:53+00:00Added an answer on May 13, 2026 at 1:52 pm

    This is a distinctive feature of numpy called ‘broadcasting’:
    if you can multiply a vector by a scalar why not allow multiplying a matrix by a vector? Just like every element of a vector is multiplied by a scalar in the first case, every cell in the matrix row is multiplied by the corresponding vector element in the second case.

    Broadcasting is done using four rules which are a bit complicated in formulation but are rather intuitive once understood:

    1. All input arrays with ndim smaller than the input array of largest ndim,
      have 1’s prepended to their shapes.
    2. The size in each dimension of the output shape is the maximum of all the
      input sizes in that dimension.
    3. An input can be used in the calculation if its size in a
      particular dimension either matches
      the output size in that dimension, or
      has value exactly 1.
    4. If an input has a dimension size of 1 in its shape, the first data entry
      in that dimension will be used for all
      calculations along that dimension. In
      other words, the stepping machinery of
      the ufunc will simply not step along
      that dimension (the stride will be 0
      for that dimension).

    For example,
    enter image description here

    The operation is possible (doesn’t result in shape mismatch error you mentioned) in three cases:

    1. The arrays all have exactly the same shape.
    2. The arrays all have the same number of dimensions and the length of each dimensions is either a common length or 1.
    3. The arrays that have too few dimensions can have their shapes prepended with a dimension of length 1 to satisfy property 2.

    More examples can be found in my recent article on broadcasting [1] or in the official docs [2].

    References

    1. Broadcasting in NumPy, 2021 https://towardsdatascience.com/58856f926d73
    2. NumPy Reference. Broadcasting http://docs.scipy.org/doc/numpy/reference/ufuncs.html#broadcasting
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 316k
  • Answers 316k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The control you probably want is System.Windows.Forms.FlowLayoutPanel (see FlowLayoutPanel@msdn) It… May 13, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer You need to call glClear() after glClearColor(). glClearColor() only sets… May 13, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer It looks like you are trying to use Clang LLVM… May 13, 2026 at 11:28 pm

Related Questions

I have a French site that I want to parse, but am running into
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.