我正在实现 Navie Bayes ,当他(我正在关注的教程的作者)使用该索引的索引和值可互换时,我感到困惑 他正在使用的代码是:
class NavieBayes:
def fit(self, X, y):
n_samples, n_features = np.shape(X)
self._classes = np.unique(y)
n_classes = len(self._classes)
self._mean = np.zeros((n_classes, n_features), dtype = np.float64)
for c in self._classes:
X_c = X[y==c]
self._mean[c , : ] = X_c.mean(axis = 0)
在For循环c中,它引用self._classes中任何元素的值。 “ X_c = X [y == c]”正在检查我们选择的元素“ c”是否等于y中的每个元素,如果是,则在该索引处返回1(“ True”)。
示例代码输入:
y = numpy.array([1, 2, 3, 3, 5, 3])
x = numpy.array([1, 2, 3, 4, 5, 6])
c = 3
print(y == c)
print(x[y==3])
示例代码输出:
[False, False, True, True, False, True]
[3, 4, 6]
在这里我们可以说c是元素的值,但是他在第二行中使用了元素的相同值对数组“ self._mean”(而不是我认为他应该使用该元素的索引)进行切片for循环,如果有人知道代码中的内容,请向我解释...