Statsmodels.api线性回归预测中的形状不匹配

时间:2020-08-18 22:10:52

标签: python numpy scipy linear-regression statsmodels

我正在使用以下代码:

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
import statsmodels.api as sm

def regress(y, X):
  X = sm.add_constant(X)
  model = sm.OLS(y, X).fit()
  ess = model.ess
  ssr = model.ssr
  tss = np.add(ess, ssr)
  dfr = model.df_resid
  ser = np.sqrt(model.scale)
  print(model.summary2())
  print(f'ESS: {np.round(ess, 3)}')
  print(f'SSR: {np.round(ssr, 3)}')
  print(f'TSS: {np.round(tss, 3)}')
  print(f'RSE: {np.round(ser, 3)}')
  return model

X = st.uniform.rvs(loc=-5, scale=10, size=100)
u = st.norm.rvs(loc=50, scale=1, size=100)
y  = np.add(np.square(X), np.add(np.multiply(X, 2), u)) 

quad_model = regress(y=y, X=X)
quad_pred = quad_model.predict(X)

beta_0 = quad_model.params[0]
beta_1 = quad_model.params[1]

y1 = beta_0 + beta_1*X

plt.subplots(1, 1, figsize=(10, 5), dpi=100)
plt.scatter(X, y, facecolors='none', edgecolors='k')

plt.plot(X, y1, color='red')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

在构造quad_pred时出现以下错误:

形状(1,100)和(2,)不对齐:100(dim 1)!= 2(dim 0)

X.shape是(100,)而y.shape是(100,)。

请建议我要去哪里了。

0 个答案:

没有答案