 # Calculate the Euclidean distance using NumPy

## Calculate the Euclidean distance using NumPy

In simple terms, Euclidean distance is the shortest between the 2 points irrespective of the dimensions. In this article to find the Euclidean distance, we will use the NumPy library. This library used for manipulating multidimensional array in a very efficient way. Let’s discuss a few ways to find Euclidean distance by NumPy library.

Method #1: Using linalg.norm()

 `# Python code to find Euclidean distance` `# using linalg.norm()` `import` `numpy as np` `# initializing points in` `# numpy arrays` `point1 ``=` `np.array((``1``, ``2``, ``3``))` `point2 ``=` `np.array((``1``, ``1``, ``1``))` `# calculating Euclidean distance` `# using linalg.norm()` `dist ``=` `np.linalg.norm(point1 ``-` `point2)` `# printing Euclidean distance` `print``(dist)`

Output:

`2.23606797749979`

Method #2: Using dot()

 `# Python code to find Euclidean distance` `# using dot()` `import` `numpy as np` `# initializing points in` `# numpy arrays` `point1 ``=` `np.array((``1``, ``2``, ``3``))` `point2 ``=` `np.array((``1``, ``1``, ``1``))` `# subtracting vector` `temp ``=` `point1 ``-` `point2` `# doing dot product` `# for finding` `# sum of the squares` `sum_sq ``=` `np.dot(temp.T, temp)` `# Doing squareroot and` `# printing Euclidean distance` `print``(np.sqrt(sum_sq))`

Output:

`2.23606797749979`

Method #3: Using square() and sum()

 `# Python code to find Euclidean distance` `# using sum() and square()` `import` `numpy as np` `# initializing points in` `# numpy arrays` `point1 ``=` `np.array((``1``, ``2``, ``3``))` `point2 ``=` `np.array((``1``, ``1``, ``1``))` `# finding sum of squares` `sum_sq ``=` `np.``sum``(np.square(point1 ``-` `point2))` `# Doing squareroot and` `# printing Euclidean distance` `print``(np.sqrt(sum_sq))`

Output:

`2.23606797749979`

Last Updated on October 28, 2021 by admin

## numpy.argmax() in Pythonnumpy.argmax() in Python

numpy.argmax() in Python The numpy.argmax() function returns indices of the max element of the array in a particular axis. Syntax :  numpy.argmax(array, axis = None, out = None) Parameters :  array : Input array to work on axis : [int, optional]Along a

## numpy.where() in Pythonnumpy.where() in Python

numpy.where() in Python The numpy.where() function returns the indices of elements in an input array where the given condition is satisfied. Syntax :numpy.where(condition[, x, y]) Parameters: condition : When True, yield x, otherwise yield y. x, y : Values from which to choose. x,

## numpy.sum() in Pythonnumpy.sum() in Python

numpy.sum() in Python numpy.sum(arr, axis, dtype, out) : This function returns the sum of array elements over the specified axis. Parameters : arr : input array. axis : axis along which we want to calculate the sum value. Otherwise, it will consider arr to

## numpy.multiply() in Pythonnumpy.multiply() in Python

numpy.multiply() in Python numpy.multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise. Syntax : numpy.multiply(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘multiply’)

## numpy.zeros() in Pythonnumpy.zeros() in Python

numpy.zeros() in Python The numpy.zeros() function returns a new array of given shape and type, with zeros. Syntax: numpy.zeros(shape, dtype = None, order = 'C') Parameters : shape : integer or sequence of integers order : C_contiguous or F_contiguous C-contiguous order in

## How to create a vector in Python using NumPyHow to create a vector in Python using NumPy

How to create a vector in Python using NumPy NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is

## How to inverse a matrix using NumPyHow to inverse a matrix using NumPy

How to inverse a matrix using NumPy The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value

## Convert a NumPy array to an imageConvert a NumPy array to an image

Convert a NumPy array to an image NumPy Or numeric python is a popular library for array manipulation. Since images are just an array of pixels carrying various color codes. NumPy can be used to convert an array into image. Apart from