删除二维特征图中指定区域的样本
数据在df_renew
中,特征分别是Blockade
与Dwell Time(ms)
,代码如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Pathpolygon_vertices = [(0.87, 5.0),(0.88, 4.0),(0.882, 3),(0.9, 2.5),(0.92, 1),(0.95, 1),(0.9, 5),(0.87, 5.0),
]
polygon_path = Path(polygon_vertices)
points = np.column_stack((df_renew["Blockade"].values,df_renew["Dwell Time (ms)"].values))
inside_mask = polygon_path.contains_points(points)
#
plt.figure(figsize=(6, 6))
plt.scatter(df_renew["Blockade"], df_renew["Dwell Time (ms)"], s=10, alpha=0.5, label="All points")
plt.scatter(df_renew["Blockade"][inside_mask], df_renew["Dwell Time (ms)"][inside_mask],color='red', s=10, label="To be removed")
plt.plot(*zip(*polygon_vertices), color='black', linestyle='--', label='Polygon')
plt.fill(*zip(*polygon_vertices), alpha=0.1, color='gray')
plt.xlabel("Blockade")
plt.ylabel("Dwell Time (ms)")
plt.legend()
plt.title("Polygon-based removal")
plt.show()
#
df_renew = df_renew[~inside_mask] # 去除多边形内的点