在Python中在散点图上绘制回归线

3

我正在尝试将回归线绘制到散点图上。我有两个函数:

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

当运行此代码时,place_y() 函数会出现错误。错误信息如下:
    return slope * x + intercept
TypeError: can't multiply sequence by non-int of type 'numpy.float64
1个回答

2

map() 期望一个函数作为第一个参数,而你却把place_y(age_x, slope, intercept)作为第一个参数传递给它(在执行时会抛出错误,因为没有定义列表和浮点数的乘法)。你必须传递函数本身,但“冻结”除了x以外的所有参数。为此,你可以使用functools.partial

import functools

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

然而,更好的方法是利用列表推导式:

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

更好的方法是利用NumPy的向量化操作。
...
    age_x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
    ...
    line_of_regression = age_x * slope + intercept
    ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接