normalize a vector to have unit norm using the given p norm

Solutions on MaxInterview for normalize a vector to have unit norm using the given p norm by the best coders in the world

showing results for - "normalize a vector to have unit norm using the given p norm"
Manuel
09 Sep 2018
1# Normalize a vector to have unit norm using the given p-norm
2
3from pyspark.ml.linalg import Vectors
4svec = Vectors.sparse(4, {1: 4.0, 3: 3.0})
5df = spark.createDataFrame([(Vectors.dense([3.0, -4.0]), 
6                             svec)], ["dense", "sparse"])
7normalizer = Normalizer(p=2.0, inputCol="dense", 
8                        outputCol="features")
9normalizer.transform(df).head().features
10# DenseVector([0.6, -0.8])
11normalizer.setParams(inputCol="sparse", 
12                     outputCol="freqs").transform(df).head().freqs
13# SparseVector(4, {1: 0.8, 3: 0.6})
14params = {normalizer.p: 1.0, normalizer.inputCol: "dense", 
15          normalizer.outputCol: "vector"}
16normalizer.transform(df, params).head().vector
17# DenseVector([0.4286, -0.5714])
18normalizerPath = temp_path + "/normalizer"
19normalizer.save(normalizerPath)
20loadedNormalizer = Normalizer.load(normalizerPath)
21loadedNormalizer.getP() == normalizer.getP()
22# True