问题描述
我正在尝试运行泊松模型,如下所示:
poisson_model_xg = smf.glm(formula="xG ~ home + team + opponent",data=xg_model_data,family=sm.families.Poisson()).fit()
我遇到以下错误:
ValueError:Endog已评估为包含多个列的数组 形状(760,9)。当变量转换为endg时会发生这种情况 是非数字(例如bool或str)。
但是我不知道这是什么意思,因为我所有的数据框都是数字的:
xg_model_data.apply(lambda s: pd.to_numeric(s,errors='coerce').notnull().all())
Out[10]:
goals True
xG True
team True
opponent True
home True
dtype: bool
解决方法
已解决。诀窍不是内容类型,而是列类型:
xg_model_data.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 760 entries,0 to 759
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 goals 760 non-null object
1 xG 760 non-null object
2 team 760 non-null object
3 opponent 760 non-null object
4 home 760 non-null object
dtypes: object(5)
memory usage: 55.6+ KB
在所需的列上应用pd.to_numeric()
之后,数据框如下所示,并且Poisson能够处理。
xg_model_data.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 760 entries,0 to 759
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 goals 760 non-null int64
1 xG 760 non-null float64
2 team 760 non-null object
3 opponent 760 non-null object
4 home 760 non-null int64
dtypes: float64(1),int64(2),object(2)
memory usage: 55.6+ KB