Python 索引和索引值是否在到达sclicing数组时相同(Navie Bayes)
我正在实施Navie Bayes,当他(我下面的教程的作者)交替使用索引和该索引处的值时,我感到困惑 他使用的代码是:Python 索引和索引值是否在到达sclicing数组时相同(Navie Bayes),python,numpy,syntax,naivebayes,numpy-slicing,Python,Numpy,Syntax,Naivebayes,Numpy Slicing,我正在实施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,
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.\u类中任何元素的值。
“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.\u mean”(我认为他应该使用该元素的索引)在for循环的第二行中,如果有人知道代码中发生了什么,请向我解释…如果你说
c
是元素的值,那么它在for循环的每次迭代中都会发生变化c
接受数组中不同类的值y
。在每次迭代中,您将获取与类c
相对应的X
中的值,并计算每列的平均值,并将其存储在数组self.\u mean
的第c
行中。