1
np_mat = np.array([[1, 2], [3, 4], [5, 6]])
np_mat + np.array([10, 10])

I am confused what the difference between np.array([10, 10]) and np.array([[10, 10]]) is. In school I learnt that only matrices with the same dimensions can be added. When I use the shape method on np.array([10, 10]) it gives me (2,)...what does that mean? How is it possible to add np_mat and np.array([10, 10])? The dimensions don't look the same to me. What do I not understand?

2
  • 2
    Read up on broadcasting, which is used in numpy for arithmetic operations with different shaped arrays Commented Sep 24, 2019 at 11:08
  • 2
    np.array([10, 10]) is single dimenssional array and np.array([[10, 10]]) is an multidimensional array as @FlyingTeller tell read up broadcasting Commented Sep 24, 2019 at 11:12

2 Answers 2

1

It looks like numpy is bending the rules of mathematics here. Indeed, it sums the second matrix [10, 10] with each element of the first [[1, 2], [3, 4], [5, 6]].

This is called https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html. The shape of [10, 10] is (2, ) (that is, mathematically, 2) and that of [[1, 2], [3, 4], [5, 6]] is (3, 2) (that is, mathematically, 3 x 2). Therefore, from general broadcasting rules, you should get a result of shape (3, 2) (that is, mathematically, 3 x 2).

I am confused what the difference between np.array([10, 10]) and np.array([[10, 10]]) is.

The first is an array. The second is an array of arrays (in memory, it is in fact one single array, but this is not relevant here). You could think of the first as a column vector (a matrix of size 2 x 1) and the second as a line vector (a matrix of size 1 x 2). However, be warned that the distinction between line and column vectors is irrelevant in mathematics until you start interpreting vectors as matrices.

Sign up to request clarification or add additional context in comments.

1 Comment

many thanks @aimery for your comment. I think I have to rework the theory again. I assumed arrays and matrices are synonyms. But obviously, it's not. I am familiar with matrices and the rules of matrix multiplication, and I have never heard of broadcasting before in the context of matrices. It seems, the arithmetic for numpy arrays works slightly differently. Many thanks for your contribution!
1

You cannot add two arrays of different sizes. But those two both have first dimension, length, equal to 2. (that is len(a) == len(b))

Shape (2,) means that the array is one-dimensional and the first dimension is of size 2.

np.array([[1, 2], [3, 4], [5, 6]]) has shape (3, 2) which means two-dimensional (3x2).

But you can add them since they are of different dimensions, and numpy coerces a number to an arbitrary array full of this same number. This is called broadcasting in numpy.

I.e. your code gets equivalent results to:

np_mat = np.array([[1, 2], [3, 4], [5, 6]])
np_mat + 10

Or to:

np_mat = np.array([[1, 2], [3, 4], [5, 6]])
np_mat + np.array([[10, 10], [10, 10], [10, 10]])

2 Comments

Many thanks @Arusekk for your comment. 1) What is the length of an array? I have always use the term length for sequences like strings or lists. But I have troubles imagining what the length of an array is. 2) So shape (2,) is the same as dimensions (2x1)? shape (2,1) does not exist? Sorry, I don't want to dig too deep into the theory. Maybe I have to rework the basics again, and then come back to your comment. Many thanks anyway!
See here for the theoretical explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.