Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

python - Access columns and rows of numpy.ndarray

I currently struggling with extracting certain columns and rows from a matrix stored as a numpy.ndarray.

I have a list in which I've appended these numpy.ndarrays.

This list is stored in a variable named data

print data[0].shape

outputs this

(400, 288)

Which I've according to the documentation have understood being the matrix has 400 rows, and 288 columns.

How do I extract all the 288 seperately?

Example:

>> import numpy as np
>> data = np.random.rand(3,3)
>> print data

[[ 0.97522481  0.57583658  0.68582806]
 [ 0.88509883  0.22261933  0.84307038]
 [ 0.59397925  0.51592125  0.54346909]]

How do I print the columns separately of this 3x3 matrix, first being

[0.97522481 , 0.88509883, 0.59397925 ]

without outputting the others?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is it what you are looking for?

import numpy as np
arr = np.array([[1, 2], 
                [3, 4], 
                [5, 6]])
print(arr.shape)
# (3, 2)
print(list(data.T))
# [array([1, 3, 5]), array([2, 4, 6])]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...