应用 PCA 并保留总方差的百分比

Apply PCA and keep a percentage of the total variance

我想对特定数据集执行主成分分析,然后将主成分提供给 LogisticRegression 分类器。

具体来说,我想应用 PCA 并使用函数 computePrincipalComponentsAndExplainedVariance 保留总方差的 90%。

下面是读取数据集的代码:

// Load the data
val text = sparkSession.sparkContext.textFile("dataset.data")        
val data = text.map(line => line.split(',').map(_.toDouble))  
// Separate to label and features
val dataLP = data.map(t => (t(57), Vectors.dense(t.take(57)))) 

我不太确定如何执行 PCA 以保持总方差的 90%。

使用函数 computePrincipalComponentsAndExplainedVariance,return 值将是一个矩阵以及一个向量,其值表示为每个主成分解释的方差。来自 documentation

Returns: a matrix of size n-by-k, whose columns are principal components, and a vector of values which indicate how much variance each principal component explains

通过使用足够大的 k 作为输入,您可以简单地将向量中的数字相加,直到达到 90% 或以上,然后使用矩阵中的那么多列。