[英]Tuple to index using numpy
It drives me crazy, but I can't figure it out I have a data matrix of (10000,4) 它使我发疯,但我无法弄清楚我的数据矩阵为(10000,4)
I need to select some rows where the elements of column 0 我需要选择一些行,其中列0的元素
ind1=np.where( (data[:,0]>55) & (data[:,0]<65) )
I want to keep that data only so 我只想保留这些数据
keep_data=data[ind1,:]
But keep_data is now (1,10000,4) 但是keep_data现在是(1,10000,4)
Why is that? 这是为什么?
PS What i do is th efollwing PS我正在做的事
keep_data=np.reshape(keep_data,(keep_data.shape[1],keep_data.shape[2]))
numpy.where
returns a tuple. numpy.where
返回一个元组。
Therefore, use ind1 = np.where((data[:,0]>55) & (data[:,0]<65))[0]
因此,使用
ind1 = np.where((data[:,0]>55) & (data[:,0]<65))[0]
Notice the [0]
indexing to select the only element of the tuple. 注意
[0]
索引选择了元组的唯一元素。
This is noted in the docs : 在文档中对此进行了说明:
numpy.where ( condition[, x, y] )
numpy.where ( condition [,x,y] )
Return elements, either from x or y, depending on condition.
根据条件从x或y返回元素。
If only condition is given, return the tuple
condition.nonzero()
, the indices where condition isTrue
.如果仅给出条件,则返回元组
condition.nonzero()
,其中condition为True
的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.