0

I have a matrix that looks like that:

>> X
>> 
[[5.1 1.4]
 [4.9 1.4]
 [4.7 1.3]
 [4.6 1.5]
 [5.  1.4]]

I want to get its first column as an array of [5.1, 4.9, 4.7, 4.6, 5.] However when I try to get it by X[:,0] i get

>> [[5.1]
   [4.9]
   [4.7]
   [4.6]
   [5. ]]

which is something different. How to get it as an array ?

Clyde Barrow
  • 1,924
  • 8
  • 30
  • 60
  • 2
    Is `X` of `np.matrix` type? That retains dimensions when indexed. Note that a couple of answers assume it is a list of lists. – hpaulj Feb 01 '20 at 13:42
  • Does this answer your question? [How to access the ith column of a NumPy multidimensional array?](https://stackoverflow.com/questions/4455076/how-to-access-the-ith-column-of-a-numpy-multidimensional-array) – sentence Feb 01 '20 at 14:06
  • To second @hpaulj's comment, I can't directly reproduce this with a `numpy` array. If it really is a numpy matrix, the answer is "it is no longer recommended to use this class, even for linear algebra. Instead use regular arrays" — quoted directly from the result of `help(numpy.matrix)`. – senderle Feb 01 '20 at 16:31

3 Answers3

0

You can use list comprehensions for this kind of thing..

import numpy as np

X = np.array([[5.1, 1.4], [4.9, 1.4], [4.7, 1.3], [4.6, 1.5], [5.0,  1.4]])
X_0 = [i for i in X[:,0]]
print(X_0)

Output..

[5.1, 4.9, 4.7, 4.6, 5.0]
DrBwts
  • 3,470
  • 6
  • 38
  • 62
0

Almost there! Just reshape your result:

X[:,0].reshape(1,-1)

Outputs:

[[5.1 4.9 4.7 4.6 5. ]]

Full code:

import numpy as np

X=np.array([[5.1 ,1.4],[4.9 ,1.4], [4.7 ,1.3], [4.6 ,1.5], [5. , 1.4]])
print(X)
print(X[:,0].reshape(1,-1))
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
0

With regular numpy array:

In [3]: x = np.arange(15).reshape(5,3)                                                                                             
In [4]: x                                                                                                                          
Out[4]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])
In [5]: x[:,0]                                                                                                                     
Out[5]: array([ 0,  3,  6,  9, 12])

With np.matrix (use discouraged if not actually deprecated)

In [6]: X = np.matrix(x)                                                                                                           
In [7]: X                                                                                                                          
Out[7]: 
matrix([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11],
        [12, 13, 14]])
In [8]: print(X)                                                                                                                   
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
In [9]: X[:,0]                                                                                                                     
Out[9]: 
matrix([[ 0],
        [ 3],
        [ 6],
        [ 9],
        [12]])
In [10]: X[:,0].T                                                                                                                  
Out[10]: matrix([[ 0,  3,  6,  9, 12]])

To get 1d array, convert to array and ravel, or in one step:

In [11]: X[:,0].A1                                                                                                                 
Out[11]: array([ 0,  3,  6,  9, 12])
hpaulj
  • 221,503
  • 14
  • 230
  • 353