Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Numpy: Extracting a row from an array

Tags:

python

numpy

I'm trying to extract a row from a Numpy array using

t = T[153,:]

But I'm finding that where the size of T is (17576, 31), the size of t is (31,) - the dimensions don't match!

I need t to have the dimensions (,31) or (1,31) so that I can input it into my function. I've tried taking the transpose, but that didn't work.

Can anyone help me with what should be a simple problem?

Many thanks

like image 594
jlt199 Avatar asked Sep 13 '25 07:09

jlt199


1 Answers

You can extract the row with a slice notation:

t = T[153:154,:]    # will extract row 153 as a 2d array

Example:

T = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

T[1,:]
# array([5, 6, 7, 8])

T[1,:].shape
# (4,)

T[1:2,:]
# array([[5, 6, 7, 8]])

T[1:2,:].shape
# (1, 4)
like image 85
Psidom Avatar answered Sep 15 '25 20:09

Psidom