Last active
January 29, 2020 20:15
-
-
Save cereniyim/04619b266bcfbbfa09d99522d8c3b70e to your computer and use it in GitHub Desktop.
Grid Search applied to Ridge Model
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
import matplotlib.pyplot as plt | |
import pandas as pd | |
from sklearn.model_selection import GridSearchCV | |
from sklearn.datasets import load_boston | |
from sklearn.linear_model import Ridge | |
#verilerin yüklenmesi | |
boston = load_boston() | |
X = boston.data | |
y = boston.target | |
# 1. Grid Seacrh (Izgara Arama Algoritması) ile Model Kurgulanması | |
# Arama alanının yaratılması 0.001'den 100'e kadar | |
alphas_grid = {'alpha': [0.001, 0.002, 0.1, 0.2, 0.5, 0.75, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50, 100]} | |
# modelin yaratılması | |
ridge_search_model = Ridge() | |
# Grid search objesinin model, alpha alanı ve 5 katlı çapraz geçerlilik kullanılarak yaratılması | |
# R2 metriği ile ölçümlenmesi | |
grid_search = GridSearchCV(estimator = ridge_search_model, param_grid=alphas_grid, | |
cv = 5, scoring = 'r2', verbose = 1, | |
n_jobs = -1, return_train_score = True) | |
# grid search objesinin verilerle eğitilmesi | |
grid_search.fit(X, y) | |
# 2. Sonuçların görselleştirilmesi | |
# Sonuçların veri çerçevesi olarak kaydedilmesi | |
ridge_results = pd.DataFrame(grid_search.cv_results_) | |
# Eğitim ve test hatasının alpha alanı ile görselleştirilmesi | |
plt.figure(figsize=(10,10)) | |
plt.plot(ridge_results['param_alpha'], ridge_results['mean_test_score'], label = 'Testing R2') | |
plt.plot(ridge_results['param_alpha'], ridge_results['mean_train_score'], label = 'Training R2') | |
# lejand, x ve y ekseninin adlandırılması, başlıklandırma | |
plt.xlabel('Alpha Values'); plt.ylabel('R2'); plt.legend(); | |
plt.title('Performance vs Alpha', size=14); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment