Numpy实现Regression,成功入职阿里月薪45K

Calculate weights by least squares (using Moore-Penrose pseudoinverse)

U, S, V = np.linalg.svd(X.T.dot(X))

S = np.diag(S)

X_sq_reg_inv = V.dot(np.linalg.pinv(S)).dot(U.T)

self.w = X_sq_reg_inv.dot(X.T).dot(y)

else:

super(LinearRegression, self).fit(X, y)

class LassoRegression(Regression):

“”"Linear regression model with a regularization factor which does both variable selection

and regularization. Model that tries to balance the fit of the model with respect to the training

data and the complexity of the model. A large regularization factor with decreases the variance of

the model and do para.

Parameters:


degree: int

The degree of the polynomial that the independent variable X will be transformed to.

reg_factor: float

The factor that will determine the amount of regularization and feature

shrinkage.

n_iterations: float

The number of training iterations the algorithm will tune the weights for.

learning_rate: float

The step length that will be used when updating the weights.

“”"

def init(self, degree, reg_factor, n_iterations=3000, learning_rate=0.01):

self.degree = degree

self.regularization = l1_regularization(alpha=reg_factor)

super(LassoRegression, self).init(n_iterations,

learning_rate)

def fit(self, X, y):

X = normalize(polynomial_features(X, degree=self.degree))

super(LassoRegression, self).fit(X, y)

def predict(self, X):

X = normalize(polynom

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值