2.Ridge Regression 岭回归
生活随笔
收集整理的這篇文章主要介紹了
2.Ridge Regression 岭回归
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Ridge?嶺回歸通過對回歸稀疏增加罰項來解決?普通最小二乘法?的一些問題.嶺回歸系數通過最小化帶罰項的殘差平方和
上述公式中,? ?是控制模型復雜度的因子(可看做收縮率的大小) :? ?越大,收縮率越大,那么系數對于共線性的魯棒性更強
和其他線性模型一樣,Ridge?調用?fit?方法,參數為X,y,并且將線性模型擬合的系數??存到成員變量?coef_?中。
import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model# X is the 10x10 Hilbert matrix X = 1. / (np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis]) y = np.ones(10)############################################################################### # Compute pathsn_alphas = 200 alphas = np.logspace(-10, -2, n_alphas) clf = linear_model.Ridge(fit_intercept=False)coefs = [] for a in alphas:clf.set_params(alpha=a)clf.fit(X, y)coefs.append(clf.coef_)############################################################################### # Display resultsax = plt.gca() ax.set_color_cycle(['b', 'r', 'g', 'c', 'k', 'y', 'm'])ax.plot(alphas, coefs) ax.set_xscale('log') ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis plt.xlabel('alpha') plt.ylabel('weights') plt.title('Ridge coefficients as a function of the regularization') plt.axis('tight') plt.show()
總結
以上是生活随笔為你收集整理的2.Ridge Regression 岭回归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1.广义线性模型
- 下一篇: 3.Lasso线性模型