首页 > 解决方案 > 为什么我的可选 python 参数会引发错误?

问题描述

我正在尝试向使用statsmodel包训练 GLM 的函数添加两个可选参数。我用这个问题来指导函数的开发:如何创建带有可选参数的 Python 函数?

基本上,我想让用户能够使用或不使用权重和偏移量。

这是功能:

def model_train(df, formula, *args, **kwargs):
    '''
    run non discrete model
    df = model set
    formula = model formula
    weight = column used for weights
    offset = column used for offsets
    '''
    weight = kwargs.get(df[weight], None)
    print(f"Weights initialized....Starting to intialize offsets")

    offset_factor = kwargs.get(df[offset], None)
    #print(f"Offset initialized....starting matrix development")

    y, x = patsy.dmatrices(formula, df, return_type = 'dataframe')
    print(f"Matrix done...starting to instantiate model")

    glm = sm.GLM(y, x, family = sm.families.Poisson(), var_weights = weight, offset = offset_factor)
    print(f"Model instantiated....starting to fit")

    glm_results = glm.fit()
    print("Model fit. If you are reading this, you're done.  Run 'model_object'[0].summary() to get summary statistics")

    return glm_results, x, y

这是它抛出的错误:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-34-0ce97f02e15e> in <module>
----> 1 model_80150 = model_train(df = train_model1, formula=formula_80150, weight = 'eunit', offset = None)

~\Documents\GitHub\Edit\run_model.py in model_train(df, formula, *args, **kwargs)
      7     offset = column used for offsets
      8     '''
----> 9     weight = kwargs.get(df[weight], None)
     10     print(f"Weights initialized....Starting to intialize offsets")
     11 

UnboundLocalError: local variable 'weight' referenced before assignment

编辑更新:

我尝试了以下TypeError: unsupported operand type(s) for &: 'NoneType' and 'str'错误

def model_train(df, formula, *args, **kwargs):
    '''
    run non discrete model
    df = model set
    formula = model formula
    weight = column used for weights
    offset = column used for offsets
    '''


    weight_value = kwargs.get('weight', None)
    print(f"Weights initialized....Starting to intialize offsets")

    offset_factor = kwargs.get('offset', None)
    print(f"Offset initialized....starting matrix development")

    y, x = patsy.dmatrices(formula, df, return_type = 'dataframe')
    print(f"Matrix done...starting to instantiate model")

    if weight_value == None:
        glm = sm.GLM(y, x, family = sm.families.Poisson())

    elif weight_value == None & offset_factor != None:
        glm = sm.GLM(y, x, family = sm.families.Poisson(), offset = df[offset_factor])

    elif weight_value != None and offset_factor == None:
        glm = sm.GLM(y, x, family = sm.families.Poisson(), var_weights = df[weight_value])

    else:
        glm = sm.GLM(y, x, family = sm.families.Poisson(), var_weights = df[weight_value], offset = df[offset_factor])
    print(f"Model instantiated....starting to fit")

    glm_results = glm.fit()
    print("Model fit. If you are reading this, you're done.  Run 'model_object'[0].summary() to get summary statistics")

    return glm_results, x, y

标签: pythonfunctionkeyword-argument

解决方案


推荐阅读