Jupyter Notebook 中数据预处理代码详解:RUL计算、传感器筛选、特征缩放及指数平滑
Jupyter Notebook 中数据预处理代码详解
本代码段展示了在 Jupyter Notebook 中进行数据预处理的常见步骤,包括计算剩余使用寿命(RUL)、筛选传感器数据、进行特征缩放以及应用指数平滑技术。
# 对于目标函数进行分段,创建 RUL 值
train = add_remaining_useful_life(train)
train['RUL'].clip(upper=threshold, inplace=True) # 将 RUL 值截断,上限为 threshold
# 去除未使用的传感器
drop_sensors = [element for element in sensor_names if element not in sensors]
# 根据操作条件进行缩放
X_train_pre = add_operating_condition(train.drop(drop_sensors, axis=1))
X_test_pre = add_operating_condition(test.drop(drop_sensors, axis=1))
X_train_pre, X_test_pre = condition_scaler(X_train_pre, X_test_pre, sensors)
# 指数平滑
X_train_pre= exponential_smoothing(X_train_pre, sensors, 0, alpha)
X_test_pre = exponential_smoothing(X_test_pre, sensors, 0, alpha) # 对数据进行指数平滑处理
代码解析:
-
计算 RUL 值:
add_remaining_useful_life(train)函数根据目标函数计算训练数据的剩余使用寿命(RUL)。train['RUL'].clip(upper=threshold, inplace=True)将 RUL 值截断,确保其不超过预设阈值threshold。
-
去除未使用传感器:
- 通过列表推导式筛选出
sensor_names中不在sensors列表中的传感器,存储在drop_sensors中。 - 使用
train.drop(drop_sensors, axis=1)和test.drop(drop_sensors, axis=1)从训练集和测试集中移除对应传感器的数据列。
- 通过列表推导式筛选出
-
基于操作条件进行特征缩放:
add_operating_condition()函数根据操作条件对数据进行调整。condition_scaler()函数对训练集X_train_pre和测试集X_test_pre进行特征缩放,sensors参数可能用于指定需要缩放的传感器数据列。
-
指数平滑:
exponential_smoothing()函数对数据进行指数平滑处理,sensors参数可能用于指定需要平滑的传感器数据列,alpha参数控制平滑程度。
这段代码展示了数据预处理的常见流程,通过 RUL 计算、传感器数据筛选、特征缩放和指数平滑等步骤,为后续的机器学习模型训练和预测做好准备。
原文地址: https://www.cveoy.top/t/topic/jPd0 著作权归作者所有。请勿转载和采集!