如何在 H2O 上对 PCA 使用拟合和变换

How to use fit and transform for PCA on H2O

我想在 H2O 上使用 PCA。在 sklearn 中,我们可以在训练集上应用 fit,然后在测试集上应用 transform。在这里,我试图在 H2O 中遵循相同的逻辑。在常见问题解答中,它说:

After the PCA model has been built using h2o.prcomp, use h2o.predict on the original data frame and the PCA model to produce the dimensionality-reduced representation. Use cbind to add the predictor column from the original data frame to the data frame produced by the output of h2o.predict. At this point, you can build supervised learning models on the new data frame.

基于此,我尝试了以下方法:

from h2o.transforms.decomposition import H2OPCA

trbb_pca = H2OPCA(k = 5, transform = "NORMALIZE", pca_method="GramSVD",
                   use_all_factor_levels=True, impute_missing=True,seed=24)

trbb_pca.train(x=trbb_cols, training_frame=train_h2o)

train_h2o_pca = train_h2o.cbind(trbb_pca.predict(train_h2o))
test_h2o_pca = test_h2o.cbind(trbb_pca.predict(test_h2o))

是不是在H2O中的train和test set上实现PCA的方式?

简短回答:是的。 H2O Python booklet 中有一个例子,为清楚起见复制在这里:

In [25]: from h2o.transforms.decomposition import H2OPCA

In [26]: pca_decomp = H2OPCA(k=2, transform="NONE", pca_method="Power")

In [27]: pca_decomp.train(x=range(0,4), training_frame=iris_df)

pca Model Build Progress: [#######################################] 100%

In [28]: pca_decomp
Out[28]: Model Details
=============
H2OPCA :  Principal Component Analysis
Model Key:  PCA_model_python_1446220160417_10

Importance of components:
                        pc1      pc2
----------------------  -------  --------
Standard deviation      7.86058  1.45192
Proportion of Variance  0.96543  0.032938
Cumulative Proportion   0.96543  0.998368

ModelMetricsPCA: pca

**
Reported on train data.
**
MSE: NaN
RMSE: NaN

In [29]: pred = pca_decomp.predict(iris_df)

pca prediction progress: [#######################################] 100%

In [30]: pred.head() # Projection results
Out[30]:
    PC1      PC2
-------  -------
5.9122   2.30344
5.57208  1.97383
5.44648  2.09653
5.43602  1.87168
5.87507  2.32935
6.47699  2.32553
5.51543  2.07156
5.85042  2.14948
5.15851  1.77643
5.64458  1.99191

在技术上有两种方法可以使用 Python 中的 PCA 估计器。旧方法位于此处 h2o.transforms.decomposition.H2OPCA。几年前,我们重写了 Python API 并移动了一些东西,包括将 PCA 变成一个合适的 "H2OEstimator",所以现在它也位于这里:h2o.estimators.pca.H2OPrincipalComponentAnalysisEstimator。这两种方法都有效,但对于新代码,我们推荐使用新方法,因为它与其他 H2O Estimators 一致。

API 是相同的,因此,虽然不是必需的,但如果您愿意,可以通过更改导入语句来切换到新的:

from h2o.transforms.decomposition import H2OPCA

至:

from h2o.estimators.pca import H2OPrincipalComponentAnalysisEstimator as H2OPCA