8000 Added basic unit test for callable kernel · scikit-learn/scikit-learn@8e5bf21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e5bf21

Browse files
Georgi PeevGeorgi Peev
Georgi Peev
authored and
Georgi Peev
committed
Added basic unit test for callable kernel
1 parent 4b831be commit 8e5bf21

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

sklearn/svm/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def fit(self, X, y, sample_weight=None):
217217
if hasattr(X, 'shape'):
218218
self.shape_fit_ = X.shape
219219
else:
220-
self.shape_fit_ = (_num_samples(X), )
220+
self.shape_fit_ = (_num_samples(X),)
221221

222222
# In binary case, we need to flip the sign of coef, intercept and
223223
# decision function. Use self._intercept_ and self._dual_coef_ internally.

sklearn/svm/tests/test_svm.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,3 +1012,31 @@ def test_gamma_scale():
10121012
# gamma is not explicitly set.
10131013
X, y = [[1, 2], [3, 2 * np.sqrt(6) / 3 + 2]], [0, 1]
10141014
assert_no_warnings(clf.fit, X, y)
1015+
1016+
1017+
def test_callable_kernel():
1018+
data = ["foo", "foof", "b", "a", "qwert", "1234567890", "abcde", "bar", "", "q"]
1019+
targets = [1, 1, 2, 2, 1, 3, 1, 1, 2, 2]
1020+
targets = np.array(targets)
1021+
1022+
def string_kernel(X, X2):
1023+
assert isinstance(X[0], str)
1024+
len = _num_samples(X)
1025+
len2 = _num_samples(X2)
1026+
ret = np.zeros((len, len2))
1027+
smaller = np.min(ret.shape)
1028+
ret[np.arange(smaller), np.arange(smaller)] = 1
1029+
return ret
1030+
1031+
svc = svm.SVC(kernel=string_kernel)
1032+
svc.fit(data, targets)
1033+
svc.score(data, targets)
1034+
svc.score(np.array(data), targets)
1035+
1036+
svc.fit(np.array(data), targets)
1037+
svc.score(data, targets)
1038+
svc.score(np.array(data), targets)
1039+
1040+
def test_string_kernel():
1041+
# meaningful string kernel test
1042+
assert True

0 commit comments

Comments
 (0)
0