cjekel / piecewise_linear_fit_py

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

Incorrect Prediction #19

Closed SebastianHD closed 5 years ago

SebastianHD commented 5 years ago

I've noticed that for small data sets, sometimes the predict function gives a result that appears to have the incorrect number of segments, even though .n_segments, .slopes, .fit_breaks, all seem to indicate the correct number of segments.

Here is an example which seems to give consistently the incorrect results

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

numSegRequest = 2

x = np.array([0.11445023, 0.49628489, 0.67287169, 0.83479312, 0.90068457,
       1.15980533, 1.34981648, 1.52888812, 1.70987057, 1.73051513])
y = np.array([ 0.0732206 ,  0.11161097,  0.01289929, -0.02448141, -0.17952348,
       -0.33457269, -0.48479134, -0.83837053, -1.41280894, -1.45807414])

my_pwlf = pwlf.PiecewiseLinFit(x,y)
res = my_pwlf.fit(numSegRequest)

plt.plot(x,y,'o')
plt.plot(x,my_pwlf.predict(x))
plt.title('number of segments: '+str(my_pwlf.n_segments))
plt.show()

print('breaks: ',my_pwlf.fit_breaks)

Which has the following results

image breaks: [0.11445023 1.29188065 1.73051513]

SebastianHD commented 5 years ago

Nevermind. I understand the issue. It just looks like that because the break is between two data points.

x2 = np.linspace(0,2,100)
plt.plot(x,y,'o')
plt.plot(x2,my_pwlf.predict(x2))
plt.title('number of segments: '+str(my_pwlf.n_segments))
plt.show()

image