赞
踩
# 包导入
import pandas as pd
import numpy as np
import tsfresh as tsf
from tsfresh import extract_features, select_features
from tsfresh.utilities.dataframe_functions import impute
# 数据读取
data_train = pd.read_csv("./data/train.csv")
data_test_A = pd.read_csv("./data/testA.csv")
print(data_train.shape)
print(data_test_A.shape)
data_train.head()
data_test_A.head()
# 对心电特征进行行转列处理,同时为每个心电信号加入时间步特征time
train_heartbeat_df = data_train["heartbeat_signals"].str.split(",", expand=True).stack()
train_heartbeat_df = train_heartbeat_df.reset_index()
train_heartbeat_df = train_heartbeat_df.set_index("level_0")
train_heartbeat_df.index.name = None
train_heartbeat_df.rename(columns={"level_1":"time", 0:"heartbeat_signals"}, inplace=True)
train_heartbeat_df["heartbeat_signals"] = train_heartbeat_df["heartbeat_signals"].astype(float)
train_heartbeat_df
# 将处理后的心电特征加入到训练数据中,同时将训练数据label列单独存储
data_train_label = data_train["label"]
data_train = data_train.drop("label", axis=1)
data_train = data_train.drop("heartbeat_signals", axis=1)
data_train = data_train.join(train_heartbeat_df)
data_train[data_train["id"]==1]
每个样本的心电特征都有205个时间步的心电信号组成。
这里用了提取了部分特征:
from tsfresh.feature_extraction import extract_features, MinimalFCParameters
train_features = extract_features(data_train, column_id='id', column_sort='time',default_fc_parameters=MinimalFCParameters())
train_features
全部特征:
from tsfresh.feature_extraction import extract_features
train_features = extract_features(data_train, column_id='id', column_sort='time')
train_features
特征选择 train_features中包含了heartbeat_signals的779种常见的时间序列特征,这其中有的特征可能为NaN值(产生原因为当前数据不支持此类特征的计算),使用以下方式去除NaN值:
python from tsfresh.utilities.dataframe_functions import impute
impute(train_features_from_file)
接下来,按照特征和响应变量之间的相关性进行特征选择,这一过程包含两步:首先单独计算每个特征和响应变量之间的相关性,然后利用Benjamini-Yekutieli procedure
进行特征选择,决定哪些特征可以被保留。
from tsfresh import select_features
# 按照特征和数据label之间的相关性进行特征选择
train_features_filtered = select_features(train_features_from_file, data_train_label)
train_features_filtered
接下来按照特征和响应变量之间的相关性进行特征选择,这一过程包含两步:
首先单独计算每个特征和响应变量之间的相关性,然后利用Benjamini-Yekutieli procedure 进行特征选择,决定哪些特征可以被保留。
from tsfresh import select_features
# 按照特征和数据label之间的相关性进行特征选择
train_features_filtered = select_features(train_features, data_train_label)
train_features_filtered
经过特征选择,留下了700个特征。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。