执行线性回归以找到估计系数并为其绘制回归线,这给了我一个错误...无法对柔韧性类型执行归约。代码有什么问题.... 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)