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

2024-10-03 17:16:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着用多项式来拟合两个列表。你知道吗

函数是pylab'spolyfit。以下工作:

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工作?你知道吗


Tags: 函数数据结构列表randompylabrandpolyfitmyinput
1条回答
网友
1楼 · 发布于 2024-10-03 17:16:07

我怀疑您需要显式地将文件中的每个项转换为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)。你知道吗

相关问题 更多 >