零基础入门数据挖掘-心跳信号分类预测task3

特征工程

直接上代码

#导包
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(),运行速度大大加快,但是最终回去的特征与不加MinimalFCParameters()之前差的多,仅有不到20个特征,但是原baseline的速度太慢,具体两者对最终的结果如何,需要在后面的模型调参和最终评分来确定

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

你可能感兴趣的:(零基础入门数据挖掘-心跳信号分类预测task3)