Scala StreamingKMeans设置种子()
我需要为种子指定一个特定值来训练StreamingKMeans。当我跑的时候Scala StreamingKMeans设置种子(),scala,apache-spark,apache-spark-mllib,Scala,Apache Spark,Apache Spark Mllib,我需要为种子指定一个特定值来训练StreamingKMeans。当我跑的时候 val km = new StreamingKMeans(3, 1.0, "points") km.setRandomCenters(10, 0.5) val newmodel = km.latestModel.update(featureVectors, 1.0, "points") val prediction3 = id_features.map(x=> (x._1, newmodel.predict(x
val km = new StreamingKMeans(3, 1.0, "points")
km.setRandomCenters(10, 0.5)
val newmodel = km.latestModel.update(featureVectors, 1.0, "points")
val prediction3 = id_features.map(x=> (x._1, newmodel.predict(x._2)))
它很好用。但当我准备用种子的时候:
km.setRandomCenters(10, 0.5).setSeed(6250L)
我得到一个错误:
value setSeed不是org.apache.spark.mllib.clustering.StreamingKMeans的成员
在这种情况下,我如何设置种子?错误告诉您,
org.apache.spark.mllib.clustering.StreamingKMeans
没有setSeed
成员(您可以从验证;奇怪的是,KMeans
类中确实存在此方法,但StreamingKMeans
没有此方法)
然而,一切都没有失去…;-)
setRandomCenters
方法采用3个参数,第三个是随机种子。它的值默认为Utils.random.nextLong
。要执行所需操作,应将该行更改为:
km.设定速度(10,0.5).设定速度(6250升)
致:
<代码>公里数(10,0.56250升)
更新:顺便说一句,Spark利用了函数式编程范式。因此,在
StreamingKMeans
实例上调用.setRandomCenter
等方法通常不会修改(或变异)该实例。相反,它会创建一个新实例,并对其应用修改
在代码中,您实际上放弃了由SetRandomCenters
所做的更改,因为您不存储结果。您的代码应该更像这样:
val km=新的流线Kmeans(3,1.0,“点”)。设置随机中心(10,0.5)
val newmodel=km.latestModel.update(featureVectors,1.0,“点”)
val prediction3=id_features.map(x=>(x._1,newmodel.predict(x._2)))
“Spark利用了函数式编程范式。因此,在(…)实例上调用(…)之类的方法通常不会修改(或变异)该实例”-“数据结构”(RDD
,Dataset
)和传统MLlib API可能是这样,但在ML API中,这是完全错误的。只是说…@user10465355是的。查看了代码之后,这确实改变了实例。