1

Here I have a matrix a=np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])

I want to select all rows, but the column I want to select is from the first to the third one.

It should be [[1,2,3],[6,7,8],[11,12,13]]

However, I have ever tried a[:,[0,2]], but it shows

 array([[ 1,  3],
       [ 6,  8],
       [11, 13]]) 

It seems not the correct, so I tried another one a[:][0:2], it still is a wrong result.

So I want to ask if there are any function or method can fix the problem?

fuglede
  • 17,388
  • 2
  • 54
  • 99
陳俊良
  • 319
  • 1
  • 6
  • 13

3 Answers3

1

Sounds like you are looking for a[:, 0:3]:

In [4]: a[:, 0:3]
Out[4]:
array([[ 1,  2,  3],
       [ 6,  7,  8],
       [11, 12, 13]])
fuglede
  • 17,388
  • 2
  • 54
  • 99
1

I think need indexing 0:3:

print (a[:,0:3])
[[ 1  2  3]
 [ 6  7  8]
 [11 12 13]]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Try the following

a=np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
a = a[:,0:3] 
print(a)
#Output
#array([[ 1,  2,  3],
#   [ 6,  7,  8],
#   [11, 12, 13]])
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56