Created
December 10, 2011 17:15
-
-
Save acadien/1455628 to your computer and use it in GitHub Desktop.
Weave used to improve speed of K-nearest neighbors algorithm
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
#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