cjekel / piecewise_linear_fit_py

fit piecewise linear data for a specified number of line segments
MIT License
301 stars 60 forks source link

Cannot run basic example - "only integer scalar arrays can be converted to a scalar index" #5

Closed tnugent97 closed 6 years ago

tnugent97 commented 6 years ago

Hi there,

When running the following code: my_pwlf = pwlf.PiecewiseLinFit(x, y) res = my_pwlf.fit(4) xHat = np.linspace(min(x), max(x), num=10000) yHat = my_pwlf.predict(xHat)

I get the error: File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pwlf/pwlf.py", line 55, in __init__ self.x_data = x[order_arg] TypeError: only integer scalar arrays can be converted to a scalar index

I have managed to work out what was causing this error but I will post this anyway in case others run into the same issue. The x and y array fed into the PiecewiseLinFit function has to be an np.array type.

To fix, I just changed the code to: my_pwlf = pwlf.PiecewiseLinFit(np.array(x), np.array(y))

cjekel commented 6 years ago

I just uploaded 0.2.1 to pypi, which now intelligently checks whether x, y, or breaks are ndarray type. If not, it will convert them. So this shouldn't be an issue for anyone else.

Thanks for posting this!