gpt4 book ai didi

python - 如何在sklearn中使用时间序列数据进行分类

转载 作者:行者123 更新时间:2023-12-01 00:40:41 25 4
gpt4 key购买 nike

我有一个时间序列数据集,如下所示,我为每个传感器记录了 2 个时间序列。 Label 列描述传感器是否有故障(即 01)。

sensor, time-series 1, time-series 2, Label
x1, [38, 38, 35, 33, 32], [18, 18, 12, 11, 09], 1
x2, [33, 32, 35, 36, 32], [13, 12, 15, 16, 12], 0
and so on ..

目前,我正在考虑两个时间序列的不同特征(例如,最小值、最大值、中值、斜率等),并考虑将它们在 sklearn 的随机森林分类器中进行如下分类。

df = pd.read_csv(input_file)
X = df[[myfeatures]]
y = df['Label']

#Random Forest classifier
clf=RandomForestClassifier(random_state = 42, class_weight="balanced", criterion = 'gini', max_depth = 3, max_features = 'auto', n_estimators = 500)

k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)

output = cross_validate(clf, X, y, cv=k_fold, scoring = 'roc_auc', return_estimator =True)
for idx,estimator in enumerate(output['estimator']):
print("Features sorted by their score for estimator {}:".format(idx))
feature_temp_importances = pd.DataFrame(estimator.feature_importances_,
index = mylist,
columns=['importance']).sort_values('importance', ascending=False)
print(feature_temp_importances)

但是,我的成绩非常低。我想知道是否可以将时间序列数据提供给随机森林分类器。例如,将 x1 特征指定为 [38, 38, 35, 33, 32], [18, 18, 12, 11, 09]。如果可以的话,我想知道如何在sklearn中做到这一点?

如果需要,我很乐意提供更多详细信息。

最佳答案

是的,您可以使用整个时间序列数据作为分类器的特征。

为此,只需使用原始数据,连接每个传感器的 2 个时间序列并将其输入分类器。

from sklearn.model_selection import StratifiedKFold, cross_validate
from sklearn.ensemble import RandomForestClassifier
import numpy as np

n_samples = 100

# generates 2 n_samples random time series with integer values from 0 to 100.
x1 = np.array([np.random.randint(0, 100, 5) for _ in range(n_samples)])
x2 = np.array([np.random.randint(0, 100, 5) for _ in range(n_samples)])

X = np.hstack((x1, x2))


# generates n_samples random binary labels.
y = np.random.randint(0, 2, n_samples)

#Random Forest classifier
clf=RandomForestClassifier(random_state = 42, class_weight="balanced", criterion = 'gini', max_depth = 3, max_features = 'auto', n_estimators = 500)

k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)

output = cross_validate(clf, X, y, cv=k_fold, scoring = 'roc_auc', return_estimator =True)

但是,您可能不想使用具有这些功能的随机森林。看看 LSTM 甚至一维 CNN,它们可能更适合这种使用整个时间序列作为输入的方法。

关于python - 如何在sklearn中使用时间序列数据进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57371065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com