Understanding numpy.linalg.norm () in IPython

I am creating a linear regression model for supervised learning.

I have a bunch of data points plotted on the graph (x1, y1), (x2, y2), (x3, y3), etc., where x is the real data and y values ​​are the values ​​of the training data.

As part of the next step when writing the basic nearest neighbor algorithm, I want to create a distance metric to measure the distance (and similarity) between two instances.

If I wanted to write a generic function to calculate the L-Norm distance in ipython, I know that many people use numpy.linalg.norm (arr, ord =, axis =). I got confused about how to format an array of data arrays so that it correctly calculates L-norm values.

If I had only two data points, say (3, 4) and (5, 9), would my array have to look like this with each data point value on the same line?

arry = ([[3,4] 
         [5,9]])

or will he need to look like where all the values ​​of the x axis are on one line and y on another?

arry = ([[3,5]
         [4,9]])
+4
source share
1 answer

numpy.linalg.norm(x) == numpy.linalg.norm(x.T)where .Tstands for transposition. So it does not matter.

For instance:

>>> import numpy as np
>>> x = np.random.rand(5000, 2)
>>> x.shape
(5000, 2)
>>> x.T.shape
(2, 5000)
>>> np.linalg.norm(x)
57.82467111195578
>>> np.linalg.norm(x.T)
57.82467111195578

Edit:

Given that your vector is basically

x = [[real_1, training_1],
     [real_2, training_2],
      ...
     [real_n, training_n]]

then the Frobenius norm basically calculates

np.sqrt(np.sum(x**2))

Are you sure this is the right indicator. There are a number of other rules. Here 3

np.sum((x[:,0]**2 - x[:,1]**2) # N-dimensional euclidean norm
np.sqrt(np.sum(x[:,0]**2) + np.sum(x[:,1]**2)) # L^2 norm
np.sqrt(x[:,0].dot(x[:,1])) # sqrt dot product
+6
source

Source: https://habr.com/ru/post/1528851/


All Articles