Skip to content

Instantly share code, notes, and snippets.

@acadien
Created December 10, 2011 17:15
Show Gist options
  • Save acadien/1455628 to your computer and use it in GitHub Desktop.
Save acadien/1455628 to your computer and use it in GitHub Desktop.
Weave used to improve speed of K-nearest neighbors algorithm
#Python Euclidean distance
dist(a,b):
return sum([(i-j)**2 for i,j in zip(a,b)])**0.5
#Scipy-Weave Euclidean distance (x10 faster than standard Python implementation)
distcode = """
double c=0.0;
for(int i=0;i<64;i++)
c += (a[i]-b[i])*(a[i]-b[i]);
return_val=sqrt(c);
"""
def dist(a,b):
return weave.inline(distcode,['a','b'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment