Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong use of covariance to calculate within-class scatter matrix #169

Open
xiongtx opened this issue Mar 30, 2024 · 0 comments
Open

Wrong use of covariance to calculate within-class scatter matrix #169

xiongtx opened this issue Mar 30, 2024 · 0 comments

Comments

@xiongtx
Copy link

xiongtx commented Mar 30, 2024

In Chpt. 5, pg. 157 it says:

$$\Sigma_i = \frac{1}{n_i} \sum_{x \in D_i} (\mathbf{x} - \mathbf{m_i})(\mathbf{x} - \mathbf{m_i})^T$$

That should be $n_i - 1$, which is what np.cov uses to get an unbiased estimator.

Then in Chpt. 5, pg. 158 the within-class scatter matrix S_W is calculated as:

d = 13 # number of features
S_W = np.zeros((d, d))
for label,mv in zip(range(1, 4), mean_vecs):
    class_scatter = np.cov(X_train_std[y_train==label].T)
    S_W += class_scatter

The covariances are not being weighted by sample sizes. The correct implementation is:

for label,mv in zip(range(1, 4), mean_vecs):
    n_i = np.sum(y_train == label)
    class_scatter = (n_i - 1) * np.cov(X_train_std[y_train==label].T)
    S_W += class_scatter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant