norm 1 of a vector code example
Example: Normalize a vector to have unit norm using the given p-norm
from pyspark.ml.linalg import Vectors
svec = Vectors.sparse(4, {1: 4.0, 3: 3.0})
df = spark.createDataFrame([(Vectors.dense([3.0, -4.0]),
svec)], ["dense", "sparse"])
normalizer = Normalizer(p=2.0, inputCol="dense",
outputCol="features")
normalizer.transform(df).head().features
normalizer.setParams(inputCol="sparse",
outputCol="freqs").transform(df).head().freqs
params = {normalizer.p: 1.0, normalizer.inputCol: "dense",
normalizer.outputCol: "vector"}
normalizer.transform(df, params).head().vector
normalizerPath = temp_path + "/normalizer"
normalizer.save(normalizerPath)
loadedNormalizer = Normalizer.load(normalizerPath)
loadedNormalizer.getP() == normalizer.getP()