I am running a linear regression in sklearn
model = LinearRegression()
model.fit(x_poly, y_true)
Instead of using the standard loss function (I think is MSE) to fit my linear regression. I would like to define a distance metric with a symmetric definite positive matrix A, i.e., the norm ||.||_{A^{-1}}. Is there a way to define this parameter?
Unfortunately, LinearRegression in scikit-learn does not natively support using custom loss functions. The LinearRegression class by default uses Ordinary Least Squares, which minimizes the standard Mean Squared Error. However, you can achieve your goal by implementing a custom loss function using an optimization library like scipy.optimize. The minimize function from scipy.optimize is used to solve the optimization problem. To incorporate the intercept, we append a column of ones to X. This method gives you complete freedom to specify any loss function you like, provided it's differentiable. If your A matrix is very large, be aware of the computational cost associated with its inverse calculation. This is independent of scikit-learn's implementation of LinearRegression, since scikit-learn does not allow to directly change the loss. If you want to use this in a pipeline or together with GridSearchCV you can wrap the logic above into a custom estimator by subclassing BaseEstimator and RegressorMixin from scikit-learn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With