要解决AutoML Tables批量预测中的丢失记录问题,可以按照以下步骤进行:
from google.cloud import automl_v1beta1 as automl
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('path/to/service_account.json')
client = automl.TablesClient(project='your-project-id', region='us-central1', credentials=credentials)
input_file = 'path/to/input_file.csv'
data = pd.read_csv(input_file)
output_file = 'path/to/output_file.csv'
with open(output_file, 'w') as f:
for index, row in data.iterrows():
# 检查记录是否有缺失值
if pd.isnull(row).any():
# 对于有缺失值的记录,将其保存到另一个文件中
f.write(','.join([str(value) for value in row.values]) + '\n')
else:
# 对于没有缺失值的记录,进行预测并保存结果
input_data = {'column1': row['column1'], 'column2': row['column2'], ...} # 根据实际列名进行调整
response = client.predict('your-model-id', input_data)
prediction = response.payload[0].tables.value
f.write(','.join([str(value) for value in row.values] + [str(prediction)]) + '\n')
在上述代码中,需要根据实际情况进行以下调整:
'path/to/service_account.json'
替换为包含您的服务帐号密钥的JSON文件的路径。'your-project-id'
替换为您的项目ID。'path/to/input_file.csv'
替换为包含要进行批量预测的数据的文件路径。'path/to/output_file.csv'
替换为保存预测结果的文件路径。'your-model-id'
替换为您要使用的AutoML Tables模型的ID。input_data
字典中调整列名。运行上述代码后,将会将具有缺失值的记录保存到一个文件中,并将预测结果保存到另一个文件中。您可以根据需要对所保存的文件进行进一步处理。