gpt4 book ai didi

python - 具有逻辑索引的散点图

转载 作者:行者123 更新时间:2023-12-01 09:09:45 24 4
gpt4 key购买 nike

我有一个 100x2 数组 D 和一个 100x1 数组 c(条目 +/- 1),我正在尝试绘制 D 中与 c = 1 相对应的列的散点图。

我尝试了这样的方法:plt.scatter(D[0][c==1],D[1][c==1])但它抛出了IndexError :数组索引过多

我知道我使用了列表理解或类似的东西。我对 Python 相当陌生,因此在格式上遇到了困难。

非常感谢。

最佳答案

概念

您可以使用 np.where 仅选择 D 中数组 C1 的行:

D = np.array([[0.25, 0.25], [0.75, 0.75]])
C = np.array([1, 0])

使用np.where,我们可以仅选择C1的行:

>>> D[np.where(C==1)]
array([[0.25, 0.25]])

示例根据您的实际数据:

D = np.random.randn(100, 2)
C = np.random.randint(0, 2, (100, 1))

valid = D[np.where(C.ravel()==1)]

import matplotlib.pyplot as plt
plt.scatter(valid[:, 0], valid[:, 1])

输出:

enter image description here

关于python - 具有逻辑索引的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51752634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com