Drawing line of regression onto scatter graph in python

Question:

I am trying to draw the line of regression onto a scatter graph. I have two functions:

def place_y(x, slope, intercept):
    return slope * x + intercept


def draw_line_of_regression():
    """The line of regression can be used to predict further values"""
    import matplotlib.pyplot as plt  # used to draw graph
    from scipy import stats

    # Example shows relationship between age and speed
    age_x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
    speed_y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]

    slope, intercept, r, p, std_error = stats.linregress(age_x, speed_y)
    # gets variables used in drawing the line of regression

    line_of_regression = list(map(place_y(age_x, slope, intercept), age_x))

    plt.scatter(age_x, speed_y)  # draws scatter graph
    plt.plot(age_x, line_of_regression)
    plt.show()  # shows the graph


draw_line_of_regression()

When this is run there is an error with the place_y() function. Error:

    return slope * x + intercept
TypeError: can't multiply sequence by non-int of type 'numpy.float64
Asked By: charlie s

||

Answers:

map() expects a function as the first argument while you’re giving it place_y(age_x, slope, intercept) as the first argument (which throws error during execution because there’s no multiplication of list and float defined). You have to pass the function itself, but "freeze" all arguments except x. To do that you can use functools.partial:

import functools

...
    line_of_regression = list(map(functools.partial(place_y, slope=slope, intercept=intercept), age_x))
    ...

However, a better way to do the same is to utilize list comprehension:

...
    line_of_regression = [place_y(x, slope, intercept) for x in age_x]
    ...

Even better is to leverage numpy’s vectorized operations

...
    age_x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
    ...
    line_of_regression = age_x * slope + intercept
    ...
Answered By: Alex Bochkarev
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.