I am using a custom metric function with scipy's cdist function. The custom function is something like
def cust_metric(u,v):
dist = np.cumsum(np.gcd(u,v) * k)
return dist
where k is an arbitrary coefficient.
Ideally, I was hoping to pass k as an argument when calling cdist like so:
d_ar = scipy.spatial.distance.cdist(arr1, arr2, metric=cust_metric(k=7))
However, this throws an error.
I was wondering if there is a simple solution that I may be missing? A quick but non-elegant fix is to declare k as a global variable and adjust it when needed.
According to its documentation, the value for
metricshould be a callable (or a string for a particular fixed collection). In your case you could obtain that throughI do imagine your actual callable would look somewhat different since the moment
uandvare 2D arrays, thenp.cumsumreturns an array, while the callable is supposed to produce a scalar. For example: