我想从.csv文件中读取数据行,每当我运行代码时,它就会弹出此错误。我不知道如何解决这个问题。我查询了一些类似的帖子,但仍然无法解决。
这是我的代码:
def getThreshold(dataSet, Attributes, isNumeric):
'''
Calculates median threshold from train dataset
'''
thresholds = []
for x in Attributes:
indx = Attributes.index(x)
numeric = isNumeric[indx]
if numeric:
listAtt = []
for row in dataSet:
listAtt.append(float(row[indx]))
# calculate median a numeric attribute column
median = statistics.median(listAtt)
thresholds.append(median)
return thresholds
这是我的示例数据(不带引号)
41,management,single,secondary,no,764,no,no,cellular,12,jun,230,2,-1,0,unknown,no
39,blue-collar,married,secondary,no,49,yes,no,cellular,14,may,566,1,370,2,failure,no
60,retired,married,primary,no,0,no,no,telephone,30,jul,130,3,-1,0,unknown,no
31,entrepreneur,single,tertiary,no,247,yes,yes,unknown,2,jun,273,1,-1,0,unknown,no
在年龄的第一列中发现问题,被标识为字符串。是csv文件或代码中的问题吗?
答案 0 :(得分:0)
将变量强制转换为float可能会引发ValueError。更改了代码以进行检查。
def getThreshold(dataSet, Attributes, isNumeric):
'''
Calculates median threshold from train dataset
'''
thresholds = []
for x in Attributes:
indx = Attributes.index(x)
numeric = isNumeric[indx]
if numeric:
listAtt = []
for row in dataSet:
value = row[indx]
# Try to convert value to float, if it fails then it keeps the original type
try:
value = float(value)
except ValueError:
pass
listAtt.append(value)
# calculate median a numeric attribute column
median = statistics.median(listAtt)
thresholds.append(median)
return thresholds
作为旁注: 所有变量应以小写字母开头。 只有类定义才能以大写字母开头。