/krr_code.py Secret
Last active
June 5, 2024 10:09
KRR model implementation on the diabetes dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.datasets import load_diabetes | |
from sklearn.kernel_ridge import KernelRidge | |
from sklearn.model_selection import train_test_split, GridSearchCV | |
from sklearn.metrics import mean_absolute_error, mean_squared_error | |
diabetes = load_diabetes() | |
data = diabetes.data | |
target = diabetes.target | |
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2) | |
krr_model = KernelRidge() | |
param_grid = { | |
"alpha": [1e-5, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 2], | |
"kernel": [ | |
"linear", | |
"rbf", | |
"poly", | |
"sigmoid", | |
], | |
} | |
grid_search = GridSearchCV( | |
krr_model, param_grid, scoring="neg_mean_absolute_error", n_jobs=-1, cv=5 | |
) | |
grid_search.fit(X_train, y_train) | |
best_params = grid_search.best_params_ | |
print(best_params) | |
best_model = grid_search.best_estimator_ | |
predictions = best_model.predict(X_test) | |
test_mae = mean_absolute_error(y_test, predictions) | |
test_mse = mean_squared_error(y_test, predictions) | |
train_predictions = best_model.predict(X_train) | |
train_mae = mean_absolute_error(y_train, train_predictions) | |
train_mse = mean_squared_error(y_train, train_predictions) | |
print(f"Test MAE : {test_mae} and Test MSE : {test_mse}") | |
print(f"Train MAE : {train_mae} and train MSE : {train_mse}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment