I did linear regression with and without regularization parameter (ridge) and found that regularization improves the regression accuracy for just some of my test data and error goes up for the rest. So it seems that regularization is destructive in some cases. So I am thinking to make ridge parameter as a function of "something" and this gets checked in the code. If it's found that regularization is not needed, the parameter lambda is set to zero otherwise to another predefined value. So I am wondering if there is any way that I can detect in a code if regularization needed or not? How can I check this?