我有Matlab(24 x 121)和標籤文件(1x 24)中生成的信號矩陣。之後予加載它,它是: 標籤從矩陣生成的矩陣與Matlab生成的Python圖形
[array(['1-2'],
dtype='<U3') array(['1-3'],
dtype='<U3')
array(['1-4'],
dtype='<U3') array(['2-2'],
dtype='<U3')
array(['2-3'],
dtype='<U3') array(['2-4'],
dtype='<U3')
array(['49-2'],
dtype='<U4') array(['49-3'],
dtype='<U4')
array(['49-4'],
dtype='<U4') array(['50-2'],
dtype='<U4')
array(['50-3'],
dtype='<U4') array(['50-4'],
dtype='<U4')
array(['51-2'],
dtype='<U4') array(['51-3'],
dtype='<U4')
array(['51-4'],
dtype='<U4') array(['52-2'],
dtype='<U4')
array(['52-3'],
dtype='<U4') array(['52-4'],
dtype='<U4')
array(['53-2'],
dtype='<U4') array(['53-3'],
dtype='<U4')
array(['53-4'],
dtype='<U4') array(['54-2'],
dtype='<U4')
array(['54-3'],
dtype='<U4') array(['54-4'],
dtype='<U4')]
和X
[[ 1.31973026 1.04553767 0.98759242 ..., 0.87344433 0.8734996
0.88148139]
[ 1.54466891 1.50167134 1.43233076 ..., 0.71953425 0.72355352
0.76595696]
[ 0.26974139 0.27669694 0.26486576 ..., 0.86765017 0.84838513
0.83147331]
...,
[ 1.28762992 1.21298643 1.08822084 ..., 0.81903216 0.7559759
0.62566092]
[ 0.96190193 0.97199575 0.93630357 ..., 0.88570213 0.78969704
0.69140163]
[ 1.70054223 1.6876721 1.66986342 ..., 0.90825585 0.92562056
0.93568893]]
我想要畫基於1-相關性度量相似性曲線圖,沒有顯示分支如果是重量> 0.7。我正在使用的代碼是:
import scipy.io
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm # Large set of colormaps
import pandas as pd
from scipy.cluster import hierarchy
from sklearn import datasets
from sklearn import metrics
from sklearn import cluster
from scipy.spatial.distance import pdist
import networkx as nx
from matplotlib import pyplot as plt
import pylab
import networkx as nx
from matplotlib import pyplot as plt
o1 = scipy.io.loadmat('out.mat')
X=(o1['out'])
print(X)
o1 = scipy.io.loadmat('labels.mat')
labels=o1['labels']
labels = labels[0]
print(labels)
corr=1-np.corrcoef(X)
print(corr)
m, n = np.shape(corr)
G = nx.Graph()
corr[np.where(corr>0.7)]=0
for i in range(m):
for j in range(n):
s=labels[i]
b=labels[j]
w=corr[i,j]
G.add_edge(s,b,weight=w)
nx.draw(G)
plt.show()
我得到一個錯誤
Traceback (most recent call last): File "C:/Users/Kristina/Desktop/NOBS/source/grafovi.py", line 36, in G.add_edge(s,b,weight=w) File "C:\Python34\lib\site-packages\networkx\classes\graph.py", line 706, in add_edge if u not in self.node: TypeError: unhashable type: 'numpy.ndarray'
我想不通的問題是什麼。
也許s和b是列表並且'G.add_edge'想要別的東西(例如元組)。列表不可用。所有不可變對象都是可散列的(typle等)。請參閱:https://docs.python.org/3/glossary.html並搜索'hashable'如果'G.add_edge'想要構造字典並使用s和b作爲關鍵字,則它必須是可散列的。 – Moritz