如何在for循环中处理含缺失编号的数据集迭代?
解决样本缺失导致循环终止的问题
嗨,我来帮你搞定这个问题!你遇到的是因为部分样本文件缺失,导致np.load抛出文件不存在的错误,进而中断循环。这里有两种实用的解决方案,你可以根据自己的需求选择:
方案一:循环内检查文件是否存在
最简单直接的方式,就是在加载文件前先检查目标文件是否存在,不存在就跳过当前循环。需要用到os.path.isfile来判断文件状态:
首先导入os模块,然后修改你的循环逻辑:
import os import numpy as np import scipy.interpolate for i in range(1, 611): # 注意这里要改成611,因为range是左闭右开,原来的range(1,610)只会到609 file_path = path_load + 'featureMatrixTrue_of_K1_%d.npy' % i # 检查文件是否存在 if not os.path.isfile(file_path): print(f"样本 {i} 不存在,跳过") continue # 下面是你原来的逻辑 trueData = np.load(file_path) output = [] for c in range(2,6): interp = scipy.interpolate.griddata((trueData[:,0],trueData[:,1]), trueData[:,c], (X.flatten(),Y.flatten())) interp = interp.reshape(num_points, -1) if c==5: interp = np.logical_and(np.where(interp < 0.92,0,1), np.where(interp > 1.06,0,1)) #interp = interp.astype(int) output.append(interp) output = np.array(output)
注意:原来的range(1,610)只会遍历到609,如果你要包含610号样本,得改成range(1,611)哦
方案二:先获取所有存在的样本编号再遍历
如果不确定到底有多少样本缺失,或者想更高效地只处理存在的文件,可以先扫描目录,提取所有存在的样本编号,再遍历这些编号:
import os import numpy as np import scipy.interpolate import glob import re # 匹配所有符合命名格式的npy文件 file_pattern = os.path.join(path_load, 'featureMatrixTrue_of_K1_*.npy') matching_files = glob.glob(file_pattern) # 提取每个文件的样本编号 sample_ids = [] for file in matching_files: # 用正则匹配文件名中的数字 match = re.search(r'featureMatrixTrue_of_K1_(\d+)\.npy', file) if match: sample_id = int(match.group(1)) sample_ids.append(sample_id) # 对编号排序(保持顺序) sample_ids.sort() # 遍历存在的样本编号 for i in sample_ids: file_path = path_load + 'featureMatrixTrue_of_K1_%d.npy' % i trueData = np.load(file_path) output = [] for c in range(2,6): interp = scipy.interpolate.griddata((trueData[:,0],trueData[:,1]), trueData[:,c], (X.flatten(),Y.flatten())) interp = interp.reshape(num_points, -1) if c==5: interp = np.logical_and(np.where(interp < 0.92,0,1), np.where(interp > 1.06,0,1)) #interp = interp.astype(int) output.append(interp) output = np.array(output)
这种方法的好处是,不管目录里新增或删除了哪些样本,代码都会自动处理所有存在的文件,不用手动维护缺失列表。
两种方案都能解决你的问题,方案一适合你已经知道少量样本缺失的情况,代码改动最小;方案二更灵活,适合样本数量不确定的场景。
内容的提问来源于stack exchange,提问作者Ben
相关产品推荐
相关产品推荐

