What does the numpy.linalg.norm function?

Question:

What is the function of numpy.linalg.norm method?

In this Kmeans Clustering sample the numpy.linalg.norm function is used to get the distance between new centroids and old centroids in the movement centroid step but I cannot understand what is the meaning by itself

Could somebody give me a few ideas in relation to this Kmeans clustering context?

What is the norm of a vector?

Asked By: bgarcial

||

Answers:

numpy.linalg.norm is used to calculate the norm of a vector or a matrix.


This is the help document taken from numpy.linalg.norm:

numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)[source]

enter image description here


This is the code snippet taken from K-Means Clustering in Python:

# Euclidean Distance Caculator
def dist(a, b, ax=1):
    return np.linalg.norm(a - b, axis=ax)

It take order=None as default, so just to calculate the Frobenius norm of (a-b), this is ti calculate the distance between a and b( using the upper Formula).


Answered By: Kinght 金

numpy.linalg.norm function is used to get the sum from a row or column of a matrix.Suppose ,

>>> c = np.array([[ 1, 2, 3],
...               [-1, 1, 4]])
>>> LA.norm(c, axis=0)
array([ 1.41421356,  2.23606798,  5.        ])
>>> LA.norm(c, axis=1)
array([ 3.74165739,  4.24264069])
>>> LA.norm(c, ord=1, axis=1)
array([6, 6])

Answered By: Sihat Afnan

I am not a mathematician but here is my layman’s explanation of “norm”:

A vector describes the location of a point in space relative to the origin. Here’s an example in 2D space for the point [3 2]:

Point in 2D space

The norm is the distance from the origin to the point. In the 2D case it’s easy to visualize the point as the diametrically opposed point of a right triangle and see that the norm is the same thing as the hypotenuse.

Point in 2D space showing relationship between norm and hypotenuse

However, In higher dimensions it’s no longer a shape we describe in average-person language, but the distance from the origin to the point is still called the norm. Here’s an example in 3D space:

Point in 3D space showing the norm

I don’t know why the norm is used in K-means clustering. You stated that it was part of determing the distance between the old and new centroid in each step. Not sure why one would use the norm for this since you can get the distance between two points in any dimensionality* using an extension of the from used in 2D algebra:

Formula for distance between 2 points in 2D space

You just add a term for each addtional dimension, for example here is a 3D version:

Formula for distance between 2 points in 3D space

*where the dimensions are positive integers

Answered By: Robb Dunlap
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.