pwlf.PiecewiseLinFit¶
- class pwlf.PiecewiseLinFit(x, y, disp_res=False, lapack_driver='gelsd', degree=1, weights=None, seed=None)¶
Methods
assemble_regression_matrix
(breaks, x)Assemble the linear regression matrix A
Calculate the slopes of the lines after a piecewise linear function has been fitted.
conlstsq
(A[, calc_slopes])Perform a constrained least squares fit for A matrix.
fit
(n_segments[, x_c, y_c, bounds])Fit a continuous piecewise linear function for a specified number of line segments.
fit_force_points_opt
(var)The objective function to perform a continuous piecewise linear fit for a specified number of breakpoints.
fit_guess
(guess_breakpoints[, bounds])Uses L-BFGS-B optimization to find the location of breakpoints from a guess of where breakpoint locations should be.
fit_with_breaks
(breaks)A function which fits a continuous piecewise linear function for specified breakpoint locations.
fit_with_breaks_force_points
(breaks, x_c, y_c)A function which fits a continuous piecewise linear function for specified breakpoint locations, where you force the fit to go through the data points at x_c and y_c.
fit_with_breaks_opt
(var)The objective function to perform a continuous piecewise linear fit for a specified number of breakpoints.
fitfast
(n_segments[, pop, bounds])Uses multi start LBFGSB optimization to find the location of breakpoints for a given number of line segments by minimizing the sum of the square of the errors.
lstsq
(A[, calc_slopes])Perform the least squares fit for A matrix.
p_values
([method, step_size])Calculate the p-values for each beta parameter.
predict
(x[, beta, breaks])Evaluate the fitted continuous piecewise linear function at untested points.
Calculate the prediction variance for each specified x location.
Calculate the coefficient of determination ("R squared", R^2) value after a fit has been performed.
standard_errors
([method, step_size])Calculate the standard errors for each beta parameter determined from the piecewise linear fit.
use_custom_opt
(n_segments[, x_c, y_c])Provide the number of line segments you want to use with your custom optimization routine.
- __init__(x, y, disp_res=False, lapack_driver='gelsd', degree=1, weights=None, seed=None)¶
An object to fit a continuous piecewise linear function to data.
Initiate the library with the supplied x and y data. Supply the x and y data of which you’ll be fitting a continuous piecewise linear model to where y(x). by default pwlf won’t print the optimization results.;
- Parameters:
- xarray_like
The x or independent data point locations as list or 1 dimensional numpy array.
- yarray_like
The y or dependent data point locations as list or 1 dimensional numpy array.
- disp_resbool, optional
Whether the optimization results should be printed. Default is False.
- lapack_driverstr, optional
Which LAPACK driver is used to solve the least-squares problem. Default lapack_driver=’gelsd’. Options are ‘gelsd’, ‘gelsy’, ‘gelss’. For more see https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.lstsq.html http://www.netlib.org/lapack/lug/node27.html
- degreeint, optional
The degree of polynomial to use. The default is degree=1 for linear models. Use degree=0 for constant models.
- weightsNone, or array_like
The individual weights are typically the reciprocal of the standard deviation for each data point, where weights[i] corresponds to one over the standard deviation of the ith data point. Default weights=None.
- seedNone, or int
Pick an integer which will set the numpy.random.seed on init. The fit and fitfast methods rely on stochastic methods and setting this value will make the results reproducible. The default behavior is to not specify a seed.
- Attributes:
- betandarray (1-D)
The model parameters for the continuous piecewise linear fit.
- break_0float
The smallest x value.
- break_nfloat
The largest x value.
- c_nint
The number of constraint points. This is the same as len(x_c).
- degree: int
The degree of polynomial to use. The default is degree=1 for linear models. Use degree=0 for constant models.
- fit_breaksndarray (1-D)
breakpoint locations stored as a 1-D numpy array.
- interceptsndarray (1-D)
The y-intercept of each line segment as a 1-D numpy array.
- lapack_driverstr
Which LAPACK driver is used to solve the least-squares problem.
- printbool
Whether the optimization results should be printed. Default is False.
- n_dataint
The number of data points.
- n_parametersint
The number of model parameters. This is equivalent to the len(beta).
- n_segmentsint
The number of line segments.
- nVarint
The number of variables in the global optimization problem.
- sendarray (1-D)
Standard errors associated with each beta parameter. Specifically se[0] correspounds to the standard error for beta[0], and so forth.
- seedint
Numpy random seed number set on init.
- slopesndarray (1-D)
The slope of each ling segment as a 1-D numpy array. This assumes that x[0] <= x[1] <= … <= x[n]. Thus, slopes[0] is the slope of the first line segment.
- ssrfloat
Optimal sum of square error.
- x_cndarray (1-D)
The x locations of the data points that the piecewise linear function will be forced to go through.
- y_cndarray (1-D)
The x locations of the data points that the piecewise linear function will be forced to go through.
- x_datandarray (1-D)
The inputted parameter x from the 1-D data set.
- y_datandarray (1-D)
The inputted parameter y from the 1-D data set.
- y_wndarray (1-D)
The weighted y data vector.
- zetandarray (1-D)
The model parameters associated with the constraint function.
Methods
fit(n_segments, x_c=None, y_c=None, **kwargs)
Fit a continuous piecewise linear function for a specified number of line segments.
fitfast(n_segments, pop=2, **kwargs)
Fit a continuous piecewise linear function for a specified number of line segments using a specialized optimization routine that should be faster than fit() for large problems. The tradeoff may be that fitfast() results in a lower quality model.
fit_with_breaks(breaks)
Fit a continuous piecewise linear function where the breakpoint locations are known.
fit_with_breaks_force_points(breaks, x_c, y_c)
Fit a continuous piecewise linear function where the breakpoint locations are known, and force the fit to go through points at x_c and y_c.
predict(x, beta=None, breaks=None)
Evaluate the continuous piecewise linear function at new untested points.
fit_with_breaks_opt(var)
The objective function to perform a continuous piecewise linear fit for a specified number of breakpoints. This is to be used with a custom optimization routine, and after use_custom_opt has been called.
fit_force_points_opt(var)’
Same as fit_with_breaks_opt(var), except this allows for points to be forced through x_c and y_c.
use_custom_opt(n_segments, x_c=None, y_c=None)
Function to initialize the attributes necessary to use a custom optimization routine. Must be used prior to calling fit_with_breaks_opt() or fit_force_points_opt().
calc_slopes()
Calculate the slopes of the lines after a piecewise linear function has been fitted.
standard_errors()
Calculate the standard error of each model parameter in the fitted piecewise linear function. Note, this assumes no uncertainty in breakpoint locations.
prediction_variance(x)
Calculate the prediction variance at x locations for the fitted piecewise linear function. Note, assumes no uncertainty in break point locations.
r_squared()
Calculate the coefficient of determination, or ‘R-squared’ value for a fitted piecewise linear function.
Examples
Initialize for x, y data
>>> import pwlf >>> my_pwlf = pwlf.PiecewiseLinFit(x, y)
Initialize for x,y data and print optimization results
>>> my_pwlf = pwlf.PiecewiseLinFit(x, y, disp_res=True)
Methods
__init__
(x, y[, disp_res, lapack_driver, ...])An object to fit a continuous piecewise linear function to data.
assemble_regression_matrix
(breaks, x)Assemble the linear regression matrix A
Calculate the slopes of the lines after a piecewise linear function has been fitted.
conlstsq
(A[, calc_slopes])Perform a constrained least squares fit for A matrix.
fit
(n_segments[, x_c, y_c, bounds])Fit a continuous piecewise linear function for a specified number of line segments.
fit_force_points_opt
(var)The objective function to perform a continuous piecewise linear fit for a specified number of breakpoints.
fit_guess
(guess_breakpoints[, bounds])Uses L-BFGS-B optimization to find the location of breakpoints from a guess of where breakpoint locations should be.
fit_with_breaks
(breaks)A function which fits a continuous piecewise linear function for specified breakpoint locations.
fit_with_breaks_force_points
(breaks, x_c, y_c)A function which fits a continuous piecewise linear function for specified breakpoint locations, where you force the fit to go through the data points at x_c and y_c.
fit_with_breaks_opt
(var)The objective function to perform a continuous piecewise linear fit for a specified number of breakpoints.
fitfast
(n_segments[, pop, bounds])Uses multi start LBFGSB optimization to find the location of breakpoints for a given number of line segments by minimizing the sum of the square of the errors.
lstsq
(A[, calc_slopes])Perform the least squares fit for A matrix.
p_values
([method, step_size])Calculate the p-values for each beta parameter.
predict
(x[, beta, breaks])Evaluate the fitted continuous piecewise linear function at untested points.
Calculate the prediction variance for each specified x location.
Calculate the coefficient of determination ("R squared", R^2) value after a fit has been performed.
standard_errors
([method, step_size])Calculate the standard errors for each beta parameter determined from the piecewise linear fit.
use_custom_opt
(n_segments[, x_c, y_c])Provide the number of line segments you want to use with your custom optimization routine.