Created
May 28, 2015 08:06
-
-
Save FedericoV/0e7d6d8c8794a99a7a42 to your computer and use it in GitHub Desktop.
Cosine Similarity that handles NaN with Numba
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numba | |
@numba.jit(target='cpu', nopython=True) | |
def fast_cosine(u, v): | |
m = u.shape[0] | |
udotv = 0 | |
u_norm = 0 | |
v_norm = 0 | |
for i in range(m): | |
if (np.isnan(u[i])) or (np.isnan(v[i])): | |
continue | |
udotv += u[i] * v[i] | |
u_norm += u[i] * u[i] | |
v_norm += v[i] * v[i] | |
u_norm = np.sqrt(u_norm) | |
v_norm = np.sqrt(v_norm) | |
if (u_norm == 0) or (v_norm == 0): | |
ratio = 1.0 | |
else: | |
ratio = udotv / (u_norm * v_norm) | |
return ratio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you considered adding this to Sklearn?