cjekel / piecewise_linear_fit_py

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

Error when using two line fitting #2

Closed vkhodygo closed 6 years ago

vkhodygo commented 6 years ago

Hi, Thanks for your work, this package is exactly what I've been looking for!

However, I have some issues. My data has a few different regions, however, I'm not sure how many: two or three. So, I tried both, and with three different fits your package works perfect, whereas when I try to fit my data with two straight lines, I get this error:


>>> res = myPWLF.fit(2)

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

File "/home/user/.local/lib/python3.6/site-packages/pwlf/pwlf.py", line 308, in fit
    polish=True, init='latinhypercube', atol=1e-4)

  File "/home/user/.local/lib/python3.6/site-packages/scipy/optimize/_differentialevolution.py", line 213, in differential_evolution

    return solver.solve()

  File "/home/user/.local/lib/python3.6/site-packages/scipy/optimize/_differentialevolution.py", line 511, in solve

    self._calculate_population_energies()

  File "/home/user/.local/lib/python3.6/site-packages/scipy/optimize/_differentialevolution.py", line 590, in _calculate_population_energies
    *self.args)

  File "/home/user/.local/lib/python3.6/site-packages/pwlf/pwlf.py", line 226, in fitWithBreaksOpt
    if np.isclose(var[0],var[1]) == True:
IndexError: index 1 is out of bounds for axis 0 with size 1```

Could you please clarify this moment?
Sincerely, V.
vkhodygo commented 6 years ago

Upd. Here is the data and code:

x = np.array([-0.8194154 , -0.64332414, -0.51838541, -0.46722015, -0.3422846 ,
       -0.21734825, -0.16619652, -0.12043967, -0.0412546 ,  0.05564586,
        0.08368175,  0.13484621,  0.1805846 ,  0.25978495,  0.35667586,
        0.38471174,  0.43586029,  0.48161459,  0.53277985,  0.560803  ,
        0.6577154 ,  0.68574174,  0.73689028,  0.78265175,  0.83380348,
        0.861833  ,  0.9587454 ,  0.98677173,  1.03790436,  1.08368175,
        1.13484621,  1.16286299,  1.25978495,  1.33895425,  1.38471174,
        1.43586029,  1.46389299,  1.560803  ,  1.63998425,  1.68574174,
        1.73689028,  1.76492298,  1.861833  ,  1.94101424,  1.98677173,
        2.03790436,  2.16286299,  2.33895425])

y = np.array([-0.8907928 , -0.75500583, -0.64727736, -0.64650693, -0.51433535,
       -0.38166944, -0.40255713, -0.16220961, -0.23907264, -0.08629738,
       -0.08070743, -0.10171921,  0.12456943,  0.08507641,  0.34500104,
        0.11968469,  0.25837385,  0.46664523,  0.62630957,  0.44249545,
        0.72326681,  0.45026451,  0.70079022,  0.86618688,  1.00060759,
        0.91496691,  1.13741741,  0.92408795,  1.20806495,  1.3476422 ,
        1.43581253,  1.31152076,  1.60827993,  1.61206297,  1.72485727,
        1.88401327,  1.79035555,  1.99959592,  2.07903646,  2.1498347 ,
        2.27258415,  2.29909307,  2.4168235 ,  2.53288172,  2.61879047,
        2.67065397,  2.81012494,  2.92261129])

import pwlf
import matplotlib.pyplot as plt
import numpy as np

myPWLF = pwlf.piecewise_lin_fit(x,y)
res = myPWLF.fit(2)

xHat = np.linspace(min(x), max(x), num=10000)
yHat = myPWLF.predict(xHat)

plt.figure()
plt.plot(x,y,'o')
plt.plot(xHat,yHat, '-')
plt.show()
cjekel commented 6 years ago

V.,

Thanks for giving me your code! I was able to reproduce the issue.

I have no idea why I was preventing the first two variables to be the same.

Can you install the new version- and test to see if this fixes your problem?

pip install --upgrade git+https://github.com/cjekel/piecewise_linear_fit_py

Thanks, CJ


per commit https://github.com/cjekel/piecewise_linear_fit_py/commit/a14c5ae3460dc4ea35e3988010cc43469232d2a3

if this fixes your issue, I'll upload to pip

vkhodygo commented 6 years ago

I've updated the code and it works perfectly. Thank you! V.