Created
December 1, 2014 11:36
-
-
Save laing20333/735b3cc6f587dcf619c3 to your computer and use it in GitHub Desktop.
cython example
This file contains 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
cimport numpy as np | |
cimport cython | |
import numpy as np | |
def kp_distance(np.ndarray object_x, np.ndarray object_y, float Wc): | |
''' Distance function for two objects | |
''' | |
cdef float res = 0.0 | |
cdef unsigned attr_idx | |
for attr_idx in xrange(0, len(object_x)): | |
cur_type = type(object_x[attr_idx]) | |
if( (cur_type == str) or (cur_type == np.string_) ): | |
# categorical attribute | |
if (object_x[attr_idx] == object_y[attr_idx]): | |
res = res + Wc | |
else: | |
# numerical attribute | |
tmp = object_x[attr_idx] - object_y[attr_idx] | |
res = res + tmp * tmp | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment