|
52 | 52 | from sklearn.preprocessing.data import PolynomialFeatures
|
53 | 53 | from sklearn.exceptions import DataConversionWarning
|
54 | 54 |
|
| 55 | +from sklearn.pipeline import Pipeline |
| 56 | +from sklearn.cross_validation import cross_val_predict |
| 57 | +from sklearn.svm import SVR |
| 58 | + |
55 | 59 | from sklearn import datasets
|
56 | 60 |
|
57 | 61 | iris = datasets.load_iris()
|
@@ -1370,6 +1374,26 @@ def test_center_kernel():
|
1370 | 1374 | assert_array_almost_equal(K_pred_centered, K_pred_centered2)
|
1371 | 1375 |
|
1372 | 1376 |
|
| 1377 | +def test_cv_pipeline_precomputed(): |
| 1378 | + """Cross-validate a regression on four coplanar points with the same |
| 1379 | + value. Use precomputed kernel to ensure Pipeline with KernelCenterer |
| 1380 | + is treated as a _pairwise operation.""" |
| 1381 | + X = np.array([[3, 0, 0], [0, 3, 0], [0, 0, 3], [1, 1, 1]]) |
| 1382 | + y_true = np.ones((4,)) |
| 1383 | + K = X.dot(X.T) |
| 1384 | + kcent = KernelCenterer() |
| 1385 | + pipeline = Pipeline([("kernel_centerer", kcent), ("svr", SVR())]) |
| 1386 | + |
| 1387 | + # did the pipeline set the _pairwise attribute? |
| 1388 | + assert_true(pipeline._pairwise) |
| 1389 | + |
| 1390 | + # test cross-validation, score should be almost perfect |
| 1391 | + # NB: this test is pretty vacuous -- it's mainly to test integration |
| 1392 | + # of Pipeline and KernelCenterer |
| 1393 | + y_pred = cross_val_predict(pipeline, K, y_true, cv=4) |
| 1394 | + assert_array_almost_equal(y_true, y_pred) |
| 1395 | + |
| 1396 | + |
1373 | 1397 | def test_fit_transform():
|
1374 | 1398 | rng = np.random.RandomState(0)
|
1375 | 1399 | X = rng.random_sample((5, 4))
|
|
0 commit comments