如何在一个单元格中组合两个绘图,Python?

2024-09-30 16:20:09 发布

您现在位置:Python中文网/ 问答频道 /正文

您能用这种方式更改我的代码,以便将这两个图放在一起,即1行2列(子图nrows=1,ncols=2)吗?目前我把这些图表放在两个单独的单元格中,我想把它们放在一个单元格中

我的代码: 第一个情节:

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()

Tags: 代码testimportmodelfigtrainpltax
1条回答
网友
1楼 · 发布于 2024-09-30 16:20:09

也许这有助于:

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()

相关问题 更多 >