8000 FIX: make GridSearchCV work with precomputed kernels by daien · Pull Request #649 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX: make GridSearchCV work with precomputed kernels #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions sklearn/grid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,20 @@ def fit_grid_point(X, y, base_clf, clf_params, train, test, loss_func,
ind = np.arange(X.shape[0])
train = ind[train]
test = ind[test]
X_train = X[train]
X_test = X[test]
if hasattr(base_clf, 'kernel_function'):
# cannot compute the kernel values with custom function
raise ValueError(
"Cannot use a custom kernel function. "
"Precompute the kernel matrix instead.")
if getattr(base_clf, 'kernel', '') == 'precomputed':
# X is a precomputed square kernel matrix
if X.shape[0] != X.shape[1]:
raise ValueError("X should be a square kernel matrix")
X_train = X[np.ix_(train, train)]
X_test = X[np.ix_(test, train)]
else:
X_train = X[train]
X_test = X[test]
if y is not None:
y_test = y[test]
y_train = y[train]
Expand Down
45 changes: 44 additions & 1 deletion sklearn/tests/test_grid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sklearn.base import BaseEstimator
from sklearn.grid_search import GridSearchCV
from sklearn.datasets.samples_generator import make_classification
from sklearn.svm import LinearSVC
from sklearn.svm import LinearSVC, SVC
from sklearn.metrics import f1_score, precision_score


Expand Down Expand Up @@ -106,6 +106,49 @@ def test_grid_search_sparse_score_func():
# cv.score(X_[:180], y[:180]))


def test_grid_search_precomputed_kernel():
"""Test that grid search works when the input features are given in the
form of a precomputed kernel matrix """
X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0)

# compute the training kernel matrix corresponding to the linear kernel
K_train = np.dot(X_[:180], X_[:180].T)
y_train = y_[:180]

clf = SVC(kernel='precomputed')
cv = GridSearchCV(clf, {'C': [0.1, 1.0]})
cv.fit(K_train, y_train)

assert_true(cv.best_score >= 0)

# compute the test kernel matrix
K_test = np.dot(X_[180:], X_[:180].T)
y_test = y_[180:]

y_pred = cv.predict(K_test)

assert_true(np.mean(y_pred == y_test) >= 0)


def test_grid_search_precomputed_kernel_error_nonsquare():
"""Test that grid search returns an error with a non-square precomputed
training kernel matrix"""
K_train = np.zeros((10, 20))
y_train = np.ones((10, ))
clf = SVC(kernel='precomputed')
cv = GridSearchCV(clf, {'C': [0.1, 1.0]})
assert_raises(ValueError, cv.fit, K_train, y_train)


def test_grid_search_precomputed_kernel_error_kernel_function():
"""Test that grid search returns an error when using a kernel_function"""
X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0)
kernel_function = lambda x1, x2: np.dot(x1, x2.T)
clf = SVC(kernel=kernel_function)
cv = GridSearchCV(clf, {'C': [0.1, 1.0]})
assert_raises(ValueError, cv.fit, X_, y_)


class BrokenClassifier(BaseEstimator):
"""Broken classifier that cannot be fit twice"""

Expand Down
0