This thread explains how to calculate the Euclidean distance between two series, which is a measure of the distance between two points in space. The Euclidean distance is defined as the straight-line distance between two points in a two-dimensional or higher-dimensional space.
To compute the Euclidean distance, we need to take the square of the difference between corresponding elements of the two series, sum the squares, and then take the square root of the result. There are several ways to do this, and some examples of code are provided below:
1. Using Pandas library:
- In this technique, we use simply mathematical expression where we first subtract the two series element-wise and then square the result.
- Then, we use Pandas’
sum()
function to sum the squares and then get a square root of this result using**0.5
.
2. Using NumPy library:
- NumPy has built-in functions of
np.sqrt()
andnp.sum()
. - This is a simple technique in which we first subtract the two series and square the results, then we sum the results using the
np.sum()
function. - Finally, we take a square root of the previous result using the
np.sqrt()
function.
3. Using SciPy library:
-
euclidean()
is a built-in function in the SciPy library which calculates the Euclidean distance between two arrays/series or sequences of values by just passing them as arguments.
4. Using Scikit Learn library:
- Scikit-Learn has a method
euclidean_distances()
which can be used to perform this task. - A point to remember for this method is that it works in 2D arrays and sequences, and because of this, there is an additional
[ ]
in whichs1
ands2
are enclosed.
5. Using a custom function:
- You can also define a custom function yourself now that you know how the formula for calculating Euclidean distance between two series or points.
- In this custom function, we looped through each element of the series’ and subtracted them, then squared using
**2
, then the square results are summed using thesum()
function. - To complete the calculation, we use the
math.sqrt()
function to get a squared root of the previous result.
If you want to learn how to calculate the metric of Mean Squared Error (MSE), you can have a look at the thread of Computing mean squared error.