【问题标题】:What is the correct way to use pyspark VectorAssembler?使用 pyspark VectorAssembler 的正确方法是什么?
【发布时间】:2019-12-24 18:58:41
【问题描述】:

我正在尝试将所有功能列合并为一个

所以:

assembler = VectorAssembler(
    inputCols=feature_list,
    outputCol='features')

其中:

feature_list 是一个包含所有特征列名称的 Python 列表

然后

trainingData = assembler.transform(df)

但是当我这样做时:

VectorAssembler 的正确使用方法是什么?

非常感谢

【问题讨论】:

    标签: pyspark


    【解决方案1】:

    没有堆栈跟踪或df 示例,很难理解您的问题。

    但我还是会回答,根据documentation

    from pyspark.ml.linalg import Vectors
    from pyspark.ml.feature import VectorAssembler
    
    dataset = spark.createDataFrame(
        [(0, 18, 1.0, Vectors.dense([0.0, 10.0, 0.5]), 1.0)],
        ["id", "hour", "mobile", "userFeatures", "clicked"])
    
    dataset.show()
    
    # +---+----+------+--------------+-------+
    # | id|hour|mobile|  userFeatures|clicked|
    # +---+----+------+--------------+-------+
    # |  0|  18|   1.0|[0.0,10.0,0.5]|    1.0|
    # +---+----+------+--------------+-------+
    
    assembler = VectorAssembler(
        inputCols=["hour", "mobile", "userFeatures"],
        outputCol="features")
    
    output = assembler.transform(dataset)
    
    print("Assembled columns 'hour', 'mobile', 'userFeatures' to vector column 'features'")
    
    output.select("features", "clicked").show(truncate=False)
    
    # +-----------------------+-------+
    # |features               |clicked|
    # +-----------------------+-------+
    # |[18.0,1.0,0.0,10.0,0.5]|1.0    |
    # +-----------------------+-------+
    

    Example Source Code

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 1970-01-01
      • 2021-11-24
      • 2021-09-23
      • 2017-04-07
      • 2013-01-09
      • 2013-03-19
      • 2019-05-11
      • 2018-02-17
      相关资源
      最近更新 更多