|
| 1 | +import time |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from sklearn.svm import SVR |
| 6 | +from sklearn.model_selection import GridSearchCV |
| 7 | +from sklearn.model_selection import learning_curve |
| 8 | +from sklearn.kernel_ridge import KernelRidge |
| 9 | + |
| 10 | +rng = np.random.RandomState(0) |
| 11 | + |
| 12 | +# ############################################################################# |
| 13 | +# Generate sample data |
| 14 | +X = 5 * rng.rand(10000, 1) |
| 15 | +y = np.sin(X).ravel() |
| 16 | + |
| 17 | +# Add noise to targets |
| 18 | +y[::5] += 3 * (0.5 - rng.rand(X.shape[0] // 5)) |
| 19 | + |
| 20 | +X_plot = np.linspace(0, 5, 100000)[:, None] |
| 21 | + |
| 22 | +# ############################################################################# |
| 23 | +# Fit regression model |
| 24 | +train_size = 100 |
| 25 | +svr = GridSearchCV(SVR(kernel='rbf', gamma=0.1), |
| 26 | + param_grid={"C": [1e0, 1e1, 1e2, 1e3], |
| 27 | + "gamma": np.logspace(-2, 2, 5)}) |
| 28 | + |
| 29 | +kr = GridSearchCV(KernelRidge(kernel='rbf', gamma=0.1), |
| 30 | + param_grid={"alpha": [1e0, 0.1, 1e-2, 1e-3], |
| 31 | + "gamma": np.logspace(-2, 2, 5)}) |
| 32 | + |
| 33 | +t0 = time.time() |
| 34 | +svr.fit(X[:train_size], y[:train_size]) |
| 35 | +svr_fit = time.time() - t0 |
| 36 | +print("SVR complexity and bandwidth selected and model fitted in %.3f s" |
| 37 | + % svr_fit) |
| 38 | + |
| 39 | +t0 = time.time() |
| 40 | +kr.fit(X[:train_size], y[:train_size]) |
| 41 | +kr_fit = time.time() - t0 |
| 42 | +print("KRR complexity and bandwidth selected and model fitted in %.3f s" |
| 43 | + % kr_fit) |
| 44 | + |
| 45 | +sv_ratio = svr.best_estimator_.support_.shape[0] / train_size |
| 46 | +print("Support vector ratio: %.3f" % sv_ratio) |
| 47 | + |
| 48 | +t0 = time.time() |
| 49 | +y_svr = svr.predict(X_plot) |
| 50 | +svr_predict = time.time() - t0 |
| 51 | +print("SVR prediction for %d inputs in %.3f s" |
| 52 | + % (X_plot.shape[0], svr_predict)) |
| 53 | + |
| 54 | +t0 = time.time() |
| 55 | +y_kr = kr.predict(X_plot) |
| 56 | +kr_predict = time.time() - t0 |
| 57 | +print("KRR prediction for %d inputs in %.3f s" |
| 58 | + % (X_plot.shape[0], kr_predict)) |
| 59 | + |
| 60 | + |
| 61 | +# ############################################################################# |
| 62 | +# Look at the results |
| 63 | +sv_ind = svr.best_estimator_.support_ |
| 64 | +plt.scatter(X[sv_ind], y[sv_ind], c='r', s=50, label='SVR support vectors', |
| 65 | + zorder=2, edgecolors=(0, 0, 0)) |
| 66 | +plt.scatter(X[:100], y[:100], c='k', label='data', zorder=1, |
| 67 | + edgecolors=(0, 0, 0)) |
| 68 | +plt.plot(X_plot, y_svr, c='r', |
| 69 | + label='SVR (fit: %.3fs, predict: %.3fs)' % (svr_fit, svr_predict)) |
| 70 | +plt.plot(X_plot, y_kr, c='g', |
| 71 | + label='KRR (fit: %.3fs, predict: %.3fs)' % (kr_fit, kr_predict)) |
| 72 | +plt.xlabel('data') |
| 73 | +plt.ylabel('target') |
| 74 | +plt.title('SVR versus Kernel Ridge') |
| 75 | +plt.legend() |
| 76 | + |
| 77 | +# Visualize training and prediction time |
| 78 | +plt.figure() |
| 79 | + |
| 80 | +# Generate sample data |
| 81 | +X = 5 * rng.rand(10000, 1) |
| 82 | +y = np.sin(X).ravel() |
| 83 | +y[::5] += 3 * (0.5 - rng.rand(X.shape[0] // 5)) |
| 84 | +sizes = np.logspace(1, 4, 7).astype(np.int) |
| 85 | +for name, estimator in {"KRR": KernelRidge(kernel='rbf', alpha=0.1, |
| 86 | + gamma=10), |
| 87 | + "SVR": SVR(kernel='rbf', C=1e1, gamma=10)}.items(): |
| 88 | + train_time = [] |
| 89 | + test_time = [] |
| 90 | + for train_test_size in sizes: |
| 91 | + t0 = time.time() |
| 92 | + estimator.fit(X[:train_test_size], y[:train_test_size]) |
| 93 | + train_time.append(time.time() - t0) |
| 94 | + |
| 95 | + t0 = time.time() |
| 96 | + estimator.predict(X_plot[:1000]) |
| 97 | + test_time.append(time.time() - t0) |
| 98 | + |
| 99 | + plt.plot(sizes, train_time, 'o-', color="r" if name == "SVR" else "g", |
| 100 | + label="%s (train)" % name) |
| 101 | + plt.plot(sizes, test_time, 'o--', color="r" if name == "SVR" else "g", |
| 102 | + label="%s (test)" % name) |
| 103 | + |
| 104 | +plt.xscale("log") |
| 105 | +plt.yscale("log") |
| 106 | +plt.xlabel("Train size") |
| 107 | +plt.ylabel("Time (seconds)") |
| 108 | +plt.title('Execution Time') |
| 109 | +plt.legend(loc="best") |
| 110 | + |
| 111 | +# Visualize learning curves |
| 112 | +plt.figure() |
| 113 | + |
| 114 | +svr = SVR(kernel='rbf', C=1e1, gamma=0.1) |
| 115 | +kr = KernelRidge(kernel='rbf', alpha=0.1, gamma=0.1) |
| 116 | +train_sizes, train_scores_svr, test_scores_svr = \ |
| 117 | + learning_curve(svr, X[:100], y[:100], train_sizes=np.linspace(0.1, 1, 10), |
| 118 | + scoring="neg_mean_squared_error", cv=10) |
| 119 | +train_sizes_abs, train_scores_kr, test_scores_kr = \ |
| 120 | + learning_curve(kr, X[:100], y[:100], train_sizes=np.linspace(0.1, 1, 10), |
| 121 | + scoring="neg_mean_squared_error", cv=10) |
| 122 | + |
| 123 | +plt.plot(train_sizes, -test_scores_svr.mean(1), 'o-', color="r", |
| 124 | + label="SVR") |
| 125 | +plt.plot(train_sizes, -test_scores_kr.mean(1), 'o-', color="g", |
| 126 | + label="KRR") |
| 127 | +plt.xlabel("Train size") |
| 128 | +plt.ylabel("Mean Squared Error") |
| 129 | +plt.title('Learning curves') |
| 130 | +plt.legend(loc="best") |
| 131 | + |
| 132 | +plt.show() |
0 commit comments