【问题标题】:Fit algorithm does not accept my data拟合算法不接受我的数据
【发布时间】:2010-11-13 08:18:34
【问题描述】:

我正在使用here 描述的算法将高斯钟形曲线拟合到我的数据中。

如果我生成我的数据数组:

x=linspace(1.,100.,100)
data= 17*exp(-((x-10)/3)**2)

一切正常。

但如果我使用

从文本文件中读取数据
file = open("d:\\test7.txt")
arr=[]
data=[]


def column(matrix,i):
    return [row[i] for row in matrix]

for line in file.readlines():
    numbers=map(float, line.split())
    arr.append(numbers)
    
data = column(arr,300)
x=linspace(1.,115.,115)

我收到错误消息:

Traceback (most recent call last):
File "readmatrix.py", line 60, in <module>    fit(f, [mu, sigma, height], data)
File "readmatrix.py", line 42, in fit    if x is None: x = arange(y.shape[0])
AttributeError: 'list' object has no attribute 'shape'

据我所知,数据中包含的值是正确的,它看起来像:

[0.108032, 0.86181600000000003, 1.386169, 3.2790530000000002, ... ]

有人知道我做错了什么吗?

谢谢!

【问题讨论】:

  • @Dzz:您甚至没有包括源代码的相关部分。第 42 行是您发生错误的地方,因为您使用的是“y.shape[0]”,而 y 是一个没有 shape 属性的列表类型。您需要提供更多来源,以便我们正确找到错误。
  • 对不起,我希望通过链接到代码来减少帖子中的文字数量。

标签: python arrays file-io


【解决方案1】:

fit 函数期望数据是一个 numpy 数组(它有一个 shape 属性)而不是一个列表(它没有),因此会出现 AttributeError。

转换您的数据:

def column(matrix,i):
    return numpy.asarray([row[i] for row in matrix])

【讨论】:

    【解决方案2】:

    balpha的解不正确;解决方案就是通过 numpy.array 将我的列表转换为 numpy 数组。

    谢谢你给我提示!

    【讨论】:

    • 哪里不对? (我刚刚使用了 numpy 文档,根据哪个 asarray 应该可以工作;我没有安装 numpy)你遇到什么错误?
    • 确实如此,但是命令 arr.append(numbers) 再次从 numpy.array 中创建了一个列表..
    • arr.append(numbers) 被称为 before column(),所以这应该没什么区别。无论如何,只要它有效...... :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2017-08-18
    • 2020-09-18
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多