Sklearn Ridge - Training a Ridge Regression Model
Python
Ridge regression is a type of regularised linear regression that uses L2 regularisation.
To alter the amount of regularisation applied to the model, the value of the alpha hyperparameter can be adjusted. The larger the alpha value, the more regularisation is applied while if alpha is equal to zero then regularisation is removed altogether.
In the example below we fit a Ridge regression model with alpha set to the default value of 1. The model is then used to make predictions for the X_test dataset and the performance of the model is evaluated by calculated the RMSE and MAE.
1| from sklearn.linear_model import Ridge 2| from sklearn.metrics import mean_squared_error, mean_absolute_error 3| 4| # initialise & fit a ridge regression model with alpha set to 1 5| # if the model is overfitting, increase the alpha value 6| model = Ridge(alpha=1) 7| model.fit(X_train, y_train) 8| 9| # create dictionary that contains the feature coefficients 10| coef = dict(zip(X_train.columns, model.coef_.T)) 11| print(coef) 12| 13| # make prediction for test data 14| y_pred = model.predict(X_test) 15| 16| # evaluate performance 17| print('RMSE:',mean_squared_error(y_test, y_pred, squared = False)) 18| print('MAE:',mean_absolute_error(y_test, y_pred))
149
132
127
119