树状图:ValueError:序列的真值不明确.使用a.empty、a.bool()、a.item()、.any()或.



我正在尝试将树状图绘制到集群数据,但这个错误阻止了我。我的约会对象在这里"https://assets.datacamp.com/production/repositories/655/datasets/2a1f3ab7bcc76eef1b8e1eb29afbd54c4ebf86f2/eurovision-2016.csv">

我首先选择了使用的列

target_col = df_euro["To country"]
feat = df_euro[["Jury A","Jury B","Jury C","Jury D","Jury E"]]
#Convert them into ndarrays
x = feat.to_numpy(dtype ='float32')
y = target_col.to_numpy()
# Calculate the linkage: mergings
mergings = linkage(x, method = 'complete')
# Plot the dendrogram
dendrogram(
mergings,
labels = y,
leaf_rotation = 90,
leaf_font_size = 6
)
plt.show()

但我犯了一个我无法理解的错误。我在谷歌上搜索了一下,发现两者都有相同的形状(1066,5(和(1066,(两个特征和target_col 中都没有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:ProgramDataAnaconda3libsite-packagesscipyclusterhierarchy.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:ProgramDataAnaconda3libsite-packagespandascoregeneric.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().

问题是dendrogram中的labels关键字参数必须有一个__bool__方法,该方法返回它是否包含任何项,就像list中一样。因此,您需要做的唯一更改是在传递参数时转换为list

dendrogram(
mergings,
labels = list(y),
leaf_rotation = 90,
leaf_font_size = 6
)

所有其他线路都可以保持不变。

如果其他人正在搜索相同的问题,通过将标签转换为列表,它将起作用。

samples= df_euro.iloc[:, 2:7].values[:42]
country_names= list(df_euro.iloc[:, 1].values[:42])
mergings = linkage(samples, method='single')
# Plot the dendrogram
fig, ax = plt.subplots(figsize=(15, 10))
fig  = dendrogram(mergings, labels=country_names)
plt.show()

相关内容

最新更新