yellowbrick t-SNE fit引发了ValueError

时间:2018-02-23 14:32:09

标签: python visualization yellowbrick

我正在尝试用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()

1 个答案:

答案 0 :(得分:2)

经过一些试验:

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需要是字符串的原因。此外,错误消息没有帮助。