為了加速我的代碼,我想通過矢量化或其他推薦的工具來交換我的 for 回圈。我發現了很多替換簡單 for 回圈的例子,但沒有找到替換嵌套 for 回圈結合條件的例子,我能夠理解/會幫助我......
使用我的代碼,我想檢查點(X,Y 坐標)是否可以通過線形(線性結構)連接。我開始時非常簡單,但隨著時間的推移,代碼本身變得過長,現在速度很慢……這是一個花費最多時間的部分的作業示例:
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import MultiLineString, LineString, Point
from shapely.affinity import rotate
from math import sqrt
from tqdm import tqdm
import random as rng
# creating random array of points
xys = rng.sample(range(201 * 201), 100)
points = [list(divmod(xy, 201)) for xy in xys]
# plot points
plt.scatter(*zip(*points))
# calculate length for rotating lines -> diagonal of bounds so all points able to be reached
length = sqrt(2)*200
# calculate angles to rotate lines
angles = []
for a in range(0, 360, 1):
angle = np.deg2rad(a)
angles.append(angle)
# copy points array to helper array (points_list) so original array is not manipulated
points_list = points.copy()
# array to save final lines
lines = []
# iterate over every point in points array to search for connecting lines
for point in tqdm(points):
# delete point from helper array to speed up iteration -> so points do not get
# double, triple, ... checked
if len(points_list) > 0:
points_list.remove(point)
else:
break
# create line from original point to point at end of line (x length) - this line
# gets rotated at calculated angles
start = Point(point)
end = Point(start.x length, start.y)
line = LineString([start,end])
# iterate over angle Array to rotate line by each angle
for angle in angles:
rot_line = rotate(line, angle, origin=start, use_radians=True)
lst = list(rot_line.coords)
# save starting point (a) and ending point(b) of rotated line for np.cross()
# (cross product to check if points on/near rotated line)
a = np.asarray(lst[0])
b = np.asarray(lst[1])
# counter to count number of points on/near line
count = 0
line_list = []
# iterate manipulated points_list array (only points left for which there has
# not been a line rotated yet)
for poi in points_list:
# check whether point (pio) is on/near rotated line by calculating cross
# product (np.corss())
p = np.asarray(poi)
cross = np.cross(p-a,b-a)
# check if poi is inside accepted deviation from cross product
if cross > -750 and cross < 750:
# check if more than 5 points (poi) are on/near the rotated line
if count < 5:
line_list.append(poi)
count = 1
# if 5 points are connected by the rotated line sort the coordinates
# of the points and check if the length of the line meets the criteria
else:
line_list = sorted(line_list , key=lambda k: [k[1], k[0]])
line_length = LineString(line_list)
if line_length.length >= 10 and line_length.length <= 150:
lines.append(line_list)
break
# use shapeplys' MultiLineString to create lines from coordinates and plot them
# afterwards
multiLines = MultiLineString(lines)
fig, ax = plt.subplots()
ax.set_title("Lines")
for multiLine in MultiLineString(multiLines).geoms:
# print(multiLine)
plt.plot(*multiLine.xy)
如上所述,它正在考慮使用 pandas 或 numpy 向量化,因此為點和線 (gdf) 構建一個 pandas df,并用不同的角度 (angles) 來旋轉線:
| 姓名 | 型別 | 尺寸 | 價值 |
|---|---|---|---|
| gdf | 資料框 | (122689, 6) | 列名:x、y、value、start、end、line |
| 角度 | 資料框 | (360, 1) | 欄目名稱:角度 |
但是我想不出用 pandas 矢量化條件替換這個嵌套 for 回圈的想法。我在文章的中途和中途找到了這篇文章,其中提到了矢量化的條件,我想知道我的代碼是否可能因為回圈內的依賴性而不適合矢量化......
如果這是正確的,則不一定需要矢量化所有可以提高性能的東西都是受歡迎的!
uj5u.com熱心網友回復:
您可以很容易地向量化計算最密集的部分:最內層的回圈。這個想法是一次計算points_list所有。np.cross可應用于每一行,np.where可用于過濾結果(并獲取 ID)。
這是(幾乎沒有測驗過的)修改后的主回圈:
for point in tqdm(points):
if len(points_list) > 0:
points_list.remove(point)
else:
break
start = Point(point)
end = Point(start.x length, start.y)
line = LineString([start,end])
# CHANGED PART
if len(points_list) == 0:
continue
p = np.asarray(points_list)
for angle in angles:
rot_line = rotate(line, angle, origin=start, use_radians=True)
a, b = np.asarray(rot_line.coords)
cross = np.cross(p-a,b-a)
foundIds = np.where((cross > -750) & (cross < 750))[0]
if foundIds.size > 5:
# Similar to the initial part, not efficient, but rarely executed
line_list = p[foundIds][:5].tolist()
line_list = sorted(line_list, key=lambda k: [k[1], k[0]])
line_length = LineString(line_list)
if line_length.length >= 10 and line_length.length <= 150:
lines.append(line_list)
這在我的機器上快了大約 15 倍。
大部分時間花在非常低效的 shapely 模塊上(尤其是rotateeven np.asarray(rot_line.coords))。確實,每次呼叫rotate大約需要 50 微秒,這簡直是瘋了:它應該不超過 50 納秒,也就是說,快 1000 倍(實際上,優化的本機代碼應該能夠在我的機器上不到 20 納秒) ). 如果你想要更快的代碼,那么請考慮不使用這個包(或提高它的性能)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/536598.html
上一篇:R添加到for回圈
