【问题标题】:how to plot a heat map on pivot_table after grid-search网格搜索后如何在 pivot_table 上绘制热图
【发布时间】:2018-07-25 07:02:04
【问题描述】:

我使用 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

我想实现这样的目标:

【问题讨论】:

  • 感谢您的建议

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


【解决方案1】:
     import seaborn as sns       
     ax = sns.heatmap(pvt)

【讨论】:

  • 您还可以添加 annot = True 以将值打印在热图上
猜你喜欢
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2012-01-02
  • 2015-10-13
  • 1970-01-01
相关资源
最近更新 更多