Nir*_*anp 4 statistics scala apache-spark
我是apache spark和scala的新手.我有这样的数据集,我从csv文件中取出并使用scala将其转换为RDD.
+-----------+-----------+----------+
| recent | Freq | Monitor |
+-----------+-----------+----------+
| 1 | 1234 | 199090|
| 4 | 2553| 198613|
| 6 | 3232 | 199090|
| 1 | 8823 | 498831|
| 7 | 2902 | 890000|
| 8 | 7991 | 081097|
| 9 | 7391 | 432370|
| 12 | 6138 | 864981|
| 7 | 6812 | 749821|
+-----------+-----------+----------+
Run Code Online (Sandbox Code Playgroud)
我想计算z得分值或标准化数据.所以我正在计算每列的z得分,然后尝试将它们组合起来,以便得到标准比例.
这是我计算第一列z分数的代码
val scores1 = sorted.map(_.split(",")(0)).cache
val count = scores.count
val mean = scores.sum / count
val devs = scores.map(score => (score - mean) * (score - mean))
val stddev = Math.sqrt(devs.sum / count)
val zscore = sorted.map(x => math.round((x.toDouble - mean)/stddev))
Run Code Online (Sandbox Code Playgroud)
我如何计算每列?或者还有其他方法来规范化或标准化数据吗?
我的要求是指定等级(或等级).
谢谢
如果要标准化列,可以使用Spark MLlib中的StandardScaler类.数据应采用RDD[Vectors[Double]其中的形式,其中Vectors是MLlib Linalg包的一部分.您可以选择使用均值或标准差或两者来标准化数据.
import org.apache.spark.mllib.feature.StandardScaler
import org.apache.spark.mllib.linalg.Vectors
val data = sc.parallelize(Array(
Array(1.0,2.0,3.0),
Array(4.0,5.0,6.0),
Array(7.0,8.0,9.0),
Array(10.0,11.0,12.0)))
// Converting RDD[Array] to RDD[Vectors]
val features = data.map(a => Vectors.dense(a))
// Creating a Scaler model that standardizes with both mean and SD
val scaler = new StandardScaler(withMean = true, withStd = true).fit(features)
// Scale features using the scaler model
val scaledFeatures = scaler.transform(features)
Run Code Online (Sandbox Code Playgroud)
此scaledFeaturesRDD包含所有列的Z分数.
希望这个答案有所帮助 有关详细信息,请查看文档.
| 归档时间: |
|
| 查看次数: |
8764 次 |
| 最近记录: |