Python:endog 已计算为具有多列的数组

Rod*_*dor 5 python type-conversion poisson

我正在尝试运行泊松模型,如下所示:

poisson_model_xg = smf.glm(formula="xG ~ home + team + opponent", data=xg_model_data, 
                        family=sm.families.Poisson()).fit()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

ValueError:endog 已计算为具有形状 (760, 9) 的多列数组。当转换为 endog 的变量是非数字(例如 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
Run Code Online (Sandbox Code Playgroud)

Rod*_*dor 11

解决了。诀窍不在于内容类型,而在于列类型:

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
Run Code Online (Sandbox Code Playgroud)

在我应用pd.to_numeric()到所需的列后,数据框如下所示,并且泊松能够处理。

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
Run Code Online (Sandbox Code Playgroud)

  • “应用”的意思是``df['col_name'] = pd.to_numeric(df['col_name'])``` (2认同)