我必须使用scikitlean的kneighborsgericator来使用Python中的用户定义函数来比较时间序列。在
knn = KNeighborsClassifier(n_neighbors=1,weights='distance',metric='pyfunc',func=dtw_dist)
问题是KNeighborsClassifier似乎不支持我的训练数据。它们是时间序列,所以它们是不同大小的列表。当我尝试使用fit
方法(knn.fit(X,Y)
)时,KNeighborsClassifier会给出以下错误消息:
看起来KNeighborsClassifier只支持相同大小的训练集(只接受长度相同的时间序列,但我不是这样),但我的老师告诉我使用KNeighborsClassifier。所以我不知道该怎么办。。。在
有什么想法吗?在
据我所知,有两个(或一个…)选项:
KNeighborsClassifier
似乎,其他的聚类算法都有,例如Spectral Clustering)。在NaN
s将数据转换为正方形,并在自定义距离函数中相应地处理这些数据。在使用
NaN
s“平方”您的数据所以,选择2就是。 假设我们有以下数据,其中每一行代表一个时间序列:
我们只需通过添加nan使数据为正方形:
^{pr2}$现在数据“适合”到算法中。您只需调整距离函数来考虑
NaN
s预计算并使用缓存函数
哦,我们也可以选择1(假设你有
N
时间序列):(N, N)
距离矩阵D
(N, 1)
矩阵,它只是[0, N)
之间的一个范围(即,距离矩阵中序列的索引)wrapper
wrapper
作为距离函数。在wrapper
函数:好吧,希望一切都清楚。在
完整示例
相关问题 更多 >
编程相关推荐