Chi-square distance calculation is a statistical method, generally measures similarity between 2 feature matrices. Such distance is generally used in many applications like similar image retrieval, image texture, feature extractions etc. The Chi-square distance of 2 arrays ‘x’ and ‘y’ with ‘n’ dimension is mathematically calculated using below formula :
In this article, we will learn how to calculate Chi-square distance using Python. Below given 2 different methods for calculating Chi-square Distance. Let’s see both of them with examples.
Method #1: Calculating Chi – square distance manually using above formula.
Input : a = [1, 2, 13, 5, 45, 23] b = [67, 90, 18, 79, 24, 98] Output : The Chi-square distance is : 133.55428601494035 Input : a = [91, 900, 78, 30, 602, 813] b = [57, 49, 36, 759, 234, 928] Output : The Chi-square distance is : 814.776999405035
Method #2: Using scipy.stats.chisquare() method
Syntax: scipy.stats.chisquare(f_obs, f_exp=None, ddof=0, axis=0)
==> f_obs : array1
==> f_exp : array2, optional
==> ddof(Delta degrees of freedom – adjustment for p-value) : int, optional
==> axis : int or None, optional
The default value of ddof and axis is 0.
==> chisq : float or ndarray
==> p-value of the test : float or ndarray
- numpy.random.chisquare() in Python
- Python | Calculate distance and duration between two places using google distance matrix API
- Minkowski distance in Python
- Python | Distance-time GUI calculator using Tkinter
- Python | Calculate Distance between two places using Geopy
- Python - Bray-Curtis distance between two 1-D arrays
- Python - Distance between collections of inputs
- Python | Sympy Line.distance() method
- Python - Actual order index distance
- Python | Maximum distance between elements
- Python | Calculate City Block Distance
- Python - Distance between occurrences
- Python - Find the Levenshtein distance using Enchant
- turtle.distance() function in Python
- Python – Sympy Polygon.distance() method
- Rearrange a string so that all same characters become d distance away
- ML | Intercluster and Intracluster Distance
- Mahotas - Distance from binary image
- Pandas - Compute the Euclidean distance between two series
- Calculate the Euclidean distance using NumPy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.