Python - 使用多个键

时间:2017-07-28 08:36:28

标签: python dictionary filter subset

我的字典" mat_contents"看起来像这样:

{'__globals__': [],
'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Wed Jul 05 14:02:28 2017',
'__version__': '1.0',
'traindata': array([[  3.37988024e-01,   2.87425150e-01,   3.36669403e-01, ...,
       3.47222222e+00,   2.89078631e+01,   2.66129299e+02],
    [  3.78449348e-01,   3.68259383e-01,   3.77523300e-01, ...,
       7.93650794e+00,   7.49814148e+01,   3.39940867e+02],
    [  4.20835393e-01,   3.83079983e-01,   4.20191384e-01, ...,
       4.13223140e+00,   3.17110530e+01,   3.13224462e+02],
    ..., 
    [  8.27125382e-01,   8.31804281e-01,   8.29763490e-01, ...,
       2.05761317e+00,   1.37089318e+01,   2.36950648e+02],
    [  6.25915416e-01,   7.65430139e-01,   6.26844490e-01, ...,
       4.38596491e+00,   2.86553873e+01,   4.07901114e+02],
    [  4.66673740e-01,   4.73032714e-01,   4.66367545e-01, ...,
       9.61538462e+00,   2.73538135e+01,   2.90957268e+02]]),
'trainlabels': array([[array(['2'], 
   dtype='<U1')],
    [array(['3'], 
   dtype='<U1')],
    [array(['2'], 
   dtype='<U1')],
    ..., 
    [array(['2'], 
   dtype='<U1')],
    [array(['2'], 
   dtype='<U1')],
    [array(['2'], 
   dtype='<U1')]], dtype=object)}

我想根据trainlabels的值过滤(子集)给定数量的行(包含&#39; traindata&#39;和#39; trainlabels&#39;)。到目前为止(在帮助下)我得到了以下命令:

labels = [mat_contents['trainlabels'][0][i] for i, val in enumerate(mat_contents['traindata'].flatten()[0]) if val == 1]
 result = np.random.choice(labels, 2, replace=False)

但是,我收到以下错误:

 TypeError: 'numpy.float64' object is not iterable

我试图在上一个问题中解决: Python - Subtract a number of samples from a given in a dictionary structure

我被卡住了,我不知道如何解决它。

0 个答案:

没有答案