回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我在研究一个有20个班的分类问题。我试图通过使用<em>matplotlib</em>的混淆矩阵来可视化结果。你知道吗</p>
<p>在计算了混淆矩阵之后,我使用了<code>plot_confusion_matrix</code>描述的<a href="https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html" rel="nofollow noreferrer">here</a>。你知道吗</p>
<pre><code>def plot_confusion_matrix(y_true, y_pred, classes,
normalize=False,
title=None,
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if not title:
if normalize:
title = 'Normalized confusion matrix'
else:
title = 'Confusion matrix, without normalization'
# Compute confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Only use the labels that appear in the data
classes = classes[unique_labels(y_true, y_pred)]
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
ax.figure.colorbar(im, ax=ax)
# We want to show all ticks...
ax.set(xticks=np.arange(cm.shape[1]),
yticks=np.arange(cm.shape[0]),
# ... and label them with the respective list entries
xticklabels=classes, yticklabels=classes,
title=title,
ylabel='True label',
xlabel='Predicted label')
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, format(cm[i, j], fmt),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
return ax
</code></pre>
<p>下面是它的样子:
<a href="https://i.stack.imgur.com/pPYnK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pPYnK.png" alt="enter image description here"/></a>
看起来问题来自处理太多的类,所以自然的解决方案是按比例放大绘图。但这样做会扭曲它。另外,如何选择正确的比例/大小?你知道吗</p>
<p>我怎样才能让它看起来更好呢?你知道吗</p>
<p>另外,你可以在csv文件<a href="https://gist.github.com/taha-yassine/1610b6dc81b80aac7decd7f5cb9dcc68" rel="nofollow noreferrer">here</a>中找到混淆矩阵。你知道吗</p>