如何在网格搜索后在pivot_table上绘制热图

时间:2018-02-14 16:18:40

标签: python-3.x pandas scikit-learn linear-regression grid-search

我使用ElasticNet进行了网格搜索,但是我无法绘制热图来查看alpha和l1比率之间的关系。我能够到达pivot_table,但我不知道如何用热图来形象化它。有人可以帮忙吗?

我的代码:

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

我希望实现这样的目标: heat map on alpha and l1 ratio

1 个答案:

答案 0 :(得分:4)

     import seaborn as sns       
     ax = sns.heatmap(pvt)