【问题标题】:Python Scatter Plot Edgecolors by LabelPython Scatter Plot Edgecolors by Label
【发布时间】:2020-09-18 20:42:10
【问题描述】:

我试图弄清楚如何动态设置数据点的边缘颜色。 可以使用 c=[数字标签数组] 设置 facecolor,但对于 edgecolors 则不然。

我试图实现的是一个图,其中 facecolor 等于实例算法的预测标签,edgecolor 等于该实例的真实标签。 这样,我可以直观地比较结果(对于进一步的工作很重要)。

有什么建议吗?到目前为止,这是我的代码:

fig, axs = plt.subplots(2, 2)
axs[0, 0].scatter(X_train[:,0], X_train[:,1], c=y_train)
axs[0, 0].set_title('org.Data with true labels')
axs[0, 1].scatter(X_train[:,0], X_train[:,1], c=y_pred)
axs[0, 1].set_title('org.Data with LOF labels')
axs[1, 0].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred)
axs[1, 0].set_title("PCAplt, labels LOF: org.Data")
axs[1, 1].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred_PCA)
axs[1, 1].set_title('PCA.plt, labels LOF: PCA')

这给了我以下情节

感谢任何建议。 谢谢!

【问题讨论】:

  • 参见this post 了解如何创建双色散点图。

标签: python matplotlib scatter


【解决方案1】:

有时,在放置一些东西休息一段时间后,答案就会出现。我找到了一个解决方案并想分享以防有人需要这个:

简而言之:我使用非常简单的列表推导预先计算了一个“真实标签颜色”列表,并使用另一个长度为 2 的列表,颜色相同。

代码:

colors = ["green","red"]
true_labels_color_encoded = ["red" if label == 1 else "green" for label in y_train]

fig, axs = plt.subplots(2, 2)
axs[0, 0].scatter(X_train[:,0], X_train[:,1], c=y_train, cmap=ListedColormap(colors), edgecolors=true_labels_color_encoded)
axs[0, 0].set_title('org.Data with true labels')
axs[0, 1].scatter(X_train[:,0], X_train[:,1], c=y_pred,cmap=ListedColormap(colors),edgecolors=true_labels_color_encoded)
axs[0, 1].set_title('org.Data with LOF labels')
axs[1, 0].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred,cmap=ListedColormap(colors),edgecolors=true_labels_color_encoded)
axs[1, 0].set_title("PCAplt, labels LOF: org.Data")
axs[1, 1].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred_PCA,cmap=ListedColormap(colors),edgecolors=true_labels_color_encoded)
axs[1, 1].set_title('PCA.plt, labels LOF: PCA')

所以 true_labels_color_encoded 看起来像 = ["red","green","green","green"] (注意:在我的例子中,y_train = 1 中的标签表示异常,0 表示正常)

这给了我以下情节:

See my plot here

使用 ISOMAP 或 t-SNE 等其他缩减技术我得到了更好的结果。

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2021-07-15
    • 2019-02-22
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多