scikit-learn学习笔记(四)Ridge Regression ( 岭回归 )
嶺回歸通過對系數的大小施加懲罰來解決 普通最小二乘 的一些問題。 ridge coefficients ( 嶺系數?)?最小化了懲罰的殘差平方和,
這里, ?是控制收縮量的復雜度參數: ?值越大,收縮量越大,因此系數變得對共線性變得更加魯棒。
與其他線性模型一樣,Ridge 將采用其擬合方法數組 X , y 并將其線性模型的系數 ?存儲在其 coef_ 成員中:
Ridge Complexity ( 嶺復雜性 )
這種方法與 Ordinary Least Squares ( 普通最小二乘方法?) 的復雜度相同。
例子1: ( 作為正則化的函數,繪制嶺系數 )# -*- coding: utf-8 -*-
"""
Created on Sat Jul 29 21:27:04 2017
@author: Administrator
"""
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)
n_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_)
ax = plt.gca()
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()??
總結
以上是生活随笔為你收集整理的scikit-learn学习笔记(四)Ridge Regression ( 岭回归 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow学习笔记(十一)读取
- 下一篇: scala把序列分解成子集(group