类内协方差矩阵

2024-04-19 22:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在通过在线jupyter笔记本工作,我被困在为什么我的代码不能工作:

https://nbviewer.jupyter.org/github/rasbt/pattern_classification/blob/master/dimensionality_reduction/projection/linear_discriminant_analysis.ipynb#LDA-in-5-steps

2.1类内散射矩阵SW:

我的代码如下:

def S_W_matrix(X,y):
    S_W = np.zeros((X.shape[1],X.shape[1]))
    n_classes = len(np.unique(y))
    for cl, mv in zip(range(1, n_classes), mean_vectors):
        class_sc_mat = np.zeros((X.shape[1],X.shape[1]))
        for row in X[y == cl]:
            row, mv = row.reshape(X.shape[1],1), mv.reshape((X.shape[1],1)) # make column vectors
            class_sc_mat += (row-mv).dot((row-mv).T)
        S_W += class_sc_mat
    return S_W

在我看来,我的n\u类是不正确的,但我一辈子都搞不清楚为什么会这样,如果我们为每个唯一的类创建矩阵?你知道吗

任何提示/建议,谢谢!你知道吗


Tags: 代码inforclnpzerosjupyterclass