无法执行缩减灵活类型

执行线性回归以找到估计系数并为其绘制回归线,这给了我一个错误...无法对柔韧性类型执行归约。代码有什么问题.... var应该在哪里定义!

import numpy as np
import matplotlib.pyplot as plt
x="GarageArea"
y="SalePrice"
def estimate_coef(x,y):

    n = np.size(x)
    m_x,m_y = np.mean(x),np.mean(y)

    SS_xy = np.sum(y*x) - n*m_y*m_x
    SS_xx = np.sum(x*x) - n*m_x*m_x

    b_1 = SS_xy / SS_xx
    b_0 = m_y - b_1*m_x
    return(GarageArea)
def plot_regression_line(x,y,b):

    plt.scatter(x,color = "m",marker = "o",s = 30)


    y_pred = b[0] + b[1]*x


    plt.plot(x,y_pred,color = "g")
    plt.xlabel('x')
    plt.ylabel('y')

    plt.show()

# Visualize your results

b=estimate_coef(x,y)
plot_regression_line(GarageArea,GarageArea,b)
esh2007 回答:无法执行缩减灵活类型

您尚未在代码中定义xy,但尝试将它们传递给plot_regression_line。不知道您是否假设当将GarageAreaSalePrice传递给estimate_coef函数时,该函数将它们本地映射到xy时,您认为可以仍然称其为。但是x函数中的yestimate_coef在函数中是局部作用域的,在函数外部不存在,因此不能被引用。

假设您打算将GarageAreaSalePrice的{​​{1}}和x传递给y函数。

plot_regression_line

这是一个假设,您在代码中定义了import numpy as np import matplotlib.pyplot as plt def estimate_coef(x,y): n = np.size(x) m_x,m_y = np.mean(x),np.mean(y) SS_xy = np.sum(y*x) - n*m_y*m_x SS_xx = np.sum(x*x) - n*m_x*m_x b_1 = SS_xy / SS_xx b_0 = m_y - b_1*m_x return(b_0,b_1) def plot_regression_line(x,y,b): plt.scatter(x,color = "m",marker = "o",s = 30) y_pred = b[0] + b[1]*x plt.plot(x,y_pred,color = "g") plt.xlabel('x') plt.ylabel('y') plt.show() # Visualize your results b=estimate_coef(GarageArea,SalePrice) plot_regression_line(GarageArea,GarageArea,b) x,但是我可能是错的,因为您还使用了yGarageArea这两个未定义的在您发布的代码中。如果这样不能回答您的问题,那么您应该编辑问题并张贴得到的错误的堆栈跟踪。

本文链接:https://www.f2er.com/3137386.html

大家都在问