如何在Sparklyr中正确使用特征转换功能

时间:2018-11-07 15:36:36

标签: r apache-spark sparklyr

假设我想在数据集的每一列上使用ft_max_abs_scaler。这是文档中的内容:

sc <- spark_connect(master = "local")
iris_tbl <- sdf_copy_to(sc, iris, name = "iris_tbl", overwrite = TRUE)

features <- c("Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width")

iris_tbl <- iris_tbl %>%
  ft_vector_assembler(input_col = features,
                      output_col = "features_temp") %>%
  ft_max_abs_scaler(input_col = "features_temp",
                     output_col = "features")

请注意,ft_vector_assembler将创建一个新列features_temp,而ft_max_abs_scaler将创建另一个新列features。现在假设我想将向量分解成单独的列,我必须这样做:

iris_tbl <- iris_tbl %>% sdf_separate_column("features", into = features) 
# result in error because column name cannot be the same

由于没有删除列的好方法,我想知道是否有更好的方法可以在不保持创建新列的情况下使用Sparklyr进行特征转换。

1 个答案:

答案 0 :(得分:-1)

iris_tbl <- iris_tbl %>%
              ft_vector_assembler(input_cols = c("Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width"), output_col = "features") 
iris_tbl <- iris_tbl %>% 
              sdf_separate_column("features", into = "new") 
iris_tbl