1

您能否以这种方式更改我的代码,以便将这 2 个图放在旁边,即 1 行和 2 列 (subplot nrows=1, ncols=2) ?目前,我将这些图表放在 2 个单独的单元格中,我希望将它们放在 1 个单元格中。

我的代码:第一个情节:

from yellowbrick.classifier import (PrecisionRecallCurve)
fig, ax = plt.subplots(figsize=(10, 6))
viz = PrecisionRecallCurve(DecisionTreeClassifier(max_depth=4))
viz.fit(X_train_model_2, y_train_model_2)
print(viz.score(X_test_model_2, y_test_model_2))
viz.ax.set(title="Krzywa precyzja-czułość klasyfikatora drzewa losowego",
           xlabel="Czułość",
           ylabel="Precyzja")
ax.legend(("Binarna krzywa precyzja-czułość",
           "Średnia precyzja = {:0.2f}".format(viz.score(X_test_model_2,y_test_model_2))),
          frameon=True,
          loc="lower left")

plt.show()

第二个情节:

import scikitplot as skplt
fig, ax = plt.subplots(figsize=(10, 6))
y_probas = decision_tree.predict_proba(X_test_model_2)
skplt.metrics.plot_cumulative_gain(y_test_model_2,
                                   y_probas,
                                   ax=ax)
ax.set(title="Krzywa skumulowanych zysków",
       xlabel="Odsetek próbek",
       ylabel="Zysk")
ax.legend(("Klasa 0",
           "Klasa 1",
           "Krzywa odniesienia"),
          frameon=True,
          loc="lower right")
plt.show()
4

1 回答 1

0

也许这有帮助:

from yellowbrick.classifier.prcurve import PrecisionRecallCurve                                                                                                                                          
import scikitplot as skplt
import numpy as np
import sklearn
import sklearn.tree
import matplotlib.pyplot as plt

#generate some test data
X = np.arange(200)+np.random.normal(0,10,200)
y = np.array([True if (x <100) and (x > 50) else False for x in X])
X = X.reshape(-1,1)

X_train_model_2 = []
y_train_model_2 = []
X_test_model_2 = []
y_test_model_2 = []

X_train_model_2,X_test_model_2,y_train_model_2,y_test_model_2=
sklearn.model_selection.train_test_split(
                                         X, y,
                                         test_size=0.4,
                                         random_state=0)

fig, (ax1, ax2) = plt.subplots(1,2)  #1 row, 2 columns

viz = PrecisionRecallCurve(sklearn.tree.DecisionTreeClassifier(max_depth=4),
      ax = ax1) #set the axis to plot one (ax1)
decision_tree = viz.fit(X_train_model_2, y_train_model_2)
print(viz.score(X_test_model_2, y_test_model_2))

#Set the attributes for plot one
ax1.set(title="Krzywa precyzja-czułość klasyfikatora drzewa losowego",
        xlabel="Czułość",
        ylabel="Precyzja")
ax1.legend(("Binarna krzywa precyzja-czułość",
        "Średnia precyzja  {:0.2f}".format(viz.score(X_test_model_2,y_test_model_2))),
        frameon=True,
        loc="lower left")
y_probas = decision_tree.predict_proba(X_test_model_2)

skplt.metrics.plot_cumulative_gain(y_test_model_2,
                                   y_probas,
                                   ax=ax2) #set the axis to plot two (ax2)

#Set the attributes for plot two
ax2.set(title="Krzywa skumulowanych zysków",
        xlabel="Odsetek próbek",
        ylabel="Zysk")
ax2.legend(("Klasa 0",
            "Klasa 1",
            "Krzywa odniesienia"),
            frameon=True,
            loc="lower right")
#Show the whole plot
plt.show()
于 2020-08-27T13:58:19.857 回答