Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to plot a heat map on pivot_table after grid-search

I ran a Grid-searching with ElasticNet but I had trouble plotting a heat map to see the relation between alpha and l1 ratio. I was able to get to the pivot_table, but I don't know how to visualize it w/ a heat map. Can anyone please help?

My codes:

from sklearn.datasets import fetch_california_housing
cal=fetch_california_housing()

X = cal.data
y = cal.target 
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

import matplotlib.pyplot as plt
%matplotlib inline

param_grid = {'alpha': np.logspace(-3, -1, 10), 'l1_ratio':[0.01, .1, .9, 
.98, 1]}
print(param_grid)
grid = GridSearchCV(ElasticNet(normalize=True), param_grid, cv=10)
grid.fit(X_train, y_train)
print("Best cross-validation score: {:.2f}".format(grid.best_score_))
print("Best parameters: ", grid.best_params_)

import pandas as pd
pvt = pd.pivot_table(pd.DataFrame(grid.cv_results_),
    values='mean_test_score', index='param_alpha', columns='param_l1_ratio')

pvt

I want to achieve something like this: heat map on alpha and l1 ratio

like image 566
Edward Lin Avatar asked Oct 31 '25 01:10

Edward Lin


1 Answers

     import seaborn as sns       
     ax = sns.heatmap(pvt)
like image 95
asimo Avatar answered Nov 02 '25 08:11

asimo