实时机器学习的时间窗口特征处理与模型训练
1. 数据读取与初步处理
当输入为 local
时,我们可以使用以下代码读取本地的 JSON 文件:
if input == 'local':
input_file = './alldata_sample.json'
flights_output = '/tmp/'
events = (
pipeline
| 'read_input' >> beam.io.ReadFromText(input_file)
| 'parse_input' >> beam.Map(lambda line:
json.loads(line))
)
此时, events
是一个字典的 PCollection
,与从 BigQuery 读取的数据格式相同。运行这段代码,确保输入文件中的事件被正确写入 CSV 文件。完成读写操作后,我们可以开始进行数据转换。
2. 数据过滤
大多数数据管道会处理数据流,选择感兴趣的数据并进行某种转换。选择要继续处理的数据的过程称为过滤。在创建训练数据集时,我们需要过滤事件集,只包含正常运行的航班:
def is_normal_opera