0%

Spark 与离线评估


模型离线评估方法

HoldOut 检验

随机分为,训练集和测试集(70%训练集;30%测试集)

交叉检验

消除随机性,k个验证集和测试集

自助法(Bootstrap)

自助采样的验证方法,N次有放回的随机抽样(改变原有数据的分布,产生偏差)

时间切割

t时刻,t+1时刻(未来信息),分布在 (t0, tn), 时间切割划分测试集和训练集,切割比例, 3:1 OR 10:1,缺点:评估过程静态

离线 Replay

离线状态下对线上更新过程进行仿真,让整个评估过程“动”起来

基于 Spark 的离线评估方法实操

Spark 随机划分测试集和训练集
1
val Array(trainingSamples, testSamples) = samples.randomSplit(Array(0.9, 0.1))
交叉检验接口
1
2
3
4
5
6
val cv = new CrossValidator()
.setEstimator(modelPipeline) # 评估对象,构建模型pipeline
.setEvaluator(new BinaryClassificationEvaluator) # 设置评估所用的方法和指标
.setEstimatorParamMaps(paramGrid)
.setNumFolds(10) // Use 3+ in practice # 交叉检验中 k 的值
val cvModel = cv.fit(training)
1
2
3
4
5
6
//找到时间切割点
val quantile = smallSamples.stat.approxQuantile("timestampLong", Array(0.8), 0.05)
val splitTimestamp = quantile.apply(0)
//切割样本为训练集和测试集
val training = smallSamples.where(col("timestampLong") <= splitTimestamp).drop("timestampLong")
val test = smallSamples.where(col("timestampLong") > splitTimestamp).drop("timestampLong")

Welcome to my other publishing channels