未定义读取器[Python]
我正在计算不同类型属性之间的距离 在下面的代码中,当我对一些5-6元组执行此操作时,效果很好,但当我通过读取.csv文件执行此操作时,会出现创建错误 请告诉我怎么了未定义读取器[Python],python,Python,我正在计算不同类型属性之间的距离 在下面的代码中,当我对一些5-6元组执行此操作时,效果很好,但当我通过读取.csv文件执行此操作时,会出现创建错误 请告诉我怎么了 Error: Traceback (most recent call last): File "bank.py", line 91, in <module> a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q = [DataItem(z) for z in reader]
Error:
Traceback (most recent call last):
File "bank.py", line 91, in <module>
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q = [DataItem(z) for z in reader]
NameError: name 'reader' is not defined
因为
reader
是一个局部范围的变量?您需要修复缩进。检查缩进,所有这些print
都应该在main
?@PRMoureu中,在这种情况下,它给出:::缩进错误:意外indent@Daniel你能更具体一点吗?因为reader
是一个局部范围的变量?你需要修正缩进。检查缩进,所有这些print
都应该在main
?@PRMoureu中,在这种情况下,它给出:::缩进错误:意外indent@Daniel你能说得更具体些吗?
Traceback (most recent call last):
File "bank.py", line 116, in <module>
main()
File "bank.py", line 93, in main
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q = [DataItem(z) for z in reader]
File "bank.py", line 56, in __init__
self.values = [type_(value) for type_,value in zip(self.types, values)]
ValueError: invalid literal for int() with base 10: 'age;"job";"marital";"education";"default";"balance";"housing";"loan";"contact";"day";"month";"duration";"campaign";"pdays";"previous";"poutcome";"y"'
import CSV
class NominalType:
name_values = {}
def __init__(self, name):
self.name = name
self.value = self.name_values[name]
def __str__(self):
return self.name
def __sub__(self, other):
assert type(self) == type(other), "Incompatible types, subtraction is undefined"
return self.value - other.value
def make_nominal_type(name_values):
try:
nv = dict(name_values)
except ValueError:
nv = {item:i for i,item in enumerate(name_values)}
class MyNominalType(NominalType):
name_values = nv
return MyNominalType
job = make_nominal_type(["unemployed", "services", "management", "blue-collar"])
contact = make_nominal_type(["cellular", "unknown"])
month = make_nominal_type(["oct", "may", "apr", "jun"])
outcome = make_nominal_type(["unknown", "faliure"])
y = make_nominal_type(["no", "yes"])
class MixedVectorType:
types = []
distance_fn = None
def __init__(self, values):
self.values = [type_(value) for type_,value in zip(self.types, values)]
def dist(self, other):
return self.distance_fn([abs(s - o) for s,o in zip(self.values, other.values)])
def make_mixed_vector_type(types, distance_fn):
tl = list(types)
df = distance_fn
class MyVectorType(MixedVectorType):
types = tl
distance_fn = df
return MyVectorType
def euclidean_dist(_, vector):
return sum(v*v for v in vector) ** 0.5
DataItem = make_mixed_vector_type(
[int, job, marital, education, default, int, housing, loan, contact, int, month, int, int, int, int, outcome, y],
euclidean_dist
)
def main():
with open('bank.csv', 'rb') as csvfile:
reader=csv.reader(csvfile)
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q = [DataItem(z) for z in reader]
print("a to b, dist = {}".format(a.dist(b)))
print("b to c, dist = {}".format(b.dist(c)))
print("c to d, dist = {}".format(c.dist(d)))
print("d to e, dist = {}".format(d.dist(e)))
print("e to f, dist = {}".format(e.dist(f)))
print("f to g, dist = {}".format(f.dist(g)))
print("g to h, dist = {}".format(g.dist(h)))
print("h to i, dist = {}".format(h.dist(i)))
print("i to j, dist = {}".format(i.dist(j)))
print("j to k, dist = {}".format(j.dist(k)))
print("k to l, dist = {}".format(k.dist(l)))
print("l to m, dist = {}".format(l.dist(m)))
print("m to n, dist = {}".format(m.dist(n)))
print("n to o, dist = {}".format(n.dist(o)))
print("o to p, dist = {}".format(o.dist(p)))
print("p to q, dist = {}".format(p.dist(q)))
if __name__=="__main__":
main()