python - Python Scatter Plot Edgecolors by Label
问题描述
我试图弄清楚如何动态设置数据点的边缘颜色。可以使用 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')
这给了我以下情节
我很感激任何建议。谢谢!
解决方案
有时,在放置东西休息一段时间后,答案就会出现。我找到了一个解决方案并想分享以防万一有人需要这个:
简而言之:我使用非常简单的列表理解预先计算了一个“真实标签颜色”列表,并使用了另一个长度为 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 表示正常)
这给了我以下情节:
使用 ISOMAP 或 t-SNE 等其他缩减技术我得到了更好的结果。
推荐阅读
- python - 您应该在 API 方法内部还是外部获取锁?
- android - 触摸 EditText 后 ListView 跳出屏幕
- razor - Html.TextBoxFor 助手不将数据绑定到模型
- android - 电话和 PhoneStateListener
- javascript - 如果对象数组中不存在值,则推零
- python - 更改 df 列格式
- git - 根据我的分支的提示列出所有比我的暂存分支更新的新提交(来自所有分支,包括获取的远程分支)?
- three.js - three.js 中几何类型的变化
- python - Python矩阵乘法3d数组
- applescript - 使用 Applescript 右键单击应用程序文件