Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

piecewise linear regression python

from scipy import optimize

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))
Comment

PREVIOUS NEXT
Code Example
Python :: try except 
Python :: Creating Python Sets 
Python :: how to use drf permission class with class method actions 
Python :: open and write in a file in python 
Python :: python remove one character from a string 
Python :: dictionary python values 
Python :: python enumerate unique values 
Python :: a sigmoid function 
Python :: python turtle module 
Python :: exponent in python 
Python :: swap 2 no in one line python 
Python :: pie chart maptlotlib larger labels 
Python :: calculate mean of column pandas 
Python :: pandas selection row/columns 
Python :: word2vec python 
Python :: extract a jar py 
Python :: group by list python 
Python :: functions in python 
Python :: adfuller test in python 
Python :: pairwise function python 
Python :: read a function of excel in python 
Python :: isodate in python 
Python :: tree python 
Python :: convert float to string python 
Python :: how to write a dataframe to s3 object in python 
Python :: how to find and remove certain characters from text string in python 
Python :: python multiply string 
Python :: pandas series remove element by index 
Python :: How to take multiple input form python 
Python :: pandas df.to_csv() accent in columns name 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =