matplotlib:带有传奇的散点图作为类的字符串

发布于 2025-01-17 16:20:52 字数 957 浏览 1 评论 0原文

绘制二元分类的散点分布['cat', 'dog']

X, y = make_classification(n_samples=1000, n_features=2, n_redundant=0,
    n_clusters_per_class=1, weights=[0.9], flip_y=0, random_state=1,)

counter = Counter(y)

for label, _ in counter.items():
    row_ix = np.where(y == label)[0]
    plt.scatter(X[row_ix, 0], X[row_ix, 1], label=label)
plt.legend()
plt.show()

输出:

在此处输入图像描述

我想替换 01 传说与。我在这里:

for label, _ in counter.items():
    row_ix = np.where(y == label)[0]
    plt.scatter(X[row_ix, 0], X[row_ix, 1], label=['cat', 'dog'])
plt.legend()
plt.show()

输出:

在此处输入图像描述

Plotting scatter distribution for binary classification ['cat', 'dog']:

X, y = make_classification(n_samples=1000, n_features=2, n_redundant=0,
    n_clusters_per_class=1, weights=[0.9], flip_y=0, random_state=1,)

counter = Counter(y)

for label, _ in counter.items():
    row_ix = np.where(y == label)[0]
    plt.scatter(X[row_ix, 0], X[row_ix, 1], label=label)
plt.legend()
plt.show()

output:

enter image description here

I wanted to replace 0, 1 legend with cat, dog. Here I go:

for label, _ in counter.items():
    row_ix = np.where(y == label)[0]
    plt.scatter(X[row_ix, 0], X[row_ix, 1], label=['cat', 'dog'])
plt.legend()
plt.show()

output:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2025-01-24 16:20:52

您需要在循环的的每次迭代中更改传奇标签的值,一种可能是使用 zip

for item, animal in zip(counter.items(), ['cat', 'dog']):
    row_ix = np.where(y == item[0])[0]
    plt.scatter(X[row_ix, 0], X[row_ix, 1], label=animal)
plt.legend()
plt.show()

You need to change the value of the legend's label in every iteration of the for loop, one possibility being the use of zip

for item, animal in zip(counter.items(), ['cat', 'dog']):
    row_ix = np.where(y == item[0])[0]
    plt.scatter(X[row_ix, 0], X[row_ix, 1], label=animal)
plt.legend()
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文