特征过程
详细部分,后续再补
记录代码部分
#导包
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
数据之前已经导入过
# 对心电特征进行行转列处理,同时为每个心电信号加入时间步特征time
train_heartbeat_df = 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 = train["label"]
data_train = train.drop("label", axis=1)
data_train = train.drop("heartbeat_signals", axis=1)
data_train = data_train.join(train_heartbeat_df)
data_train
特征抽取
由于电脑配置不够,所以在baseline给出的代码中加入了MinimalFCParameters(),运行速度大大加快
from tsfresh.feature_extraction import extract_features, MinimalFCParameters
train_features = extract_features(data_train,default_fc_parameters=MinimalFCParameters(), column_id='id', column_sort='time')
train_features
去除nan值
from tsfresh.utilities.dataframe_functions import impute
impute(train_features)
# 按照特征和数据label之间的相关性进行特征选择
train_features_filtered = select_features(train_features, data_train_label)
train_features_filtered