gpt4 book ai didi

python - 如何 "standardize"具有可变长度的数据集?

转载 作者:行者123 更新时间:2023-12-01 07:15:43 25 4
gpt4 key购买 nike

我不确定我的问题措辞是否正确,但要点如下:我正在使用的数据集 SVC 2004 有 x 个文件,每个文件都有 y 个 7 元组,因此数据集的形状变为 (x, y, 7)。我已经对数据进行了标准化,并将其插入一维 CNN 中进行特征提取,并插入 RNN 作为分类器。但问题是:每个文件的 y 永远不会相同。这会在创建顺序模型时导致问题,因为它需要恒定的形状。这是我的一些代码:

//DataPreprocessing
def load_dataset_normalized(path):
file_names = os.listdir(path)

num_of_persons = len(file_names)

initial_starting_point = np.zeros(np.shape([7]))

highest_num_of_points = find_largest_num_of_points(path)

x_dataset = []
y_dataset = []

current_file = 0

for infile in file_names:
full_file_name = os.path.join(path, infile)
file = open(full_file_name, "r")
file_lines = file.readlines()

num_of_points = int(file_lines[0])

x = []
y = []
time_stamp = []
button_status = []
azimuth_angles = []
altitude = []
pressure = []

for idx, line in enumerate(file_lines[1:]):
idx+=1
nums = line.split(' ')

if idx == 1:
nums[2] = 0
initial_starting_point = nums

x.append(float(nums[0]))
y.append(float(nums[1]))
time_stamp.append(0.0)
button_status.append(float(nums[3]))
azimuth_angles.append(float(nums[4]))
altitude.append(float(nums[5]))
pressure.append(float(nums[6]))

else:
x.append(float(nums[0]))
y.append(float(nums[1]))
time_stamp.append(10)
button_status.append(float(nums[3]))
azimuth_angles.append(float(nums[4]))
altitude.append(float(nums[5]))
pressure.append(float(nums[6]))

max_x = max(x)
max_y = max(y)
max_azimuth_angle = max(azimuth_angles)
max_altitude = max(altitude)
max_pressure = max(pressure)

min_x = min(x)
min_y = min(y)
min_azimuth_angle = min(azimuth_angles)
min_altitude = min(altitude)
min_pressure = min(pressure)

#Alignment normalization:
for i in range(num_of_points):
x[i] -= float(initial_starting_point[0])
y[i] -= float(initial_starting_point[1])
azimuth_angles[i] -= float(initial_starting_point[4])
altitude[i] -= float(initial_starting_point[5])
pressure[i] -= float(initial_starting_point[6])

#Size normalization
for i in range(num_of_points):
x[i] = ((x[i] - max_x) / (min_x - max_x))
y[i] = ((y[i] - max_y) / (min_y - max_y))
azimuth_angles[i] = ((azimuth_angles[i] - max_azimuth_angle) / (min_azimuth_angle - max_azimuth_angle))
altitude[i] = ((altitude[i] - max_altitude) / (min_altitude - max_altitude))
pressure[i] = ((pressure[i] - max_pressure) / (min_pressure - max_pressure))

#data points to dataset
x_line = []
for i in range (num_of_points):
x_line.append(([x[i], y[i], time_stamp[i], button_status[i], azimuth_angles[i], altitude[i], pressure[i]]))
if (num_of_points < 713) and (i == num_of_points-1):
for idx in range(713 - num_of_points):
x_line.append([0, 0, 0, 0, 0, 0, 0])

if i == num_of_points-1:
x_dataset.append(x_line)

current_file += 1

infile_without_extension = infile.replace('.TXT','')
index_of_s = infile_without_extension.find("S")
index_of_num = index_of_s + 1
sig_ID = int(infile_without_extension[index_of_num:])
if sig_ID < 21:
y_dataset.append([1,0])
else:
y_dataset.append([0,1])

x_dataset = np.array([np.array(xi) for xi in x_dataset])
y_dataset = np.asarray(y_dataset)
return x_dataset, y_dataset, highest_num_of_points

//Class that creates my model (creation of model works perfectly)
class crnn_model:
def build_model(self, input_shape_num, x_train, y_train, x_test, y_test):
model = Sequential()
model.add(Conv1D(filters=50, kernel_size=3, activation='sigmoid', input_shape = (713, 7)))
model.add(MaxPooling1D(pool_size=3))
model.add(LSTM(2))
model.compile(optimizer='adam', loss='mse', metrics = ['accuracy'])
model.summary()

print(model.fit(x_train, y_train, epochs=50, verbose=0))

yhat = model.predict(x_test, verbose=0)
print(yhat)

我考虑过使用具有最多 7 元组数量的文件作为形状,因为我现在已经使用上面的代码 (713) 进行了硬编码。这是一个好的选择吗?如果不是,我该如何“标准化”或“规范化”CNN 输入形状的点数 (y)?

最佳答案

这里指的是可变长度序列,可以通过输入input_shape=(None, 7)来实现。这表示每个批处理的 y 可能会有所不同,但不会在批处理内发生变化。所以我们通过以下方式解决这个问题:

  • 实现 Sequence它获取数据并将其分成批处理。对于每个批处理,它使用 pad_sequences 通过填充返回(batch_size, y_max_within_batch, 7) .
  • 设置input_shape=(None, 7),以便在运行时您的模型可以接受不同长度的输入。这表示我会在每批中使用许多 7 元组。
  • 可以选择使用Masking Layer掩盖填充序列,以便您的模型不使用填充作为特征,而仅预测样本的正确长度。您还可以预测填充,这将使预测更容易考虑序列的长度。

这些主要是在 Keras 中处理不同长度序列所需的工具/层。您如何使用它们以及填充的效果等取决于您要解决的任务。

关于python - 如何 "standardize"具有可变长度的数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57980344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com