2017-04-26 50 views
0

我嘗試在一組數據中進行一些分析,我找到了一個有用的教程來進行這些分析。本教程需要使用一個文件包含從一組文件如何從一個數組移動到Python中的一組文件?

tempTraces = np.load(r'C:\\Users\\user\\2016.06.01-09.41.16_traces.npy') 
    for i in range(len(tempTraces)): 
     HW = tempHW[i] 
     print (tempTraces[i]) 
     tempTracesHW[HW].append(tempTraces[i]) 
    print(tempTracesHW) 

    # Switch to numpy arrays 
    tempTracesHW = [np.array(tempTracesHW[HW]) for HW in range(9)] 
    print (len(tempTracesHW[8])) 

的結果是連接數據:

[ array([[ 0.06835938, 0.0390625 , 0.07519531, ..., 0.0546875 , 
     0.08886719, 0.02734375], 
     [ 0.06542969, 0.04199219, 0.07714844, ..., 0.06152344, 
     0.07324219, 0.09472656], 
     [ 0.06640625, 0.04101562, 0.07714844, ..., 0.10742188, 
     0.13574219, 0.03222656], 
     [ 0.06445312, 0.03613281, 0.07519531, ..., 0.14160156, 
     0.1171875 , 0.14257812], 
     [ 0.06347656, 0.04003906, 0.07519531, ..., 0.05566406, 
     0.08300781, 0.02539062], 
     [ 0.06542969, 0.0390625 , 0.08105469, ..., 0.03222656, 
     0.06738281, 0.07714844]]), 

array([[ 0.06640625, 0.04199219, 0.07519531, ..., 0.02148438, 
     0.0859375 , 0.12695312], 
     [ 0.06640625, 0.04199219, 0.078125 , ..., 0.08886719, 
     0.02734375, 0.02734375], 
     [ 0.06738281, 0.04394531, 0.07910156, ..., 0.06347656, 
     0.08496094, 0.02050781], 
     ..., 
     [ 0.0546875 , 0.03320312, 0.07519531, ..., 0.14355469, 
     0.0390625 , 0.06738281], 
     [ 0.06152344, 0.03808594, 0.07421875, ..., 0.04882812, 
     0.04296875, 0.09082031], 
     [ 0.06640625, 0.03515625, 0.07617188, ..., 0.14355469, 
     0.04003906, 0.06542969]]), ...] 

len(tempTracesHW[8]) = 7 

在我來說,數據沒有連接,並且我寧願不將它們串聯因爲它需要大量的內存,它代表了我一個很大的問題,所以我嘗試這種代碼轉換爲一組文件的使用它:

path ='C:\\Users\\user\\Traces' 
traces= os.listdir(path) 
for i in range(len(traces)): 
    HW = tempHW[i] 
    tempTracesHW.append([np.load(os.path.join(path, trace)) for trace in traces]) 
print (tempTracesHW) 

,我有結果是WR ong:

[[], [], [], [], [], [], [], [], [], array([[ 0.01437869, 0.01506449, 0.01579909, ..., 0.04166172, 
     0.0417285 , 0.04172079]], dtype=float32), array([[ 0.01437869, 0.01506449, 0.01579909, ..., 0.04166172, 
     0.0417285 , 0.04172079]], dtype=float32), array([[ 0.01437869, 0.01506449, 0.01579909, ..., 0.04166172, 
     0.0417285 , 0.04172079]], dtype=float32),... 


len(tempTracesHW[8])=0 

我真的需要幫助。

回答

0

您可能想過濾從os.listdir()獲得的文件列表 。並非每個文件可能包含有用的數據。 考慮fnmatch以匹配有意義的文件名。

from fnmatch import fnmatch 

path ='C:\\Users\\user\\Traces' 
traces= [ 
    file_name for file_name in os.listdir(path) 
       if fnmatch(file_name, "*.dat") 
] 
for i in range(len(traces)): 
    HW = tempHW[i] 
    tempTracesHW.append([np.load(os.path.join(path, trace)) for trace in traces]) 
print (tempTracesHW) 

此外,你可以嘗試:

for i, trace in enumerate(traces): 
    HW = tempHW[i] # unclear what HW does, assume influence on np.load() 
    tempTracesHW.append(np.load(os.path.join(path, trace))) 
+0

謝謝你的答案,但它給了我同樣的結果:[[] [] [] [] [] [] ,[],[],[],[],[array([[0.01437869,0.01506449,0.01579909,...,0.04166172, ,0.0417285,0.04172079]],dtype = float32),array [[0.01059266,0.01118085,0.0117973,...]。 ..,-0.01831369, -0.01834194,-0.01837534]],dtype = float32), – Guillaume

+0

考慮第二個變更,如果'np.load()'返回仍然是[]您可能想要查看的幾個文件功能。 –

+0

非常感謝您的幫助,我正在檢查它 – Guillaume

相關問題