Table of Contents

#Ridge Regression which is considered

#L2 Regularization

#helps with overfitting in linear regression models

#keeping the coefficients small

# lead to a model that is less prone to overfitting






#balance between fitting the data and keeping the coefficients small

#more robust and stable models, particularly when dealing with datasets that have highly correlated predictor variables
				
					from sklearn.datasets import make_regression
				
			
				
					X, y = make_regression(n_samples=100, n_features=4, noise=0.1, random_state=42, effective_rank=2)
				
			
				
					from sklearn.model_selection import train_test_split
				
			
				
					X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=19)
				
			
				
					from sklearn.preprocessing import StandardScaler
				
			
				
					scaler = StandardScaler()
				
			
				
					X_train = scaler.fit_transform(X_train)
				
			
				
					X_test = scaler.transform(X_test)
				
			
				
					from sklearn.linear_model import Ridge
				
			
				
					ridge = Ridge()
				
			
				
					ridge.fit(X_train, y_train)
				
			
				
					from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
				
			
				
					y_pred = ridge.predict(X_test)
				
			
				
					mean_absolute_error(y_test, y_pred)
				
			
				
					mean_squared_error(y_test, y_pred)
				
			
				
					r2_score(y_test, y_pred)
				
			
#regularization strength, often denoted as alpha
#L2 regularization penalty

#A larger alpha value results in stronger regularization, which tends to
#shrink the coefficients towards zero
				
					param_grid = {
    'alpha' : [0.001, 0.01, 0.1, 1.0, 10.0, 100.0]
}
				
			
				
					from sklearn.model_selection import GridSearchCV
				
			
				
					ridge_cv = GridSearchCV(ridge, param_grid, cv=3, n_jobs=-1)
				
			
				
					ridge_cv.fit(X_train, y_train)
				
			
				
					y_pred = ridge_cv.predict(X_test)
				
			
				
					mean_absolute_error(y_test, y_pred)
				
			
				
					mean_squared_error(y_test, y_pred)
				
			
				
					r2_score(y_test, y_pred)
				
			
				
					ridge_cv.best_estimator_
				
			
				
					ridge3 = Ridge(alpha=0.001)
				
			
				
					ridge3.fit(X_train, y_train)
				
			
				
					ridge3.intercept_
				
			
				
					ridge3.coef_
				
			
#if a coefficient was zero, then the lasso model would disregard it

Free Community

Join 1,000+ AI Automation Builders

Weekly tutorials, live calls & direct access to Ryan & Matt.

Join Free →

Keep Learning

Kaggle House price prediction Regression Analysis

train_df = train_df.drop(columns=['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'GarageYrBlt', 'GarageCond', 'BsmtFinType2']) test_df = test_df.drop(columns=['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'GarageYrBlt', 'GarageCond', 'BsmtFinType2']) #drop GarageArea or GarageCars...

kaggle titanic tutorial

https://www.kaggle.com/code/ryannolan1/titanic-voting-classifier-0-78947?scriptVersionId=149342442&cellId=2https://www.kaggle.com/code/ryannolan1/titanic-voting-classifier-0-78947?scriptVersionId=149342442&cellId=2 #military - Capt, Col, Major #noble - Jonkheer, the Countess, Don, Lady, Sir #unmaried Female - Mlle, Ms, Mme #NEW Drop...

hyperparameter tuning with scikit learn

We would be looking at tuning hyperparameters with Scikit-Learn. Scikit-Learn is a powerful machine learning library for Python. It provides simple ,...

principal component analysis scikit learn

PCA (Principal Component Analysis) in Python using Scikit-learn is a technique used to reduce the number of features in a dataset while...