cjekel / piecewise_linear_fit_py

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

forcing fits through points with degrees > 1 #54

Closed TobI-SE closed 4 years ago

TobI-SE commented 4 years ago

Hi, I am trying to force a fit through a point using the pwlf library. If I force the fit through a certain data point with a degree=1 everything works perfectly, but as soon as I set the degree=2 the following error comes up:

ValueError: could not broadcast input array from shape (5,1) into shape (11,1)

This is the code I used:

import numpy as np
import pwlf

#generate sin wave data
x = np.linspace(0, 10, num=100)
y = np.sin(x * np.pi / 2)
# add noise to the data
y = np.random.normal(0, 0.15, 100) + y

#linear fit
my_pwlf_1 = pwlf.PiecewiseLinFit(x, y, degree=1)
my_pwlf_1.fit(n_segments=6, x_c = [0], y_c = [0])
y_predict_1 = my_pwlf_1.predict(x)

# quadratic fit
my_pwlf_2 = pwlf.PiecewiseLinFit(x, y, degree=2)
my_pwlf_2.fit(n_segments=5, x_c = [0], y_c = [0])
y_predict_2 = my_pwlf_2.predict(x)
cjekel commented 4 years ago

Thanks Tobi. I guess I missed this test case when I've implemented this, and I think is a higher priority on my todo list since this is intended to work.

However, It's probably a week until I can look at this in detail.

Any help until then as to why this isn't working as intended is appreciated.

cjekel commented 4 years ago

Hi @TobI-SE

Thank you for the issue, and easy to reproduce code. I believe I've fixed the issue, and I have added your code as a test case. Do you mind checking this PR to see if it indeeds solves your issue? https://github.com/cjekel/piecewise_linear_fit_py/pull/56

Thanks, CJ

cjekel commented 4 years ago

I think this fixed the issue, but I'll leave this open for now. At the moment I'm going to release a new pwlf version which includes these changes.

TobI-SE commented 4 years ago

Hi cjekel, sorry for the late response. It all works perfectly, thank you.

cjekel commented 4 years ago

Thanks for making me aware of the issue and giving me a basic example to work with!