There is a data sample. It needs to find the coefficients of the polynomial of the 3rd degree and a piecewise linear function. Polina I found very simple using functions
np.polyfit.
and
np.poly1d.
. It was possible to build a schedule and obtain a specific equation. Now I would like to do the same to obtain the coefficients of several linear functions. I managed to find an example of a code that built a schedule, but to remove the equations for three straight lines. Of course, I can count them manually, but I would like to know if there is a regular method for solving this task?
Import NUMPY AS NP
Import Matplotlib.pyPlot AS PLT
from scipy import optimize
np.random.seed (9999)
x= np.random.normal (0, 1, 1000) * 10
y= np.where (x <
-15, -2 * x + 3, NP.Where (X <
10, X +
48, -4 * x + 98)) + np.random.normal (0, 3, 1000)
Plt.scatter (x, y, s= 5, color= u'b ', marker='. ', Label=' Scatter Plt ')
Def PieceWise_Linear (X, X0, X1, B, K1, K2, K3):
CONDLIST= [X <
x0, (X >
= x0) &
(X <
x1), X >
= x1]
FUNCLIST= [LAMBDA X: K1 * X + B, LAMBDA X: K1 * X + B + K2 *
(x -x0), lambda x: k1 * x + b + k2 * (x -x0) + k3 * (x -x1)]
Return NP.PieceWise (X, Condlist, FunClist)
P, E= Optimize.curve_Fit (PieCewise_Linear, X, Y)
xD= np.linspace (-30, 30, 1000)
Plt.plot (X, Y, "O")
PLT.plot (XD, PieCewise_Linear (XD, * P))
Plt.Show ()
SCIPY.INTERPOLATE.SPLINE tried? A piecewise linear function is a spline of the first degree.
Alex Alex2021-04-08 04:25:00@Alexalex seems to be what it is necessary, but still can not get the equation of the constructed function. And I wanted to know, you can somehow set the number of linear functions that need to be built. For example, for my data, I need to build no more than three linear functions.
leach2021-04-08 07:38:35- python : Summation by Vector NUMPY Indexes
- Capture a video stream of computer windows using Python
- python : NUMPY Installation Error
- python : Use NUMPY for images in Matplotlib
- python : How can I change the binary picture code?
- python : I can not figure out why the InLineKeyboard Telebot watches
- python : As a column from XLSX to turn into data recorded through the comma, in TXT
- Why is the variable from another file does not work in the function of the second file? Python
- python : Why I can't generate dictionaries with such generators
- python : Status Machine on PyQT5
There is still an idea manually split the sample on certain areas and for each site to build a first-degree polynomial, but still I would like to know what if there is a finished function for this
leach2021-04-07 20:58:50