我正在尝试使用 [https://github.com/jim-spyropoulos/Trajectory-Analysis-and-Classification-in-Python-Pandas-and-Scikit-Learn] 1 中的 fast_dtw_neighbors.py 计算轨迹之间的DTW相似 性当将轨迹作为参数提供给 fastdtw 函数时,就像 .py 文件一样:
# %load fast_dtw_neigbors.py
import csv
import os
import re
import numpy as np
import pandas as pd
from fastdtw import fastdtw
from auxiliaryfunctions import *
df = pd.read_pickle('./final_cleaned.df')
# l = df['timestamp_longitude_latitude'].iloc[0]
with open('./test_set_a1.csv', 'r') as f:
reader = csv.reader(f)
data = (list(rec) for rec in csv.reader(f, delimiter=','))
i = 0
count = 0
trajectories_read = [] # list with all trajectories read
for row in data:
if (i != 0):
tmp_list = [] # to read triples
trajectory = [] # here will be the final trajectory
j = 0
while (j != len(row)):
tmp_list = [float(re.sub('[[]', '', row[j])), float(row[j + 1]), float(re.sub('[]]', '', row[j + 2]))]
j = j + 3
trajectory.append(tmp_list)
tmp_list = []
trajectories_read.append(trajectory)
i = i + 1
print("Dataset read successfully.")
j = 0
for traj in trajectories_read:
# computeDTW similarity of given trajectory with all trajectories of the cleaned_dataset
distances = []
for elem in df['timestamp_longitude_latitude']:
distance, path = fastdtw(elem, traj, dist=haversine_np)
distances.append(distance)
# find the 5 smaller using np arrays
distancesnp = np.array(distances)
sorted_ind = np.argsort(distancesnp)[:5]
# for each we print the results
k = 0
for elem in sorted_ind:
print_results(df, distancesnp, elem, "./DTWresults/trajectory" + str(j) + "neighbor" + str(k))
k = k + 1
j = j + 1
我收到此错误:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12280/1160975391.py in <module>
10 for elem in df['timestamp_longitude_latitude']:
11
---> 12 distance, path = fastdtw(elem, traj, dist=haversine_np)
13 distances.append(distance)
14 # find the 5 smaller using np arrays
D:\anaconda\envs\geo_env\lib\site-packages\fastdtw.py in fastdtw(x, y, radius, dist)
15 return dtw(x, y, dist=dist)
16
---> 17 x_shrinked = __reduce_by_half(x)
18 y_shrinked = __reduce_by_half(y)
19 distance, path = fastdtw(x_shrinked, y_shrinked, radius=radius, dist=dist)
D:\anaconda\envs\geo_env\lib\site-packages\fastdtw.py in __reduce_by_half(x)
42
43 def __reduce_by_half(x):
---> 44 return [(x[i//2] + x[1+i//2]) / 2 for i in xrange(0, len(x), 2)]
45
46
D:\anaconda\envs\geo_env\lib\site-packages\fastdtw.py in <listcomp>(.0)
42
43 def __reduce_by_half(x):
---> 44 return [(x[i//2] + x[1+i//2]) / 2 for i in xrange(0, len(x), 2)]
45
46
TypeError: unsupported operand type(s) for /: 'list' and 'int'
我使用 type() 检查了traj和elem类型,它们都是列表。我还在 Github 存储库中打开了一个问题,但没有得到任何响应。