4
\$\begingroup\$

Here is the code I'm working on. I'm trying to find the optimum alpha value to get the minimum MAE/MAD. With scipy.optimize, it works quite well. However, when scipy.optimize achieves the optimum alpha, it also achieves the forecast values that essentially I'm trying to get to. Is there a way to return or access and store those forecast values named yPredList without disturbing the work scipy.optimize is doing? Because otherwise I'm going to have to do the forecast calculations from scratch, which is unnecessary. Thanks.

import scipy.optimize as spop
ObservedSpeeds = [85, 64, 97 ,82, 12, 30, 47, 20, 45, 90, 60, 42, 50, 67, 28] #This is not the actual dataset, I actually have a data set of 525,600.
def ErrorCalc(alpha,ObservedSpeeds):
    yPredList = [0,ObservedSpeeds[0]] #First prediction is 0 and the second prediction is the first observation
    for iters in range(2,len(ObservedSpeeds)):
        yPredList.append(np.multiply((1-alpha),yPredList[iters-1]) + np.multiply(alpha,ObservedSpeeds[iters-1]))
    NewList = [np.abs(y-o) for y,o in zip(yPredList,ObservedSpeeds)]
    NewError = np.mean(NewList)
    return NewError

result = spop.minimize_scalar(ErrorCalc,bounds=[0,1],args=ObservedSpeeds,method='bounded')
alpha = result.x
\$\endgroup\$
2
  • \$\begingroup\$ Do I get it right if I say that you are asking us how to add a feature in your code? If so this question is off-topic as the code is not working as intended. \$\endgroup\$ Commented Nov 6, 2016 at 10:41
  • \$\begingroup\$ This has now been answered on SO. \$\endgroup\$ Commented Nov 9, 2016 at 8:22

1 Answer 1

0
\$\begingroup\$

Unfortunately I don't know SciPy or anything about the problem you're solving. I can however comment on coding style.

Style is important

Python has a style convention referred to as PEP 8. The full specification can be found on Python's website. One often repeated mantra is "readability counts", because code is often read more than written. Because consistency improves readability, and conventions enforce consistency, I think following the PEP 8 style guide is important. It will make it easier for other people to understand your code.

The following sections describe the changes I think you could make to improve the style and in turn readability of the code.

Use newlines to separate logical units

This will make the code look better organized and it will make it easier to at a glance get a grasp of what parts the program is composed of.

import scipy.optimize as spop
ObservedSpeeds = [85, 64, 97 ,82, 12, 30, 47, 20, 45, 90, 60, 42, 50, 67, 28] #This is not the actual dataset, I actually have a data set of 525,600.
def ErrorCalc(alpha,ObservedSpeeds):
    yPredList = [0,ObservedSpeeds[0]] #First prediction is 0 and the second prediction is the first observation
    for iters in range(2,len(ObservedSpeeds)):
        yPredList.append(np.multiply((1-alpha),yPredList[iters-1]) + np.multiply(alpha,ObservedSpeeds[iters-1]))
    NewList = [np.abs(y-o) for y,o in zip(yPredList,ObservedSpeeds)]
    NewError = np.mean(NewList)
    return NewError

becomes

import scipy.optimize as spop

ObservedSpeeds = [85, 64, 97 ,82, 12, 30, 47, 20, 45, 90, 60, 42, 50, 67, 28] #This is not the actual dataset, I actually have a data set of 525,600.

def ErrorCalc(alpha,ObservedSpeeds):
    yPredList = [0,ObservedSpeeds[0]] #First prediction is 0 and the second prediction is the first observation

    for iters in range(2,len(ObservedSpeeds)):
        yPredList.append(np.multiply((1-alpha),yPredList[iters-1]) + np.multiply(alpha,ObservedSpeeds[iters-1]))

    NewList = [np.abs(y-o) for y,o in zip(yPredList,ObservedSpeeds)]
    NewError = np.mean(NewList)

    return NewError

snake_case vs CamelCase

In Python, snake_case style is used for variable and function names while CamelCase is used for class names. To someone used to this convention (like me), your code is very confusing at first.

This includes changes to ErrorCalc, ObservedSpeeds, NewError etc.

Allow operators some space

I think the most significant possible style change would be to add some spacing around or after some operators. Generally putting a space after , and spaces around binary operators like +, - etc improve readability a lot.

Conclusion

With a few minor changes, your code can become much easier for other (and yourself) to understand. Learning this is something that will benefit you in the future as well.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.