Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Slopes in Numpy (or Scipy)

I am trying to find the fastest and most efficient way to calculate slopes using Numpy and Scipy. I have a data set of three Y variables and one X variable and I need to calculate their individual slopes. For example, I can easily do this one row at a time, as shown below, but I was hoping there was a more efficient way of doing this. I also don't think linregress is the best way to go because I don't need any of the auxiliary variables like intercept, standard error, etc in my results. Any help is greatly appreciated.

    import numpy as np     from scipy import stats      Y = [[  2.62710000e+11   3.14454000e+11   3.63609000e+11   4.03196000e+11         4.21725000e+11   2.86698000e+11   3.32909000e+11   4.01480000e+11         4.21215000e+11   4.81202000e+11]         [  3.11612352e+03   3.65968334e+03   4.15442691e+03   4.52470938e+03         4.65011423e+03   3.10707392e+03   3.54692896e+03   4.20656404e+03         4.34233412e+03   4.88462501e+03]         [  2.21536396e+01   2.59098311e+01   2.97401268e+01   3.04784552e+01         3.13667639e+01   2.76377113e+01   3.27846013e+01   3.73223417e+01         3.51249997e+01   4.42563658e+01]]     X = [ 1990.  1991.  1992.  1993.  1994.  1995.  1996.  1997.  1998.  1999.]      slope_0, intercept, r_value, p_value, std_err = stats.linregress(X, Y[0,:])     slope_1, intercept, r_value, p_value, std_err = stats.linregress(X, Y[1,:])     slope_2, intercept, r_value, p_value, std_err = stats.linregress(X, Y[2,:])     slope_0 = slope/Y[0,:][0]     slope_1 = slope/Y[1,:][0]     slope_2 = slope/Y[2,:][0]     b, a = polyfit(X, Y[1,:], 1)     slope_1_a = b/Y[1,:][0] 
like image 261
hotshotiguana Avatar asked Mar 02 '12 18:03

hotshotiguana


People also ask

How do you find the slope of data in Python?

You can find the line's slope using the linregress() function if we define the x and y coordinates as arrays. The following code uses the linregress() method of the SciPy module to calculate the slope of a given line in Python.

How do you find the slope of a linear regression line in Python?

Calculate xmean, ymean, Sxx, Sxy to find the value of slope and intercept of regression line. Code 3: Plot the given data points and fit the regression line. Code 5: Use scikit library to confirm the above steps.

How do u calculate the slope?

Pick two points on the line and determine their coordinates. Determine the difference in y-coordinates of these two points (rise). Determine the difference in x-coordinates for these two points (run). Divide the difference in y-coordinates by the difference in x-coordinates (rise/run or slope).


1 Answers

The fastest and the most efficient way would be to use a native scipy function from linregress which calculates everything:

slope : slope of the regression line

intercept : intercept of the regression line

r-value : correlation coefficient

p-value : two-sided p-value for a hypothesis test whose null hypothesis is that the slope is zero

stderr : Standard error of the estimate

And here is an example:

a = [15, 12, 8, 8, 7, 7, 7, 6, 5, 3] b = [10, 25, 17, 11, 13, 17, 20, 13, 9, 15] from scipy.stats import linregress linregress(a, b) 

will return you:

LinregressResult(slope=0.20833333333333337, intercept=13.375, rvalue=0.14499815458068521, pvalue=0.68940144811669501, stderr=0.50261704627083648) 

P.S. Just a mathematical formula for slope:

enter image description here

like image 79
Salvador Dali Avatar answered Sep 22 '22 08:09

Salvador Dali