MCPcopy
hub / github.com/scikit-learn/scikit-learn / string_kernel

Function string_kernel

sklearn/svm/tests/test_svm.py:1400–1410  ·  view source on GitHub ↗
(X1, X2)

Source from the content-addressed store, hash-verified

1398 y = np.array([1, 1, 2, 2, 1])
1399
1400 def string_kernel(X1, X2):
1401 assert isinstance(X1[0], str)
1402 n_samples1 = _num_samples(X1)
1403 n_samples2 = _num_samples(X2)
1404 K = np.zeros((n_samples1, n_samples2))
1405 for ii in range(n_samples1):
1406 for jj in range(ii, n_samples2):
1407 K[ii, jj] = X1[ii].count("A") * X2[jj].count("A")
1408 K[ii, jj] += X1[ii].count("B") * X2[jj].count("B")
1409 K[jj, ii] = K[ii, jj]
1410 return K
1411
1412 K = string_kernel(data, data)
1413 assert_array_equal(np.dot(X, X.T), K)

Callers 1

Calls 1

_num_samplesFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…