1
import numpy as np

X = np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1], [1, 0, 1, 0]])

y = np.array([0, 1, 0, 1])

counts = {}

print(X[y == 0])

# prints = [[0 1 0 1]
# [0 0 0 1]]

我想知道为什么要X[y==0]打印两个数据点。它不应该只打印[0 1 0 1]吗?

因为X[0]

4

1 回答 1

3

y == 0 gives an array with same dimensions as y, with elements True where the corresponding element in y is 0, and False otherwise.

Here, y has 0 elements at indices 0 and 2. So, X[y == 0] gives you an array containing X[0] and X[2].

于 2020-02-14T22:31:06.940 回答