我下面的Python代码非常慢,是否有可能用Numpy语句完全编写此部分?
getToken = () => {
axios({
method: 'get',
url: '/csrf',
timeout: 1000,
headers: 'csrf-token'
}).then(csrfToken => {
console.log(csrfToken);
this.setState({ accessGranted: true })
}).catch(err => console.log(err));
}
首先,我创建一个带有零( m = len(self.man_list)
s = len(self.newnew)
self.zeroMatrix = np.zeros((m,s))
for k in range(m):
a1 = self.man_list[k][2]
b1 = self.man_list[k][0]
a2 = self.man_list[k][3]
b2 = self.man_list[k][1]
for f, part in enumerate(self.extra_list):
x1 = self.extra_list[f][0]
y1 = self.extra_list[f][2]
x2 = self.extra_list[f][1]
y2 = self.extra_list[f][3]
first = np.array((x1, y1))
second = np.array((x2, y2))
third = np.array((a1, b1))
forth = np.array((a2, b2))
dist1 = np.linalg.norm(first - third)
dist2 = np.linalg.norm(second - forth)
distance = (dist1 + dist2)
self.zeroMatrix[k][f] = distance
)的矩阵。
self.zeroMatrix
和self.man_list
包含直线的起点和终点。
例如:
self.extra_list
我得到从第一个列表的每一行到另一列表的每一行的距离,然后将此距离值注册在self.man_list = [ [ [1,2], [3,4] ],...]
self.extra_list = [ [ [11,30], [4, 10] ],...]
中。
非常感谢您的回答!
答案 0 :(得分:2)
您需要对呼叫进行引导:
man_list = np.array(self.man_list)
extra_list = np.array(self.extra_list)
然后创建所需的子矩阵:
first = extra_list[:, None, ::2]
second = extra_list[:, None, 1::2]
third = man_list[None, :, 2::-2]
fourth = man_list[None, :, 3::-2]
现在,在最后一个轴(轴2)上计算范数。
dist1 = np.linalg.norm(first - third, axis=2)
dist2 = np.linalg.norm(second - fourth, axis=2)
distance = (dist1 + dist2)
现在,您应该在distance
中拥有所需的矩阵。