我正在尝试绘制一个树状图来聚类数据,但这个错误阻止了我。我的约会在这里。
我首先选择了要使用的列:
df_euro = pd.read_csv('https://assets.datacamp.com/production/repositories/655/datasets/2a1f3ab7bcc76eef1b8e1eb29afbd54c4ebf86f2/eurovision-2016.csv')
samples = df_euro.iloc[:, 2:7].values[:42]
country_names = df_euro.iloc[:, 1].values[:42]
# Calculate the linkage: mergings
mergings = linkage(samples , method = 'complete')
# Plot the dendrogram
dendrogram(
mergings,
labels = y,
leaf_rotation = 90,
leaf_font_size = 6
)
plt.show()
但是我收到了这个我无法理解的错误。我用谷歌搜索并检查两者是否具有相同的形状 (1066,5) 和 (1066,)。features 和 country_names 都没有 NA。
我知道问题出在标签上,因为当我删除它时它运行良好,但我找不到解决它的方法。任何帮助将不胜感激。或者,如何将标签添加到树状图中?
我得到的错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-113-7fffdc847e5e> in <module>
4 mergings = linkage(feat, method = 'complete')
5 # Plot the dendrogram
----> 6 dendrogram(
7 mergings,
8 labels = target_col,
C:\ProgramData\Anaconda3\lib\site-packages\scipy\cluster\hierarchy.py in dendrogram(Z, p, truncate_mode, color_threshold, get_leaves, orientation, labels, count_sort, distance_sort, show_leaf_counts, no_plot, no_labels, leaf_font_size, leaf_rotation, leaf_label_func, show_contracted, link_color_func, ax, above_threshold_color)
3275 "'bottom', or 'right'")
3276
-> 3277 if labels and Z.shape[0] + 1 != len(labels):
3278 raise ValueError("Dimensions of Z and labels must be consistent.")
3279
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1476
1477 def __nonzero__(self):
-> 1478 raise ValueError(
1479 f"The truth value of a {type(self).__name__} is ambiguous. "
1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().