Skip to content

Instantly share code, notes, and snippets.

@laing20333
Created December 1, 2014 11:36
Show Gist options
  • Save laing20333/735b3cc6f587dcf619c3 to your computer and use it in GitHub Desktop.
Save laing20333/735b3cc6f587dcf619c3 to your computer and use it in GitHub Desktop.
cython example
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