将列表转换为polyfit的正确输入

时间:2014-08-16 05:12:13

标签: python

我试图将多项式拟合到两个列表中。

该功能为pylab's polyfit。以下作品:

z4 = polyfit(randInput, y, 4)

以下打破了它:

z4 = polyfit(myInput, y, 4)

如果

randInput = random.rand(6)

然后

randInput = [ 0.02634194  0.70933754  0.99000924  0.53837119  0.61318163  0.89089385]

但是,试着自己做:

myInput = ['1', '22', '23', '24', '25', '26']

randInput是什么类型的数据结构?如何将myInput转换为相同类型的数据结构,以便polyfit有效?

1 个答案:

答案 0 :(得分:2)

我怀疑您需要明确地将文件中的每个项目转换为int

x = []
with open('test3.txt') as file:
    for line in file:
        xs,ys = line.split(',',1)
        x.append(int(xs.rstrip()))
print x

random.rand正在生成float个对象的列表,但您正在从文本文件中读取整数。由于您没有包含您获得的实际错误消息,因此很难确定,但该方法可能会接受任何数字类型(包括intfloat)。

相关问题