MATH ML Learning Lab
A02
A02 · 20–30 min

协方差与相关性Covariance & Correlation

两个特征围绕各自均值的变化是否一起发生?Do two features vary together around their respective means?

前置知识:Prerequisites: B05:矩阵;B16:期望、方差与样本统计。B05: matrices; B16: expectation, variance and sample statistics.

复习前置:Review prerequisites: B05 · 矩阵与批量计算Matrices & Batch Computation · B16 · 期望、方差与样本统计Expectation, Variance & Sample Statistics

学完后你能:After this lesson you can: 中心化数据,解释经验协方差并识别未定义的相关系数。Center data, interpret empirical covariance and identify undefined correlation.

动手观察Explore

调整点云最后一个 y 坐标并切换预设,观察中心化后的协方差和相关系数。Adjust the last y-coordinate and switch presets to observe centered covariance and correlation.

从观察到解释From observation to explanation

先中心化Center first

对每个特征分别减去自己的样本均值,得到相对于中心的偏差。每个点的两个偏差同号时乘积为正,异号时为负。Subtract each feature’s sample mean to get deviations from the center. The product of a point’s deviations is positive if their signs agree, negative if they differ.

平均偏差乘积Average deviation products

协方差是这些乘积的平均。正值表示在这组数据中常同向变化,负值表示常反向变化;协方差矩阵对称,对角线是各特征的描述性方差。Covariance is the average of these products. A positive value indicates that deviations often move together in this dataset, while a negative value indicates opposite movement. The covariance matrix is symmetric and its diagonal contains descriptive variances.

去掉尺度Remove scale

相关系数用两个标准差归一化协方差。任一特征方差为零时分母为零,相关系数未定义。这里用 1/n 描述手中的样本;估计总体时另有 1/(n−1) 的条件与用途。Correlation normalizes covariance by the two standard deviations. If either variance is zero, the denominator is zero and correlation is undefined. We use 1/n to describe the available sample; 1/(n−1) has a separate estimation purpose and conditions.

公式与符号Formula and symbols

n 是样本数,xᵢⱼ 是样本 i 的特征 j,x̄ⱼ 是该特征均值;C 是采用 1/n 的经验协方差矩阵。相关系数 r 要求两个方差 C₁₁、C₂₂ 均大于零。n is sample count, xᵢⱼ feature j of sample i, and x̄ⱼ that feature’s mean. C is the empirical covariance matrix using 1/n. Correlation r requires both variances C₁₁ and C₂₂ to be positive.

留意这个区别Watch for this

零相关不等于独立;协方差带单位并受特征缩放影响。Zero correlation does not imply independence. Covariance has units and changes when features are rescaled.

继续探索Explore further

课后练习

检验你的理解