加快Python中一个点是否处于某个形状的顺序检查

2024-05-19

我有一个代码,用于顺序确定是否在我的中找到每对笛卡尔坐标DataFrame落入某些几何封闭区域。但我怀疑它相当慢,因为它不是矢量化的。这是一个例子:

from matplotlib.patches import Rectangle

r1 = Rectangle((0,0), 10, 10)
r2 = Rectangle((50,50), 10, 10)

df = pd.DataFrame([[1,2],[-1,5], [51,52]], columns=['x', 'y'])

for j in range(df.shape[0]):
    coordinates = df.x.iloc[j], df.y.iloc[j]
    if r1.contains_point(coordinates):
        df['location'].iloc[j] = 0
    else r2.contains_point(coordinates):
        df['location'].iloc[j] = 1

有人可以提出一种加速方法吗?


最好将矩形面片转换为数组,并在推导出它们的分布范围后对其进行处理。

def seqcheck_vect(df):
    xy = df[["x", "y"]].values
    e1 = np.asarray(rec1.get_extents())
    e2 = np.asarray(rec2.get_extents())
    r1m1, r1m2 = np.min(e1), np.max(e1)
    r2m1, r2m2 = np.min(e2), np.max(e2)
    out = np.where(((xy >= r1m1) & (xy <= r1m2)).all(axis=1), 0, 
                   np.where(((xy >= r2m1) & (xy <= r2m2)).all(axis=1), 1, np.nan))
    return df.assign(location=out)

对于给定的样本,函数输出:


基准:

def loopy_version(df):
    for j in range(df.shape[0]):
        coordinates = df.x.iloc[j], df.y.iloc[j]
        if rec1.contains_point(coordinates):
            df.loc[j, "location"] = 0
        elif rec2.contains_point(coordinates):
            df.loc[j, "location"] = 1
        else:
            pass
    return df

测试在DF10K 行:

np.random.seed(42)
df  = pd.DataFrame(np.random.randint(0, 100, (10000,2)), columns=list("xy"))

# check if both give same outcome
loopy_version(df).equals(seqcheck_vect(df))
True

%timeit loopy_version(df)
1 loop, best of 3: 3.8 s per loop

%timeit seqcheck_vect(df)
1000 loops, best of 3: 1.73 ms per loop

因此,矢量化方法比循环方法快大约 2200 倍。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

加快Python中一个点是否处于某个形状的顺序检查 的相关文章

随机推荐