【问题标题】:Conversion Matlab to Python labels将 Matlab 转换为 Python 标签
【发布时间】:2016-09-10 01:31:02
【问题描述】:

我正在 Matlab 中编写部分代码,然后继续使用 python。在 Matlab 中,我有类似的标签

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

在 Python 中,我需要将其加载为具有`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 print 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__': []}

【问题讨论】:

  • 我从未使用过 scipy.io.loadmat 函数,但查看文档,它返回一个字典。如果你输入'print o1',你会得到什么?如果它确实返回了字典,我认为您可以只从值中提取数据 - 看起来您只需要一个列表。
  • 看起来您放入 matlab 的标签与您在 python 中读取的内容不匹配...您确定这些标签反映了您正在阅读的内容吗?

标签: python matlab type-conversion


【解决方案1】:

如果你定义这样的标签并运行以下代码会发生什么:

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

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 2014-03-05
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多