调整图黄砖模型-python

Adjust figure yellow bricks model - python

我正在尝试调整黄砖图形的轴限制。但是,我似乎无法调整它。我可以更改轴标签和标题,但不能更改限制。如果我不使用 visualizer.show() 渲染图形,它会起作用,但随后我会丢失标签、标题、图例等

from sklearn.linear_model import RidgeClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OrdinalEncoder, LabelEncoder
from yellowbrick.classifier import ROCAUC
from yellowbrick.datasets import load_game
import matplotlib.pyplot as plt

X, y = load_game()

X = OrdinalEncoder().fit_transform(X)
y = LabelEncoder().fit_transform(y)

fig, ax = plt.subplots()
ax.set_xlim([-0.05, 1.0])
ax.set_ylim([0.0, 1.05])

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

fig, ax = plt.subplots(figsize = (10,6))

model = RidgeClassifier()
visualizer = ROCAUC(model, classes=["win", "loss", "draw"], ax = ax)

visualizer.fit(X_train, y_train)       
visualizer.score(X_test, y_test)        
visualizer.show()                       

您可以尝试调用 visualizer.finalize() 方法,然后访问底层的 matplotlib 轴来更改限制,而不是调用 visualizer.show() 方法。您还覆盖了 ax,这对您也没有任何帮助。

这是完整的代码示例:

from sklearn.linear_model import RidgeClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OrdinalEncoder, LabelEncoder
from yellowbrick.classifier import ROCAUC
from yellowbrick.datasets import load_game
import matplotlib.pyplot as plt

X, y = load_game()

X = OrdinalEncoder().fit_transform(X)
y = LabelEncoder().fit_transform(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

fig, ax = plt.subplots(figsize = (10,6))

model = RidgeClassifier()
visualizer = ROCAUC(model, classes=["win", "loss", "draw"], ax=ax)

visualizer.fit(X_train, y_train)       
visualizer.score(X_test, y_test)        
visualizer.finalize()   
ax.set_xlim([-0.05, 1.0])
ax.set_ylim([0.0, 1.05])