Numpy basics quiz

Concat 2 arrays horizontally,

a = np.arange(1,17).reshape(4,4)

b = np.arange(17,33).reshape(4,4)

how is np.concatenate((a,b),axis=0) a right answer for this question?

Hey @prerit_goyal, it is the correct answer. Let me explain you why. We need to combine two matrices of size (4,4) horizontally. So we can say we want a matrix of shape (8,4) after concatenation correct ? Now if we take the x-axis of a 2D plane and put both the matrices one above it and one below it, we can say we have accomplished what we wanted to do initially. So that’s why to reference the x-axis or the horizontal axis we put axis=0. Here’s a screenshot of the same for your reference :

Screenshot 2021-05-17 at 12.55.48 PM

I hope this clears your doubt ! :slight_smile:
Happy Learning !

1 Like

If:
np.concatenate((a,b),axis=0) is right for the given question,
then:
np.hstack((a,b)) should be a wrong answer cause it gives totally a different output for the question, isn’t it??!!

np.hstack stacks arrays in sequence horizontally (column wise). So you can imagine it as building the array in the horizontal direction. But yes both the outputs are completely opposite. It is difficult to interpret that whether the questions demands axis to be horizontal (np.concatenate()) or whether the questions wants us to build the final array in the horizontal direction (np.hstack()).

So thank you for bringing it in my notice. Can you please tell me the question number in the numpy quiz so that I can get it corrected by the technical team on the backend?

It is question 9 in the quiz!!

Thank you, this clears my doubt for the question.!!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.