其他分享
首页 > 其他分享> > 20210322_23期_心跳检测_Task03_特征工程

20210322_23期_心跳检测_Task03_特征工程

作者:互联网

三、特征工程

在这里插入图片描述

这里写目录标题

来源

Datewhle23期__数据挖掘心跳检测 :
https://github.com/datawhalechina/team-learning-data-mining/tree/master/HeartbeatClassification
作者:鱼佬、杜晓东、张晋、王皓月、牧小熊、姚昱君、杨梦迪

论坛地址:http://datawhale.club/t/topic/1574

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

1 特征工程

在这里插入图片描述

图片来源 :https://www.jianshu.com/p/7066558bd386

2 载入数据及预处理

2.1 载入数据

path = 'C:/Users/GJX/Desktop/Datawhale-学习/23期/心电图/data/'
train = pd.read_csv(path+'train.csv')
test = pd.read_csv(path+'testA.csv')
train.head()

在这里插入图片描述

2.2 数据预处理

# 对心电特征进行行转列处理,同时为每个心电信号加入时间步特征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列单独存储
train_label = train["label"]
train = train.drop("label", axis=1)
train = train.drop("heartbeat_signals", axis=1)
train = train.join(train_heartbeat_df)    #连接训练集和处理后的df

train

3 使用 tsfresh 进行时间序列特征处理

3.1 tsfresh

3.2 特征提取

from tsfresh import extract_features

# 特征提取
train_features = extract_features(train, column_id='id', column_sort='time')
train_features

id		sum_values		abs_energy		mean_abs_change		mean_change 	...
0			38.927945			18.216197			0.019894					-0.004859			...
1			19.445634			7.705092			0.019952					-0.004762			...
2			21.192974			9.140423			0.009863					-0.004902			...
...		...						...						...								...						...
99997	40.897057			16.412857			0.019470					-0.004538			...
99998	42.333303			14.281281			0.017032					-0.004902			...
99999	53.290117			21.637471			0.021870					-0.004539			...

100000 rows × 779 columns

3.3 特征选择

from tsfresh.utilities.dataframe_functions import impute

# 去除抽取特征中的NaN值
impute(train_features)
id		sum_values		abs_energy		mean_abs_change		mean_change 	...
0			38.927945			18.216197			0.019894					-0.004859			...
1			19.445634			7.705092			0.019952					-0.004762			...
2			21.192974			9.140423			0.009863					-0.004902			...
...		...						...						...								...						...
99997	40.897057			16.412857			0.019470					-0.004538			...
99998	42.333303			14.281281			0.017032					-0.004902			...
99999	53.290117			21.637471			0.021870					-0.004539			...

100000 rows × 779 columns
from tsfresh import select_features

# 按照特征和数据label之间的相关性进行特征选择
train_features_filtered = select_features(train_features, data_train_label)

train_features_filtered
id		sum_values		fft_coefficient__attr_"abs"__coeff_35		fft_coefficient__attr_"abs"__coeff_34		...
0			38.927945			1.168685																0.982133																...
1			19.445634			1.460752																1.924501																...
2			21.192974			1.787166																2.1469872																...
...		...						...																			...																			...
99997	40.897057			1.190514																0.674603																...
99998	42.333303			1.237608																1.325212																...
99999	53.290117			0.154759																2.921164																...

100000 rows × 700 columns

可以看到经过特征选择,留下了700个特征。

参考资料

  1. https://www.jianshu.com/p/7066558bd386 特征工程介绍
  2. http://joyfulpandas.datawhale.club/Content/ch6.html pandas教程
  3. https://www.codetd.com/article/3219752 tsfresh

标签:...,features,23,df,20210322,特征,train,heartbeat,Task03
来源: https://blog.csdn.net/yuliuchenyin/article/details/115096967