作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个时间序列数据集,如下所示,我为每个传感器记录了 2 个时间序列。 Label
列描述传感器是否有故障(即 0
或 1
)。
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/
我是一名优秀的程序员,十分优秀!