K-Means聚类算法
Posted Plum_Brilliant
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K-Means聚类算法相关的知识,希望对你有一定的参考价值。
from pyspark.sql import Row
from pyspark.ml.clustering import KMeans,KMeansModel
from pyspark.ml.linalg import Vectors
def f(x):
rel = {}
rel[\'features\']=Vectors. \\
dense(str(x[2]),str(x[24]),str(x[28]),str(x[29]))
rel[\'label\'] = str(x[22])
return rel
data = spark.sparkContext.textFile("file:///home/hw17685187119/student2.txt").map(lambda line: line.split(\';\')).map(lambda p: Row(**f(p))).toDF()
kmeansmodel = KMeans().setK(3).setFeaturesCol(\'features\').setPredictionCol(\'prediction\').fit(data)
results = kmeansmodel.transform(data).collect()
for item in results:
print(str(item[0])+\' is predcted as cluster\'+ str(item[1]))
results2 = kmeansmodel.clusterCenters()
for item in results2:
print(item)
kmeansmodel.computeCost(data)
以上是关于K-Means聚类算法的主要内容,如果未能解决你的问题,请参考以下文章