我有一个后续问题:How to normalize with PCA and scikit-learn .
我正在创建一个情绪检测系统,我现在要做的是:
- 拆分所有情绪的数据(将数据分布到多个子集)。
- 将所有数据加在一起(将多个子集合并为一组)
- 获取组合数据的PCA参数(self.pca = RandomizedPCA(n_components=self.n_components, whiten=True).fit(self.data))
- 针对每种情绪(每个子集),将 PCA 应用于该情绪(子集)的数据。
我应该在以下位置进行规范化:步骤 2) 规范化所有组合数据,以及步骤 4) 规范化子集。
编辑
我想知道对所有数据的规范化和对子集的规范化是否相同。现在,当我根据@BartoszKP 的建议尝试简化我的示例时,我发现我对规范化工作的理解是错误的。两种情况下的规范化工作方式相同,所以这是一种有效的方式,对吧? (见代码)
from sklearn.preprocessing import normalize
from sklearn.decomposition import RandomizedPCA
import numpy as np
data_1 = np.array(([52, 254], [4, 128]), dtype='f')
data_2 = np.array(([39, 213], [123, 7]), dtype='f')
data_combined = np.vstack((data_1, data_2))
#print(data_combined)
"""
Output
[[ 52. 254.]
[ 4. 128.]
[ 39. 213.]
[ 123. 7.]]
"""
#Normalize all data
data_norm = normalize(data_combined)
print(data_norm)
"""
[[ 0.20056452 0.97968054]
[ 0.03123475 0.99951208]
[ 0.18010448 0.98364753]
[ 0.99838448 0.05681863]]
"""
pca = RandomizedPCA(n_components=20, whiten=True)
pca.fit(data_norm)
#Normalize subset of data
data_1_norm = normalize(data_1)
print(data_1_norm)
"""
[[ 0.20056452 0.97968054]
[ 0.03123475 0.99951208]]
"""
pca.transform(data_1_norm)
请您参考如下方法:
是的,如 the documentation 中所述,normalize 所做的是独立于其他样本缩放单个样本:
Normalization is the process of scaling individual samples to have unit norm.
这在 the documentation of the Normalizer class 中有额外解释:
Each sample (i.e. each row of the data matrix) with at least one non zero component is rescaled independently of other samples so that its norm (l1 or l2) equals one.
(强调我的)
