Choose to minimize an estimate of out-of-sample mean squared prediction error, commonly K-fold cross-validation or a separate validation set. As increases, the coefficient vector is shrunk toward zero. This generally increases bias of an estimator but decreases variance of an estimator; the minimizing value balances the two contributions in the bias-variance tradeoff. The independent test set in the question can assess the final choice, but repeatedly selecting on that same set would cause data leakage.
The -nearest-neighbour classifier finds the training predictors nearest to and returns the majority class among their labels. Increasing averages more labels and reduces variance of an estimator, but uses observations farther from and therefore increases bias of an estimator; decreasing reverses this bias-variance tradeoff.