Numpy linear regression with regularization
The problem is:
features.transpose().dot(features)
may not be invertible. And numpy.linalg.inv works only for full-rank matrix according to the documents. However, a (non-zero) regularization term always makes the equation nonsingular.
By the way, you are right about the implementation. But it is not efficient. An efficient way to solve this equation is the least squares method.
np.linalg.lstsq(features, labels)
can do the work for np.linalg.pinv(features).dot(labels)
.
In a general way, you can do this
def get_model(A, y, lamb=0): n_col = A.shape[1] return np.linalg.lstsq(A.T.dot(A) + lamb * np.identity(n_col), A.T.dot(y))