我有一個學校作業,我被困在重復事情上。
我想迭代 mathplotlib 影像矩陣并從我創建的生成器中插入影像。
# this is the generator, it yields back 2 integers (true_value and prediction)
# and a {64,} image shape (image)
def false_negative_generator(X_test, y_test, predicted):
for image, true_value, prediction in zip(X_test, y_test, predicted):
if true_value != prediction:
yield image, true_value, prediction
我迭代的代碼顯然不好,但我不知道如何實作我想要的迭代。
# axrow is a row in the plot matrix, ax is a cell in the matrix
for image, lable, prediction in false_negative_generator(X_test, y_test, predicted):
for axrow in axes:
for ax in axrow:
ax.set_axis_off()
ax.imshow(image.reshape(8, 8), cmap=plt.cm.gray_r, interpolation="nearest")
ax.set_title(f"{lable} {prediction}")
我希望這個問題是清晰易懂的。我很想知道未來改進的問題是否不是 100%。
謝謝!
編輯:
我的目標是將生成器中的每個物件插入到單個矩陣單元中。\
[我現在得到的是這個(所有矩陣單元格中生成器的最后一個物件,當我想要每個單元格中的不同物件時):1
uj5u.com熱心網友回復:
您可能可以使用以下內容:
iterator = false_negative_generator(X_test, y_test, predicted)
for axrow in axes:
for ax in axrow:
image, lable, prediction = next(iterator)
ax.set_axis_off()
ax.imshow(image.reshape(8, 8), cmap=plt.cm.gray_r, interpolation="nearest")
ax.set_title(f"{lable} {prediction}")
這會創建迭代器,但尚未檢索資料。然后,該next()函式每次在嵌套回圈內推進迭代器,從迭代器中檢索必要的專案。
uj5u.com熱心網友回復:
假設生成器回傳的影像數量與圖形的軸數相同,您可以執行以下操作:
i = 0 # counter
axs = axes.flatten() # convert the grid of axes to an array
for image, lable, prediction in false_negative_generator(X_test, y_test, predicted):
axs[i].set_axis_off()
axs[i].imshow(image.reshape(8, 8), cmap=plt.cm.gray_r, interpolation="nearest")
axs[i].set_title(f"{lable} {prediction}")
i = 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461701.html
標籤:Python matplotlib
下一篇:Python的默認調色板
