如何按类别(Python)对我的numpy文件进行分类?

时间:2017-04-27 22:36:26

标签: python numpy

假设我有500个numpy文件,我必须进行一些分析,所以我为每个文件计算自己的HW,所有这些HW都存储在一个我称之为tempHW的表中。基于此表,我查看每个跟踪并确定它所属的组。我列出了跟踪列表,然后我可以遍历我的跟踪列表并将每个跟踪附加到正确的类别。

path ='C:\\Users\\user\\Test\\F_Key'

traces= os.listdir(path)  
print(len(traces)) 

tempHW=[6, 5, 6, 3, 3, 7, 4, 2, 5, 1, 4, 2, 5, 4, 3, 4, 5, 6, 4, 5, 5, 3, 1, 4, 4, 2, 3, 3, 4,
        4, 5, 4, 5, 3, 5, 4, 3, 6, 2, 5, 2, 6, 5, 3, 5, 5, 5, 2, 5, 6, 3, 3, 4, 6, 4, 4, 6, 6,
        6, 5, 4, 3, 5, 4, 3, 3, 3, 3, 6, 3, 0, 3, 3, 5, 5, 4, 7, 4, 5, 3, 3, 4, 4, 6, 4, 5, 4,
        6, 3, 3, 0, 3, 6, 4, 2, 2, 7, 5, 5, 4, 5, 4, 4, 4, 5, 2, 6, 6, 5, 5, 2, 4, 2, 6, 5, 4,
        3, 4, 5, 4, 4, 3, 5, 1, 5, 5, 3, 5, 6, 3, 4, 6, 5, 3, 5, 6, 5, 4, 5, 7, 3, 4, 4, 6, 4,
        4, 4, 6, 6, 6, 3, 8, 6, 3, 3, 5, 3, 3, 5, 5, 5, 6, 6, 4, 2, 2, 4, 3, 5, 4, 4, 6, 5, 5, 
        3, 3, 4, 5, 4, 4, 3, 5, 6, 5, 7, 4, 6, 4, 4, 4, 6, 3, 3, 5, 6, 5, 3, 5, 3, 3, 2, 6, 4,
        5, 2, 2, 5, 3, 5, 4, 4, 2, 5, 4, 5, 3, 4, 4, 5, 3, 4, 3, 6, 5, 3, 5, 5, 4, 3, 3, 4, 7,
        6, 6, 4, 3, 3, 4, 4, 3, 4, 5, 5, 4, 5, 3, 5, 6, 3, 4, 4, 2, 5, 3, 0, 4, 4, 2, 4, 4, 5,
        4, 5, 4, 4, 2, 5, 4, 5, 5, 1, 2, 6, 5, 4, 4, 4, 4, 5, 1, 4, 6, 5, 5, 5, 7, 4, 4, 3, 6,
        3, 3, 3, 4, 3, 5, 3, 2, 4, 5, 5, 4, 5, 4, 3, 6, 3, 4, 6, 2, 6, 5, 2, 5, 2, 5, 6, 3, 4,
        5, 5, 3, 4, 3, 8, 6, 4, 7, 4, 4, 3, 5, 4, 5, 3, 4, 6, 6, 4, 1, 3, 2, 6, 5, 4, 2, 6, 2,
        6, 2, 3, 3, 5, 3, 4, 5, 1, 3, 4, 5, 4, 5, 2, 2, 4, 6, 5, 2, 2, 5, 5, 4, 4, 2, 3, 4, 3,
        3, 4, 3, 1, 3, 2, 4, 3, 3, 3, 2, 5, 5, 6, 3, 4, 2, 2, 6, 5, 5, 4, 5, 4, 1, 5, 4, 4, 6, 
        7, 2, 2, 2, 4, 4, 2, 4, 6, 5, 4, 3, 5, 2, 5, 4, 5, 3, 6, 4, 4, 5, 4, 4, 4, 4, 5, 2, 6,
        3, 6, 4, 5, 4, 2, 4, 4, 2, 4, 6, 4, 4, 2, 3, 3, 4, 4, 4, 4, 3, 6, 5, 2, 6, 3, 5, 4, 4,
        6, 2, 4, 3, 6, 4, 5, 3, 4, 2, 4, 4, 5, 3, 2, 5, 6, 5, 3, 6, 3, 4, 3, 3, 5, 1, 3, 3, 4,
        4, 5, 6, 2, 5, 5, 3]
print(len(tempHW))
tempTracesHW = [[] for _ in range(9)]
for i in range(len(traces)):
    HW = tempHW[i]
    filepath= os.path.join(path, traces[i])
    trace, = np.load(filepath)
    tempTracesHW[HW].append(trace)
print (tempTracesHW)

然后,我在一个numpy表中转换这个列表。

tempTracesHW =  np.array([np.array(tempTracesHW[HW]).astype('float32') for HW in range(9)])
print(tempTracesHW.shape)
print(tempTracesHW[5].shape)

然而,当我的代码到达那些行时,我的代码就被种下了:

 for trace in os.listdir(path):
           tempTracesHW[HW].append([np.load(os.path.join(path, trace))])

我真的不知道如何解决这个问题,在调试时,我的代码完全被阻止了,

1 个答案:

答案 0 :(得分:0)

这对我有用:

    tempTracesHW = [ [] for _ in range(9)]
    for i in range(len(traces)):
        HW = tempHW[i]
        tempTracesHW[HW].append( np.load(os.path.join(path, traces[i])) )

    tempTracesHW = np.array( [np.array(tempTracesHW[HW]).astype('float32') for HW in range(9)] )
  

输入:对于classify 6两个:

    [array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14]])]
  

输出

    tempTracesHW.shape=9
    tempTracesHW[6].shape=(2, 3, 5)
    array([[[  0.,   1.,   2.,   3.,   4.],
            [  5.,   6.,   7.,   8.,   9.],
            [ 10.,  11.,  12.,  13.,  14.]],

           [[  0.,   1.,   2.,   3.,   4.],
            [  5.,   6.,   7.,   8.,   9.],
            [ 10.,  11.,  12.,  13.,  14.]]], dtype=float32)

使用Python测试:3.4.2 - numpy:1.8.2