无监督学习K-means聚类
object Demo06KKK { def main(args: Array[String]): Unit = { val spark: SparkSession = SparkSession .builder() .master("local[*]") .appName("Demo2Person") .getOrCreate() import spark.implicits._ import org.apache.spark.sql.functions._ val kmeansDF: Dataframe = spark.read .format("csv") .option("sep", ",") .schema("x Double,y Double") .load("sparkproject/data/kmeans") val kmeansdata: Dataframe = kmeansDF.as[(Double, Double)] .map { case (x: Double, y: Double) => { val denseVec: linalg.Vector = Vectors.dense(Array(x, y)) Tuple1(denseVec) } }.toDF("features") val km: KMeans = new KMeans() .setK(2) val model: KMeansModel = km.fit(kmeansData) val resDF: Dataframe = model.transform(kmeansData) resDF.show(1000,false) } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)