How do I use a function or set of functions from another module as a parameter in my own function I'm defining?
I'm trying to write a function that compares one vector q
and calculates its distance from each vector in a set of vectors x
. The function get_distances
should take the one vector q
, the set of vectors x
, and a parameter dist_method
. dist_method
is supposed to take any one of the distance calculations from scipy.spatial.distance and use that for the calculation, so I could call the function something like this: distances = get_distances(q, x, 'euclidean')
Here's the scipy reference page - get_distances
should be able to take any of the distance functions braycurtis, canberra, ... , sqeuclidean, wminkowski:
https://docs.scipy.org/doc/scipy/reference/spatial.distance.html
At the top of the file1.py
file where this function is located, I've imported scipy.spatial.distance, from which I thought I should have access to functions to use just like distance.euclidean(), but when I call get_distances
in the interpreter, I get AttributeError: 'module' object has no attribute 'dist_method'
.
I found a lot of answers already like the below that say functions are 'first-class objects' and that I should be able to use them as arguments just like any other argument, and I've tried using the **kwargs concept, but I can't put it all together.
- Passing functions with arguments to another function in Python?
- How do I pass a method as a parameter in Python
- https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments
Can someone help me understand what I'm missing?
KNN.py:
import numpy as np
import scipy.spatial.distance as dist
def get_distances(q, x, dist_method='euclidean', *args, **kwargs):
"""Query dataset to get distances for KNN
Given a numpy array of vectors x and
query point q, use dist_method to calculate
distance from q to each vector in x
Parameters:
q: tuple
x: numpy array
dist_method (optional): distance function from scipy.spatial.distance
Returns: list of distances
"""
return [dist.dist_method(q, x_i) for x_i in x]
def load_samples():
x = np.array([[1, 6],[2, 4],[3, 7],[6, 8],[7, 1],[8, 4]])
y = np.array([[7],[8],[16],[44],[50],[68]])
q = (4, 2)
return x, y, q
Here's what I'm doing in the interpreter:
>>> import KNN as knn
>>> x, y, q = knn.load_samples()
>>> x
array([[1, 6],
[2, 4],
[3, 7],
[6, 8],
[7, 1],
[8, 4]])
>>> d = knn.get_distances(q, x, 'cityblock')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "KNN.py", line 19, in get_distances
return [dist.dist_method(q, x_i) for x_i in x]
AttributeError: 'module' object has no attribute 'dist_method'