cjekel / piecewise_linear_fit_py

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

Model dumping #13

Closed binkeleraible closed 5 years ago

binkeleraible commented 6 years ago

Hi Charles, this is an awesome lib. But as it seems there is no possibility to dump the learned linear spline model to a file. This would make it quite more applicable to practical use cases....are there any plans to support this feature?

cjekel commented 6 years ago

You can use pickle for model persistence. I just uploaded an example that shows how to do this. https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/model_persistence.py

I haven't tried to use scikit-learn's joblib, but I suspect it will work as well. http://scikit-learn.org/stable/modules/model_persistence.html

Does this help?

binkeleraible commented 6 years ago

Ok that helps Thanks. I hope the file does not get too big. Only persisting the relevant data is more efficient. But we probably are taking about some kb...

Outlook for Androidhttps://aka.ms/ghei36 herunterladen

On Fri, Sep 14, 2018 at 2:12 PM +0200, "Charles Jekel" notifications@github.com<mailto:notifications@github.com> wrote:

You can use pickle for model persistence. I just uploaded an example that shows how to do this. https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/model_persistence.py

I haven't tried to use scikit-learn's joblib, but I suspect it will work as well. http://scikit-learn.org/stable/modules/model_persistence.html

Does this help?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/cjekel/piecewise_linear_fit_py/issues/13#issuecomment-421339693, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AoDUx4ERQjow6dTFfC83uosn9CPwN9vYks5ua501gaJpZM4Woog6.

cjekel commented 6 years ago

This does save the X and Y data along with all model parameters. I have an example with 1.8 million data points, and the file is ~60 mb. Much of the object is very much dependent upon the X and Y data. I haven't tried joblib, but I suspect it will be better...

What do you want to do with your stored model parameters?

In order to predict you need to save the break point locations (self.fit_breaks) and the beta parameters (self.beta). Then a prediction is just

my_pwlf.predict(x_hat, beta, breaks)
cjekel commented 6 years ago

Hi,

I finally had time to take a look at this. It looks like there was a bug that was unnoticed in the predict function for custom model parameters. Thanks for making me aware of this issue!

This should be fixed now in https://github.com/cjekel/piecewise_linear_fit_py/commit/d298db562c691804e30d9cd111539b2bb6e04b20 . I uploaded a new version of pwlf to pypi.

I now have a bare minimum example of model persistence for future prediction at https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/model_persistence_prediction.py

Essentially all you need to save are the break point locations and beta parameters. With these values you can predict for new data. Note that this still requires initialization of the the object for the data set, but it's more efficient than storing the data set in a pickled object. For example:

# fit the data with for 4 line segments
my_pwlf.fit(4)

# save only the parameters necessary to predict for new data
np.save('ex_data/saved_parameters.npy', [my_pwlf.beta, my_pwlf.fit_breaks])

# load the parameters necessary for prediction
my_prev_model = np.load('ex_data/saved_parameters.npy')

# initialize new object
my_pwlf_new = pwlf.PiecewiseLinFit(x, y)

# predict with the saved parameters
y_hat = my_pwlf_new.predict(x, beta=my_prev_model[0], breaks=my_prev_model[1])

Please let me know if this helps you out!

Thanks, CJ

cjekel commented 5 years ago

Closed for inactivity.