Np linalg norm python что делает
Перейти к содержимому

# Np linalg norm python что делает

## numpy.linalg.norm#

This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.

Parameters : x array_like

Input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned.

Order of the norm (see table under Notes ). inf means numpy’s inf object. The default is None.

If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned. The default is None.

New in version 1.8.0.

If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.

New in version 1.10.0.

Norm of the matrix or vector(s).

Similar function in SciPy.

For values of ord < 1 , the result is, strictly speaking, not a mathematical ‘norm’, but it may still be useful for various numerical purposes.

## numpy.linalg.norm¶

This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.

x : array_like

Input array. If axis is None, x must be 1-D or 2-D.

Order of the norm (see table under Notes ). inf means numpy’s inf object.

If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned.

keepdims : bool, optional

If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.

New in version 1.10.0.

n : float or ndarray

Norm of the matrix or vector(s).

For values of ord <= 0 , the result is, strictly speaking, not a mathematical ‘norm’, but it may still be useful for various numerical purposes.

The following norms can be calculated:

ord norm for matrices norm for vectors
None Frobenius norm 2-norm
‘fro’ Frobenius norm
‘nuc’ nuclear norm
inf max(sum(abs(x), axis=1)) max(abs(x))
-inf min(sum(abs(x), axis=1)) min(abs(x))
0 sum(x != 0)
1 max(sum(abs(x), axis=0)) as below
-1 min(sum(abs(x), axis=0)) as below
2 2-norm (largest sing. value) as below
-2 smallest singular value as below
other sum(abs(x)**ord)**(1./ord)

The Frobenius norm is given by [R46]:

Since the ravel() method flattens an array without making any copies and ord specifies the type of norm that will be computed, the above usage is equivalent to:

But watch out! The function can calculate many different kinds of norms. And if you specify the ord argument, then matrices (arrays with ndim=2 ) are treated differently than vectors (arrays with ndim=1 ). This leads to a somewhat surprising result:

That is, even though ord=2 is the default behavior for vectors (and for vectors ord=2 does mean L2 norm), np.linalg.norm(x, ord=2) does not compute the L2 norm if x has more than 1 dimension. In fact, somewhat stupidly, ord=2 actually means something different for matrices in np.linalg.norm() .

In order to avoid getting tricked by this behavior, it’s worth taking a look at the API and some example use cases.

The np.linalg.norm() function has three important arguments: x , ord , and axis .

• x : this is an array-like input. If ord and axis are both None , then np.linalg.norm() will return the L2 norm of x.ravel() , which is a flattened (i.e. 1-dimensional) view of the array.
• ord : the type of norm. If you just pass in x and ord leaving axis as None , then x must be 1-dimensional or 2-dimensional, otherwise you will get an exception. Most commonly, when x is a vector, you will want ord=2 or ord=1 for L2 and L1 norms respectively. And when x is a matrix, you will want ord=’fro’ for the Frobenius norm. But NumPy does support other norms which you can look up in their docs.
• axis : the axis (or axes) to reduce with the norm operation. If this is an int then you will get vector norms along that dimension and if this is a 2-tuple, then you will get matrix norms along those dimensions.

That’s all a little too confusing for my preference. So instead of worrying about the combination of the number of dimensions of your x argument and ord , my recommendation is to use x by itself when you want an L2 norm or Frobenius norm (which is the same as the L2 norm on the flattened matrix):

Sometimes the ord and axis arguments are unavoidable (and I’ll show an example below), but only if 1) you need to reduce one or two of the dimensions or 2) you want to compute a norm other than L2.

#### Examples

##### Relative error

Let’s start with an easy example. A great use case for norms is computing the relative error between two arrays. For scalars, relative error is usually calculated with |x — x’| / |x| . Think of this like the size of the difference divided by the size of the original number.

Since norms are a way to encode the size of an array with a single number, you can use norms to do something very similar for arrays:

##### Normalization

You can normalize an array in order to force it to have a norm that you specify. For example, you can generate a random array that has an L2 norm of (approximately) 3. Just multiply every element by 3 and divide by the L2 norm:

If you wanted the vector have a unit norm, you would simply divide every element by the norm.

Sometimes, you may want to do this for your dataset. Say you have a matrix of data where every row is a sample and every column is a feature. If you want every row to have a unit norm, you can:

1. Compute the row-wise norms (reducing the column dimension)
2. Divide every element by its row norm

Here’s the code to normalize rows by their L2 norms for a randomly generated dataset with 10 rows and 3 columns:

Notice: row_l2_norms will be a vector with size 10. If we want to use broadcasting rules to divide every element in data , which has shape (10, 3) , we need to add a dummy dimension to give row_l2_norms shape (10, 1) . That’s what the None index is doing.

Additionally, since the input is a matrix and we’re passing in axis=1 , the function will compute the vector norm of each row. This means it’s safe to pass in ord=1 to get the row-wise L1 norms:

This would not work if data had more than 2 dimensions.

By the way, scikit-learn provides a convenience function so you can more easily normalize rows of a dataset to have L1 or L2 unit norms. Here’s an example of normalizing every row by its L1 norm:

## What does the numpy.linalg.norm function?

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?

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:

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

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).

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]:

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.

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:

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:

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