PySpark - MinMaxScaler

news/2025/11/23 21:09:10/文章来源:https://www.cnblogs.com/zhangzhihui/p/19261489

 

from pyspark.ml.feature import MinMaxScalerscaler = MinMaxScaler(inputCol='features', outputCol='scaled_features')
scaler_model = scaler.fit(df)
scaled_df = scaler_model.transform(df)
scaled_df.show(truncate=False)

 

+---+--------------+---------------+
|id |features      |scaled_features|
+---+--------------+---------------+
|0  |[1.0,0.1,-1.0]|(3,[],[])      |
|1  |[2.0,1.1,1.0] |[0.5,0.1,0.5]  |
|2  |[3.0,10.1,3.0]|[1.0,1.0,1.0]  |
+---+--------------+---------------+

Please help me understand the values of the scaled_features column.

 

Here is what’s happening in your scaled_features column and why you see values like (3,[],[]) for the first row.


✅ How MinMaxScaler Works

MinMaxScaler rescales each feature column-wise to the range [0, 1]:

1

Applied per column, not per row.


📌 Your Input

Row 0: [1.0, 0.1, -1.0]
Row 1: [2.0, 1.1,  1.0]
Row 2: [3.0,10.1,  3.0]

Let’s compute min and max for each column:

Feature IndexValuesMinMaxRange
0 1.0, 2.0, 3.0 1.0 3.0 2.0
1 0.1, 1.1, 10.1 0.1 10.1 10.0
2 -1.0, 1.0, 3.0 -1.0 3.0 4.0

📌 Compute Scaled Values

Row 1 (id = 1) → [2.0, 1.1, 1.0]

Index 0:

(2.0−1.0)/(3.0−1.0)=1/2=0.5

Index 1:

(1.1−0.1)/(10.1−0.1)=1/10=0.1

Index 2:

(1.0−(−1.0))/(3.0−(−1.0))=2/4=0.5

✔ Output: [0.5, 0.1, 0.5]

Row 2 (id = 2) → [3.0, 10.1, 3.0]

All are max values → scale to 1:

[1.0, 1.0, 1.0]


❗ Why Row 0 → (3,[],[]) (all zeros)

For row 0:

[1.0, 0.1, -1.0]

Index 0:

(1.0−1.0)/2.0=0

Index 1:

(0.1−0.1)/10.0=0

Index 2:

(−1.0−(−1.0))/4.0=0

👉 All scaled values are 0:

[0.0, 0.0, 0.0]

But Spark uses SparseVector format to store vectors with many zeros:

(3, [], [])

This means:

  • size = 3

  • no non-zero indices = []

  • no non-zero values = []

This is equivalent to:

[0.0, 0.0, 0.0]

✅ Summary

idfeaturesscaled_featuresMeaning
0 [1.0,0.1,-1.0] (3,[],[]) all zeros → min values
1 [2.0,1.1,1.0] [0.5,0.1,0.5] mid-range values
2 [3.0,10.1,3.0] [1.0,1.0,1.0] max values

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/974225.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ubuntu 无网络连接/无网络标识解决方法

ubuntu 无网络连接/无网络标识解决方法问题描述:Ubuntu物理机突然无法SSH远程,打开物理机发现无网络连接/无网络标识 并且wifi和有线网络均无法使用(Ubuntu 没有可用的网络设备) 最终发现问题原因: 傻X 内核自动升…

EPS操作基础:无人机地形测量

加载osgb模型 1、软件主界面点击“三维测图”丨“osgb数据转换” 2、软件主界面点击“三维测图”丨“加载本地倾斜模型” 由DOM和DSM生成垂直倾斜模型 一、加载模型 1、软件主界面点击“三维测图”丨“生成垂直摄影模型…

[清华集训 2014] Sum

求 \(\sum\limits_{d=1}^{n} (-1)^{\lfloor d\sqrt{r} \rfloor}\) 的值。 \(T \leq 10^4\) 组数据,\(n \leq 10^9\),\(r \leq 10^4\)。首先特判 \(\sqrt{r}\) 为整数的情况,若 \(\sqrt{r}\) 为偶数则答案为 \(n\),…

深入解析:HiTooler File Finder: macOS上速度碾压Spotlight,媲美「Everything」的文件搜索神器

深入解析:HiTooler File Finder: macOS上速度碾压Spotlight,媲美「Everything」的文件搜索神器pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !i…

P13552 鱼类考古学

转移与操作到变得更方便感觉上先做与运算肯定不优,尝试证明。\(a\otimes b=a+b-(a|b)\),故 \((a\otimes b)+c=a+b-(a|b)+c\geq (a+b)\otimes c=a+b+c-((a+b)|c)\)。这是因为 \(a|b\leq \max(a,b)\)。那么我们现在就是…

P14134 【MX-X22-T5】「TPOI-4E」Get MiN? Get MeX!

构造二进制分组以快速查询我们先观察当存在 \(0\) 时会发生什么。那么此时 \(\min\) 一定为 \(0\),即如果使用一操作那么等价于求 \(\operatorname{mex}\),用二操作那么等价于求 \(-\operatorname{mex}\)。我们发现,…

并查集的板子和最小生成树

做到的题目是 修路 修路成本 通过几个人认识 想到哪写到哪了 #include<bits/stdc++.h> using namespace std; int n;int cost;int num; struct node {int u,v,w;bool operator < (const node &it) const{…

uniapp本地打包详细教程 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

Java高效开发实战:10个让代码质量飙升的黄金法则(2025版)

法则1:日志优化 - 使用结构化日志与异步处理 在微服务架构下,传统日志已难以满足复杂场景需求。现代实践是采用结构化日志配合异步处理: // 使用SLF4J + Logback配置异步日志// 记录结构化日志 logger.info("用…

使用injected Provider在remix中调试合约的坑 -- 时间(或者最新块)更新不及时 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

《道德经》第三十八章 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025年必收藏的8款AI论文写作神器!助你高效搞定学术写作

本文分享2025年必收藏的8款AI论文写作神器。校园里不少同学用AI写论文遇查重率高难题,作者与舍友搜罗讨论后发现这些工具。如PaperFine,20分钟可生成2万字论文,有在线改稿等强大功能;TXYZ能助力文献理解;PaperNex…

bfs dfs板子默写 真的好怕像上次一样这种题AC不了啊

Bfs的板子 #include <bits/stdc++.h> using namespace std; int n,m;int visited[100][100];int g[100][100]; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; void bfs(int x,int y) {queue<pair<int,int&…

贪心题目

贪心题目 CF2166C Cyclic Merging 尽可能用较小的数来进行较多的合并操作。所以把数按从小到大的顺序删去,删去时选择和较小的一边合并。 合并的过程用双向列表维护就好了。 P3462 POI 2007 ODW-Weights 对于每个 \(…

【做题记录】HZOJ 多校-数论/多校-字符串/多校-图论Ⅲ

I Ⅱ Ⅲ 26. 数论 H. [arc137_d]Prefix XORs 对于普通的前缀和,有 \(S_i^{(k)}=\sum_{j=1}^{i}{i-j+k-1\choose k-1}a_j\),其中 \(S_i^{(k)}\) 表示 \(k\) 次前缀和后 \(a_i\) 的值。 那么对于异或和,\(a_i\) 对第…

2025软件工程L班

结对编程 https://github.com/102301617/roll-call-system.git https://www.cnblogs.com/skjs/p/19261416 https://www.bilibili.com/video/BV1d9UNBhEvH/?vd_source=e1544ea07b79d2fb2225a86c33224c43 一、结对探索(…

2025-11-23

CF Problem - 1632C - Codeforces(枚举)(1600) 先加再按位或,一定最优 #include <bits/stdc++.h> using namespace std; #define LL long long const LL mod = 998244353; const int N=2e5+10;void solve() {…