【问题标题】:What does X_set[y_set == j, 0] mean?X_set[y_set == j, 0] 是什么意思?
【发布时间】:2019-01-07 21:05:09
【问题描述】:

最近,我一直在关注一个教程,其中我想出了以下代码

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)

这里,y_set 是一个具有二进制值的向量 0、1 和 X_set 是一个有两列的数组。 我特别不明白如何解释以下代码行

X_set[y_set == j, 0], X_set[y_set == j, 1]

【问题讨论】:

  • 我们很确定y_set == j, 0 将返回True 或False。然后,如果您的 X_set 由 pandas 类型定义。我们将按布尔值进行过滤。

标签: python numpy machine-learning


【解决方案1】:

这里发生了一些事情。现在,我将放弃循环,但我们知道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])

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

plt.scatter(filtered_values_in_first_column_of X_set, 
            filtered_values_in_second_column_of X_set)

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()

您可以在此处查看更多关于numpy indexing 的信息。一旦你分解了这些步骤,它并不太复杂,但我认为这是完成这项任务的一种不必要的复杂方式。

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

【讨论】:

  • 谢谢。这很有帮助。
  • 机器学习逻辑回归分类教程
猜你喜欢
  • 1970-01-01
  • 2017-07-20
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 2020-01-07
  • 1970-01-01
  • 2018-09-10
相关资源
最近更新 更多