Spark ML 正则化 标准化 归一化 ---- spark 中的归一化
2023-04-18 12:52:53 时间
文章大纲
spark 中的归一化
MaxAbsScaler
- http://spark.apache.org/docs/latest/api/scala/org/apache/spark/ml/feature/MaxAbsScaler.html
MinMaxScaler
- http://spark.apache.org/docs/latest/api/scala/org/apache/spark/ml/feature/MinMaxScaler.html
Rescale each feature individually to a common range [min, max] linearly using column summary statistics, which is also known as min-max normalization or Rescaling. The rescaled value for feature E is calculated as:
For the case (E_{max} == E_{min}), (Rescaled(e_i) = 0.5 * (max + min)). note :
Since zero values will probably be transformed to non-zero values, output of the transformer will be DenseVector even for sparse input.
核心代码:主要就是计算 最大最小值
override def fit(dataset: Dataset[_]): MinMaxScalerModel = {
transformSchema(dataset.schema, logging = true)
val Row(max: Vector, min: Vector) = dataset
.select(Summarizer.metrics("max", "min").summary(col($(inputCol))).as("summary"))
.select("summary.max", "summary.min")
.first()
copyValues(new MinMaxScalerModel(uid, min.compressed, max.compressed).setParent(this))
}
注意: 上面的计算方式, 我们发现只能支持Vector的形式,那么对于但一值的情况如何转换呢?
val temp_mean = df_num.select(functions.mean(df_num.col("features"))).collect()(0)
println(temp_mean.getDouble(0))
val Row(mean2: Vector) =Row(Vectors.dense(temp_mean.getDouble(0)))
val df_num = spark.createDataFrame(Seq(
(0, 0.5, -1.0),
(1, 1.0, 1.0),
(2, 10.0, 2.0),
(3, 10.0, 0.0)
)).toDF("id", "features","result")
df.show()
参考文献
系列文章:
spark 中的 特征相关内容处理的文档
- http://spark.apache.org/docs/latest/api/scala/org/apache/spark/ml/feature/index.html
概念简介
- https://blog.csdn.net/u014381464/article/details/81101551
参考:
- https://segmentfault.com/a/1190000014042959
- https://www.cnblogs.com/nucdy/p/7994542.html
- https://blog.csdn.net/weixin_34117522/article/details/88875270
- https://blog.csdn.net/xuejianbest/article/details/85779029
相关文章
- 自主Web服务器Http_Server
- Vue3中的setup语法糖、computed函数、watch函数
- 微信小程序完整项目实战(前端+后端)
- 20分钟上手ES6,不会ES6好意思说自己会JS ?
- vue3的基本使用(超详细)
- VSCode安装配置使用教程(最新版超详细保姆级含插件)一文就够了
- Vue3通透教程【五】Vue3中的响应式数据 reactive函数、ref函数
- 2022最新版40个前端练手项目【附视频+源码】
- 【面试大全-Nginx】-Nginx Web服务器
- 2023年新年烟花代码(背景音乐完整版)
- 你评论,我赠书~【哈士奇赠书 - 12期】-〖爬虫逆向进阶实战〗参与评论,即可有机获得
- 前端面试八股文(超详细)
- webpack看这一篇就够了
- 瞧不上alert 老古董?使用alert实现一个精美的弹窗
- Http中你必须知道那点事
- 推荐5个很牛的开源项目
- 5分钟搞懂 强缓存与协商缓存
- 超详细的VSCode下载和安装教程以及解决VSCode下载速度特别慢的问题
- Vue3实战教程(快速入门)
- 【HTML | CSS | JAVASCRIPT】耗时一下午,整理出了一个精美的响应式登陆注册表单(附源码)