How to Ridge Regression in Python
Published on Aug. 22, 2023, 12:16 p.m.
Here’s an example of how to perform Ridge Regression in Python using the scikit-learn library:
- Import packages and load data
import pandas as pd
from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
data = pd.read_csv(‘data.csv’)
X = data.drop([‘target_variable’], axis=1)
y = data[‘target_variable’]
2. Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
3. Create the Ridge Regression model and fit the data
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)
4. Evaluate the model's performance
print(‘R- Squared:’, ridge.score(X_test, y_test))
In this example, `alpha` is the regularization parameter that controls the strength of the penalty term. A higher value of `alpha` will result in a stronger penalty, which can help avoid overfitting.
This is a basic implementation of Ridge regression in Python. You can modify it as per your requirements and also experiment with different values of `alpha` to obtain better results.