Prince Kumar
Prince Kumar

Reputation: 388

Does python's numpy module treats both row vector and column vector in same way?

I was playing around with numpy matrices to learn more about sub-matrices. Below is the result which I got when I tried to extract a row vector and a column vector:

>>> import numpy as np
>>> x = np.zeros(shape=(8,9))
>>> x
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> y = x[:, 0]
>>> y
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> z = x[0, :]
>>> z
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

The thing is that both row vector and column vector are shown in same fashion. So I don't know whether they are treated as same or different.

Upvotes: 2

Views: 129

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

You're currently fetching only a single row/column from the array, so that's the best way to represent a 1-D view of the row/column returned.

Use slicing to get results like this:

In [16]: x[:,:1]
Out[16]: 
array([[ 0.],                                                                                    
       [ 0.],                                                                                    
       [ 0.],                                                                                    
       [ 0.],                                                                                    
       [ 0.],                                                                                    
       [ 0.],                                                                                    
       [ 0.],                                                                                    
       [ 0.]])                                                                                   

In [17]: x[:1,:]                                                                                 
Out[17]: array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

Upvotes: 2

Related Questions