2016-05-13 59 views
0

我在Matlab中編寫代碼的一部分,然後進入python。在Matlab我有這樣將Matlab轉換爲Python標籤

labels=[1 2 49 49 50 50 51]; 

和在Python標籤我需要加載到have`labels = [ '1', '2', '49', '49', '50', '50' ?,'51' ]

我怎樣努力 o1 = scipy.io.loadmat('labels.mat') labels1=np.array(o1['labels']) 但我得到

[[array([[1]], dtype=uint8) array([[1]], dtype=uint8) 
    array([[1]], dtype=uint8) array([[2]], dtype=uint8) 
    array([[2]], dtype=uint8) array([[2]], dtype=uint8) 
    array([[49]], dtype=uint8) array([[49]], dtype=uint8) 
    array([[49]], dtype=uint8) array([[50]], dtype=uint8) 
    array([[50]], dtype=uint8) array([[50]], dtype=uint8) 
    array([[51]], dtype=uint8) array([[51]], dtype=uint8) 
    array([[51]], dtype=uint8) array([[52]], dtype=uint8) 
    array([[52]], dtype=uint8) array([[52]], dtype=uint8) 
    array([[53]], dtype=uint8) array([[53]], dtype=uint8) 
    array([[53]], dtype=uint8) array([[54]], dtype=uint8) 
    array([[54]], dtype=uint8) array([[54]], dtype=uint8)]] 

我需要這個,因爲這些都是下一步標籤:

Y=pdist(X, 'correlation') 
Z=hierarchy.linkage(Y, method='complete', metric='correlation') 
row_dendr = hierarchy.dendrogram(Z, labels=labels1) 

我可以用Matlab以其他方式將它保存起來嗎?我在循環中製作的那些標籤,因此它是一個很大的向量。 如果鍵入onlu打印O1我得到:

{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri May  13 20:11:14 2016', '__version__': '1.0', 'labels': array([[array([[1]],  dtype=uint8), array([[1]], dtype=uint8), 
    array([[1]], dtype=uint8), array([[2]], dtype=uint8), 
    array([[2]], dtype=uint8), array([[2]], dtype=uint8), 
    array([[49]], dtype=uint8), array([[49]], dtype=uint8), 
    array([[49]], dtype=uint8), array([[50]], dtype=uint8), 
    array([[50]], dtype=uint8), array([[50]], dtype=uint8), 
    array([[51]], dtype=uint8), array([[51]], dtype=uint8), 
    array([[51]], dtype=uint8), array([[52]], dtype=uint8), 
    array([[52]], dtype=uint8), array([[52]], dtype=uint8), 
    array([[53]], dtype=uint8), array([[53]], dtype=uint8), 
    array([[53]], dtype=uint8), array([[54]], dtype=uint8), 
    array([[54]], dtype=uint8), array([[54]], dtype=uint8)]], dtype=object), '__globals__': []} 
+0

我從來沒有使用scipy.io.loadmat功能,但看着文檔,它會返回一個字典。如果你輸入'print o1',你會得到什麼?如果它確實正在返回一本字典,我想你可能只能從這些值中提取數據 - 看起來你只是想要一個列表。 – flyingmeatball

+0

我編輯了我的帖子! –

+0

它看起來像你放入matlab中的標籤與你正在閱讀的python不匹配...你確定這些標籤反映了你正在閱讀的內容嗎? – flyingmeatball

回答

0

,如果你像這樣定義的標籤,並運行下面的代碼會發生什麼:

from itertools import chain 
labels1=o1['labels'] 
list(chain(*labels1))