# 回归

### 岭回归

```# Author: Fabian Pedregosa -- <fabian.pedregosa@inria.fr>
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)
y[0:5] = 0
# #############################################################################
# Compute paths

n_alphas = 200
alphas = np.logspace(-10, -2, n_alphas)
#print alphas
coefs = []
scores = []
for a in alphas:
ridge = linear_model.Ridge(alpha=a, fit_intercept=True)
ridge.fit(X, y)
scores.append(ridge.score(X,y))
coefs.append(ridge.coef_)

# #############################################################################
# Display results
#print scores
fig, axes = plt.subplots(1,2)
ax0, ax1 = axes
ax0.plot(alphas, scores)
ax0.set_xscale('log')
ax0.set_xlabel('alpha')
ax0.set_ylabel('scores')

ax1.plot(alphas, coefs)
ax1.set_xscale('log')
#ax.set_xlim(ax.get_xlim()[::-1])  # reverse axis
fig.set_figwidth = 20
fig.set_figheight = 8

ax1.set_xlabel('alpha')
ax1.set_ylabel('weights')

plt.title('Ridge coefficients as a function of the regularization')
plt.axis('tight')

plt.show()```

### huber回归

```import numpy as np
del plt
import matplotlib.pyplot as plt

from sklearn.datasets import make_regression
from sklearn.linear_model import HuberRegressor, Ridge

def huberloss(var, delta):
if abs(var) > delta:
return delta*abs(var)-1./2*delta*delta
else:
return 1./2*var*var

# Generate toy data.
rng = np.random.RandomState(0)
X, y = make_regression(n_samples=20, n_features=1, random_state=0, noise=4.0,
bias=100.0)

# Add four strong outliers to the dataset.
X_outliers = rng.normal(0, 0.5, size=(4, 1))
y_outliers = rng.normal(0, 2.0, size=4)
X_outliers[:2, :] += X.max() + X.mean() / 4.
X_outliers[2:, :] += X.min() - X.mean() / 4.
y_outliers[:2] += y.min() - y.mean() / 4.
y_outliers[2:] += y.max() + y.mean() / 4.
X = np.vstack((X, X_outliers))
y = np.concatenate((y, y_outliers))
fig, axes = plt.subplots(1,3)
ax_loss,ax_score, ax1 = axes

ax1.plot(X, y, 'b.')

# Fit the huber regressor over a series of epsilon values.
#colors = ['r-', 'b-', 'y-', 'm-']

x = np.linspace(X.min(), X.max(), 7)
epsilon_values = [1,1.2,1.35, 1.5, 1.75, 1.9]
scores = []
losses =[]

for k, epsilon in enumerate(epsilon_values):
huber = HuberRegressor(fit_intercept=True, alpha=0.0, max_iter=100,
epsilon=epsilon)
huber.fit(X, y)
np.sum(huberloss(el, epsilon) for el in (huber.predict(X)-y))
losses.append(np.sum(huberloss(el, epsilon) for el in (huber.predict(X)-y)))
scores.append(huber.score(X,y))
coef_ = huber.coef_ * x + huber.intercept_
ax1.plot(x, coef_,  label="huber loss, %s" % epsilon)
#ax1.plot(x, coef_, colors[k], label="huber loss, %s" % epsilon)

ax_loss.plot(epsilon_values, losses)
ax_loss.set_xlabel('delta')
ax_loss.set_ylabel('loss')

ax_score.plot(epsilon_values, scores)
ax_score.set_xlabel('delta')
ax_score.set_ylabel('scores')

# Fit a ridge regressor to compare it to huber regressor.
ridge = Ridge(fit_intercept=True, alpha=0.0, random_state=0, normalize=True)
ridge.fit(X, y)
coef_ridge = ridge.coef_
coef_ = ridge.coef_ * x + ridge.intercept_
ax1.plot(x, coef_, 'g-', label="ridge regression")
ax1.set_title("Comparison of HuberRegressor vs Ridge")
ax1.set_xlabel("X")
ax1.set_ylabel("y")
ax1.legend(loc='lower center')
plt.show()```
• 第一幅图使用loss总和来评估回归效果，delta=1，损失最少，从第三幅图来看也是拟合效果最好的
• 第二幅图使用回归类的R2来评估,对于存在离群点，R2不适用

### logcosh回归(使用SGD实现回归算法)

```class SDGReggressor():
def __init__(self, eta, X, Y, N,regular1=0, regular2=0):
self.eta = eta
self.X = X
self.Y = Y
self.N = N
self.w = np.array([0]*len(X[0]))
self.w0 = 0
self.m = len(X)
self.n = len(X[0])
self.regular2 = regular2
self.regular1 = regular1
def output_y(self, x):
return np.dot(x,self.w)+self.w0
def loss(self, value):
return np.log(np.cosh(value))
def derivative(self, value):
return np.tanh(value)
def regular_fun(self):
return self.regular2*np.dot(self.w,self.w)+self.regular1*abs(self.w.sum())
def training(self):
self.errors = []
for times in xrange(self.N):
delta_y = self.Y-self.output_y(self.X)
error = (self.loss(delta_y)).sum()+self.regular_fun()
self.w0 += self.eta*self.derivative(delta_y).sum()
r1=0
if abs(self.w.sum()) > 0:
r1=1
elif abs(self.w.sum()) == 0:
r1 = 0
else:
r1 = -1
self.w = self.w + (self.eta*np.dot(self.derivative(delta_y),self.X)+2.0*self.regular2*self.w+r1*self.regular1)
self.errors.append(error)

per = SDGReggressor(1e-2, X, y, 1000, regular1=0, regular2=0)
per.training()

plt.plot(xrange(per.N), per.errors)
plt.xlabel('loop')
plt.ylabel('errors')
plt.show()```

```plt.plot(X, y, 'b.')
plt.plot([-1,3],[-1,3]*per.w+per.w0)
plt.xlabel('X')
plt.ylabel('y')
plt.show()```
• logcosh对于带有离群点数据也能很好的拟合，但是logcosh不需要调参数delta
• 需要对y数据进行缩小,当y稍微大一点，cosh(y)就趋向于∞

cosh(x)

### 为什么能减弱离群点的能量

tanh(x)

`self.w = self.w + (self.eta*np.dot(self.derivative(delta_y),self.X)+2.0*self.regular2*self.w+r1*self.regular1)`

95 篇文章31 人订阅

0 条评论

## 相关文章

40215

3515

### 统计学习方法之K近邻法1.k近邻法（k-nearest neighbor，k-NN）2.k近邻模型3.k近邻算法的实现

1.k近邻法（k-nearest neighbor，k-NN） k近邻算法是一个基本分类和回归方法，k-NN的输入时实例的特征向量，对应于特征空间的点，输出是...

3155

### Very Deep Convolutional Networks for Large-Scale Image Recognition—VGG论文翻译—中英文对照

Very Deep Convolutional Networks for Large-Scale Image Recognition ABSTRACT In t...

2930

### （数据科学学习手札29）KNN分类的原理详解&Python与R实现

KNN（k-nearst neighbors，KNN）作为机器学习算法中的一种非常基本的算法，也正是因为其原理简单，被广泛应用于电影/音乐推荐等方面，即有些...

51413

4795

4087

### scikit-learn 梯度提升树(GBDT)调参小结

在梯度提升树(GBDT)原理小结中，我们对GBDT的原理做了总结，本文我们就从scikit-learn里GBDT的类库使用方法作一个总结，主要会关注调参...

5103

### Python编程任务 | 斯坦福CS231n-深度学习与计算机视觉课程

Assignment 3 04 Python编程任务（2-layer神经网络） · Assignment1的神经网络部分，我们需要完成neural_net....

6717

### 精确率与召回率，RoC曲线与PR曲线

在机器学习的算法评估中，尤其是分类算法评估中，我们经常听到精确率(precision)与召回率(recall)，RoC曲线与PR曲线这些概念，那这些概念...

1552