Python:endog 已评估为具有多列的数组 [英] Python: endog has evaluated to an array with multiple columns
问题描述
我正在尝试运行泊松模型,如下所示:
I'm trying to run a Poisson model, like this:
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).
ValueError: endog has evaluated to an array with multiple columns that has shape (760, 9). This occurs when the variable converted to endog is non-numeric (e.g., bool or str).
但我不知道这是什么意思,因为我所有的数据框都是数字:
But I can't figure out what does it mean, since all my dataframe is numeric:
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
推荐答案
已解决.技巧不在于内容类型,而在于列类型:
Solved. The trick was not in content type, but in columns type:
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()
后,数据框如下所示,并且泊松能够处理.
After I applied pd.to_numeric()
on desired columns, the dataframe looks like the following, and Poisson is able to process.
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
这篇关于Python:endog 已评估为具有多列的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!