我试图通过 ml.linalg 方法使用 PCA 来减少广泛的数据集(51 个特征,约 1300 个个体),如下所示:
1)将我的列命名为一个列表:
features = indi_prep_df.select([c for c in indi_prep_df.columns if c not in{'indi_nbr','label'}]).columns
Run Code Online (Sandbox Code Playgroud)
2)导入必要的库
from pyspark.ml.feature import PCA as PCAML
from pyspark.ml.linalg import Vector
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.linalg import DenseVector
Run Code Online (Sandbox Code Playgroud)
3)将特征折叠为 DenseVector
indi_feat = indi_prep_df.rdd.map(lambda x: (x[0], x[-1], DenseVector(x[1:-2]))).toDF(['indi_nbr','label','features'])
Run Code Online (Sandbox Code Playgroud)
4)删除除了保留索引的功能之外的所有内容:
dftest = indi_feat.drop('indi_nbr','label')
Run Code Online (Sandbox Code Playgroud)
5)实例化PCA对象
dfPCA = PCAML(k=3, inputCol="features", outputCol="pcafeats")
Run Code Online (Sandbox Code Playgroud)
6)并尝试拟合模型
PCAout = dfPCA.fit(dftest)
Run Code Online (Sandbox Code Playgroud)
但我的模型无法收敛(错误如下)。我尝试过的事情: - 均值填充或零填充 NA 和 Null 值(视情况而定) - 减少特征数量(减少到 25 个,然后我改用 SKlearn 的 PCA)
Py4JJavaError: An error occurred …Run Code Online (Sandbox Code Playgroud)