【问题标题】:Ignoring the first column of the kmeans cluster input data忽略 kmeans 集群输入数据的第一列
【发布时间】:2017-01-30 13:23:45
【问题描述】:


输入数据
(键、数据点)

A,1,2,0,1,2,1,2,1
B,2,2,3,1,1,1,1,0
C,3,1,2,3,4,5,0,1
D,1,2,0,1,2,5,0,1
....

我有上述格式的输入数据。我想对上述数据执行 Kmeans 聚类,忽略第一列,并想确定每个记录属于哪个中心。我已经丢弃了第一个 col(键)并能够使用下面的代码找到集群中心,但我希望输出格式如下
(key,cluster_center)
A,0
B,2
C,1
D,0
...

代码:

data = sc.textFile("/home/user/inputfile.txt")
parsedData = data.map(lambda line: array([long(x.strip()) for x in line.split(',')]))
model = KMeans.train(parsedData, 3, maxIterations=10, runs=10, initializationMode="random")
centers = model.clusterCenters
for center in centers: print(center)

【问题讨论】:

  • 有什么问题?我不明白。
  • 使用数组子集[1:]删除第一列?

标签: apache-spark pyspark cluster-analysis k-means


【解决方案1】:

filtered_cols = parsed_data.map(lambda x: x[1])
模型 = KMeans.train(filtered_cols, 3, maxIterations=10, runs=10, initializationMode="random")
中心 = model.clusterCenters
居中居中:打印(居中)

【讨论】:

    猜你喜欢
    • 2017-07-28
    • 2017-06-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2016-06-23
    • 2016-02-22
    • 1970-01-01
    • 2016-11-09
    相关资源
    最近更新 更多