|
| 1 | +#!/usr/bin/python |
| 2 | +# coding:utf8 |
| 3 | + |
| 4 | +''' |
| 5 | +Created on Jan 8, 2011 |
| 6 | +Update on 2017-05-18 |
| 7 | +@author: Peter Harrington/小瑶 |
| 8 | +《机器学习实战》更新地址:https://github.com/apachecn/MachineLearning |
| 9 | +''' |
| 10 | + |
| 11 | + |
| 12 | +# Isotonic Regression 等式回归 |
| 13 | +print(__doc__) |
| 14 | + |
| 15 | +# Author: Nelle Varoquaux <nelle.varoquaux@gmail.com> |
| 16 | +# Alexandre Gramfort <alexandre.gramfort@inria.fr> |
| 17 | +# License: BSD |
| 18 | + |
| 19 | +import numpy as np |
| 20 | +import matplotlib.pyplot as plt |
| 21 | +from matplotlib.collections import LineCollection |
| 22 | + |
| 23 | +from sklearn.linear_model import LinearRegression |
| 24 | +from sklearn.isotonic import IsotonicRegression |
| 25 | +from sklearn.utils import check_random_state |
| 26 | + |
| 27 | +n = 100 |
| 28 | +x = np.arange(n) |
| 29 | +rs = check_random_state(0) |
| 30 | +y = rs.randint(-50, 50, size=(n,)) + 50. * np.log(1 + np.arange(n)) |
| 31 | + |
| 32 | +ir = IsotonicRegression() |
| 33 | + |
| 34 | +y_ = ir.fit_transform(x, y) |
| 35 | + |
| 36 | +lr = LinearRegression() |
| 37 | +lr.fit(x[:, np.newaxis], y) # 线性回归的 x 需要为 2d |
| 38 | + |
| 39 | +segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)] |
| 40 | +lc = LineCollection(segments, zorder=0) |
| 41 | +lc.set_array(np.ones(len(y))) |
| 42 | +lc.set_linewidths(0.5 * np.ones(n)) |
| 43 | + |
| 44 | +fig = plt.figure() |
| 45 | +plt.plot(x, y, 'r.', markersize=12) |
| 46 | +plt.plot(x, y_, 'g.-', markersize=12) |
| 47 | +plt.plot(x, lr.predict(x[:, np.newaxis]), 'b-') |
| 48 | +plt.gca().add_collection(lc) |
| 49 | +plt.legend(('Data', 'Isotonic Fit', 'Linear Fit'), loc='lower right') |
| 50 | +plt.title('Isotonic regression') |
| 51 | +plt.show() |
| 52 | + |
| 53 | +# Kernel ridge regression ( 内核岭回归 ) |
| 54 | + |
| 55 | +# 2.1 Comparison of kernel ridge regression and SVR ( 内核岭回归与 SVR 的比较 ) |
| 56 | + |
| 57 | +# Authors: Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> |
| 58 | +# License: BSD 3 clause |
| 59 | + |
| 60 | +''' |
| 61 | +from __future__ import division |
| 62 | +import time |
| 63 | +
|
| 64 | +import numpy as np |
| 65 | +
|
| 66 | +from sklearn.svm import SVR |
| 67 | +from sklearn.model_selection import GridSearchCV |
| 68 | +from sklearn.model_selection import learning_curve |
| 69 | +from sklearn.kernel_ridge import KernelRidge |
| 70 | +import matplotlib.pyplot as plt |
| 71 | +
|
| 72 | +rng = np.random.RandomState(0) |
| 73 | +
|
| 74 | +# 生成样本数据 |
| 75 | +X = 5 * rng.rand(10000, 1) |
| 76 | +y = np.sin(X).ravel() |
| 77 | +
|
| 78 | +# 给目标增加噪音 |
| 79 | +y[::5] += 3 * (0.5 - rng.rand(X.shape[0] // 5)) |
| 80 | +
|
| 81 | +X_plot = np.linspace(0, 5, 100000)[:, None] |
| 82 | +
|
| 83 | +# Fit regression model ( 拟合 回归 模型 ) |
| 84 | +train_size = 100 |
| 85 | +svr = GridSearchCV(SVR(kernel='rbf', gamma=0.1), cv=5, |
| 86 | + param_grid={"C": [1e0, 1e1, 1e2, 1e3], |
| 87 | + "gamma": np.logspace(-2, 2, 5)}) |
| 88 | +
|
| 89 | +kr = GridSearchCV(KernelRidge(kernel='rbf', gamma=0.1), cv=5, |
| 90 | + param_grid={"alpha": [1e0, 0.1, 1e-2, 1e-3], |
| 91 | + "gamma": np.logspace(-2, 2, 5)}) |
| 92 | +
|
| 93 | +t0 = time.time() |
| 94 | +svr.fit(X[:train_size], y[:train_size]) |
| 95 | +svr_fit = time.time() - t0 |
| 96 | +print("SVR complexity and bandwidth selected and model fitted in %.3f s" |
| 97 | + % svr_fit) |
| 98 | +
|
| 99 | +t0 = time.time() |
| 100 | +kr.fit(X[:train_size], y[:train_size]) |
| 101 | +kr_fit = time.time() - t0 |
| 102 | +print("KRR complexity and bandwidth selected and model fitted in %.3f s" |
| 103 | + % kr_fit) |
| 104 | +
|
| 105 | +sv_ratio = svr.best_estimator_.support_.shape[0] / train_size |
| 106 | +print("Support vector ratio: %.3f" % sv_ratio) |
| 107 | +
|
| 108 | +t0 = time.time() |
| 109 | +y_svr = svr.predict(X_plot) |
| 110 | +svr_predict = time.time() - t0 |
| 111 | +print("SVR prediction for %d inputs in %.3f s" |
| 112 | + % (X_plot.shape[0], svr_predict)) |
| 113 | +
|
| 114 | +t0 = time.time() |
| 115 | +y_kr = kr.predict(X_plot) |
| 116 | +kr_predict = time.time() - t0 |
| 117 | +print("KRR prediction for %d inputs in %.3f s" |
| 118 | + % (X_plot.shape[0], kr_predict)) |
| 119 | +
|
| 120 | +# 查看结果 |
| 121 | +sv_ind = svr.best_estimator_.support_ |
| 122 | +plt.scatter(X[sv_ind], y[sv_ind], c='r', s=50, label='SVR support vectors', |
| 123 | + zorder=2) |
| 124 | +plt.scatter(X[:100], y[:100], c='k', label='data', zorder=1) |
| 125 | +plt.hold('on') |
| 126 | +plt.plot(X_plot, y_svr, c='r', |
| 127 | + label='SVR (fit: %.3fs, predict: %.3fs)' % (svr_fit, svr_predict)) |
| 128 | +plt.plot(X_plot, y_kr, c='g', |
| 129 | + label='KRR (fit: %.3fs, predict: %.3fs)' % (kr_fit, kr_predict)) |
| 130 | +plt.xlabel('data') |
| 131 | +plt.ylabel('target') |
| 132 | +plt.title('SVR versus Kernel Ridge') |
| 133 | +plt.legend() |
| 134 | +
|
| 135 | +# 可视化训练和预测时间 |
| 136 | +plt.figure() |
| 137 | +
|
| 138 | +# 生成样本数据 |
| 139 | +X = 5 * rng.rand(10000, 1) |
| 140 | +y = np.sin(X).ravel() |
| 141 | +y[::5] += 3 * (0.5 - rng.rand(X.shape[0] // 5)) |
| 142 | +sizes = np.logspace(1, 4, 7, dtype=np.int) |
| 143 | +for name, estimator in {"KRR": KernelRidge(kernel='rbf', alpha=0.1, |
| 144 | + gamma=10), |
| 145 | + "SVR": SVR(kernel='rbf', C=1e1, gamma=10)}.items(): |
| 146 | + train_time = [] |
| 147 | + test_time = [] |
| 148 | + for train_test_size in sizes: |
| 149 | + t0 = time.time() |
| 150 | + estimator.fit(X[:train_test_size], y[:train_test_size]) |
| 151 | + train_time.append(time.time() - t0) |
| 152 | +
|
| 153 | + t0 = time.time() |
| 154 | + estimator.predict(X_plot[:1000]) |
| 155 | + test_time.append(time.time() - t0) |
| 156 | +
|
| 157 | + plt.plot(sizes, train_time, 'o-', color="r" if name == "SVR" else "g", |
| 158 | + label="%s (train)" % name) |
| 159 | + plt.plot(sizes, test_time, 'o--', color="r" if name == "SVR" else "g", |
| 160 | + label="%s (test)" % name) |
| 161 | +
|
| 162 | +plt.xscale("log") |
| 163 | +plt.yscale("log") |
| 164 | +plt.xlabel("Train size") |
| 165 | +plt.ylabel("Time (seconds)") |
| 166 | +plt.title('Execution Time') |
| 167 | +plt.legend(loc="best") |
| 168 | +
|
| 169 | +# 可视化学习曲线 |
| 170 | +plt.figure() |
| 171 | +
|
| 172 | +svr = SVR(kernel='rbf', C=1e1, gamma=0.1) |
| 173 | +kr = KernelRidge(kernel='rbf', alpha=0.1, gamma=0.1) |
| 174 | +train_sizes, train_scores_svr, test_scores_svr = \ |
| 175 | + learning_curve(svr, X[:100], y[:100], train_sizes=np.linspace(0.1, 1, 10), |
| 176 | + scoring="neg_mean_squared_error", cv=10) |
| 177 | +train_sizes_abs, train_scores_kr, test_scores_kr = \ |
| 178 | + learning_curve(kr, X[:100], y[:100], train_sizes=np.linspace(0.1, 1, 10), |
| 179 | + scoring="neg_mean_squared_error", cv=10) |
| 180 | +
|
| 181 | +plt.plot(train_sizes, -test_scores_svr.mean(1), 'o-', color="r", |
| 182 | + label="SVR") |
| 183 | +plt.plot(train_sizes, -test_scores_kr.mean(1), 'o-', color="g", |
| 184 | + label="KRR") |
| 185 | +plt.xlabel("Train size") |
| 186 | +plt.ylabel("Mean Squared Error") |
| 187 | +plt.title('Learning curves') |
| 188 | +plt.legend(loc="best") |
| 189 | +
|
| 190 | +plt.show() |
| 191 | +''' |
0 commit comments