簡體   English   中英

如何修復 Seaborn clustermap 矩陣?

[英]How to fix Seaborn clustermap matrix?

我有一個三列 csv 文件,我正在嘗試將其轉換為集群熱圖。 我的代碼如下所示:

sum_mets = pd.read_csv('sum159_localization_met_magma.csv')
df5 = sum_mets[['Phenotype','Gene','P']]

clustermap5 = sns.clustermap(df5, cmap= 'inferno',  figsize=(40, 40), pivot_kws={'index': 'Phenotype', 
                                  'columns' : 'Gene',
                                  'values' : 'P'})

然后我收到這個 ValueError:

ValueError: The condensed distance matrix must contain only finite values.

對於上下文,我的所有值都不為零。 我不確定它無法處理哪些值。 提前感謝任何可以提供幫助的人。

雖然你沒有 NaN,但你需要檢查你的觀察是否完整,因為下面有一個 pivot,例如:

df = pd.DataFrame({'Phenotype':np.repeat(['very not cool','not cool','very cool','super cool'],4),
                   'Gene':["Gene"+str(i) for i in range(4)]*4,
                   'P':np.random.uniform(0,1,16)})

pd.pivot(df,columns="Gene",values="P",index="Phenotype")

Gene    Gene0   Gene1   Gene2   Gene3
Phenotype               
not cool    0.567653    0.984555    0.634450    0.406642
super cool  0.820595    0.072393    0.774895    0.185072
very cool   0.231772    0.448938    0.951706    0.893692
very not cool   0.227209    0.684660    0.013394    0.711890

上面沒有 NaN 的樞軸,並且繪制得很好:

sns.clustermap(df,figsize=(5, 5),pivot_kws={'index': 'Phenotype','columns' : 'Gene','values' : 'P'})

在此處輸入圖像描述

但是假設我們有 1 少觀察:

df1 = df[:15]
pd.pivot(df1,columns="Gene",values="P",index="Phenotype")

Gene    Gene0   Gene1   Gene2   Gene3
Phenotype               
not cool    0.106681    0.415873    0.480102    0.721195
super cool  0.961991    0.261710    0.329859    NaN
very cool   0.069925    0.718771    0.200431    0.196573
very not cool   0.631423    0.403604    0.043415    0.373299

如果您嘗試調用 clusterheatmap,它會失敗:

sns.clustermap(df1, pivot_kws={'index': 'Phenotype','columns' : 'Gene','values' : 'P'})
The condensed distance matrix must contain only finite values.

我建議檢查缺失值是有意的還是錯誤的。 因此,如果您確實有一些缺失值,您可以繞過聚類但預先計算鏈接並將其傳遞給 function,例如使用以下相關性:

import scipy.spatial as sp, scipy.cluster.hierarchy as hc

row_dism = 1 - df1.T.corr()
row_linkage = hc.linkage(sp.distance.squareform(row_dism), method='complete')
col_dism = 1 - df1.corr()
col_linkage = hc.linkage(sp.distance.squareform(col_dism), method='complete')

sns.clustermap(df1,figsize=(5, 5),row_linkage=row_linkage, col_linkage=col_linkage)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM