Last active
March 23, 2017 01:12
-
-
Save tejasshah93/8dc7dcbc7463f36e171d5db66355ff87 to your computer and use it in GitHub Desktop.
PySparNN custom distance metric
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
import sklearn | |
class UserCustomDistance(MatrixMetricSearch): | |
def __init__(self, features, records_data): | |
super(UserCustomDistance, self).__init__(features, records_data) | |
self.matrix = self.matrix | |
self.max_overlap = self.matrix.shape[0] # for testing purpose | |
@staticmethod | |
def features_to_matrix(features): | |
return features | |
@staticmethod | |
def vstack(matrix_list): | |
return _np.vstack(matrix_list) | |
def _transform_value(self, v): | |
return v | |
def user_distance_metric(self, u, v): | |
"""Calculates overlap between two sparse vectors. | |
Subtracts this overlap from the max_overlap for distance | |
""" | |
rep = _sparse.csr_matrix(_np.minimum(u.A, v.A)) | |
return self.max_overlap - rep.sum() | |
def _distance(self, a_matrix): | |
return sklearn.metrics.pairwise.pairwise_distances( | |
a_matrix, self.matrix, lambda u, v: self.user_distance_metric(u, v)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment