views:

196

answers:

2

Hi!

I'm using the algorithm described here to fit Gaussian bell curves to my data.

If I generate my data array with:

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

everything works fine.

But if I read the data from a text file using

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)

I get the error message:

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'

As far as I can see, the values included in data are correct, it looks like:

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

Has someone a clue what I am doing wrong?

Thanks!

+3  A: 

The fit function expects the data as a numpy Array (which has a shape attribute) and not a list (which does not), hence the AttributeError.

Convert your data:

def column(matrix,i):
    return numpy.asarray([row[i] for row in matrix])
balpha
Almost! Many thanks for the hint!
Dzz
+1  A: 

The solution of balpha is not correct; the solution is simply to convert my list to a numpy array via numpy.array.

Thanks for giving me a hint!

Dzz
In what way is it not correct? (I just used the numpy documentation, according to which asarray should work; I don't have numpy installed) What error are you getting?
balpha
It does, but the command arr.append(numbers) makes again a list out of the numpy.array..
Dzz
arr.append(numbers) is called *before* column(), so that shouldn't make a difference. Anyway, as long as it works... :-)
balpha