简体   繁体   中英

Accessing second index in 3-d numpy array

I have a 2-d numpy array that I convert into a 3-d array by using:

trainSet = numpy.reshape(trainSet, (trainSet.shape[0], 1, trainSet.shape[1]))

My problem is I'm trying to adjust values in the 1st axis (the new one). However, no matter what I try I can not actually figure out how to access the 1's in the new axis. I've tried a multitude of print statements such as

print(trainSet[0][0][0])
print(trainSet[1][0][1])

but no matter what I try I can not print out any 1's, just the contents of the 2-d array. I know it's a 3d array because when I use

print(trainSet.shape)

I get

(12, 1, 793)

Any help would be immensely appreciated!

The one refers to the size of that particular dimension, not the content of the array. The array [[5 5]] has shape (1, 2) but, still, you don't really expect its values must include 1 for that reason, do you?

What inserting a dimension of length one does is increasing the nesting level by one, so a 2d array [[ab] [cd]] will become [[[ab] [cd]]] or [[[ab]] [[cd]]] or [[[a] [b]] [[c] [d]]] depending on where you insert the new axis.

In particular, there are no new elements.

如果我没记错你的问题,你想要的是

trainSet[:,0,:]

值的数量保持不变,您只是在创建3D结构而不是2D结构。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM