python定义函数和写循环批量处理数据集

对于初学者想要使用python批量处理数据时,如果方法相同,但是数据集不一样,或者其中一些参数不一样,可以使用循环的方式高效处理。例如,我需要使用以下代码进行数据集预测,但同时需要使用不同的模型和数据集进行预测时,写循环分析过程

config_path = 'configs/seqlevel_all.yml'
config = load_config(config_path)
class Config:
    def __init__(self, path, file_list):
        self.path = path
        self.file_list = file_list
abs_path = testfile_path
test_name=test_name
val_set = [SeqLevelDataset(Config(path=abs_path, file_list=[test_name]))] 
ckpt_path = model_path
model.load_state_dict(torch.load(ckpt_path))
print('Predicting val...')
results = model.predict(model.val_dataloader())
print('Saving results...')
if len(results) == 4:
    pd.DataFrame(zip(*results), columns=['cdr3', 'epi', 'y_true', 'y_pred']).to_csv(result_path+'probability.csv', index=False)
else:
    pd.DataFrame(zip(*results), columns=['Epitope', 'Class', 'y_true', 'y_prob', 'epi_id']).to_csv(result_path+name+'probability.csv', index=False)
print('Done')

1.当只运行一个模型和一个数据集,保存结果为一个路径是,我们需要考虑输入三个位置参数(如下),在运行时,首先给定

模型路径:testfile_path=测试数据集的路径

测试数据集路径:model_path=使用模型的路径

结果保存路径:result_path=结果保存的路径

2.当我们处理两个或两个以上数据集时,我们发现每次运行时需要都运行一遍上述代码,非常麻烦,这时可以将上述代码封装为函数,可以直接调用封装好的函数,首先整个代码,含有三个变量,模型路径,测试数据集路径,结果保存路径,其他是固定不变的。使用  def 定义一个名字为Validation_main的函数,需要填入三个参数 testfile_path,model_path,result_path,这三个参数的书写需要与代码中使用的保持一致,修改如下

def Validation_main(testfile_path,model_path,result_path):
    config_path = 'configs/seqlevel_all.yml'
    config = load_config(config_path)
    class Config:
        def __init__(self, path, file_list):
            self.path = path
            self.file_list = file_list
    abs_path = testfile_path
    val_set = [SeqLevelDataset(Config(path=abs_path, file_list=[test_name]))] 
    ckpt_path = model_path
    model.load_state_dict(torch.load(ckpt_path))
    print('Predicting val...')
    results = model.predict(model.val_dataloader())
    print('Saving results...')
    if len(results) == 4:
        pd.DataFrame(zip(*results), columns=['cdr3', 'epi', 'y_true', 'y_pred']).to_csv(result_path+'probability.csv', index=False)
    else:
        pd.DataFrame(zip(*results), columns=['Epitope', 'Class', 'y_true', 'y_prob', 'epi_id']).to_csv(result_path+name+'probability.csv', index=False)
    print('Done')


运行上一段代码后,下一次使用可以写为即调用了上一段代码
testfile_path=testfile_path
model_path=model_path
result_path=result_path
Validation_main(testfile_path,model_path,result_path)

3.当运行多个模型,在写循环时,除了上述需要考虑的输出外,我们还需要考虑文件输出不同的名字,我们可以通过循环的方式进行数据批量处理,运行下述代码,即可同时输出两个模型,两个数据集相互交叉预测的结果

#此时有模型model1,model2保存在路径models_path中
#有数据集data1,data2保存在路径data_path中
#将所有得到的结果输出到result_path文件夹中
#为了区分不同模型和不同数据集得到的结果,我们将结果命名为[model1_data1,model2_data2,model1_data2,model2_data1]

model=[model1,model2]
data=[data1,data2]
name=[model1_data1,model1_data2,model2_data1,model2_data2]
for i in model:
    for j in data:
        for k in name
            model_path=models_path+ i
            testfile_path=models_path+ j
            result_path=result_path+ k
            Validation_main(testfile_path,model_path,result_path)


以上就是简单示例,定义函数时,需要注意的点为变量需要写入def定义的函数中作为参数,定义的参数与代码中实际变量名字保持一致,写循环时,需要注意的是,输出的结果需要保存为不同的名字,否则会覆盖上一次的结果。

你可能感兴趣的:(python,机器学习,开发语言)