我对python完全不熟悉,并且在处理这些问题时遇到了一些麻烦。我一直在下面的代码中得到与第14行及以后相对应的上述错误消息(以p =开头)。任何帮助都感激不尽。谢谢!
file = open('random.txt','r')
lines = file.readlines()
file.close()
import pandas
colnames = ['x', 'y', 'e']
data = pandas.read_sv('random.txt', names=colnames)
x = data.x.tolist()
y = data.x.tolist()
e = data.x.tolist()
p = [sum(1/int(a**2) for a in e)]
q = [sum(b/int(a**2) for b, a in zip(x, e))]
r = [sum(c/int(a**2) for c, a in zip(y, e))]
s = [sum(b**2/int(a**2) for b, a in zip(x, e))]
t = [sum(b*c/int(a**2) for c, b, a in zip(y, x, e))]
delta = p*s - q**2
a = (r*s - q*t)/delta
b = (p*t - q*r)/delta
import math
Ua = math.sqrt(s/delta)
Ub = math.sqrt(p/delta)
print('a', 'b', 'Ua', 'Ub')
答案 0 :(得分:0)
似乎data.x.tolist()
返回字符串列表。在进行数学运算之前,您必须将每个项目转换为float
或int
:
x = [float(a) for a in data.x.tolist()]
y = [float(a) for a in data.x.tolist()]
e = [float(a) for a in data.x.tolist()]
希望它有所帮助。