簡體   English   中英

Python樹狀圖必須有ak使得(k \\ choose 2)= n)

[英]Python dendrogram there must be a k such that (k \choose 2)=n)

我試圖在Python中制作樹狀圖,以獲取某些數據的中值,然后讓我計算中值之間的歐幾里得距離。 我的某些數據最終變為負值,因此我必須獲取絕對值並抵消所有中位數。

如果我只有3個值進行比較,這似乎很好用,但是由於某種原因,如果我有4個或5個值,它給我一個錯誤,“必須是ak使得(k \\ choose 2)= n)”,但是如果我有6個值,僅給出最后4個值的樹狀圖。

我正在使用Python 3.7.1,有人知道是否存在某種錯誤嗎? 原因我無法理解我的代碼適用於3個值,不適用於4或5個值,並且如果我有6個值,則給出最后4個值的樹狀圖。

import numpy as np
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as shc

#Calculate the median values of each group & make an array
a=10
b=-2
c=5
d=2.1
data = np.array([a,b,c,d])

#Find the lowest value because you can't make a dendrogram with a negative number
low = np.min(data)

#Offset data by the absolute of the lowest value +1, cause a 0 value won't work on a dendrogram
offset = abs(low) + 1
offset_array = []

# v = value, add offset to all values & save as an array
for v in data:
    offset_array.append(v+offset)

#Make an array of the offset values to calculate distances
cluster = np.array(offset_array)

# Labels for each value
#headings = ['a', 'b', 'c', 'd']

df = np.array(cluster)

#Size of figure (x, y)
plt.figure(figsize=(5, 5))
ax = plt.subplot()
#Change x axis range as required
dt = 0.01
ax.semilogx(dt, np.exp(dt))
plt.title('Gram positive distance')
plt.xlabel('Euclidean distance')

dend = shc.dendrogram(shc.linkage(df, metric='euclidean'),
                      orientation='left', leaf_font_size=8, labels=headings)

我認為問題在於鏈接功能。 對於鏈接功能:“可以將n個維度上m個觀察向量的集合作為m x n數組傳遞。”

因此,我通過重塑df數組制作了一個新的測試變量:

測試= df.reshape(len(df),1)

然后將此新變量傳遞到樹狀圖函數中:

dend = shc.dendrogram(shc.linkage(test,metric ='euclidean'),direction ='left',leaf_font_size = 8,labels = heading)

暫無
暫無

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

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