Matplotlib 如何正确绘制从MLPClassizer(损耗曲线)获取的损耗值

Matplotlib 如何正确绘制从MLPClassizer(损耗曲线)获取的损耗值,matplotlib,plot,machine-learning,scikit-learn,neural-network,Matplotlib,Plot,Machine Learning,Scikit Learn,Neural Network,我使用以下代码通过给定的数据集拟合模型: tr_X, ts_X, tr_y, ts_y = train_test_split(X, y, train_size=.8) model = MLPClassifier(hidden_layer_sizes=(32, 32), activation='relu', solver=adam, learning_rate='adaptive', e

我使用以下代码通过给定的数据集拟合模型:

tr_X, ts_X, tr_y, ts_y = train_test_split(X, y, train_size=.8)
model = MLPClassifier(hidden_layer_sizes=(32, 32),
              activation='relu',
              solver=adam,
              learning_rate='adaptive',
              early_stopping=True)

model.fit(tr_X, tr_y)
prd_r = model.predict(ts_X)
test_acc = accuracy_score(ts_y, prd_r) * 100.
loss_values = model.estimator.loss_curve_
print (loss_values)
如上所述,通过调用
loss\u curve\
返回损失列表,可以获取每个批次的损失值。我明白了:

[0.69411586222116872, 0.6923803442491846, 0.66657293575365906, 0.43212054205535255, 0.23119813830216157, 0.15497928755966919, 0.11799652235604828, 0.095235784011297939, 0.079951427356068624, 0.069012741113626194, 0.061282868601098078, 0.054871864138797251, 0.049835046972801049, 0.046056362860260207, 0.042823979794540182, 0.040681220899240651, 0.038262366774481374, 0.036256840660697079, 0.034418333946277503, 0.033547227978657508, 0.03285581956914093, 0.031671266419493666, 0.030941451221456757]
我想绘制这些结果,以表示此模型的
损失曲线。问题是我不知道在这种情况下,
x轴和
y轴是什么。如果我将
y轴
设为这些损失值,那么这里的
x轴
应该是什么来显示损失曲线的减少或增加

任何提示或想法都值得欣赏。

绘图()命令重载,不需要x轴。如果只传入
损耗曲线
,默认x轴将是绘制的y值列表中的相应索引。例如,如果我们运行

import matplotlib.pyplot as plt

plt.plot(loss_values)
plt.show()
然后我们得到以下图表:


让我们根据绘图的迭代时间来演示绘图损失和精度

import numpy as np
import matplotlib.pyplot as plt
def draw_result(lst_iter, lst_loss, lst_acc, title):
    plt.plot(lst_iter, lst_loss, '-b', label='loss')
    plt.plot(lst_iter, lst_acc, '-r', label='accuracy')

    plt.xlabel("n iteration")
    plt.legend(loc='upper left')
    plt.title(title)

    # save image
    plt.savefig(title+".png")  # should before show method

    # show
    plt.show()


def test_draw():
    # iteration num
    lst_iter = range(100)

    # loss of iteration
    lst_loss = [0.01 * i + 0.01 * i ** 2 for i in xrange(100)]
    # lst_loss = np.random.randn(1, 100).reshape((100, ))

    # accuracy of iteration
    lst_acc = [0.01 * i - 0.01 * i ** 2 for i in xrange(100)]
    # lst_acc = np.random.randn(1, 100).reshape((100, ))
    draw_result(lst_iter, lst_loss, lst_acc, "sgd_method")


if __name__ == '__main__':
    test_draw()
输出如下:


Its
模型.损耗曲线
。我认为您需要看到这个示例:数组可以拥有的最大大小是
max_iter
param(默认值为200)。此外,由于达到阈值时的提前停止标准,它的大小可能小于该值。