# How to use units for a slope calculation?

Simple question: is the result of a slope calculation in Python (say, stats.linregress() function) in units of radians, or unitless (e.g. what's 18 deg or 0.314 rad would be equivalent to 0.2 in the function output)?

• There's no "slope calculation in Python". You're probably using a library, and thus you should read its documentation. – Vincent Savard Aug 23 '16 at 15:45
• Split hairs much? Here's the doc scipy.stats.linregress, it doesn't state whether or not there or units for slope. – CB001 Aug 23 '16 at 15:50
• Not at all, I have no idea what you're talking about. I can't read your mind. You should make it easy for others to help you. – Vincent Savard Aug 23 '16 at 15:50
• slope would be rise/run - for example: if 2 units up (y) gives you +1 x unit, then the slope would be 2. – Aaron Hall Aug 23 '16 at 15:59
• The complete calculation that `stats.linregress()` makes can be found here: github.com/scipy/scipy/blob/v0.18.0/scipy/stats/…. It's merely 50 lines of python code (not counting the comments). – Robert Harvey Aug 23 '16 at 16:31

## 3 Answers

`scipy.stats.linregress` returns the slope in terms of rise over run. As Stephen C. Steels answer says, the units of the resultant slope is the units of the y-axis multiplied by the inverse of the units of the x-axis (eg if y-units are metres, and x-units are seconds, then slope's units will be metres/second).

If you want the angle of slope (in radians and degrees), use arctangent:

``````import math
from scipy.stats import linregress

slope = linregress(xs, ys)  # slope in units of y / x
slope_angle = math.atan(slope)  # slope angle in radians
slope_angle_degrees = math.degrees(slope_angle)  # slope angle in degrees
``````

Slope and angle both measure how "not flat" something is, but they do so very differently. For instance, if you have a board that's not flat, the angle between the board and the ground measures how much you would rotate the board to make it flat, while the slope measures the height of the board divided by its horizontal length. They are related by slope = tangent(angle), but they are different concepts with different units. Also, while slope makes sense for any two dimensions, angle makes sense only when the dimensions are in the same space. So, for instance, 50 miles per hour can be interpreted as the slope of the graph of a car's position, but it would not make sense to try to convert that into an angle.

While there are ways to encode units in computer programs, as a general rule computer programs simply do not work with units. It's up to whoever uses a program to deal with units. The stats.linregress() function takes no units as inputs, and gives no units as outputs. If, rather than "what are the units of the output", you mean "what units should I add to the output for a physical interpretation", then you should add the units of y divided by the units of x. Note that if your input has multiple features with multiple units, then each coefficient will have different units. (This is why features are generally normalized before regularizing; regularization involves taking the sum of the coefficients (or their squares), and adding numbers with different units doesn't make sense.)

• "as a general rule computer programs simply do not work with units'. This is a bad general rule, as there is tons of design and engineering software that use units, and every professional financial software on the planet uses units, only called currency in that context. – whatsisname Jan 13 '18 at 2:42
• @whatsisnane I suppose it depends on what one means by "program", "unit", and "work with". Units are generally only part of the UI. All the program knows is that it's been told to print ASCII character 36 (\$) before the number. – Acccumulation Jan 14 '18 at 23:23

The library calculates the numerical value of the slope given the numerical values of your coordinates. It interpretation as a quantity (that is, a numerical value combined with units) is up to your program. Unless the library function is doing something very strange, the units for the slope through a set of points (x, y) would be unit of slope = (unit of y)/(unit of x).

So, for example, if x and y are both distances expressed in the identical units, the slope would be dimensionless. However, if x is a time in seconds, and y is a voltage in volts, then the slope would be in volts/second.

## protected by gnatJan 12 '18 at 23:16

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?