【问题标题】:Pyspark Dataframes: Creating a features column to be used in the clustering in pythonPyspark Dataframes:创建要在 python 中的聚类中使用的特征列
【发布时间】:2019-07-25 23:29:33
【问题描述】:

我正在尝试在 pyspark 中创建实施 k-means 聚类。我使用 mnist 作为我的数据集,它有数百个具有整数值的列。

创建数据框后,当我尝试创建要在聚类中使用的特征列时,我不知道要为VectorAssembler 提供什么作为inputCols 参数。下面是我的代码

sc = SparkContext('local')
spark = SparkSession(sc)

df = spark.read.csv('mnist_train.csv')
df.show()
df_feat = df.select(*(df[c].cast("float").alias(c) for c in df.columns[0:]))
df_feat.show()

vecAssembler = VectorAssembler(inputCols = ???????, outputCol = "features")

对于我正在使用的这个大整数值数据,我应该为inputCols 设置什么参数?

【问题讨论】:

    标签: python dataframe pyspark cluster-analysis k-means


    【解决方案1】:

    Vectorassembler 需要列名列表来创建特征向量。因此,对于 mnist 数据集,您可以给他除标签之外的所有内容。例如:

    #I assume that df_feat.columns[0] is the column which contains the labels
    cols = df_feat.columns[1:]
    vecAssembler = VectorAssembler(inputCols = cols, outputCol = "features")
    

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 2018-08-22
      • 2019-12-01
      • 2018-09-25
      • 2017-05-15
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      相关资源
      最近更新 更多