8000 TST make sure DistMetric is picklable with both predefined as well as… · scikit-learn/scikit-learn@dd043be · GitHub
[go: up one dir, main page]

Skip to content

Commit dd043be

Browse files
committed
TST make sure DistMetric is picklable with both predefined as well as custom metric
1 parent 42f31e8 commit dd043be

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

sklearn/neighbors/tests/test_dist_metrics.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
import pickle
23

34
import numpy as np
45
from numpy.testing import assert_array_almost_equal
@@ -9,6 +10,10 @@
910
from nose import SkipTest
1011

1112

13+
def dist_func(x1, x2, p):
14+
return np.sum((x1 - x2) ** p) ** (1. / p)
15+
16+
1217
d 8000 ef cmp_version(version1, version2):
1318
version1 = tuple(map(int, version1.split('.')[:2]))
1419
version2 = tuple(map(int, version2.split('.')[:2]))
@@ -122,18 +127,24 @@ def haversine_slow(x1, x2):
122127

123128

124129
def test_pyfunc_metric():
125-
def dist_func(x1, x2, p):
126-
return np.sum((x1 - x2) ** p) ** (1. / p)
127-
128130
X = np.random.random((10, 3))
129131

130132
euclidean = DistanceMetric.get_metric("euclidean")
131133
pyfunc = DistanceMetric.get_metric("pyfunc", func=dist_func, p=2)
132134

135+
# Check if both callable metric and predefined metric initialized
136+
# DistanceMetric object is picklable
137+
euclidean_pkl = pickle.loads(pickle.dumps(euclidean))
138+
pyfunc_pkl = pickle.loads(pickle.dumps(pyfunc))
139+
133140
D1 = euclidean.pairwise(X)
134141
D2 = pyfunc.pairwise(X)
135142

143+
D1_pkl = euclidean_pkl.pairwise(X)
144+
D2_pkl = pyfunc_pkl.pairwise(X)
145+
136146
assert_array_almost_equal(D1, D2)
147+
assert_array_almost_equal(D1_pkl, D2_pkl)
137148

138149

139150
if __name__ == '__main__':

0 commit comments

Comments
 (0)
0