/usr/share/pyshared/mlpy/_ridgeregression.py is in python-mlpy 2.2.0~dfsg1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | ## Ridge Regression
## This code is written by Davide Albanese, <albanese@fbk.eu>.
## (C) 2010 Fondazione Bruno Kessler - Via Santa Croce 77, 38100 Trento, ITALY.
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
__all__ = ["RidgeRegression", "KernelRidgeRegression"]
import numpy as np
class RidgeRegression(object):
"""Ridge Regression and Ordinary Least Squares (OLS).
"""
def __init__(self, alpha=0.0):
"""Initialization.
:Parameters:
alpha : float (>= 0.0)
regularization (0.0: OLS)
"""
self.alpha = alpha
self.__beta = None
self.__beta0 = None
def learn(self, x, y):
"""Compute the regression coefficients.
:Parameters:
x : numpy 2d array (n x p)
matrix of regressors
y : numpy 1d array (n)
response
"""
if not isinstance(x, np.ndarray):
raise ValueError("x must be an numpy 2d array")
if not isinstance(y, np.ndarray):
raise ValueError("y must be an numpy 1d array")
if x.ndim > 2:
raise ValueError("x must be an 2d array")
if x.shape[0] != y.shape[0]:
raise ValueError("x and y are not aligned")
xm = x - np.mean(x)
n = x.shape[0]
p = x.shape[1]
if n < p:
xd = np.dot(xm, xm.T)
if self.alpha:
xd += self.alpha * np.eye(n)
xdi = np.linalg.pinv(xd)
self.__beta = np.dot(np.dot(xm.T, xdi), y)
else:
xd = np.dot(xm.T, xm)
if self.alpha:
xd += self.alpha * np.eye(p)
xdi = np.linalg.pinv(xd)
self.__beta = np.dot(xdi, np.dot(xm.T, y))
self.__beta0 = np.mean(y) - np.dot(self.__beta, np.mean(x, axis=0))
def pred(self, x):
"""Compute the predicted response.
:Parameters:
x : numpy 2d array (nxp)
matrix of regressors
:Returns:
yp : 1d ndarray
predicted response
"""
if not isinstance(x, np.ndarray):
raise ValueError("x must be an numpy 2d array")
if x.ndim > 2:
raise ValueError("x must be an 2d array")
if x.shape[1] != self.__beta.shape[0]:
raise ValueError("x and beta are not aligned")
p = np.dot(x, self.__beta) + self.__beta0
return p
def selected(self):
"""Returns the regressors ranking.
"""
if self.__beta == None:
raise ValueError("regression coefficients are not computed. "
"Run RidgeRegression.learn(x, y)")
sel = np.argsort(np.abs(self.__beta))[::-1]
return sel
def beta(self):
"""Return b_1, ..., b_p.
"""
return self.__beta
def beta0(self):
"""Return b_0.
"""
return self.__beta0
class KernelRidgeRegression(object):
"""Ridge Regression and Ordinary Least Squares (OLS).
"""
def __init__(self, kernel, alpha):
"""Initialization.
:Parameters:
alpha : float (> 0.0)
"""
self.alpha = alpha
self.__kernel = kernel
self.__x = None
self.__c = None
def learn(self, x, y):
"""Compute the regression coefficients.
:Parameters:
x : numpy 2d array (n x p)
matrix of regressors
y : numpy 1d array (n)
response
"""
if not isinstance(x, np.ndarray):
raise ValueError("x must be an numpy 2d array")
if not isinstance(y, np.ndarray):
raise ValueError("y must be an numpy 1d array")
if x.ndim > 2:
raise ValueError("x must be an 2d array")
if x.shape[0] != y.shape[0]:
raise ValueError("x and y are not aligned")
n = x.shape[0]
p = x.shape[1]
K = self.__kernel.matrix(x)
tmp = np.linalg.inv(K + (self.alpha * np.eye(n)))
self.__c = np.dot(y, tmp)
self.__x = x.copy()
def pred(self, x):
"""Compute the predicted response.
:Parameters:
x : numpy 2d array (n x p)
matrix of regressors
:Returns:
yp : 1d ndarray
predicted response
"""
if not isinstance(x, np.ndarray):
raise ValueError("x must be an numpy 2d array")
if x.ndim > 2:
raise ValueError("x must be an 2d array")
if x.shape[1] != self.__x.shape[1]:
raise ValueError("x is not aligned")
y = np.empty(x.shape[0])
for i in range(x.shape[0]):
k = self.__kernel.vector(x[i], self.__x)
y[i] = np.sum(self.__c * k)
return y
|