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定义的函数中作为参数,定义的参数与代码中实际变量名字保持一致,写循环时,需要注意的是,输出的结果需要保存为不同的名字,否则会覆盖上一次的结果。

相关推荐

  1. python定义函数循环批量处理数据

    2024-01-22 13:52:01       37 阅读
  2. Python代码实现数据批量处理(一)

    2024-01-22 13:52:01       16 阅读
  3. Python函数定义函数处理

    2024-01-22 13:52:01       36 阅读
  4. Python利用列表、字典zip函数处理数据

    2024-01-22 13:52:01       9 阅读
  5. 利用Python进行图像XML标注数据批量处理

    2024-01-22 13:52:01       13 阅读
  6. python函数定义调用

    2024-01-22 13:52:01       27 阅读
  7. Python定义调用函数

    2024-01-22 13:52:01       12 阅读
  8. 数据结构 / 队列 / 循环队列 / 结构体定义创建

    2024-01-22 13:52:01       44 阅读
  9. python处理数据函数】---误区2

    2024-01-22 13:52:01       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 13:52:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 13:52:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 13:52:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 13:52:01       18 阅读

热门阅读

  1. RepLKNet 学习笔记

    2024-01-22 13:52:01       37 阅读
  2. C语言中malloc的用法和意义(附带源码)

    2024-01-22 13:52:01       35 阅读
  3. Spark在降本增效中的一些思考

    2024-01-22 13:52:01       29 阅读
  4. brpc负载均衡load balance和服务发现name servicing

    2024-01-22 13:52:01       29 阅读
  5. 计算机通信:HTTP协议

    2024-01-22 13:52:01       34 阅读
  6. 编程笔记 html5&css&js 050 CSS表格2-1

    2024-01-22 13:52:01       26 阅读
  7. Golang爬虫技术

    2024-01-22 13:52:01       34 阅读
  8. 用go语言删除重复文件

    2024-01-22 13:52:01       35 阅读
  9. Vue.js:构建用户界面的渐进式框架

    2024-01-22 13:52:01       26 阅读