2017年2月13日 Ridge Regression

岭回归实现与比较
本文介绍了如何使用Python从零开始实现岭回归,并将其结果与Scikit-learn库中的岭回归模型进行对比。通过随机生成的数据集,展示了岭回归求解过程及误差计算。

Suppose that for a known matrix A and vector b, we wish fo find a vector X such that

The ridge regression approach seeks to minimize the sum of squared residuals with a regularization term

An explicit solution is given by

import numpy as np
import ml_metrics as mtr

#prepare data
n_samples, n_features = 10, 5
np.random.seed(0)
X = np.random.randn(n_samples, n_features)
y = np.random.randn(n_samples)
y = (y-np.mean(y))/np.std(y)

#ridge regression implementation
def ridge_regression(X, y, alpha):
    tik_mat = alpha * np.identity(X.shape[1])
    coef = np.dot(np.transpose(X), X) + np.dot(np.transpose(tik_mat), tik_mat)
    coef = np.linalg.inv(coef)
    coef = np.dot(coef, np.transpose(X))
    coef = np.dot(coef, y)
    return coef

#train
coef = ridge_regression(X, y, 0)
print mtr.mse(np.dot(X, coef), y)
print coef

#0.677751350808
#[-0.30898281  0.02387927 -0.04666003 -0.2501281   0.16215742]

#scikit-learn implementation
from sklearn.linear_model import Ridge
r = Ridge(alpha=0, fit_intercept=False)
r.fit(X,y)
print mtr.mse(r.predict(X), y)
print r.coef_

#0.677751350808
#[-0.30898281  0.02387927 -0.04666003 -0.2501281   0.16215742]

 

转载于:https://my.oschina.net/airxiechao/blog/862746

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值