Spark ML:数据反规范化

时间:2018-05-10 18:51:10

标签: scala apache-spark dataframe machine-learning

我需要对Spark中使用ML的MinMaxScaler方法规范化的数据进行去规范化。

我可以按照以下步骤规范化我的数据:Spark: convert an RDD[LabeledPoint] to a Dataframe to apply MinMaxScaler, and after scaling get the normalized RDD[LabeledPoint]我之前发布过。

例如,原始df有两个第一列,并且在缩放后,结果为:

+------+--------------------+--------------------+
|labels|            features|      featuresScaled|
+------+--------------------+--------------------+
|   1.0|[6.0,7.0,42.0,1.1...|[1.0,0.2142857142...|
|   1.0|[6.0,18.0,108.0,3...|[1.0,1.0,1.0,1.0,...|
|   1.0|[5.0,7.0,35.0,1.4...|[0.0,0.2142857142...|
|   1.0|[5.0,8.0,40.0,1.6...|[0.0,0.2857142857...|
|   1.0|[6.0,4.0,24.0,0.6...|[1.0,0.0,0.0,0.0,...|
+------+--------------------+--------------------+

问题是,现在我需要做相反的过程:去标准化。

为此,我需要min向量中每个要素列的maxfeatures值,以及要非规范化的值。

要获得minmax,我要求MinMaxScaler如下:

val df_fitted = scaler.fit(df_all)
val df_fitted_original_min = df_fited.originalMin   // Vector
val df_fitted_original_max = df_fited.originalMax   // Vector

df_fited_original_min[1.0,1.0,7.0,0.007,0.052,0.062,1.0,1.0,7.0,1.0]
df_fited_original_max[804.0,553.0,143993.0,537.0,1.0,1.0,4955.0,28093.0,42821.0,3212.0]

另一方面,我有这样的DataFrame:

+--------------------+-----+--------------------+--------------------+-----+-----+--------------------+--------------------+--------------------+-----+
|               col_0|col_1|               col_2|               col_3|col_4|col_5|               col_6|               col_7|               col_8|col_9|
+--------------------+-----+--------------------+--------------------+-----+-----+--------------------+--------------------+--------------------+-----+
|0.009069428120139292|  0.0|9.015488712438252E-6|2.150418860440459E-4|  1.0|  1.0|0.001470074844665...|2.205824685144127...|2.780971210319238...|  0.0|
|0.008070826019024355|  0.0|3.379696051366339...|2.389342641479033...|  1.0|  1.0|0.001308210192425627|1.962949264985630...|1.042521123176856...|  0.0|
|0.009774715414895803|  0.0|1.299590589291292...|1.981673063697640...|  1.0|  1.0|0.001584395736407...|2.377361424206848...| 4.00879434193585E-5|  0.0|
|0.009631155146285946|  0.0|1.218569739510422...|2.016021040879828E-4|  1.0|  1.0|0.001561125874539...|2.342445354515269...|3.758872615157643E-5|  0.0|

现在,我需要应用以下等式来获取新值,但我不知道如何制作它。

X_original = ( X_scaled * (max - min) ) + min

对于DF中的每个位置,我必须将此等式与相应的maxmin值应用到向量中。

例如:DF的第一行和第一列是0.009069428120139292。在同一列中,相应的minmax值为:1.0804.0。 因此,非规范化值为:

X_den = ( 0.009069428120139292 * (804.0 - 1.0) ) + 1.0

有必要澄清在程序期间首先归一化的DF被修改。由于我需要应用去规范化(如果不是,最简单的方法是保留原始DF的副本)。

1 个答案:

答案 0 :(得分:0)

我从下面的https://stackoverflow.com/a/50314767/9759150得到了答案,再加上我的问题,我已经完成了非规范化过程。

让我们将normalized_df视为包含10列的数据框(在我的问题中显示):

import org.apache.spark.sql.Column
import org.apache.spark.sql.functions._

val updateFunction = (columnValue: Column, minValue: Int, maxValue: Int) =>
    (columnValue * ( lit(maxValue) - lit(minValue))) + lit(minValue)

val updateColumns = (df: DataFrame, minVector: Vector, maxVector: Vector, updateFunction: (Column, Int, Int) => Column) => {
    val columns = df.columns
    minVector.toArray.zipWithIndex.map{
      case (updateValue, index) =>
        updateFunction( col(columns(index.toInt)), minVector(index).toInt, maxVector(index).toInt ).as(columns(index.toInt))
    }
}

var dfUpdated = normalized_df.select(
  updateColumns(normalized_df, df_fitted_original_min, df_fitted_original_max, updateFunction) :_*
)