0

I'm getting the TypeError: 'numpy.float64' object is not callable error for the following code:

import numpy as np
from scipy.optimize import minimize

def ses(data, alpha):
    fit=[]
    fit.append(alpha*data[1] + (1-alpha)*data[0])
    for i in range(2, len(data)):
        fit.append(data[i]*alpha + fit[i-2]*(1-alpha))
    return fit

def rmse(data, fit):
    se=[]
    for i in range(2,len(data)):
        se.append((data[i]-fit[i-2])*(data[i]-fit[i-2]))
    mse=np.mean(se)
    return np.sqrt(mse)


alpha=0.1555 # starting value
fit=ses(d[0], alpha)
error=rmse(d[0], fit)

result=minimize(error, alpha, (fit,), bounds=[(0,1)], method='SLSQP') 

I've tried many alternatives and its just not working. Changed the lists to arrays and made the multiplications involve no exponentials (np.sqrt() as opposed to ()**0.5)

EDIT:

def ses(data, alpha):
    fit=[]
    fit.append(alpha*data[1] + (1-alpha)*data[0])
    for i in range(2, len(data)):
        fit.append(data[i]*alpha + fit[i-2]*(1-alpha))
    return fit

def rmse(data, alpha):
    fit=ses(data, alpha)
    se=[]
    for i in range(2,len(data)):
        print i, i-2
        se.append((data[i]-fit[i-2])*(data[i]-fit[i-2]))
    mse=np.mean(se)
    return np.sqrt(mse)


alpha=0.1555 # starting value
data=d[0]


result = minimize(rmse, alpha, (data,), bounds=[(0,1)], method='SLSQP')

Ok guys, thanks. Have edited to this and I have stopped the error, however now I am getting an index out of bounds error, which is strange as without the minimize line, the code runs perfectly fine.

EDIT 2:

There was a series of silly errors, most of which I didn't know were problems, but were solved by trial and error.

For some working code of optimized exponential smoothing:

def ses(data, alpha):
    'Simple exponential smoothing'

    fit=[]
    fit.append(data[0])
    fit.append(data[1]) ## pads first two
    fit.append(alpha*data[1] + (1-alpha)*data[0])

    for i in range(2, len(data)-1):
        fit.append(alpha*data[i] + (1-alpha)*fit[i])
    return fit


def rmse(alpha, data):
    fit=ses(data, alpha)
    se=[]
    for i in range(2,len(data)):
        se.append((data[i]-fit[i-2])*(data[i]-fit[i-2]))
    mse=np.mean(se)
    return np.sqrt(mse)

alpha=0.5
data = d[0]

result = minimize(rmse, alpha, (data,), bounds=[(0,1)], method='SLSQP')
3
  • 1
    Post the complete Traceback. Makes it much easier. Commented Aug 4, 2014 at 9:13
  • Where is minimize() defined? Post this function. Commented Aug 4, 2014 at 9:15
  • I think you have the alpha and data arguments back-to-front in your function definitions. Commented Aug 4, 2014 at 13:46

1 Answer 1

1

Its hard to tell exactly what the problem is here. I assume that minimize is actually Scipy's minimize.

If so the first argument should be a function. Instead, you are passing the output of the rmse function, which is a double precision number.

error=rmse(d[0], fit) # <--- returns a number

You should have:

result=minimize(<some function here>, alpha, (fit,), bounds=[(0,1)], method='SLSQP')

When minimize is called, it attempts to call error, thus throwing a TypeError: 'numpy.float64' object is not callable

There is a straightforward tutorial here that walks through exactly how to use minimize with the sequential least squares programming optimization algorithm.

I would hazard a guess that you actually want to be passing rmse as the first argument:

result=minimize(rmse, alpha, (fit,), bounds=[(0,1)], method='SLSQP')

After all, the rmse function is giving you the error value and that is what you are minimising in such an optimisation.

Sign up to request clarification or add additional context in comments.

1 Comment

Please read the documentation for minimize, your current code is clearly passing a number as the first argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.