【发布时间】:2022-12-29 03:30:30
【问题描述】:
我在使用 tslearn 库实现 DTW 分类时遇到了一些问题。特别是在训练模型时。
我有多个可变长度和多个维度的时间序列数据。在第一步,我所做的是将多个文件上传到数据帧列表中以进行可视化。然后我将那些过滤掉所有列的数据帧列表转换为 numpy 数组。分为训练和测试,然后尝试以 dtw 作为距离度量来训练模型。
我不确定我在哪里遗漏了一些东西,因为我面临ValueError:设置带有序列的数组元素。
我试图展平我的 numpy 数组并将其提供给模型,我试图重塑它 (-1,1),我试图将 dtype 更改为 float 但没有任何效果(似乎我遗漏了什么)
以下是我分享的代码的 sn-p:
这些是列表中时间序列的多个数据帧,我进一步减少到仅使用整个列表中的一列
from tslearn.neighbors import KNeighborsTimeSeriesClassifier
from tslearn.metrics import dtw
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
print(timeseries())
OUTPUT : /
time IR TC \
0 0.006361 111.089076 36.395268
1 0.012713 108.589543 36.389164
2 0.018961 107.910312 36.387853
3 0.025192 107.363165 36.389093
4 0.031445 106.785048 36.381344
... ... ... ... ...
p TS IRtime
0 4.318834 44.917064 09:15:15
1 4.522443 45.028859 09:15:15
2 4.485727 45.021396 09:15:15
3 4.518629 45.351248 09:15:15
4 4.513145 45.645672 09:15:15
... ... ... ...
[9589 rows x 10 columns],
time IR TC \
0 0.006373 75.354227 38.953333
1 0.012679 74.493989 38.968091
2 0.018946 76.143107 38.969021
3 0.025177 77.940211 38.962893
4 0.031404 79.705939 38.965134
... ... ... ... ...
p TS IRtime
0 4.519344 33.986072 09:16:23
1 4.551769 34.114317 09:16:23
2 4.522443 34.183959 09:16:23
3 4.535080 34.111575 09:16:23
4 4.532219 34.109334 09:16:23
... ... ... ...
[8376 rows x 10 columns],
ts = timeseries()
[i.drop(columns=['time','IRtime','value','value.1','value.2','value.3','IR','TS','p'],inplace = True) for i in ts]
X = []
for i in ts:
i = i.values
X.append(i.flatten())
y = labels()['target'].values
X = np.array(X)
y = np.array(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print(X_train)
O> [array([36.39526801, 36.38916449, 36.38785319, ..., 39.40492146,
39.40108292, 40.43806081]),
array([38.95333278, 38.96809089, 38.96902072, ..., 40.1875067 ,
40.1795912 , 40.18588545]),
array([39.6719503 , 39.67082973, 39.66928001, ..., 40.43901449,
40.43760782, 40.43856149]),
array([39.82344148, 39.81049535, 39.80937479, ..., 40.63313492,
40.64379223, 40.62316902]),
array([40.0663662 , 40.07699967, 40.06004811, ..., 40.88702689,
40.89880477, 40.89603912]),
# My X_train and X_test looks exactly the same
print(y_train)
O>array([0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1,
1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1]
knn = KNeighborsTimeSeriesClassifier(n_neighbors=2,metric =dtw)
knn.fit(X_train, y_train)
Error :
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
ValueError: setting an array element with a sequence.
我的问题如下:
Q1。我该如何解决这个问题?
Q2:他们是否有任何其他库执行以下计算距离矩阵并对其进行分类? (我也尝试过 dtai 来查找距离矩阵,但不明白如何在我的分类中使用该矩阵,因为我在尝试拟合模型时遇到了同样的问题)
Q3。我想在动态时间扭曲中实现多维(多特征),即 IR、TC、TS 一起进行分类。我们可以使用一些方法使其工作吗?
【问题讨论】:
标签: python numpy time-series classification dtw