MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / hamming

Function hamming

numpy_ml/utils/distance_metrics.py:109–132  ·  view source on GitHub ↗

Compute the Hamming distance between two integer-valued vectors. Notes ----- The Hamming distance between two vectors **x** and **y** is .. math:: d(\mathbf{x}, \mathbf{y}) = \\frac{1}{N} \sum_i \mathbb{1}_{x_i \\neq y_i} Parameters ---------- x,y : :py:c

(x, y)

Source from the content-addressed store, hash-verified

107
108
109def hamming(x, y):
110 """
111 Compute the Hamming distance between two integer-valued vectors.
112
113 Notes
114 -----
115 The Hamming distance between two vectors **x** and **y** is
116
117 .. math::
118
119 d(\mathbf{x}, \mathbf{y}) = \\frac{1}{N} \sum_i \mathbb{1}_{x_i \\neq y_i}
120
121 Parameters
122 ----------
123 x,y : :py:class:`ndarray <numpy.ndarray>` s of shape `(N,)`
124 The two vectors to compute the distance between. Both vectors should be
125 integer-valued.
126
127 Returns
128 -------
129 d : float
130 The Hamming distance between **x** and **y**.
131 """
132 return np.sum(x != y) / len(x)

Callers 1

test_hammingFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_hammingFunction · 0.72