def fit(self, X, y): self._check(X, y) if dim(y) == 1: raw_X = X if self.fit_intercept: X = hstack([ones(shape(X)[0], 1), X]) beta = zeros(shape(X)[1]) # row vector X_T = matrix_transpose(X) if self.fit_intercept: beta[0] = sum(minus(reshape(y, -1), dot( raw_X, beta[1:]))) / (shape(X)[0]) for _ in range(self.max_iter): start = 1 if self.fit_intercept else 0 for j in range(start, len(beta)): tmp_beta = [x for x in beta] tmp_beta[j] = 0.0 r_j = minus(reshape(y, -1), dot(X, beta)) # r_j = minus(reshape(y,-1) , dot(X, tmp_beta)) arg1 = dot(X_T[j], r_j) arg2 = self.alpha * shape(X)[0] if sum(square(X_T[j])) != 0: beta[j] = self._soft_thresholding_operator( arg1, arg2) / sum(square(X_T[j])) else: beta[j] = 0 if self.fit_intercept: beta[0] = sum( minus(reshape(y, -1), dot( raw_X, beta[1:]))) / (shape(X)[0]) # # add whatch # self.beta = beta # self._whatch(raw_X,y) if self.fit_intercept: self.intercept_ = beta[0] self.coef_ = beta[1:] else: self.coef_ = beta self.beta = beta return self elif dim(y) == 2: if self.fit_intercept: X = hstack([ones(shape(X)[0], 1), X]) y_t = matrix_transpose(y) betas = [] for i in range(shape(y)[1]): betas.append(self._fit(X, y_t[i])) batas = matrix_transpose(betas) self.betas = batas
def fit(self, X, y, weights=None): X, y = self._check(X, y) if self.fit_intercept: m, n = shape(X) bias = ones(m, 1) X = hstack([bias, X]) eye = identity_matrix(shape(X)[1]) from linalg.matrix import diag if not self.penalty_bias: eye[0][0] = 0 # add weights if weights != None: assert (len(weights) == shape(X)[0]) X = matrix_matmul(diag(weights), X) X_T = matrix_transpose(X) self.W = matrix_matmul( matrix_matmul( matrix_inverse( plus(matrix_matmul(X_T, X), multiply(eye, self.alpha * shape(X)[0])) # plus(matrix_matmul(X_T,X),multiply(eye,self.alpha)) ), X_T), y) self.importance_ = sum(self.W, axis=1) if self.fit_intercept: self.importance_ = self.importance_[1:]
def predict(self, X): assert (self.beta != None or self.betas != None) if self.fit_intercept: X = hstack([ones(shape(X)[0], 1), X]) if self.beta != None: return dot(X, self.beta) else: return matrix_matmul(X, self.betas)
def predict(self, X): assert (self.W != None) if self.fit_intercept: m, n = shape(X) bias = ones(m, 1) X = hstack([bias, X]) result = matrix_matmul(X, self.W) if self.dim_Y == 1: result = [x[0] for x in result] return result
def fit(self, X, y): X, y = self._check(X, y) if self.fit_intercept: m, n = shape(X) bias = ones(m, 1) X = hstack([bias, X]) X_T = matrix_transpose(X) # print matrix_matmul(X_T,X) self.W = matrix_matmul( matrix_matmul(matrix_inverse(matrix_matmul(X_T, X)), X_T), y)
def fit(self, X, y): X, y = self._check(X, y) if self.fit_intercept: m, n = shape(X) bias = ones(m, 1) X = hstack([bias, X]) eye = identity_matrix(shape(X)[1]) from linalg.matrix import diag if self.penalty_loss: eye = diag(self.penalty_loss) X_T = matrix_transpose(X) self.W = matrix_matmul( matrix_matmul( matrix_inverse( plus(matrix_matmul(X_T, X), multiply(eye, self.alpha * shape(X)[0]))), X_T), y) self.importance_ = sum(self.W, axis=1) if self.fit_intercept: self.importance_ = self.importance_[1:]