jthomperoo / predictive-horizontal-pod-autoscaler

Horizontal Pod Autoscaler built with predictive abilities using statistical models
Apache License 2.0
330 stars 28 forks source link

Linear regression model fails on first run #50

Closed jthomperoo closed 3 years ago

jthomperoo commented 3 years ago

Describe the bug When using the linear regression model, once the first replica has been calculated the model will output a stack trace for the first run:

I0405 22:48:02.973332       1 shell.go:84] Shell command failed, stderr: 2021/04/05 22:48:02 exit status 1: Traceback (most recent call last):
  File "/app/algorithms/linear_regression/linear_regression.py", line 131, in <module>
    print(math.ceil(model.predict([[1, 0]])[0]), end="")
  File "/usr/local/lib/python3.8/site-packages/statsmodels/base/model.py", line 1099, in predict
    predict_results = self.model.predict(self.params, exog, *args,
  File "/usr/local/lib/python3.8/site-packages/statsmodels/regression/linear_model.py", line 380, in predict
    return np.dot(exog, params)
  File "<__array_function__ internals>", line 5, in dot
ValueError: shapes (1,2) and (1,) not aligned: 2 (dim 1) != 1 (dim 0)

To Reproduce Steps to reproduce the behavior:

  1. Run the example in examples/simple-linear

Expected behavior A stack trace should not be dumped out here, instead it should skip this first run and rely solely on the calculated target value.

There should be a minimum length check added to https://github.com/jthomperoo/predictive-horizontal-pod-autoscaler/blob/master/internal/prediction/linear/linear.go#L56 that if there is too few data points to do a linear regression with (less than 2) then it should skip the prediction and just use the calculated value.

Kubernetes Details (kubectl version): v1.20.0