We now have all the needed statistics to normalize the data. Recall that the formula for normalizing a variable x
is as follows:
In order to implement this, we will wrap the needed computations into a function and invoke it for both the training and test datasets:
- Use the SparkR
selectExpr
expression to calculate the normalized version of each variable using the formula above. - Also, create a new variable with old appended to the name, which preserves the original value of the variable. After testing, you should remove these extra variables to save space, but it is good to retain them while debugging:
normalize_it <- function (x) { selectExpr(x, "age as ageold","(age-age_mean)/ age_std as age", "mass as massold","(mass-mass_mean)/ mass_std as mass", "triceps as tricepsold", "(triceps-triceps_mean)/ triceps_std as triceps", "pressure as pressureold", ...