按行划分数据帧(或numpy数组)的正确方法

2024-05-15 08:10:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我是机器学习领域的新手,我正在学习rnn来对时间序列进行分类。 我正在研究这个数据集https://archive.ics.uci.edu/ml/datasets/EEG+Eye+State# 由14个时间序列组成,每个时间序列的步数等于14980 我想得到的是一组时间序列,正好有20个时间步,因此一个numpy数组具有形状(749,20,14) 其中749是时间序列数,20是时间序列的时间步数,14是每个时间步的值数。 然后将该阵列输入网络进行训练。 实现这一目标的正确方法是什么

从dataframe开始,最后一列包含用于对时间序列进行分类的整数

#how to divide it right?
data = arff.loadarff('./datasets/eeg_eye_state.arff')

df = pd.DataFrame(data[0])
df['eyeDetection'] = df['eyeDetection'].str.decode('utf-8')
df['eyeDetection'] = df['eyeDetection'].astype(str).astype(int)

Tags: 机器arffdfdata时间分类序列领域
1条回答
网友
1楼 · 发布于 2024-05-15 08:10:10

由于您正在使用EEG Eye State数据集,并且:

All values are in chronological order with the first measured value at the top of the data.

您可以使用tensorflow.keras实用程序类中的^{}来生成时态数据批

from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator

n_input = 20
batch_size = 749
data_input = df.drop(columns=['eyeDetection'])

data_gen = TimeseriesGenerator(data_input, df.eyeDetection, length=n_input, batch_size=batch_size)

batch_0 = data_gen[0]
x, y = batch_0

print(x.shape)
print(y.shape)

#feed possibly to a model.fit()
#model.fit(data_gen, ...)
(749, 20, 14)
(749,)

相关问题 更多 >