gpt4 book ai didi

scala - Spark (Scala) 中的 K-means - 当模型由标准化数据制成时如何将集群编号映射回客户 ID

转载 作者:可可西里 更新时间:2023-11-01 16:02:56 26 4
gpt4 key购买 nike

以下代码用于获取模型。我面临的问题是将集群编号映射回客户 ID。这是因为,我的模型是在标准化数据上训练的,但带有客户 ID 的数据包含未标准化的数据。我不知道如何映射回去。

import org.apache.spark.SparkContext._
import org.apache.spark.mllib.clustering.{KMeans, KMeansModel}
import org.apache.spark.mllib.linalg.Vectors
import scala.collection.mutable.ArrayBuffer
import org.apache.spark.mllib.feature.StandardScaler
import org.apache.spark.mllib.util.MLUtils
// importing the data for clustering
val data = sc.textFile("hdfs://path/data_for_clus1")
val vectors = data.map(s => s.split('\1')).map(s => s.slice(1, s.size))
val parsedData = vectors.map(s => Vectors.dense(s.map(_.toDouble)))

val dataAsArray = parsedData.map(_.toArray)
// Using Standardscaler to standardize data
val features = dataAsArray.map(a => Vectors.dense(a))
val scaler = new StandardScaler(withMean = true, withStd = true).fit(features)
val scaledFeatures = scaler.transform(features)


val WSSEBuffer = ArrayBuffer[Double]();
// K-means
val numClusters = 20
val numIterations = 500
val clusters = KMeans.train(scaledFeatures, numClusters, numIterations)
val WSSSE = clusters.computeCost(scaledFeatures)

使用模型“集群”,我想为表“数据”中的客户 ID 提供集群编号。

最佳答案

将您的数据解析为

val newdata = Array[(customerID, featureArray)]

然后

newdata.map(customer => (customer._1, clusters.predict(customer._2)))

不确定这是否是一种有效的方式

关于scala - Spark (Scala) 中的 K-means - 当模型由标准化数据制成时如何将集群编号映射回客户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37739787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com