【问题标题】:yellowbrick t-SNE fit raises ValueErrorYellowbrick t-SNE 拟合引发 ValueError
【发布时间】:2018-08-03 15:28:16
【问题描述】:

我正在尝试使用 Yellowbrick 包中的 t-SNE 可视化数据。我遇到了一个错误。

import pandas as pd
from yellowbrick.text import TSNEVisualizer
from sklearn.datasets import make_classification

## produce random data
X, y = make_classification(n_samples=200, n_features=100,
                       n_informative=20, n_redundant=10,
                       n_classes=3, random_state=42)

## visualize data with t-SNE
tsne = TSNEVisualizer()
tsne.fit(X, y)
tsne.poof()

错误(由 fit 方法引发):

ValueError: The truth value of an array with more than one element
             is ambiguous. Use a.any() or a.all()

【问题讨论】:

  • 抱歉,这是自 numpy 1.13 发布以来出现的问题,将在 Yellowbrick 的 0.6 版本中修复。我已经为它打开了一个问题here

标签: python visualization yellowbrick


【解决方案1】:

在尝试了一些参数之后:

tsne.fit(X, y.tolist())

这不会引发错误,但不会产生输出。

最后,用字符串列表替换是可行的:

y_series = pd.Series(y, dtype="category")
y_series.cat.categories = ["a", "b", "c"]
y_list = y_series.values.tolist()

tsne.fit(X, y_list)
tsne.poof()

该库旨在分析文本数据集,也许这就是为什么 documented 不需要 y 是字符串的原因。此外,错误消息没有帮助。

【讨论】:

  • 这是一个很好的临时解决方案;不是 y 需要是字符串,只是可视化器试图找出类名。不幸的是,numpy 数组评估虚假性的方式已经改变,这导致了这个错误。正如我上面提到的,这将在即将推出的 Yellowbrick 0.6 版本中进行更新。
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 2016-04-27
  • 2019-03-21
  • 2022-01-11
  • 2020-10-09
相关资源
最近更新 更多