python - 为什么我的可选 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
解决方案
推荐阅读
- ssh-tunnel - 通过 rsa 的 ssh 连接 nornir_napalm
- python - 启动内核时发生错误,我不知道如何修复它
- python - 将文件创建日期更改为 TIFF
- javascript - 如何获取每个表单javascript的值?
- c++ - 拦截系统调用的内核模块会导致用户空间程序的执行出现问题
- python - 合并 2 个 csv 文件 pandas 时如何解决 Empty DataFrame
- r - R中的概率图| 如何制作一个从0到1的完整情节?
- javascript - 改进在反应中调用 onKeyup 事件方法
- python - PyGame 不会在窗口上加载图像并立即崩溃
- reactjs - 更改箭头/图标颜色材料 ui 选择