The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Adding a row and column vector in NumPy

Adding a row and column vector in NumPy

0
Votes
1
Answer

As per the linear algebra rules we should not be bale to add a row and column vector . But i am able to do this operation in NumPy. Furthermore i am unable to understand the output.
Example:
v = np.array([1,2,3])
row_vector = v.reshape(1,3)
column_vector = v.reshape(3,1)

row_vector + column_vector
O/P

array([[2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])

Also i have a doubt that are row and column vectors , vectors or matrices. Because the row and column vectors have 2 dimesnions : (1,m) or (m,1) respectively.

Also using the attribute ‘row_vector.ndim’ gives output 2.

Any help will be appreciated.

Thanks.

 

1 Answer

365 Team

Hi Yash, 
thank you for reaching out!
The operation + sums the two vectors by adding corresponding elements. 
For the example you’ve shown:
Adding a 3×1 row_vector and 1×3 column_vector, results in a 3*3 matrix, where each element (i, j) equals:
row_vector(j) + column_vector(i).
So, for the first row in your example we’d have [ row_vector_1 + column_vector_1, row_vector_2 + column_vector_1, row_vector_3 + column_vector_1] = [1+1, 2+1, 3+1]
Generally on the topic of vector sums using +: 
To be able to use + the two elements need to be compatible. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are either the same or one of them is 1. 
Hope this sheds some light on the topic!
 
Best, 
Eli

Thanks for the help. I read online and found that this is broadcasting. It would really helpful if you introduced this concept in your already excellent mathematics course.

6 months
×
LAST CHANCE
Ready to Learn Data Science?
50% OFF