我想在我的数据集中使用Kmeans算法后在我的数据集中添加预测列,我不知道如何实现这一点。下面是我到目前为止使用的代码(摘自spark文档)
case class MyCase(sId: Int, tId:Int, label:Double, sAuthors:String, sYear:Int, sJournal:String,
tAuthors:String, tYear:Int,tJournal:String, yearDiff:Int,nCommonAuthors:Int,isSelfCitation:Boolean
,isSameJournal:Boolean,cosSimTFIDF:Double,sInDegrees:Int,sNeighbors:Array[Long],tInDegrees:Int ,tNeighbors:Array[Long],inDegreesDiff:Int,commonNeighbors:Int,jaccardCoefficient:Double)
val men = Encoders.product[MyCase]
val ds: Dataset[MyCase] = transformedTrainingSetDF.as(men)
//KMEANS
val numOfClusters = 2
val kmeans = new KMeans().setK(numOfClusters).setSeed(1L)
val model = kmeans.fit(ds)
// Evaluate clustering by computing Within Set Sum of Squared Errors.
val WSSSE = model.computeCost(ds)
println(s"Within Set Sum of Squared Errors = $WSSSE")
// Shows the result.
println("Cluster Centers: ")
model.clusterCenters.foreach(println)
使用KMeansModel.transform:
def transform(dataset: Dataset[_]): DataFrame
转换输入数据集。
model.transform(ds)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。