我已经制作了一个加载数据文件的代码。我已经这样做了如果数据不符合特定要求就会出现错误信息。但是我需要它也只返回有效数据。我似乎无法自己找到办法,所以我希望有人可以帮助我。我的代码如下
import numpy as np
def dataLoad(filename):
initialData = np.loadtxt(filename)
for i in range (len(initialData)):
if initialData[i,0]>60 or initialData[i,0]<10:
print("Temperature must be between 10 and 60. Error in column 1 row {}.".format(initialData.shape[0]))
if initialData[i,1]<0:
print("Bacteria Growth Rate must be higher than, or equal to 0. Error in column 2 row row {}.".format(initialData.shape[0]))
if initialData[i,2] not in [1, 2, 3, 4]:
print("Bacteria Category must be one of the numbers: 1, 2, 3 and 4. Error in column 3 row {}.".format(initialData.shape[0]))
else:
pass
data = initialData.reshape(-1,3)
return data
答案 0 :(得分:0)
该问题的一个解决方案是跟踪无效行的索引
将它们存储在列表中,然后使用np.delete在返回之前从initialData
删除它们。该函数将数组,要删除的索引列表以及应删除数据点的轴作为输入,在这种情况下,删除行时将为0。
您的代码看起来像这样:
import numpy as np
def dataLoad():
del_indices= []
initialData = np.loadtxt(filename)
for i in range (len(initialData)):
if initialData[i,0]>60 or initialData[i,0]<10:
print("Temperature must be between 10 and 60. Error in column 1 row {}.".format(initialData.shape[0]))
del_indices.append(i)
if initialData[i,1]<0:
print("Bacteria Growth Rate must be higher than, or equal to 0. Error in column 2 row row {}.".format(initialData.shape[0]))
del_indices.append(i)
if initialData[i,2] not in [1, 2, 3, 4]:
print("Bacteria Category must be one of the numbers: 1, 2, 3 and 4. Error in column 3 row {}.".format(initialData.shape[0]))
del_indices.append(i)
initialData = np.delete(initialData,del_indices, axis=0)
data = initialData.reshape(-1,3)
return data