jscott
jscott

Reputation: 407

Using scipy.optimize.curve_fit

I am new to scipy and I am unable to use the curve_fit function. I think there is some scipy/numpy data wrapper that needs to be used for independent and dependent data sets. The windowCurrent and windowVoltage are queues that hold a sliding set of points from my data set.

How can I wrap the list of current/voltage pairs to avoid this error?

TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

Code:

for line in inputFileContents[:maxlen]:
    print line
    timeStamp,voltage,current = line.split(",")
    if windowCurrent == None and windowVoltage == None:
        windowCurrent = deque(current, maxlen)
        windowVoltage = deque(voltage, maxlen)
    else:
        windowCurrent.append(current)
        windowVoltage.append(voltage)

for lineConents in inputFileContents:
    timeStamp,voltage,current = line.split(",")
    windowCurrent.append(current)
    windowVoltage.append(voltage)
    curveList.append([timeStamp, op.curve_fit(logCurve, np.array(list(windowCurrent)), np.array(list(windowVoltage)))])
    curveListPopulate(curveList)

Also: doing list(windowCurrent), leaving off the np.array wrapping, also returns an error.

Link to full text of error

Upvotes: 2

Views: 942

Answers (2)

MihaPirnat
MihaPirnat

Reputation: 163

I agree with silvado, the problem is in passing string numpy arrays to curve_fit.

This might help in resolving problem:

How to convert an array of strings to an array of floats in numpy?

Upvotes: 0

silvado
silvado

Reputation: 18167

Try converting current and voltage to float before appending them to windowCurrent and windowVoltage.

Explanation:

Numpy's arrays can hold a variety of data types, not only numbers. In your case, it seems to be a string datatype: line.split returns strings, and thus you get a numpy array of strings. Obviously, you can't subtract strings from each other.

Upvotes: 2

Related Questions