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.