如何创建保留列的布尔掩码?
How to create a Boolean Mask which preserves a column?
例如:
说我有
x = np.array[['positive','a'],['negative','b'],['positive','c'], ['negative','d']]
我想要一个值为
的新数组
v = [['positive','a'], ['positive','c']]
这是怎么做到的?谢谢你的时间!
更正语法:
In [359]: x = np.array([['positive','a'],['negative','b'],['positive','c'], ['ne
...: gative','d']])
In [360]: x
Out[360]:
array([['positive', 'a'],
['negative', 'b'],
['positive', 'c'],
['negative', 'd']], dtype='<U8')
只有 select 个带有索引的“行”:
In [361]: x[[0,2],:]
Out[361]:
array([['positive', 'a'],
['positive', 'c']], dtype='<U8')
等效的布尔掩码:
In [362]: mask=np.zeros(4,bool)
In [363]: mask[[0,2]]=True
In [364]: mask
Out[364]: array([ True, False, True, False])
In [365]: x[mask,:]
Out[365]:
array([['positive', 'a'],
['positive', 'c']], dtype='<U8')
例如: 说我有
x = np.array[['positive','a'],['negative','b'],['positive','c'], ['negative','d']]
我想要一个值为
的新数组v = [['positive','a'], ['positive','c']]
这是怎么做到的?谢谢你的时间!
更正语法:
In [359]: x = np.array([['positive','a'],['negative','b'],['positive','c'], ['ne
...: gative','d']])
In [360]: x
Out[360]:
array([['positive', 'a'],
['negative', 'b'],
['positive', 'c'],
['negative', 'd']], dtype='<U8')
只有 select 个带有索引的“行”:
In [361]: x[[0,2],:]
Out[361]:
array([['positive', 'a'],
['positive', 'c']], dtype='<U8')
等效的布尔掩码:
In [362]: mask=np.zeros(4,bool)
In [363]: mask[[0,2]]=True
In [364]: mask
Out[364]: array([ True, False, True, False])
In [365]: x[mask,:]
Out[365]:
array([['positive', 'a'],
['positive', 'c']], dtype='<U8')