X_set [y_set == j,0]是什么意思?

Bec*_*cky 5 python numpy machine-learning

最近,我一直在关注一个教程,在那里我提出了以下代码

for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
        c = ListedColormap(('red', 'green'))(i), label = j)
Run Code Online (Sandbox Code Playgroud)

这里,y_set是具有二进制值的向量0,1并且X_set是具有两列的数组.我特别不理解如何解释以下代码行

X_set[y_set == j, 0], X_set[y_set == j, 1]
Run Code Online (Sandbox Code Playgroud)

rog*_*osh 4

这里发生了一些事情。现在,我将放弃循环,但我们知道它将j接受值,y_set因此将为零或一。首先制作两个数组:

import numpy as np

X_set = np.arange(20).reshape(10, 2)
y_set = np.array([0, 1, 1, 1, 0, 0, 1, 1, 0, 1])
Run Code Online (Sandbox Code Playgroud)

从上面来看,这段代码基本上是在做:

plt.scatter(filtered_values_in_first_column_of X_set, 
            filtered_values_in_second_column_of X_set)
Run Code Online (Sandbox Code Playgroud)

y_set正在提供过滤器。我们可以通过构建步骤来实现这一目标:

print("Where y_set == 0: Boolean mask.")
print(y_set == 0)
print()

print("All rows of X_set indexed by the Boolean mask")
print(X_set[y_set == 0])
print()

print("2D indexing to get only the first column of the above")
print(X_set[y_set == 0, 0])
print()
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看有关numpy索引的更多信息。一旦你分解了这些步骤,它就不会太复杂,但我认为这是实现这项任务的一种不必要的复杂方式。

通过循环for,他们可以根据值是否通过y_set等于 0 或 1 进行过滤,使用两种不同的颜色重复绘图。