GridSearchCV 结果热图

spo*_*shr 4 python matplotlib scikit-learn seaborn sklearn-pandas

我正在尝试为 sklearn 的 GridSearchCV 结果生成热图。我喜欢sklearn-evaluation的一点是它非常容易生成热图。然而,我遇到了一个问题。当我将参数指定为 None 时,例如

max_depth = [3, 4, 5, 6, None]
Run Code Online (Sandbox Code Playgroud)

生成热图时,它显示错误:

TypeError: '<' not supported between instances of 'NoneType' and 'int'
Run Code Online (Sandbox Code Playgroud)

有什么解决方法吗?我找到了其他生成热图的方法,例如使用 matplotlib 和 seaborn,但没有什么能像 sklearn-evalutaion 那样提供漂亮的热图。

在此输入图像描述

lif*_*ful 5

我摆弄着这个grid_search.py文件/lib/python3.8/site-packages/sklearn_evaluation/plot/grid_search.py。在第 192/193 行更改行

row_names = sorted(set([t[0] for t in matrix_elements.keys()]),
                   key=itemgetter(1))
col_names = sorted(set([t[1] for t in matrix_elements.keys()]),
                   key=itemgetter(1))
Run Code Online (Sandbox Code Playgroud)

到:

row_names = sorted(set([t[0] for t in matrix_elements.keys()]),
                   key=lambda x: (x[1] is None, x[1]))
col_names = sorted(set([t[1] for t in matrix_elements.keys()]),
                   key=lambda x: (x[1] is None, x[1]))
Run Code Online (Sandbox Code Playgroud)

排序时将所有内容移动None到列表末尾是基于安德鲁·克拉克之前的回答

使用此调整,我的演示脚本如下所示:

import numpy as np
import sklearn.datasets as datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn_evaluation import plot

data = datasets.make_classification(n_samples=200, n_features=10, n_informative=4, class_sep=0.5)


X = data[0]
y = data[1]

hyperparameters = {
    "max_depth": [1, 2, 3, None],
    "criterion": ["gini", "entropy"],
    "max_features": ["sqrt", "log2"],
}

est = RandomForestClassifier(n_estimators=5)
clf = GridSearchCV(est, hyperparameters, cv=3)
clf.fit(X, y)
plot.grid_search(clf.cv_results_, change=("max_depth", "criterion"), subset={"max_features": "sqrt"})


import matplotlib.pyplot as plt

plt.show()
Run Code Online (Sandbox Code Playgroud)

输出如下图所示: 在此输入图像描述

  • 这是一个很好的解决方案。目前,我使用的是seaborn 热图,这需要一些手动数据操作。很高兴看到这个解决方案合并到 sklearn-evaluation 中。 (2认同)