岭回归实现(最小二乘法的带惩罚项版)
# 代码实现
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
def main():
# X 是一个 10x10 的 希尔伯特矩阵(Hilbert matrix)
X = 1. / (np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis])
#十个1的向量
y = np.ones(10)
# 计算路径(Compute paths)
n_alphas = 200
alphas = np.logspace(-10, -2, n_alphas)
coefs = []
for a in alphas:
ridge = linear_model.Ridge(alpha=a, fit_intercept=False) #每个循环都要重新实例化一个estimator对象
ridge.fit(X, y)
coefs.append(ridge.coef_)
# 展示结果
ax = plt.gca()
ax.plot(alphas, coefs)
ax.set_xscale('log')
ax.set_xlim(ax.get_xlim()[::-1]) # 反转数轴,越靠左边 alpha 越大,正则化也越厉害
plt.xlabel('alpha')
plt.ylabel('weights')
plt.title('Ridge coefficients as a function of the regularization')
plt.axis('tight')
plt.show()
if __name__='__main__':
main()