0

I have two array and after plotting the line graph with respect to x-axis and y-axis, I want to extract the linear part of graph in python. How can i do that with precision? For example: x-axis = [1,2,3,4] (Time in seconds) and y-axis = [values]. So, just want to calculate the slope and intercept of the linear part.

I tried using linregress function from scipy but the results weren't accurate enough. Is there any other function I can use.

For example, this graph

Now I want just this part

cssc
  • 1
  • 1
  • you mean a linear regression? check the definition, and if that is what you meant there are plenty of libs that do that.. – Je Je Apr 07 '23 at 10:05
  • 1
    What do you mean 'linear part'? Do you mean derivative? – Simon Goater Apr 07 '23 at 10:09
  • No, not linear regression. i updated the post and explained it in the photo. – cssc Apr 07 '23 at 10:17
  • So you need to say at which x value you want the slope and intercept. How is anyone supposed to know you didn't want the slope and intercept of the first flat part in your example? – Simon Goater Apr 07 '23 at 10:19
  • i want the slope and intercept of longest straight line which is there in the graph and it doesn't depend on values of x-axis. Maybe I am not explaining properly. – cssc Apr 07 '23 at 10:24
  • Does this answer your question? [How to apply piecewise linear fit in Python?](https://stackoverflow.com/questions/29382903/how-to-apply-piecewise-linear-fit-in-python) – foglerit Apr 08 '23 at 22:10
  • No, it doesn't answer my question. – cssc Apr 10 '23 at 04:45

1 Answers1

0

polyfit function to fit a polynomial to your data. You'll need to specify the degree of the polynomial (in this case, 1 for a linear fit), as well as your x and y data:

slope, intercept = np.polyfit(x_axis, y_axis, 1)