NameError:未定义名称“onset_to_death”。适用于Py2,但不适用于Py3

时间:2018-02-16 21:07:12

标签: python statsmodels nameerror

我正在参加用Python 2.7编写的在线python课程(EpiSkills,它使用Jupyter笔记本),我使用的是Python 3.6.4,因此我遇到了一些兼容性问题。大部分时间我都能够绊倒,但无法弄清楚这一点,所以希望有人可以提供帮助。

我从以下包开始:

import pandas as pd
import epipy
import seaborn as sns
%pylab inline

import statsmodels.api as sm
from scipy import stats
import numpy as np

使用以下代码创建一个pandas系列和模型:

multivar_model = sm.formula.glm('age ~ onset_to_hospital + onset_to_death + 
        data=my_data).fit()
new_data = pd.Series([6, 8, 'male'], index=['onset_to_hospital', 'onset_to_death', 'sex'])

当我尝试将此用于以下代码时,我抛出了我附加的错误:

multivar_model.predict(new_data)

NameError part1 NameError part2

预期的输出是这样的:

array([ 60.6497459])

我知道很多NameErrors是因为在本地环境中指定了某些东西,而不是全局环境,但是我不确定如何在这个实例中纠正它。非常感谢任何帮助。

谢谢! ç

 ---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\compat.py in call_and_wrap_exc(msg, origin, f, *args, **kwargs)
    116     try:
--> 117         return f(*args, **kwargs)
    118     except Exception as e:

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\eval.py in eval(self, expr, source_name, inner_namespace)
    165         return eval(code, {}, VarLookupDict([inner_namespace]
--> 166                                             + self._namespaces))
    167 

<string> in <module>()

NameError: name 'onset_to_death' is not defined

The above exception was the direct cause of the following exception:

PatsyError                                Traceback (most recent call last)
<ipython-input-79-e0364e267da7> in <module>()
----> 1 multivar_model.predict(new_data)

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\statsmodels\base\model.py in predict(self, exog, transform, *args, **kwargs)
    774                 exog_index = exog.index
    775             exog = dmatrix(self.model.data.design_info.builder,
--> 776                            exog, return_type="dataframe")
    777             if len(exog) < len(exog_index):
    778                 # missing values, rows have been dropped

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\highlevel.py in dmatrix(formula_like, data, eval_env, NA_action, return_type)
    289     eval_env = EvalEnvironment.capture(eval_env, reference=1)
    290     (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
--> 291                                       NA_action, return_type)
    292     if lhs.shape[1] != 0:
    293         raise PatsyError("encountered outcome variables for a model "

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\highlevel.py in _do_highlevel_design(formula_like, data, eval_env, NA_action, return_type)
    167         return build_design_matrices(design_infos, data,
    168                                      NA_action=NA_action,
--> 169                                      return_type=return_type)
    170     else:
    171         # No builders, but maybe we can still get matrices

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\build.py in build_design_matrices(design_infos, data, NA_action, return_type, dtype)
    886         for factor_info in six.itervalues(design_info.factor_infos):
    887             if factor_info not in factor_info_to_values:
--> 888                 value, is_NA = _eval_factor(factor_info, data, NA_action)
    889                 factor_info_to_isNAs[factor_info] = is_NA
    890                 # value may now be a Series, DataFrame, or ndarray

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\build.py in _eval_factor(factor_info, data, NA_action)
     61 def _eval_factor(factor_info, data, NA_action):
     62     factor = factor_info.factor
---> 63     result = factor.eval(factor_info.state, data)
     64     # Returns either a 2d ndarray, or a DataFrame, plus is_NA mask
     65     if factor_info.type == "numerical":

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\eval.py in eval(self, memorize_state, data)
    564         return self._eval(memorize_state["eval_code"],
    565                           memorize_state,
--> 566                           data)
    567 
    568     __getstate__ = no_pickling

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\eval.py in _eval(self, code, memorize_state, data)
    549                                  memorize_state["eval_env"].eval,
    550                                  code,
--> 551                                  inner_namespace=inner_namespace)
    552 
    553     def memorize_chunk(self, state, which_pass, data):

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\compat.py in call_and_wrap_exc(msg, origin, f, *args, **kwargs)
    122                                  origin)
    123             # Use 'exec' to hide this syntax from the Python 2 parser:
--> 124             exec("raise new_exc from e")
    125         else:
    126             # In python 2, we just let the original exception escape -- better

~\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\patsy\compat.py in <module>()

PatsyError: Error evaluating factor: NameError: name 'onset_to_death' is not defined
    age ~ onset_to_hospital + onset_to_death + sex
                              ^^^^^^^^^^^^^^

0 个答案:

没有答案